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+backtracking+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

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to