[antlr-interest] disambiguating sempred in a closure?
Harald M. Müller
harald_m_mueller at gmx.de
Fri Dec 21 13:49:45 PST 2007
Hi -
As far I can see, the sempreds ARE at the top of the (inner) rule. Below is
the code produced by ANTLR 2.7.5 for C# - I did not run it, but it looks
pretty correct.
And I would not go to ANTLR 3 with an 1100 line grammar, unless you plan
this as a major (and maybe failing) step in your project; I stopped working
on porting a 300 line grammar (lexer + parser) after I found that some
features from ANTLR 2.x are not (yet) present in ANTLR 3.x ...
Regards
Harald M.
public void prod() //throws RecognitionException,
TokenStreamException
{
returnAST = null;
ASTPair currentAST = ASTPair.GetInstance();
AST prod_AST = null;
something();
astFactory.addASTChild(currentAST, returnAST);
{ // ( ... )*
for (;;)
{
if (((LA(1)==KERMIT||LA(1)==FOZZIE))&&(
muppet )) <<< go into this branch if muppet is true
{
{
switch ( LA(1) )
{
case KERMIT:
{
AST tmp1_AST = null;
tmp1_AST =
astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp1_AST);
match(KERMIT);
break;
}
case FOZZIE:
{
AST tmp2_AST = null;
tmp2_AST =
astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp2_AST);
match(FOZZIE);
break;
}
default:
{
throw new
NoViableAltException(LT(1), getFilename());
}
}
}
}
else if
(((LA(1)==LEONARDO||LA(1)==DONATELLO))&&( turtle )) { <<< go into
this branch if turtle is true
{
switch ( LA(1) )
{
case LEONARDO:
{
AST tmp3_AST = null;
tmp3_AST =
astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp3_AST);
match(LEONARDO);
break;
}
case DONATELLO:
{
AST tmp4_AST = null;
tmp4_AST =
astFactory.create(LT(1));
astFactory.addASTChild(currentAST, tmp4_AST);
match(DONATELLO);
break;
}
default:
{
throw new
NoViableAltException(LT(1), getFilename());
}
}
}
}
else
<<< exit the loop if none of the above (lookaheads and flags) are true
{
goto _loop5_breakloop;
}
}
_loop5_breakloop: ;
} // ( ... )*
prod_AST = currentAST.root;
returnAST = prod_AST;
ASTPair.PutInstance(currentAST);
}
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of J Chapman Flack
> Sent: Friday, December 21, 2007 4:52 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] disambiguating sempred in a closure?
>
>
> Hi,
>
> I have backed myself into a corner where I need semantic
> information to decide whether to take an exit branch:
>
> prod : something
> ( options { greedy=true; }
> : { muppet }? ( KERMIT | FOZZIE )
> | { turtle }? ( LEONARDO | DONATELLO )
> )*
> ;
>
> so that even if the lookahead is KERMIT or FOZZIE I should
> take the exit branch if I'm not looking for muppets. (Before
> you ask, no, I don't know this statically; actions may alter
> these flags on any iteration. Such is the language I'm trying
> to parse.)
>
> antlr 2.7.7 treats these sempreds as validating because they
> are not at the top of the production, so they throw
> exceptions instead of exiting the closure. I haven't tried to
> port my grammar to antlr 3 yet.
>
> 1. Is there a way to refactor this production in antlr 2.7.7
> to get the behavior I want?
>
> 2. Is it easier in antlr 3? Is it enough easier that I should
> bite the bullet and port the 1100 line grammar rather than
> fussing with it in 2.7.7?
>
> Thanks,
> -Chap
>
More information about the antlr-interest
mailing list