Ask coding questions

← Back to all posts
Trying to add subtract multiply and divide variables together but Im getting an error
JacobMcPherson1

When I try to define a variable like t = 5 and do println(t*t) I get an error. Can someone please help me fix this? The problem is in parser.py

Answered by IMayBeMe [earned 5 cycles]
View Answer
Voters
JacobMcPherson1
Comments
hotnewtop
IMayBeMe

In line 151 try switching the two p.NAME‘s to p.NAME0 and p.NAME1respectively. Also if you want to lower the complexity of your parser, I would recommend adding an interpreting stage and having the parser return an AST

JacobMcPherson1

@IMayBeMe Thank you very much sir! And do you mind helping me make the interpreting stage, and explaining what an AST is?

IMayBeMe

@JacobMcPherson1 Though I don’t have enough time to make an interpreting stage, I can explain what an AST is an get you started in tinge write direction.

For starters, currently when you look at the design of your language, you can see that the syntax is turned into a stream toke using the lexer which will stay the same. Right now, that stream of tokens is being parsed and executed at the same time which can lead to confusion for the parser. To alleviate this what you can do is have the parser return a set of instructions in a tree like form. This is where the AST comes in as it stands for abstract syntax tree and is essentially a set of instructions of the last stage. Now back to the parser, since python doesn’t actually have a tree data structure, we are better off using nested tuples with instructions something like this: (“println”, (“add”, p.NAME0, p.NAME1))

That was a very generalized example but my point here being that you will have to interpret the AST recursively. Here is a good example on an interpret/execute stage: https://www.google.com/amp/s/www.geeksforgeeks.org/how-to-create-a-programming-language-using-python/amp/

VulcanWM

Maybe it is because tt is an integer and you can only print strings?
Try converting t
t into a string

JacobMcPherson1

@VulcanWM My "programming language" has support for printing integers so that cant be it. But thanks anyway!

VulcanWM

oh okay, what command do I do to see the error? @JacobMcPherson1

JacobMcPherson1

@VulcanWM make any variable with a number, for example, t = 5 then hit enter and type println(t + t)

VulcanWM

oh okay, and wheres the code for that? @JacobMcPherson1

JacobMcPherson1

@VulcanWM the error is in the parser.py file if that's what you mean