user2124486
user2124486

Reputation: 23

ANTLR grammar: already known token used within not known expressions

I have a combined ANTLR grammar, which shall be used for parsing several lines of information. It is possible that at the time of writing the grammar, not all lines are already completely known and defined within the grammar. This shall be recognized. Following example is a simplified one:

rule:    (line)+ EOF;
LF:      ('\n'|'\r\n');
WS:      ' ';

INTEGER: ('0'..'9');
VALUE:   ('a'..'z'|'A'..'Z'|'0'..'9');

line:    'car' WS VALUE WS LF (subline LF)*;
subline: '>' (description | id | type | unknownsubline);

description: ('description' WS VALUE);
id:          ('id' WS INTEGER);
type:        ('type' WS VALUE);

unknownsubline:          (VALUE | WS | INTEGER)*;   /*don't known yet -> shall be logged...*/

I put the following input:

car car1
>description redPorsche
>id 123
>type pkw
>tires 4
>specifica fast,car
car car2
>description blueTruck

The line >tires 4 is recognized successfully within the ANTLR interpreter within Eclipse. But the next line >specifica fast,carcauses an NoViableAltException due to the word car is an already defined token, which is here used within an unknown context.

Is there any possibility to avoid this behaviour? Is it possible to recognise VALUE that contain already defined tokens?

Upvotes: 0

Views: 154

Answers (1)

Apalala
Apalala

Reputation: 9224

Don't make 'car' a keyword. Use a syntactic action instead:

line : car WS VALUE WS LF (subline LF)*;

car : id=VALUE {$id.text == "car"}? ();

Note that your definition of VALUE seems to be missing a + at the end.

Upvotes: 1

Related Questions