I have an ANTLR grammar that can parse and evaluate simple expressions like 1+2*4, etc.
What I would like to do is to evaluate expressions like 2+$a-$b/4 where the $ variables are dynamic variables, that come from an external source and are continuously updated.
Is there any design pattern on how to do this using ANTLR, best practices, etc?
- Shall I “substring” the $a with the updated value ($a -> 4.34)
- A nicer way to do this?
Thx
There is actually an example for this in the ANTLR book (the ANTLR Definitive Reference.) The pattern is to parse the variable values and add them to a dictionary in the target language:
where ‘expr’ can be any valid expression (including an expression containing a variable.)