CS 325 Project Part II
You are to write a parser for
SIMPL. The following is the LL(1)
grammar for the SIMPL language. Note: the \ indicates a meta-character is being
used as a literal
<prog>
::= <id><idtype> \( <varlist> \) \| <varlist>
\| <statlist> !
<varlist> ::= <id> <idtype> <varlist>
| e
<idtype>
::= ‘bool | e
<statlist> ::= <stat> <statlist> | e
<stat> ::= <assign> | <ifstat> |
<whilestat>
<assign> ::= <id> <- <expr>
!
<ifstat> ::= if <expr> ? <statlist>
: <statlist> !
<whilestat> ::= while
<expr> ? <statlist> !
<expr> ::= <aexp>
<exprtail>
<exprtail ::= = <aexp>
<exprtail> | > <aexp>
<exprtail> | e
<aexp> ::= <mexp>
<aexptail>
<aexptail> ::= + <mexp>
<aexptail> | - <mexp>
<aexptail> | e
<mexp> ::= <atom> <mexptail>
<mexptail> ::= * <atom> <mexptail> | / <atom> <mexptail>
| e
<atom> ::= <id> | <num> | <boolean>
| - <atom> | \( <expr> \)
Error handling is very simple. When your compiler encounters a lexical or syntax error it should print an appropriate error message an abort by throwing an exception. This will require that you also change the error handling in the scanner. Try to make your error messages as meaningful as you can.
You need to write the driver for your program which will parse an input file and print out the syntax tree.
The nodes of the SIMPL syntax tree is given below.
Node Type |
Children |
Description |
PROG |
p1 p2 p3 p4 |
The top level program node, with name p1, input
variables p2, local variables p3, |
ID |
|
A node representing a variable reference or declaration. |
SPACE |
p1 p2 |
A list of variable declarations, with head p1, and
tail p2. |
NULL |
|
The empty list node. |
! |
p1 p2 |
A list of statements, with head p1, and tail p2. |
IF |
p1 p2 p3 |
An if statement node, with
test expression p1, then part p2, and else
part p3. |
WHILE |
p1 p2 |
A while statement node, with test expression
p1, and body p2. |
<- |
p1 p2 |
An assignment statement node, with variable l-value p1,
and r-value expression p2. |
> |
p1 p2 |
A greater than operator, with left operand p1, and
right operand p2. |
= |
p1 p2 |
An equals operator, with left
operand p1, and right operand p2. |
+ |
p1 p2 |
An addition or OR operator, with
left operand p1, and right operand p2. |
- |
p1 p2 |
A subtraction operator, with left operand p1, and
right operand p2. |
* |
p1 p2 |
A multiplication or AND operator, with left operand p1,
and right operand p2. |
/ |
p1 p2 |
A division operator, with left operand p1, and
right operand p2. |
NEG |
p1 |
A unary negation or NOT operator with operand p1 |
NUM |
|
A node representing a counting number constant. |
BOOL |
|
A node representing a boolean
constant |
As an example of a
syntax tree, the following SIMPL program produces the syntax tree below:
x(y)
|z w| if y > 10? x <- z ! : !!
prog
id x
space
id y
e
space
id z
space
id w
e
if
>
id y
num 10
!
<-
id x
id z
e
e