[antlr-interest] left recursion removal
Jim Idle
jimi at temporal-wave.com
Wed Jul 6 16:42:16 PDT 2011
With the example this time:
ifStatement
: IF e=expression
(
t=THEN
(
// Single line IF
statement as the last token was not a NL
//
s1=statements
((ELSE)=>ELSE s2=statements)?
-> {$s2.tree == null}?
^(IFLINE $e ^(THEN $s1))
->
^(IFLINE $e ^(THEN $s1) ^(ELSE $s2*))
| (NL|COLON)+
// We discovered a NL token, hence
we process
// a block If here as it has to be a
block when the line with
// the IF on it has a NL before the
first statement.
//
ib=ifBlock
-> ^(IF $e $ib)
)
| (NL|COLON)+
// We did not see a THEN clause, but it is
optional on block IF statements
// (which is a bit silly, but a left over from
VB 1 I suspect. Hence we process
// a block If here.
//
ib=ifBlock
-> ^(IF $e $ib)
)
;
ifBlock
: tb=block
(eib+=elseIfStatement)*
(
ec=elseClause // Else Clause
handles END IF
| END IF
)
-> ^(THEN $tb?) $eib* $ec?
;
elseClause
: ELSE^ (
(NL!|COLON!)+ block END! IF!
| statement
)
;
elseIfStatement
: ELSEIF e=expression THEN? (NL|COLON)+ b=block
-> ^(ELSEIF $e $b?)
;
Where 'block' is one or more statements (might need zero or more for
powerbuilder).
Jim
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Sébastien Kirche
> Sent: Wednesday, July 06, 2011 4:28 PM
> To: John B. Brodie
> Cc: antlr-interest
> Subject: Re: [antlr-interest] left recursion removal
>
> Le 7 juillet 2011 00:14, John B. Brodie <jbb at acm.org> a écrit :
> > Greetings!
> >
> > [...]
> >
> > unable to reproduce.
> >
> > given your admittedly partial grammar, i tried to construct a
> complete
> > example by adding the missing elements and creating an AST (so i
> could
> > know the resultant parse).
> >
> > my test rig is attached.
> >
> > it runs without error when Tool'd, compiled, and executed from the
> > command-line (FWIW i use Ubunto 11.04 Linux running Sun Java 6 and
> the
> > Antlr version from the antlr-3.4-complete.jar file).
> >
> > Please try to post the *smallest* yet *complete* example of your
> > problem.
>
> Sorry for not having posted a more complete code before, I though that
> should have been enough...
>
> I have worked further on my grammar, with trying to define more
> precisely what an expression should be, with operator precedence. This
> made me rewrite the singleStatement by moving the funCall rule to the
> primary rule. But my problem about the if-then-else construct is not
> gone.
>
> I have stripped down my grammar, keeping only the expression and
> subsequent rules, removed all that define loops, switches, etc.
> Given that simplified grammar, the following script should pass the
> parsing, but it can't yet :
>
> ---------------------------------------
> string s1, s2
>
> if s1='42' then s2='421'
>
> if s2='421' then
> string s3
> s3='123'
> else
> string s4
> s4='666'
> end if
>
> ---------------------------------------
>
> Also, to Jim Idle : the language I would be able to parse is
> Powerbuilder. You can see it as a sort of basic, while it accepts
> syntax like i++ or s += 'foo' He has also the ability to accept inline
> sql statements... I tried to play on the EndOfLine delimiter to handle
> the two if-then-else syntaxes but without success.
>
> --
> Sébastien Kirche
More information about the antlr-interest
mailing list