简体   繁体   中英

How to use EOF in Xtext

I'm trying to make an Xtext grammar recognize the end of a file as a valid statement, but all I've tried so far does not work.

Trying to use EOF directly will show me a " no viable alternative at input 'EOF' " message in the editor:

Object :
    typ=('char'| 'any' | 'number' | 'space' | 'symbol') ( ' ' | ';' | EOF )
;

So I tried to introduce a terminal that links to EOF:

terminal ETX: EOF;
...
Object :
    typ=('char'| 'any' | 'number' | 'space' | 'symbol') ( ' ' | ';' | ETX )
;

The xtext is compilable that way, but will give me the same error as above in the
"language editor window" (the one which opens when running the xtext as eclipse application)
The error occurs when I do not type a space or semicolon behind the last element (char):

symbol number char(!)

The same thing happens when I put all three allowed elements into a terminal:

terminal END: ( ' ' | ';' | EOF);
...
Object :
    typ=('char'| 'any' | 'number' | 'space' | 'symbol') END
;

The grammar is used to describe a kind of regular-expression-language,
which will be used to parse Strings of a defined format. For example:
(I will replace spaces with underscores to make this clear)

char_symbol_number_
char_symbol_number;

are accepted by the code correction (no errors are displayed).

Whereas

char_symbol_number

will give me the above mentioned error in the second eclipse editor window.
(The one with code completion etc. for my defined language.)

To point this out, what I'm trying to accomplish is a pure cosmetical adjustment,
so that I can leave the last statement as is without having to add a space or semicolon.


So the question is: How do I make Xtext recognize EOF (nothing) as a valid ending statement?

I can only guess from the grammar snippets how your final syntax should look like, but wouldn't

Object: typ=('char'|'any') ';'? ;

work for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM