Grammar in ANTLR and the selected words -
edit: changed example explain better trying get. grammar:
integer : ' int '; let : [a-z] ; cchar : let | '-' | ' ' ; wor : cchar+; aaa : wor+ | wor* integer wor* ; aaa root. , writing eg.: 'xx int xx int'. result: 'x x int x x n t'. first int should catched, next 1 should not give "extraneous input" mistake splitted letters.
how can fix it?
this seems work wanted:
let : [a-z]; int : 'int '; cchar : let | '-' | ' '; wor: cchar+; int_string: int; aaa: (wor|int_string)+; what grammar says that: alow me word or integer declaration, integer declaration if 'int' followed space defined lexer item, else words.
now following not work:
let : [a-z]; int : 'int'; cchar : let | '-' | ' '; wor: cchar+; int_string: int ' '; aaa: (wor|int_string)+; after moving space parser rules instead of lexer rules, fails parse 'intt' example, in fact word has 'int' substring. happens because lexer part seems read occurence of 'int' int , wor not parse 'intt' string now, tries match (wor int (cchar t)) , fails reason not matching 'int' separate cchars.
the first example's wor rule parses 'intt' (wor (cchar i) (cchar n) (cchar t) (cchar t)). makes sense. first example's grammar can't match in lexer phase because space character required lexer rule int not there in 'intt'.
why has done so? i'd think because lexer runs before parser , parser gets semantic equivalent. replacing lexer rule int 'int' in int_string in second example yields same behaviour expect antlr generates hidden lexer rule match. not 100% though.
tell me if helps, if come way fix second case, i'll make edit :)
Comments
Post a Comment