Skip to content

How to avoid parsing the same input multiple times? #92

Answered by renggli
albertodev01 asked this question in Q&A
Discussion options

You must be logged in to vote

There are different ways of solving your problem, each with different performance characteristics of performance, flexibility and required coding:

The typical approach described in compiler-construction books is to build an abstract syntax tree (AST), a tree of objects that represent your expression. To evaluate your expression you can then traverse the tree (or print, simplify, or optimize it). However, this comes at the cost of creating a lot of classes for each node type (unary operator, binary operator, value, variable, ...); but is likely the best way in terms of execution speed and flexibility.

A quick and dirty alternative for your use-case is to make you parser build functions of …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by renggli
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #92 on December 08, 2020 19:51.