A Converter from Expression to Graph(dot)
-
build using
Stack
stack build
-
run with
Stack
stack run
-
test with
Stack
stack test
An expression in each line, whose syntax is as below.
We use syntax based on (Actually the same as by now) this CodeWars Kata which meas we Parse an function
defined as below.
function ::= '[' arg-list ']' expression
arg-list ::= /* nothing */
| variable arg-list
expression ::= term
| expression '+' term
| expression '-' term
term ::= factor
| term '*' factor
| term '/' factor
factor ::= number
| variable
| '(' expression ')'
For example these functions below are OK
[ a b ] a*a + b*b
[ first second ] (first + second) / 2
[a b c]a*a+b*b+(a+b)*(a+c)/2
NOTICE: This program DOES NOT generate any image format such as svg
or jpeg
but the content of an dot
file, which can convert to an image file using Graphviz.
So you can use command like this to generate an image
echo "[a b] (a+b)/2" | stack run | dot -Tsvg > graph.svg
BTW this result is as below
Symbol | Meaning |
---|---|
Circle | Operator such as + ,- ,* ,/ |
Box | An Immediately number(only Integers for now) |
Box3D | The Index(beginning with 0 ) of an Argument |
-
When combining sub-trees, there are still some duplicate errors in graph (
[a b] a*a + a*b + a*b + b*b
as below)
- More operator such as
^
- Some new feature like
let...in...
- Named function
- Recursion support