[antlr-interest] Re: Beginner question, about syntactic predicates. YES but...

tbrandonau <tom at psy.unsw.edu.au> tom at psy.unsw.edu.au
Thu Feb 6 17:13:28 PST 2003


Well, Michael's first message solved your problem, then you changed 
the problem by adding the entity\attribute rules.

Don't have Antlr on this machine, so not sure of the syntax. But the 
ideas are hopefully right. Look at syntactic\semantic predicates and 
semantic actions (any code in curlies).

Couple of ways to fix it:

I'm pretty sure this will work, and basically makes Antlr do what you 
want, try entity and if it fails try attribute:
entSetAtt
    :    (entity) => entity 
    |    attribute
    ;

Or you can fix your initial attempt at syntactic predicates (changed 
to rule names to make it shorter):
> entSetAtt
>     :    (simpleAttribute) => attribute
>     |     entity
>     ;
This won't work because Antlr isn't smart enough to know that you 
want simpleAttribute at the end, so you need:
 entSetAtt
     :    (entityComponent (COLON entityComponent)* simpleAttribute) 
=> attribute
     |     entity
     ;

Or you can adapt Michael's example:
> entSetAtt
>     :    entityComponent (COLON entityComponent)* (simpleAttribute)?
>     ;
with a semantic action (which I imagine is the fastest way):
entSetAtt
    :    entityComponent (COLON entityComponent)*
         (
             simpleAttribute { <ASTCodeHere> }
         )?
    ;
With some code to build the proper AST in there.

Basically RTFM, in the ANTLR meta-lang section, the Interpretation of 
semantic actions, Semantic predicates and Syntactic predicates (and 
especially Fixed depth lookahead and syntactic predicates ) (on from 
http://www.antlr.org/doc/metalang.html#_bb18). They basically 
describe your exact question. And there's probably a few FAQ entries.

Once you master these techniques and understand Antlr's approximate 
LL methods which cause problems like this you'll be able to do almost 
anything.

Tom.

--- In antlr-interest at yahoogroups.com, Anthony Brenelière 
<abreneliere at t...> wrote:
> The difference between 'attribute' and 'entity' is there is '
> simpleAttribute' at the end of 'attribute'.
> 
> I would like to keep the attribute rule because i would like to use 
it
> as a not in a tree.
> 
> That kind of rules worked fine with a LR grammar, but does not seem 
to
> work in the LL grammar.
> 
> So when i test the grammar on a sample code, attributes are not
> predicted in the rule entSetAtt, so 'ent1:ent2:ent3.att' is a syntax
> error, thought it should be accepted by the grammar.
> 
> I tought real LL parsers tried the rule 'entity', and if there was a
> syntax error, returned back to the rule 'entSetAtt' to try for other
> alternatives, like the 'attribute' rule..
> 
> I see no solution for my problem
> 
> ---
> 
> entSetAtt   // HERE IS THE NON-DETERMINISM THAT CAUSE ERRORS WITH 
VALID
> CODE
> 	:	entity 
> 	|	attribute
> ;
> 
> entity // (supplier WHERE name="Telys"):client
> 	:	entityComponent (COLON entity)?
> ;
> 
> attribute	// ::supplier:client.address
> 	:	entity simpleAttribute 
> 	|	simpleAttribute 
> ;
> 
> entityComponent // (supplier WHERE name="Telys")
> 	:	LPAREN IDENTIFIER clause RPAREN
> 	|	IDENTIFIER 
> ;
> 
> simpleAttribute	// .name
> 	:	DOT IDENTIFIER 
> ;
> 
> ---
> 
> Cordially,
> Anthony


 

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



More information about the antlr-interest mailing list