Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Compiler construction I

Excercise sheet 2

On Moodle, you can find a zipped project archive as a


stepping stone for the next exercises.
1. The lecture of introduces a small compiler that translates
addition and subtraction of single-digit numbers in infix
notation to the corresponding postfix notation according
to the following translation scheme:
expr → expr + term {printf("+");}
| expr – term {printf("-");}
| term
term → 0 {printf("0");} | ... | 9 {printf("9");}
Write a predictive parser in C that parses infix notation
and prints out postfix notation. The download for this
exercise already includes a solution (for the case that
none has been created during the lecture) – however,
you should try to write your own parser from scratch.
Note:
1. The first step should be eliminating the left recursion
in the grammar defiinition.
2. The finished program parser.c can be compiled on a
UNIX system simply by typing "make parser" in a
terminal – the needed compilation steps can be
resolved by the make tool via implicit rules.
2. Simplify your predictive parser from above so that the
function R() (or rest()) introduced during elimination of
left recursion is removed in favour of a single expr()
function.
3. Construct a recursive-descent parser for the following
grammar:
S→+SS
|-SS
|a

You might also like