Skip to content

zoltanvi/FormulaParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FormulaParser

A C# console application prototype for evaluating expressions at runtime.

It tokenizes, parses and interprets the input string, and gives a single number or an array of numbers (Signal) back as a result.

Demo

Demo

AST

How does it work?

  1. The input formula gets tokenized.
Input: "5 * 3 + 1"
Tokens: {{5}, {*}, {3}, {+}, {1}}
  1. An Abstract Syntax Tree is built from the tokens.
Tokens: {{5}, {*}, {3}, {+}, {1}}
Tree:
      +
     / \
    *   1
   / \
  5   3
  1. The Abstract Syntax Tree is interpreted. The tree is traversed in Depth First Traversal and it is evaluated in that order.
Tree:
    +
   / \
  *   1
 / \
5   3

1st evaluation:
  *
 / \     -> 15
5   3

2nd evaluation:
  +
 / \     -> 16
15  1

References

https://en.wikipedia.org/wiki/Context-free_grammar

https://en.wikipedia.org/wiki/Abstract_syntax_tree

https://en.wikipedia.org/wiki/Arity

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages