[antlr-interest] how to get the value of an identifier
Thierry USO
thierry.uso at wanadoo.fr
Wed Sep 30 13:20:36 PDT 2009
It works!
LETTER and DIGIT fragment rules and EOL were needed. See below.
grammar ADL; // Rally Application Development Language
constant_definition
: CONST {System.out.print("CONST ");}
(
(' '|'\t')* Identifier {System.out.print($Identifier.text + " ");} (' '|'\t')*
'=' {System.out.print("= ");} (' '|'\t')*
CharString {System.out.print($CharString.text + " ");}
(' '|'\t')* ';' {System.out.println(";");} (' '|'\t')* EOL
)+
;
CONST
: 'CONST'
;
Identifier
: LETTER (LETTER|'_'|DIGIT)+
;
CharString
: '"' (LETTER)+ '"'
;
EOL
: '\r'? '\n'
;
fragment
LETTER
: ('a'..'z'|'A'..'Z')
;
fragment
DIGIT
: ('0'..'9')
;
Thanks again. antlr-interest is great !
>
>Your grammar does not handle New-Line characters ('\n' and possibly
>'\r'). You should have received many "no viable alternative at ..."
>error messages....
>
>As others have stated you should:
>
>1) make LETTER and DIGIT fragement rules.
>2) make ConstDef be a parser rule
>3) when you do 2) you should add a WHITESPACE lexer rule, perhaps
>putting the WHITESPACE (including new-lines) on the HIDDEN channel (e.g.
>{ $channel = HIDDEN; } )
>
>attached is a complete example with the above suggestions applied. Oh by
>the way.... in my example I construct an AST during the parse and then
>print the AST at the end; rather than doing the System.out stuff during
>the parse....
>
>Hope this helps...
> -jbb
>
More information about the antlr-interest
mailing list