Course Homework for Programming Languages. Implementing ARITH in Python
As per the homework requirement, this script will do the following things:
- The script gets inputs via stdin and output via stdout
- The script parses the input into an Abstract Syntax Tree (AST)
- The AST is evaulated in the interpreter and the script outputs the results.
arith has three features, addition, multiplication and subtraction. Operations are applied to integers.
python3 >=3.6 pyinstaller ==3.6
The grammar in the homework is
<expr> ::== <int>
|<expr> + <expr>
|<expr> * <expr>
However, to capture the order of predence, my grammar is
<expr> ::== <trm>
| <expr> + <trm>
| <expr> - <trm>
<trm> ::== <fac>
|<trm> * <fac>
<fac> ::== <int>
The AST is generated with this grammar.
Coming from not really knowing how interpreters work and needing to finish the homework within a week, here are the links I went over to get familiar with the topic:
- Grammar, Syntax and Semantics: https://opendsa-server.cs.vt.edu/ODSA/Books/PL/html/index.html
- Explanation of the concept: https://blog.usejournal.com/writing-your-own-programming-language-and-compiler-with-python-a468970ae6df
- Tutorial for Python implementation: https://blog.usejournal.com/writing-your-own-programming-language-and-compiler-with-python-a468970ae6df