[antlr-interest] Hoisting of a disambiguating semantic predicates

Jim Idle jimi at temporal-wave.com
Tue Oct 30 10:17:32 PDT 2007


It seems to me that you are possibly trying to encode a semantic decision in
the syntactical makeup of foo and bar and also expression the grammar
ambiguously whether you can distinguish foo from bar anyway (as in even if
you didn't need the predicates). It depends on what you are trying to
distinguish,  but would your case not simplify to:

 

foo bar ( foo ( bar foo)*)?

 

Then, knowing what sequence you actually found, you could rewrite the
appropriate unambiguous tree.

 

Jim

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Paul Eames
Sent: Tuesday, October 30, 2007 9:03 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Hoisting of a disambiguating semantic predicates

 

Hi all,

 

I am using semantic predicates to disambiguate two syntactically identical
rules as follows:

 

foo : 

(

    {isFoo(input.LT(1))}?

    x+=THING

)+ ;

 

bar : 

(

    {isBar(input.LT(1))}?

    x+=THING

)+ ;

 

This is fine until I start using these rules in other rules like the
following:

 

a : foo bar | foo (bar foo)+;

 

The generated disabiguating check code in rule "a" hoists the semantic
predicate out of foo which is not sufficient to disambiguate and so this
rule will always take the first alternative.

 

Generated code snippet:

...

if ( (isFoo(input.LT(1)) ) {
    alt5=1;
}
else if ( (isFoo(input.LT(1)) ) {
    alt5=2;

}
else {
    NoViableAltException nvae =
        new NoViableAltException("203:1: a: ( ( foo bar ) | ( foo ( bar foo
) )+ );", 5, 4, input);

    throw nvae;
}

...

 

Is there a cunning grammar design decision that I can take to avoid this
problem?

 

Many thanks,

Paul.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071030/4f43623f/attachment-0001.html 


More information about the antlr-interest mailing list