tags:

views:

63

answers:

1

Hi,

This ANTLR example does not parse input "1;" . Can you explain why? It parses "11;".

grammar TestGrammar;

options {
    output=AST;
}

expr:       mexpr (PLUS^ mexpr)* SEMI!;
mexpr:      atom (STAR^ atom)*; 
atom:       INT; 

LPAREN:     '('; 
RPAREN:     ')'; 
STAR:       '*'; 
PLUS:       '+'; 
SEMI:       ';';

protected
DIGIT:      '0'..'9';
INT:        (DIGIT)+;

WS:         (' ' | '\t' | '\n' | '\r') {
                $channel = HIDDEN;
            };
+1  A: 

For the java target, if you change: protected DIGIT : '0'..'9' ;

to fragment DIGIT : '0'..'9' ;

it will work.

Hope this helps you.

WayneH
Confirmed, changing protected to fragment.
Pindatjuh
I figured out. Explanation: Protected keyword works in earlier versions of ANTLR. It does not work in ANTLR 3. Now one has to use fragment instread of protected. Protected is probably ignored in ANTLR 3. So what happens? ANTLR interprets both INT and DIGIT as a token. As they are very similar, the parsing fails. By using fragment, DIGIT is no longer a token. Fragment says that DIGIT is part of a rule or another token. Then the example starts working.
Aftershock