1

I have an error (like said in the title) with one rule that i dont know how resolve.

i have written the following rule :

FunctionArguments returns FunctionArgs::IFunctionArguments :
    FunctionArgumentsNormal
    | FunctionArgumentsForIter
    ;

FunctionArgumentsNormal returns FunctionArgs::IFunctionArguments :
    {FunctionArgs::FunctionArguments} args+=Expression (',' args+=Expression)* 
    | {FunctionArgs::FunctionArguments} argNames+=NamedArguments (',' argNames+=NamedArguments)*
    ;

FunctionArgumentsForIter returns FunctionArgs::IFunctionArguments :
    {FunctionArgs::FunctionArgumentsIterator} exp=Expression 'for' iterators=ForIterator
    ;

Could you help me to resolve it by left-factoring this expression or give any others solutions please ?

Garett J
  • 13
  • 5
  • 1
    Please state, whether you want to use the Xtext grammar or the ANTLR grammar. They are quite different and this affects how left recursion can be solved. – A.H. Jun 07 '13 at 14:07

1 Answers1

0

In a LL grammar you can have no left-recursion. The problem is that a LL parser can choose to make a derivation like this: FunctionArguments -> FunctionArgumentsNormal -> FunctionArguments -> FunctionArgumentsNormal ... Your grammar contains what is called indirect left recursion. You can find an example in this wikipedia-article which also contains a solution as to how you can fix it: Left-Recursion. A good place to start is by (I suppose you already have done this) writing your grammar in a very simple way without all the annotations and things included in your example. If you have the grammar on the simple form:

S -> A | B
B -> "terminal1" B
  |  b
A -> a "terminal2"

it is much easier to do the necessary rewriting.

Kent Munthe Caspersen
  • 5,918
  • 1
  • 35
  • 34
  • i think that it's going to help me. i will try later.. meanwhile, thx – Garett J Jun 08 '13 at 16:05
  • I know, it's been some time. Still I am not sure, I understand how the grammar given by the OP can generate the above mentioned cycle. At least not from the given grammar snippet. Where does a derivation from `FunctionalArgumentsNormal -> FunctionArguments` happen? – Johnson Feb 20 '18 at 12:33
  • The derivation `FunctionalArgumentsNormal -> FunctionArguments` happens when applying the 2nd rule (either side of the or sign `|`): `{FunctionArgs::FunctionArguments} args+=Expression (',' args+=Expression)*` – Kent Munthe Caspersen Feb 21 '18 at 11:03