java - Antlr4 doesn't recognize identifiers -


i'm trying create grammar parses file line line.

grammar comp;  options  {     language = java; }  @header {     package analyseur;     import java.util.*;     import component.*; }  @parser::members {     /** line write in new java file */     public string line; }  start            : objectrule        {system.out.println("obj");  line = $objectrule.text;}         | anystring         {system.out.println("any");  line = $anystring.text;}         ;  objectrule : objectkeyword id ;  anystring : any_string ;   objectkeyword :  'object' ; id  :   [a-za-z]+ ; any_string :  (~'\n')+ ; whitespace : (' '|'\t') -> skip; 

when send lexem 'object o' grammar, output instead of obj.

'object o'   =>  'any'   // obj 

i know any_string longer wrote lexer tokens in order. problem ?

thank ! ;)

for lexer rules, rule longest match wins, independent of rule ordering. if match length same, first listed rule wins.

to make rule order meaningful, reduce possible match length of any_string rule same or less key word or id:

any_string: ~( ' ' | '\n' | '\t' ) ; // also?: '\r' | '\f' | '_'  

update

to see lexer doing, dump token stream.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -