Lukasz Guminski wrote:
Thank you, but it doesn't work. LT(k) returns a character. I am sending you my grammar and the output I got.

Grammar:
grammar block;
options{
 k = 3;
}

@members{
  Stack<String> stack = new Stack<String>();
  boolean isBlockOpen(String name)
  {
     System.out.println("isBlockOpen() has been invoked with a value : " + name);
     return stack.size()==0||!stack.peek().equals(name);
  }
}


data: blocks EOF;
blocks:block+;
block: block_open blocks? block_close;
block_open
    :  {isBlockOpen(input.LT(3).getText())}? =>
    BLOCK_BOUNDARY SPACE block_name NEWLINE {stack.push($block_name.text);System.out.println("start of block " + $block_name.text);};
block_close:BLOCK_BOUNDARY SPACE block_name NEWLINE {System.out.println("end of block " + $block_name.text);stack.pop();};
block_name:LETTER+ ALPHANUM*;


SPACE    :     ' ';
LETTER    :     ('a'..'z'|'A'..'Z');

Well, you have LETTER as a non fragment rule, so you are always going to get this instead of ALPHANUM. You should be getting an ambiguity warning too I suspect and you block_name rule should be ID:

fragment
LETTER    :     ('a'..'z'|'A'..'Z');
fragment
ALPHANUM    :    (LETTER|'0'..'9');

ID : LETTER ALPHANUM* ;

Also, do you need the SPACE token? It will be a lot easier in the parser if you hide that and regardless, if the user has 2 spaces not one, then all your calculations will be out anyway. You should probably hide the insignificant_char token too.

I think that you need to be doing this a different way really. You could easily do this in the lexer.

Perhaps you should practice a bit on simpler problems before diving in to this one? I think you are suffering from not understanding the lexer/parser fundamentals yet.

Jim


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to