[antlr-interest] newbie left-recursive problem

chintan chintanraoh at gmail.com
Thu Jul 5 08:03:21 PDT 2007


On Thu, 2007-07-05 at 07:19 -0700, ali azimi wrote:
Hello,

> Hi,
>  
> I have read some articles about left-recursive problem and managed to
> understand the nature of the problem. However I can not remedy the
> problem I am facing in my grammar. I am hoping you can help me. 
>  
> You can see left-recursive problems in the followings. how can I
> remedy them?
> variable:fieldmodify|elementmodify|variableidentifier 
>  ;
> elementmodify
>  :variable'('expression(','expression)*')'
>  ;
> fieldmodify
>  :variable'!'fieldname
>  ;
> expression 
>  :literalidentifier|synonymidentifier|variableidentifier|fieldextract|
> elementextract|infixexpression|parenthesisexpression
>  |operatorapplication|valuemake|imperativeoperator|
> conditionalexpression|forallname|spellingexpression
>  ;
make it 
expression:
	others| fieldextract|elementextract|infixexpression;
others:
literalidentifier|synonymidentifier|variableidentifier |
parenthesisexpression|operatorapplication|valuemake|imperativeoperator|
conditionalexpression|forallname|spellingexpression;

Look at the rule "expression";

substitute  all rules  in expression except others
It becomes

expression:
others
|expression dyadicoperator expression |monadicoperator expression
|expression '('expression (',' expression)*')'
|expression '!'fieldname;

which is same as 
expression:
others
| expression(
	dyadicoperator expression | '('expression (',' expression)*')' |
	 '!'fieldname;
            )
| monadicoperator expression
;

which is same as

expression:
(others | monadicoperator expression)
((dyadicoperator expression) | '('expression (','expression)*')' |
'!'fieldname;)* ;

I vaguely remember what was taught in Theory Of Computation.
I tired to be as correct as possible. Correct me if i am wrong in any of
the steps . Hope this helps though :) .

You wont need rules quoted below.
ie infixexpression ,elementextract and fieldextract

>  
> and the followings:
>  
> infixexpression
>  :expression dyadicoperator expression
>  |monadicoperator expression
>  ;
> elementextract
>  :expression '('expression (',' expression)*')'
>  ;
> fieldextract
>  :expression '!'fieldname
>  ;
>  
>  
> Thank you so much in advance,
> Al
Cheers,
Chintan Rao H
> 
> ______________________________________________________________________
> Get the free Yahoo! toolbar and rest assured with the added security
> of spyware protection. 



More information about the antlr-interest mailing list