$24
You may use any programming language that runs on the ilab machines for these problems.
Use a recursive descent parser to implement an interpreter for this (LL(1)) arithmetic grammar:
hProgrami ::= hStmtListi .
hStmtListi ::= hStmti hNextStmti hNextStmti ::= ; hStmtListi j
hStmti ::= hAssigni j hPrinti
hAssigni ::= hIdi = hExpri
hPrinti ::= ! hIdi
hExpri ::= + hExpri hExpri
- hExpri hExpri
* hExpri hExpri
/ hExpri hExpri
hIdi
hConsti
hIdi ::= a j b j c
hConsti ::= 0 j 1 j 2 j 3 j 4 j 5 j 6 j 7 j 8 j 9
The arithmetic operators indicate addition, subtraction, multiplication, and (integer) division, respectively.
For example, on the input
a=3;b=2;c=+ab;!c.
Your program should print 5.
For invalid inputs, such as
a=+12;!a.b
you should print \syntax error".
For runtime errors, print \exception".
Please treat referencing a variable before it’s assigned a value as an error.
Note that all tokens are single characters, and you can assume we will not include any whitespace, which should help simplify the scanner implemen-tation.
Consider this grammar of boolean expressions:
hProgrami ::= hExpri .
hExpri ::= hExpri && hExpri j hExpri jj hExpri j hExpri ^ hExpri j hExpri
j hConsti
j ( hExpri )
hConsti ::= F j T
where the expression operators denote AND, OR, XOR, and NOT, re-spectively.
What happens if you try to use this grammar in ex/bison?
Modify the grammar to make ex/bison happy, without changing the language itself. Binary operators should have the precendence &&, ^, || (highest to lowest) and be left associative.
Use your modi ed grammar and ex/bison to implement an inter-preter for the language. For invalid inputs, you should print \syntax error". For example, on input F || T., you should print T. (just T, not the period)
2