Question 1
Given the following grammar:
S
® EE
® E[I]E
® idI
® id,II
® idWrite 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 a fragment of propositional logic:
S
® EE
® T and EE
® TT
® T => FT
® FF
® trueF
® falseF
® (E)Write a program that takes any valid input for this language and writes out the boolean resulting from the reduction of this input. (=> is a single token which represents logical implication - e1 => e2 is true unless e1 is true and e2 is false).
SolutionQuestion 3
Given the following grammar for a fragment of Pascal:
S
® StatStat
® case Exp of Caselist endStat
® id:=ExpCaselist
® Caselist;CaseCaselist
® CaseCase
® id:StatExp
® idWrite a program that translates this into the equivalent in C, where the equivalent is as follows:
|
Pascal |
C |
|
Stat ® case Exp of Caselist end |
Stat ® switch (Exp) {Caselist} |
|
Stat ® id:=Exp |
Stat ® id=Exp; |
|
Caselist ® Caselist ; Case |
Caselist ® Caselist Case |
|
Case ® id:Stat |
Case ® case id:Stat break; |
(id consists of a sequence of one or more alphabetic characters)
Solution