It seems that sometimes Antlr lexer makes a bad choice, which rule to use when tokenizing the character stream ... I'm trying to figure out how to help Antlr make the right choice for a person, I want to parse the text as follows:
d/dt(x)=a
a=d/dt
d=3
dt=4
This is a sad syntax that uses an existing language, and I'm trying to write a parser. "D / dt (x)" represents the left side of the differential equation. Ignore the jargon if you need to, just know that it is not a "d" divided by a "dt". However, the second appearance of "d / dt" is indeed "d" divided by "dt".
Here is my grammar:
grammar diffeq_grammar;
program : (statement? NEWLINE)*;
statement
: diffeq
| assignment;
diffeq : DDT ID ')' '=' ID;
assignment
: ID '=' NUMBER
| ID '=' ID '/' ID
;
DDT : 'd/dt(';
ID : 'a'..'z'+;
NUMBER : '0'..'9'+;
NEWLINE : '\r\n'|'\r'|'\n';
lexer "d/dt (" DDT. ! "d" , "/", "hmmm, "/", ". ... , "(" "(" MismatchedTokenException!
, , , , :
grammar diffeq_grammar;
program : (statement? NEWLINE)*;
statement
: diffeq
| assignment;
diffeq : ddt id ')' '=' id;
assignment
: id '=' number
| id '=' id '/' id
;
ddt : 'd' '/' 'd' 't' '(';
id : CHAR+;
number : DIGIT+;
CHAR : 'a'..'z';
DIGIT : '0'..'9';
NEWLINE : '\r\n'|'\r'|'\n';
, , . 2 , , , ... . - Antlr lexer : DDT ID. DDT, . DDT , lexer ID.
, (.. , . ).
lexer DDT Antlr... .
- Java.
!
UPDATE
, , !! , . , , ( ), , . , ; , .