rangegrammarkeywordxtextdisambiguation

Xtext disambiguation


Given the following grammar:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Program:
    {Range} ID '.' '.' ID
  | {Group} ID ID ID ID
;

terminal ID:
    'a' | '.'
;

and the following input:

a . . a

I would argue that there are two ways in which the string can be parsed: as a Range (the first alternative) or as a Group (the second alternative). When I try this in my generated IDE and inspect the Ecore model, a Range is instantiated.

What makes Xtext decide in favor of the Range?

Edit: specifically, I'm wondering why the Xtext grammar itself is not ambiguous, since a range 'a'..'z' can be parsed as either a Group of Keyword, Wildcard, Wildcard, Keyword or as a CharacterRange of Keyword, Keyword.


Solution

  • Keywords become Lexer Rules a well. Thus you have two Lexer Rules

    terminal FULL_STOP_KEYWORD: '.' ;
    

    and

    terminal ID: 'a' | '.';
    

    The Lexer is not stateful. Only one rule can win. Thus '.' will always be lexed as Keyword and never as ID