I have a couple of ANTLR rules that I don’t know how to make them work.
The first rule:
STRING_LITERAL
: '"' ( EscapeSequence | ~('\\'|'"') )* '"'
;
The second rule:
element
: name '=' math_formula ;
math_formula
: '"' expression '"';
Expression is a regular C-like expression
Syntax example
"count" = "array[3]"
count is a string, and array [3] is an expression
My problem is that the lexer always returns both "count" and "array [3]" as strings, and Parser cannot recognize the expression.
I am using java target.
EDIT: change "variable_name" to "count".
EDIT2: explained my second attempt below:
I can detect the beginning of an expression with "=", but I cannot detect the end of the expression in Lexer, causing false line detection when I have two elements separated by the ","
"count1" = "array[1]",
"count2" = "array[2]"
'= " START_EXPRESSION, , , , ", \n ", .
3:
STRING_LITERAL
STRING_LITERAL
: (~('=') '"' ( EscapeSequence | ~('\\'|'"') )* '"')=> '"' ( EscapeSequence | ~('\\'|'"') )* '"'
;
, , ~ ('=') , somthing
user186512