[antlr-interest] (no subject)
Imre András
iar73 at freemail.hu
Sat Nov 1 07:14:14 PDT 2008
Gerald Rosenberg <gerald at certiv.net> írta:
> At 06:39 PM 10/31/2008, =?ISO-8859-2?Q?Imre_Andr=E1s?= wrote:
>
> >Now how can I explicitly tell that SEQUENCE is a keyword, and not a valid ID?
>
>
> tokens { SQ; }
>
> ID: { 'SEQUENCE' }=> 'SEQUENCE' {$type = SQ}
> | ('a'..'z'|'A'..'Z')+ ;
> it with some necessary changes (please find the current grammar below).
This compiles ok (I'm still wondering about the $type and token magic), but there is a problem with processing A ::= SEQUENCE { a, SEQUENCE, c} as input. A MismatchedTokenException occurs instead of treating SEQUENCE as an ID.
How can I tell ANTLR that 'SEQUENCE' means a list when followed by '{', otherwise it is an ID?
Thanks,
András
--- grammar --------------------
grammar keyword;
tokens { SQ; }
DEFINES : '::=' ;
WS : (' '|'\t')+ {skip();} ;
SEQUENCE : 'SEQUENCE';
prog: stat+ ;
stat: assignment;
assignment : leftValue DEFINES rightValue {System.out.println($leftValue.value + "=" + $rightValue.value);};
leftValue returns [String value]: ID {$value = $ID.text;};
rightValue returns [String value]: ID {$value = $ID.text;}| sequence {$value = $sequence.value;};
sequence returns [String value]: SEQUENCE '{' e=rightValue {$value=$e.value;} (',' e=rightValue {$value += $e.value;})* '}';
ID: ( SEQUENCE )=> SEQUENCE {$type = SQ;} | ('a'..'z'|'A'..'Z')+ ;
--- end of grammar ---
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081101/82a6636c/attachment.html
More information about the antlr-interest
mailing list