[antlr-interest] Rookie problem

Gerald Rosenberg gerald at certiv.net
Thu Apr 3 16:30:56 PDT 2008


One way is to use a validating semantic predicate:

keyword : w1=word w2=word { helper.isKeywordPhrase($w1.text, $w2.text); }?
              | w=word { helper.isKeyword($w.text); }?
              ;

word : ( 'a'..'z' | 'A'..'Z' )+ ;

where the helper methods are lookup boolean return methods, the lexer 
is set to skip whitespace, and (for simplicity) the parser is set to 
backtrack.

After you understand the predicate use, look in the wiki/archives for 
the list operator '+=' and switch to using variable lists instead of instances.


At 03:15 PM 4/3/2008, Marko Simovic wrote:
>You're right on the money with that Adam. For the life of me i can't 
>figure out a solution either. Forget the if-then statement thing 
>from before and here is a simpler example:
>
>union:  name ('union' name)*;
>
>this rule works well on stuff with syntax:
>
>abcxyz union qrtabc
>
>however, what would be the modification needed to do this:
>
>abc xyz union qrtabc
>
>I'm not smart enough for that problem
>Marko
>
>-----Original Message-----
>From: Adam Connelly 
><<mailto:Adam%20Connelly%20%3cadam.rpconnelly at googlemail.com%3e>adam.rpconnelly at googlemail.com>
>To: Marko Simovic 
><<mailto:Marko%20Simovic%20%3cmarkobarko at gmail.com%3e>markobarko at gmail.com>
>Cc: <mailto:antlr-interest at antlr.org>antlr-interest at antlr.org
>Subject: Re: [antlr-interest] Rookie problem
>Date: Thu, 3 Apr 2008 17:31:53 +0100
>
>Not 100% on this, but I would guess that the "name" rule that's part 
>of "condition" is consuming the "then".  Basically if I'm right 
>about this, antlr thinks that the "then" keyword of the if statement 
>is part of a name.   Unfortunately I don't know a solution for this 
>of the top of my head.   Sorry, Adam
>
>   On 03/04/2008, Marko Simovic 
> <<mailto:markobarko at gmail.com>markobarko at gmail.com> wrote:
>Hi all,
>
>I've just started using Antlr and I'm having a bit of a problem with 
>a grammar definiton. I want my grammar to support a language with 
>multi-word variable names. However, making the adjustment breaks my 
>if-then conditional recognition.
>
>The grammar at the end of this e-mail works fine for variable names 
>without spaces. If i change the 'name' definition to the following:
>
>name: String (' ' String)*;
>
>then the 'if' statement can no longer be recognized. What am i doing wrong?
>
>Thanks,
>Marko
>
>grammar test;
>
>String : ('a'..'z' | 'A'..'Z')+;
>
>ConditionOperator
>: '<' | '>';
>
>name
>: String;
>
>condition
>: name (ConditionOperator name)*;
>selection
>: 'if' condition 'then' condition;
>
>statement
>: selection;
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080403/466ef50b/attachment-0001.html 


More information about the antlr-interest mailing list