Search code examples
parsinggrammarxtextbacktracking

Xtext: enter wrong rule or 'missing RULE_* at'


I want to parse all names from a random text. Names will be formatted like this:

Lastname F.

where F - first letter of first name. So, I created this grammar:

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

generate article "http://www.xtext.org/example/mydsl/Article"

Model : {Model}((per += Person)|(words += NON_WS))*;
Person : lastName = NAME firstName = IN;

terminal NAME : ('A'..'Z')('a'..'z')+;
terminal IN : ('A'..'Z')'.';
terminal NON_WS : !(' '|'\t'|'\r'|'\n')+;

It works on this example:

Lastname F. some text. Lastname F.

But it crashes on this one:

Lastname F. some text. New sentence. Lastname F.
                           ^^^^^^^^^ missing RULE_IN at 'sentence.'

How do I include a checking of all tokens before the generation of the 'Person' object or before the entering the 'Person' rule?


Solution

  • lexing is done kontext free. thus one lexed a name, always lexed a name

    Model : {Model}((per += Person)|(words += (NON_WS|NAME)))*;