Search code examples
nestedgrammarantlr3xtext

xtext nested {} Decision can match input such as "'}'" using multiple alternatives


I am new to xtext (antlr), and don't understand why there is ambiguity between IconType '}' and WindowType '}'.
I get the

warning(200): ../org.simsulla.tools.ui.gui/src-gen/org/simsulla/tools/ui/gui/parser/antlr/internal/InternalGui.g:137:1: Decision can match input such as "'windowType'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../org.simsulla.tools.ui.gui.ui/src-gen/org/simsulla/tools/ui/gui/ui/contentassist/antlr/internal/InternalGui.g:117:38: Decision can match input such as "'windowType' '=' '{' 'name' '=' RULE_ID 'iconType' '=' '{' 'name' '=' RULE_ID 'spriteType' '=' RULE_STRING 'Orientation' '=' RULE_STRING '}' '}'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input

This is my grammar

Ui:
    guiTypes+=GuiTypes+;

GuiTypes:
    windowType+=WindowType+
;

WindowType:
    'windowType' '=' '{'
        'name' '=' name=STRING
        iconType+=IconType* 
    '}'
;

IconType:
    'iconType' '=' '{'
        'name' '=' name=STRING
        'spriteType' '=' spriteType=STRING
        'Orientation' '=' Orientation=STRING
    '}'
;

Solution

  • The grammar fragment has multiple ways of repeating WindowType:

    • deriving from Ui to GuiTypes to multiple WindowType instances, or
    • deriving from Ui to multiple GuiTypes instances where each derives to a WindowType, or
    • any combination of the above.

    So a given input corresponds to multiple different parse trees. The grammar is thus ambiguous.