1
votes

I´m new to xtext and have a (for me) big problem. I wrote some code and it wont compile. I wanted to make a whitespace aware language. Here is my code:

Model:
    (declarationsc+=Declaration);

Declaration:
    Section;

    // Start new section - Needs at least one message
Section:
     (name+=ID) a+=Message;

    // New Message - Needs indentation and at least one Signal or Struct
Message:
    ( name+=ID ((st+=Struct2) | (sig+=Signal)) )+;

    // New Signal with indentation -> If Signal follows Message
Signal:
    BEGIN
     name+=ID  (struc+=Struct)?  (asf+=Signal2)?  (struc+=Struct)?
    END;

    // New Signal without indentation -> If Struct contains no Signal
Signal2:
    ( name+=ID   (struc+=Struct)?)+;

    // New Struct without indentation -> Can follow a Signal
Struct:
    ( name+=ID (stru+=Struct2)? (st+=Signal)? (sk+=Signal2)?)+;

    // New Struct with indentation -> If Struct follows Message
Struct2:
    BEGIN
     name+=ID (st+=Signal)? (ad+=Struct)?  (sign+=Signal2)? 
    END;

    // The following synthetic tokens are used for the indentation-aware blocks
terminal BEGIN:
    'synthetic:BEGIN'; // increase indentation
terminal END:
    'synthetic:END'; // decrease indentation
// Single line comment a
terminal SL_COMMENT:
    '#' !('\n' | '\r')* ; // ('\r'? '\n')?;

Please explain me why and how I can solve this.

1

1 Answers

0
votes

Your grammar is highly ambiguous. How should the parser decide when facing an identifier which rule it actually has to follow? Everything just starts with an identifier (ID) followed by other identifiers. There is no way to actually parse the text.

You may consider to add keywords to introduce rules, e.g. Section: 'section' name=ID a=Message

Please also note that += will result in a collection valued feature, but at least for name it does not make sense.

Please consider to follow some of the standard examples. Lorenzo Bettini's book is also a valuable source of information to understand basic concepts.