[antlr-interest] Hoisting of a disambiguating semantic predicates

Paul Eames peames at nexagent.com
Tue Oct 30 10:19:31 PDT 2007


By the way, I know that in this simple example I could recast rule "a"
to not use alternatives and rather write it as:
 
a: foo bar (foo (bar foo)*)?
 
... but I have more complex rules where this approach becomes
increasingly unclear.
 
I wish I was working on something less ambiguous :-)

________________________________

From: Paul Eames 
Sent: 30 October 2007 16:03
To: 'antlr-interest at antlr.org'
Subject: 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/922c2ff7/attachment.html 


More information about the antlr-interest mailing list