Download the lab zip file: lab5.zip
(i) Using a Parser Generator
The folder arith contains a Python implemenation of the arithmetic language from figure 6.17 of the textbook. It is based on the parser described in the testbook on pages 233-235. The readme file provides and example execution.
You can read more about the implementation here
.
Modify the grammar to add exponents.
expr -> term + expr | term
term -> pow * term | pow
pow -> factor ^ pow | factor
factor -> ( expr ) | number
number -> number digit | digit
Hint: The exponent 2^5 is written pow(2,5) in python.
After modifying the code, you should be able to replicate the following example
Hello. Type ctrl-D (End of File) to quit.
Type an expression followed by enter. For example, 5 * 6 + 4 * ( 8 + 3)
2^8
The Answer is 256
3^9
The Answer is 19683
(8*6)^3
The Answer is 110592
You can read more about PLY on their website
(ii) Compiling DOT files
The folder regexp contains a parser that reads in regular expressions and outputs dot files showing the NDFA for the regular expression. The code given only handles union and single characters.
This exercise will show you how to run your code and generate an image from the DOT file. You will NEED to do this in lab to show your modification work.
Run the parser generator using the following example. Keep in mind, the code is unfinished, (,),*, and concat do not work yet. You will fix this in lab.
$ python reg_expr.py
Generating LALR tables
Hello. Type ctrl-D (End of File) to quit.
Enter a regular expression using a-z, *, and | )
Example: (a|b)*abb
a|b|c
Graph Written to output_0.dot
a
Graph Written to output_1.dot
To view the DOT file you have two options.
dot -Tgif -o0.gif output_0.dot
then view it display 0.gif
. If you are using tux remotely through ssh, you can only view images if you are using ssh forwarding.