parsing - ANTLR4 grammar not behaving as expected -


i have data required parsed. using antlr4 tool auto generate java parsers , lexers, can use form structured data input data given below grammar:

grammar subdata; subdata:     data+; data:     array; array:     '[' obj (',' obj)* ']'; intarray:     '[' number (',' number)* ']'; number:     int; obj:     '{' pair (',' pair)* '}'; pair:     key '=' value; key:     word; value:     int | word | intarray; word:     [a-za-z0-9]+; int:     [0-9]+; ws:     [ \t\n\r]+ -> skip; 

test input data:

[     {omedademographictype=1, omedademographicid=100, omedademographicvalue=4},      {omedademographictype=1, omedademographicid=101, omedademographicvalue=26},      {         omedademographictype=2, omedademographicid=102, omedademographicvalue=[16,34]     } ] 

ouput:

line 5:79 mismatched input '16' expecting int line 5:82 mismatched input '34' expecting int 

gui tree o/p

parser failing although have integer value @ above expected position.

you've made classic mistake of not ordering lexer rules properly. should read , understand priority rules , consequences.

in case, int never able match since word rule can match int rule can, , it's defined first in grammar. these 16 , 32 example words.

you should remove ambiguity not allowing word start digit:

word:     [a-za-z] [a-za-z0-9]*; int:     [0-9]+; 

or swapping order of rules:

int:     [0-9]+; word:     [a-za-z0-9]+; 

in case, can't have words numeric, still able start number.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -