[antlr-interest] left recursion removal
Jim Idle
jimi at temporal-wave.com
Wed Jul 6 14:27:39 PDT 2011
What language? This is usually distinguished by the fact that multi
statement blocks must begin on a new line after THEN or ELSE. VB.Net for
instance.
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 12:20 PM
> To: antlr-interest
> Subject: [antlr-interest] left recursion removal
>
> Hi,
>
> the language for which I am trying to build the grammar has 2 flavors
> of if-then-else constructs
> - a single line : if <condition> then <statement> [else <statement>]
> - a multi line : if <condition> then <statements> [else <statements>]
> end if
>
> I have defined the following (partial) :
>
> codeBlock
> : (compoundStatement)*
> ;
>
> compoundStatement
> : (
> ifStatement
> | singleStatement
> ) (';' | EOL)
> ;
>
> singleStatement
> : localVariableDeclaration
> | funCall
> | 'return' expression
> ;
>
> ifStatement
> : singleLineIf
> | multiLineIf
> ;
>
> singleLineIf
> : 'if' expression 'then' singleStatement EOL
> ;
>
> multiLineIf
> : 'if' expression 'then' codeBlock 'end if'
> ;
>
>
> I understand why the naive ifStatement fails with the following "
> [fatal] rule ifStatement has non-LL(*) decision due to recursive rule
> invocations reachable from alts 1,2. Resolve by left-factoring or
> using syntactic predicates or using backtrack=true option."
>
> I would like to avoid general backtracking, so after searching for a
> while and reading the article
> http://www.antlr.org/wiki/display/ANTLR3/How+to+remove+global+backtrack
> ing+from+your+grammar
> I have tried first
>
> ifStatement
> : 'if' expression 'then' (singleStatement EOL)=>
singleLineIf
> | multiLineIf
> ;
>
> or
>
> ifStatement
> : 'if' expression 'then' (singleStatement EOL | codeBlock
'end
> if')
> ;
>
> But they fail both with the same fatality.
> How this case should be processed ?
> --
> Sébastien Kirche
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address
More information about the antlr-interest
mailing list