0

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

Raven
  • 2,951
  • 2
  • 26
  • 42

1 Answers1

0

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

Sebastian Zarnekow
  • 6,609
  • 20
  • 23
  • Not exactly, GLOBALVARCONTENT doesn't allow to start with an underscore so it can be distinguished from LOCALVARCONTENT :) – Raven Aug 07 '14 at 08:04
  • That's true, but it'll still shadow the ID rule in almost all cases (especially in combination with LOCALVARNAME). You may want to try this array name to see the effect: ^_myArray = [] – Sebastian Zarnekow Aug 08 '14 at 07:18
  • Ok now I have it working. Thank you. Is it possible that Terminal-Rules which are defined in your grammar are "checked" before the imported Terminal-Rules? Because as far as I have understood now the problem was that the Parser has checked the name of my Input and compared it with the terminals of my grammar and because it has seen that the name (for example _array1_) matches my GLOBALVARCONTENT-Rule it stopped checking that it could be an ID (Array),too, so it was clear for it that this must be a global Variable. – Raven Aug 08 '14 at 08:54