• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Antlr4 Help: Algebraic Expressions

Status
Not open for further replies.
Level 31
Joined
Jul 10, 2007
Messages
6,306
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