#Parse math expressions

42 messages · Page 1 of 1 (latest)

pure dome
#

My program is taking in strings such as "5.5 + 5", "sin(90)", "5^2" and so fourth. How can I categorise pieces of the string? (5 is a number and + is an operation etc) And how can I process the pieces so I can return 10.5 after "5.5 + 5"?

Here is the list of valid pieces I want in my program.

nums, decimal and negator:
1 2 3 4 5 6 7 8 9 0 . -

operations:
+ - * / ^

math commands:
sin(...) cos(...) tan(...) sqrt(...)

custom commands:
help quit
native quartzBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hollow surge
#

the step of taking a string and splitting it up into individual tokens (pieces) is called tokenization

#

from those tokens you can create an abstract sytax tree you do that with a parser constructs that AST based on a grammar

#

once you have an AST you can evaluate it

pure dome
#

thanks! I'll look it up

subtle raptor
#

there's a lot to learn

#

also related to compilers

pure dome
#

haha I'll try to keep it simple then 😅

hollow surge
#

it took me the better part of a year to understand parsing and being able to write my own grammar

#

if you dont want to write your own parser take a look at

  • flex
  • yacc
  • gnu bison
  • packcc
#

there are tons of parser generators

#

oh and

pure dome
#

feels a bit cheaty since this is the hardest part of the program. do you think it is too hard for a beginner using the tokens listed above as well as () and spaces for separation?

hollow surge
#

im not a beginner and it took me a year

#

but im also not the brightest trolol

hardy parrot
#

When I made my own calculator app I consumed tokens and then immediately evaluated them.

pure dome
#

haha very well, I'll keep this thread open and look at different articles for now

subtle raptor
hollow surge
#

yeah theres also a thing called uh

hardy parrot
hollow surge
#

In computer science, the shunting yard algorithm is a method for parsing arithmetical or logical expressions, or a combination of both, specified in infix notation. It can produce either a postfix notation string, also known as Reverse Polish notation (RPN), or an abstract syntax tree (AST). The algorithm was invented by Edsger Dijkstra, first p...

#

thats straight forward

#

you convert smth like
1 + 2 (infix notation)
into

  • 1 2 (polish notation)
    which you can feed into that algorithm
hardy parrot
#

You don't need the "to Polish notation" part if all you want is to build a syntax tree or to evaluate it.

subtle raptor
hardy parrot
#

It's 2#value.

subtle raptor
#

ok

hardy parrot
#

Also there is a button on the virtual keyboard.

#

So you can write 2√value.

hollow surge
night berry
#

You can process the string itself through shunting yard but it's better if you tokenize it first (lexer)

native quartzBOT
#

@pure dome Has your question been resolved? If so, type !solved :)

desert lily
pure dome
#

thanks everyone, haven't progressed a lot but working on a tokenizer. if anyone is interested, I found a very good video about the shunting yard algorithm

native quartzBOT
#

@pure dome Has your question been resolved? If so, type !solved :)

tribal cove
pure dome
#

update: I finished my tokenizer (function got pretty big, around 100 lines 😅 ) and got some error handling (5..3, 5**1, sin)90) etc are invalid)

I understand the shunting yard algorithm enough to try my own implementation now, thanks again

#

!solved