Motivation
Drawing a venn diagram isn't that hard.However, drawing a venn diagram from a formula is complex.
Therefore, this project supports a language that can draw how venn diagram looks like.
This project was made for 3 set but it can be extended to more sets.
Language specification
This langauge consists of the following properties.In the example below, S0,S1,S2,U are basic sets.
In this language, union represented as +.
In this language, intersection represented as *.
In this language, difference represented as -.
There is no re-delcaration for any variables.
Implementation
This language consists of three compilation phases.1. Scaner, 2.Parser 3.Evaluation. It doesn't convert result to the binary or bytecode because it doesn't have any interaction after it produced.
To parse language, it uses a top-down parser.
Language consist of following rules.
Notice that as it shows, language has following priority.
- Intersection applied first.
- Union applied second.
- Difference apllied last.
$ \begin{array}{lcl} Program & ::= & Statement[\n]Program \\ & ::= & Statement \\ Statement & ::= & Declaration \\ & ::= & Print \\ & ::= & Comment \\ & ::= & λ \\ Declaration & ::= & Var [=] Exp\_Diff \\ Print & ::= & [\text{print1}] Exp\_Diff \\ & ::= & [\text{print2}] Exp\_Diff \\ Exp\_Diff & ::= & Exp\_Diff [-] Exp\_Uni \\ & ::= & Exp\_Uni \\ Exp\_Uni & ::= & Exp\_Uni [+] Exp\_Inter \\ & ::= & Exp\_Inter \\ Exp\_Inter & ::= & Exp\_PT [*] Exp\_Inter \\ & ::= & Exp\_PT \\ Exp\_PT & ::= & [(] Exp\_Diff [)] \\ & ::= & Var \\ Var & ::= & [a-zA-Z][a-zA-Z0-9]* \\ Comment & ::= & [/][/][\text{Any character except }\n]* \\ \end{array} $
Scanner
Scanner will produce a list of tokens from a code.Possible tokens types are as follows.
Variable, Operation, Print, Parenthesis, EndOfLine.
In the scanning process, it will ignore comments and empty lines.
Parser and evaluation
Parser will produce an AST from a list of tokens.AST's node will be one of followings.
1. Operation, it will be a regular operation node to compute.
They are one of followings, +(union), -(difference), *(intersection), and =(assignment).
In the parsing, it will read out the parenthesis and make a correct orders.
Notice that union and difference are left-associative and all others are right-associative.
2. Print, it's only system call that this langauge has. It will print out result to the canvas.
3. Varaible, this node means an object for a variable.
If it is left of the assignment operation, it will mean a name of variable.
If it is right of the assignment operation or some where other, it will mean a value of variable.
4. State, this node doesn't do anything but it chains statements.
In the evaluation stage, it follows ast and evaulates.