Don't forget to use the search engine at antlr.markmail.org Do the following:
1) Do not use 'LITERALS', make tokens for the lexer, list all the keywords in order (or at least in one group); 2) Replace ID with a parser rule id; 3) Make the id rule: id: ID | MEASUREMENT ; Jim > -----Original Message----- > From: [email protected] [mailto:antlr-interest- > [email protected]] On Behalf Of Anders Sollander > Sent: Monday, February 22, 2010 7:31 AM > To: [email protected] > Subject: [antlr-interest] Trouble using keyword tokens > > Hi, > > I have an issue with a parser that is using certain keywords within the > parser rules, and these keywords can also appear as random identifiers > in other rules. When this happens, the parser reports an error, and I > see no way to avoid this. The grammar and the test input can be found > below. > When the parser reaches the second HELLO rule, it stumbles on the token > MEASUREMENT (evidently because it's present as a word in the meas > rule). Is there a workaround for this? > > Thanks > Anders > > Grammar (testkeyword.g): > ---------------------------------------------------- > > grammar testkeyword; > > prog : body+ ; > body : meas | words ; > > meas : > BEGIN 'MEASUREMENT' > INT > END 'MEASUREMENT' ; > > words : > BEGIN id1=ID > INT ID+ > END id2=ID ; > > BEGIN : '/begin'; > END : '/end'; > > ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ; > INT : '0'..'9'+ ; > WS : ( ' ' | '\t' | '\r' | '\n' ) {$channel=HIDDEN;} ; > > > Test input: > ----------------------------------------------------------- > /begin HELLO > 13 I say hello > /end HELLO > /begin MEASUREMENT > 44 > /end MEASUREMENT > /begin HELLO > 55 Use MEASUREMENT word > /end HELLO > > > > > > > List: http://www.antlr.org/mailman/listinfo/antlr-interest > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your- > email-address List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- 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.
