• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Antlr4 Help: Algebraic Expressions

Status
Not open for further replies.
So, currently I'm working on exercise 2 in the Antlr4 book, and I have built a 5 operation calculator that works with doubles.

It's able to parse things like this and give the results

a = 2 3(3+3)(3)a
b = 4(2^3*2)
a/b


Now, what I currently can't do is stuff like this

//where a has no value
3a + 4a //should output 7a, but will output 0 as a is assumed to be 0


Any suggestions? Right now I'm just using a Visitor. Will I need to change over to a Listener?

Should the Visitor instead return expressions?


Thanks =)
 
Well I don't know of Antlr4 but I've worked a lot with parsers and lexers.

Normaly after you've made your syntactic tree and passed it by the semantic analyser. You should build a symbol table with your identifiers and (in this case) a program flow variable which stores results as they are calculated. Later in your interpreter, if you get into a syntactic treee and you find (Exp Ident + Expt Ident) you should say it returns (Exp + Exp) Ident and keep it stored in your program flow variable.
 
Status
Not open for further replies.
Top