Question 1
Given the following grammar:
S ® D
D ® D;D
D ® I:id
I ® id
I ® I,id
Write a parser for this language, i.e. a program that will take any input and answer "yes" if it corresponds to this grammar, and "no" otherwise (id consists of a sequence of one or more alphabetic characters).
SolutionQuestion 2
Given the following grammar for arithmetic expressions:
S
® EE
® E+TE
® TT
® T*FT
® FF
® numF
® (E)Write a program that takes any valid input for this language and writes out the integer resulting from the evaluation of the input expression (num consists a sequence of one or more decimal digits).
SolutionQuestion 3
Given the following grammar for a fragment of Pascal:
S
® StatStat
® begin Statlist endStat
® while Expr do StatStat
® id:=ExprStatlist
® Statlist;StatStatlist
® StatExpr
® idWrite a program that translates this into the equivalent in C, where the equivalent is as follows:
|
Pascal |
C |
|
Stat ® begin Statlist end |
Stat ® { Statlist } |
|
Stat ® while Expr do Stat |
Stat ® while (Expr) Stat |
|
Stat ® id:=Expr |
Stat ® id=Expr; |
|
Statlist ® Statlist ; Stat |
Stat ® Statlist Stat |
(id consists of a sequence of one or more alphabetic characters)
Solution