Search code examples
eclipsextextambiguous-grammar

Rule not recognized


I've written a grammar which should allow me to define variables and arrays. Everything worked fine until I split up the variables into local and global variables. Now my parser doesn't recognize the arrays anymore (it says it would be a variable and gives me syntax errors for that).

My Grammar:

grammar sqf.Sqf with org.eclipse.xtext.common.Terminals

generate sqf "http://www.Sqf.sqf" 

Model:
    elements += Element*
;

    Element:
        Declaration ";" | Command ";"
    ;

        Declaration:
            Array | Variable
        ;

            Variable:
                LocalVariable | GlobalVariable
            ;

            LocalVariable:
                name=LOCALVARNAME "=" content=VARCONTENT (("+"|"-"|"*"|"/") content2+=VARCONTENT)*
            ;

            GlobalVariable:
                name=GLOBALVARNAME "=" content=VARCONTENT (("+"|"-"|"*"|"/") content2+=VARCONTENT)*
            ;


            Array:
                name=ID "=" content=ArrayLiteral | name=ID "=" "+" content2=[Array]
            ;

                ArrayLiteral:
                    "[" (content += ArrayContent)* "]" (("+"|"-")content1+=Extension)*
                ;

                    ArrayContent:
                        content01=Acontent ("," content02+=Acontent)*
                    ;

                        Acontent:
                            STRING | DOUBLE | ArrayLiteral
                        ;

                    Extension:
                        STRING | DOUBLE
                    ;




    Command:
        Interaction
    ;

        Interaction:
            hint
        ;

            hint:
                Normal | Format | Special
            ;

                Normal:
                    name=("hint" | "hintC" | "hintCadet" | "hintSilent") content=STRING
                ;

                Format:
                    name=("hint" | "hintC" | "hintCadet" | "hintSilent") "format" "[" content=STRING "," variable=DECREF "]"
                ;

                Special:
                    hintCArray
                ;

                    hintCArray:
                        title=STRING "hintC" (content1=ArrayLiteral | content=STRING)
                    ;


VARCONTENT:
    STRING | DOUBLE | DECREF | "true" | "false" | "nil"
;

DOUBLE:
    INT ("."INT)?
;

DECREF:
    ref1=[Array|ID] | ref2=[LocalVariable|LOCALVARNAME] | ref3=[GlobalVariable|GLOBALVARNAME]
;

terminal LOCALVARNAME:
    "_" ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
;

terminal GLOBALVARNAME:
    '^'?('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
;

Has anybody of you an idea what the problem is?
(Any other code improvements are welcome, too)
Greets Krzmbrzl


Solution

  • Your rule GLOBALVARNAME completely shadows the rule ID. You could simply use ID instead of GLOBALVARNAME.