1

I am currently working on an Xtext grammar and got some issues with left recursive graphs. I already eliminated all direct left recursions in my grammar but now I have some indirect left recursions, which are shown in the IDE with the message This rule call is part of a left recursive call graph.

Here is an example of my problem:

grammar com.stackoverflow.Example with org.eclipse.xtext.common.Terminals

generate example "http://stackoverflow.com/Example"

Type:
    var157=ValueType | var158=ReferenceType;

ValueType:
    var160=StructType | var161=EnumType;

StructType:
    var162=TypeName | var163=SimpleType | var164=NullableType;

TypeName:
    var165=ID;

SimpleType:
    var166=NumericType | "bool";

NumericType:
    "decimal";

NullableType:
    var169=NonNullableValueType "?";

NonNullableValueType:
    var170=Type;

EnumType:
    var171=TypeName;

ReferenceType:
    var172=ClassType | var173=InterfaceType | var174=ArrayType;

ClassType:
    var176=TypeName | "object" | "dynamic" | "string";

InterfaceType:
    var177=TypeName;

ArrayType:
    var178=NonArrayType "[]";

NonArrayType:
    var180=Type;

How can I resolve such left recursions?

boindiil
  • 5,805
  • 1
  • 28
  • 31

1 Answers1

1

this grammar is not really left factored. and it is highly ambigous. to get this running here is a starting point (ignoring the ambiguities and leaving out some stuff)

Type:
    ReferenceType;

TypeName returns Type:
    var165=ID;

ReferenceType returns Type:
     ClassType (({NullableType.type=current} "?") | ({ArrayType.componentType=current} "[]"))*;

ClassType returns Type:
    TypeName | ({ClassType} type=("object" | "dynamic" | "string"));
Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32