Beginner question, about syntactic predicates. YES but...

micheal_jor <open.zone at virgin.net> open.zone at virgin.net
Fri Feb 7 03:31:28 PST 2003


> The difference between 'attribute' and 'entity' is there is '
> simpleAttribute' at the end of 'attribute'.

We've established that and my last message modified your grammar to 
correctly reflect that.

> I would like to keep the attribute rule because i would like to use 
it
> as a not in a tree.

You don't need to keep the attribute rule for that. You just need to 
guide ANTLR's tree construction so that it builds the tree you want.

> That kind of rules worked fine with a LR grammar, but does not seem 
to
> work in the LL grammar.

That shouldn't be suprising at all. LR parsing is NOT LL parsing.

> 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.

They are not predicted because of errors in your grammar and, because 
you are still treating ANTLR as an LR parser.

> 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..

"Real LL parsers"?. Ouch!

> I see no solution for my problem

There are many possible solutions for your problem. Which you choose 
(or even if you get to choose i.e. solve it yourself) depends on how 
well you've grokked the LL(k) way of thinking.

Reading your grammar extract below, I can see that entSetAtt can be 
one of:
a. a "simpleAttribute"
b. an "entity"
c. an "entity" followed by a "simpleAttibute"

In other word:

entSetAtt
: simpleAttribute
| entity ( simpleAttribute )?
;

If as you say, the former "attribute" rule needs special tree-
onstruction handling, then try this

entSetAtt
: simpleAttribute  { <insert-attribute-tree-build-code> }
| entity ( simpleAttribute { <insert-attribute-tree-build-code> } )?
;

where <insert-attribute-tree-build-code> is stuff like:
## = #( [ATTRIBUTE], #entSetAtt ); 

On the other hand, if you want to keep the attribute rule and rely on 
a syntactic predicate, read Tom's post. He suggests a number of 
solutions, one of which is based on your original formulation of the 
problem.

Incidentally, there is a typo in Tom's first solution (he was nowhere 
near an ANTLR box):

entSetAtt
: (entity) => entity 
| attribute
;

should be:

entSetAtt
: (attribute)=> attribute
| entity 
;

Cheers,

Micheal

> ---
> 
> 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



More information about the antlr-interest mailing list