Conditional lexer rules in Antlr4 -
given grammar:
grammar colontest; main : statement* eof; statement : num_literal expression semicolon; expression : primary (mult_op primary)*; primary : word+; num_literal : [0-9]+; semicolon : ';'; mult_op : '*' | '/'; // | ':'; word : ('a'..'z' | 'a'..'z')+; ws : [ \t\r\n]+ -> skip; colon : ':' -> skip;
and input:
1 : statement; 2 : splitted statement : 2 lines; 3 : * b / c; 4 : * b : c;
in 4'th line second colon skipped because of "colon" lexer rule. need colon, because part of language (lets should part of mult_op keyword too). how achieve this?
edit 1:
after deleting colon : ':' -> skip;
, inserting:
statement : num_literal ':' expression (':' expression)* semicolon;
the tree looks this:
the desired tree should this:
edit 2:
how this? implicit tokens defined - works.
grammar multiline; main : statement* eof; statement : num_literal ':' expression semicolon; expression : primary ((mult_op|':') primary)*; primary : word+; num_literal : [0-9]+; semicolon : ';'; mult_op : '*' | '/'; word : ('a'..'z' | 'a'..'z')+; ws2 : [\r\n]+ [ \t]+ ':' -> skip; // removes not needed colons ws : [ \t\r\n]+ -> skip;
can improve code in other way?
tokens such :
should not skipped. then, alter statement
rule be:
statement : num_literal expression (':' expression)* semicolon;
Comments
Post a Comment