0
votes

I try an Antlr4 grammar file. When I change define of ID property

ID :[A-Z]+;

to

ID: [A-Z][A-Za-z0-9_]* ;

I got this error.

line 1:7 mismatched input 'E550' expecting {'W', 'I'}

line 1:12 mismatched input ';' expecting {'W', 'I'}

Actualy I know the reason. which mathces with the longest one. But I must use ID Like erroneous way. and my foo must be E or I and Number. How can I make it happen? any help is appreciate.

Here is my code snippet which causes the error.

QUEST E550 ;

Here is my grammar

grammar test;

block: foo+;
foo:ID op=(WARNING|INFORMATION)INT SCOL;
SCOL :';';
WARNING :'W';
INFORMATION :'I';
ID: [A-Z]+ ;
//if I change to ID: [A-Z][A-Za-z0-9_]* ; error occurs
INT : [0-9]+;
SPACE: [ \t\r\n] -> skip;
OTHER: . ;
1
QUEST E550 ; cannot be parsed by the grammar you posted. But I can make an educated guess you forgot to add ERROR : 'E'; to the example grammar.Bart Kiers

1 Answers

0
votes

If your ID rule cannot start with W, I or E, then you will need to exclude those from the start:

ID: [A-DF-HJ-VX-Z] [A-Za-z0-9_]* ;

Of course, then input like EEEEE will not become an ID. To account for such cases, you could (1) let your ID rule start with a single uppercase other than W, I or E followed by the rest, or (2) let it start with 2 letters followed by the rest:

ID
 : [A-DF-HJ-VX-Z] [A-Za-z0-9_]* // (1)
 | [A-Z] [A-Z] [A-Za-z0-9_]*    // (2) 
 ;