[antlr-interest] keywords appended to identifiers, suggestions?

Matthew Ford Matthew.Ford at forward.com.au
Thu Jun 12 06:40:15 PDT 2003


How about just finding NAMEs and then using Java String.endsWith() to test
for keywords.
matthew

----- Original Message -----
From: "gdave" <dave.green at valley.net>
To: <antlr-interest at yahoogroups.com>
Sent: Thursday, June 12, 2003 11:32 PM
Subject: [antlr-interest] keywords appended to identifiers, suggestions?


> I have a simple grammar which needs to identify normal runs of
> characters as NAMES, but certain keywords as KEYWORDS. The tricky
> part which I'm unable to solved is that I would like to identify
> keywords even when they are appended to name. Furthermore, I would
> NOT like to identify them when they are embedded in a name. a grammar
> example that fails to meet this need:
>
> class P extends Parser;
> startRule
>     :
>     n:NAME
>         {System.out.println("name: " + n.getText());}
>     | KEYWORD
>         {System.out.println("keyword");}
>     ;
>
> class L extends Lexer;
>
> protected
> KEYWORD : "FOO" | "BAR";
>
> protected
> NAME:   ('A'..'Z')+
>     ;
>
> KEY_OR_NAME :
>  (KEYWORD) => KEYWORD {$setType(KEYWORD);}
>  | NAME {$setType(NAME);}
>  ;
>
> Test cases that work according to my wishes:
> FOO DAVE -> keyword name:DAVE
> DAVE FOO -> name:DAVE keyword
> FOODAVE -> keyword name:DAVE
>
>
> Test cases which the grammar mishandles:
> DAVEFOO -> name:DAVE keyword
> DAVEFOOSUFFIX -> name:DAVEFOOSUFFIX
>
> How can modify the grammar to keep the NAME expression from
> swallowing the trailing keywords? If it did recognize trailing
> keywords, how can I keep it from splitting names because they have
> embedded keywords? thanks in advance for any suggestions. I'm on my
> fourth or fifth pouring over of the antlr manual and haven't found a
> construct that I have been able to use to any advantage in this
> problem.
>
> Dave
>
>
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list