Compiler Construction Written Assignment Lexical Analysis and

Compiler Construction
Written Assignment
Lexical Analysis and Parsing
Universidade de Lisboa
Faculdade de Ciências
Departamento de Informática
Mestrado em Engenharia Informática
2014/2015
Due Date:
17 March
Lexical Analysis
1. Write a regular expression for number constants according to the
following rules and examples.
• An unsigned number is a sequence of digits; an unsigned
number is a number;
• A decimal number is an unsigned number followed by a dot
followed by another unsigned number; a decimal number is a
number;
• A real number is written with a decimal or scale factor or both;
the scale factor is of the form E (or e) followed by a sign (+ or
−) followed by an unsigned integer; a real number is a number;
• Any number can be preceded by a sign, + or −.
Examples:
7
+05
1E100
32980986
0.54
-4E-17
324.96+e05
2. Convert the regular expression x∗ · 3 | 3 · ((3∗ · Z | a) · x)∗ to a
nondeterministic finite automata.
3. Convert the following nondeterministic finite automaton to a
deterministic automaton.
Parsing
1. Write a context-free grammar that generates the following language:
{ssr | s ∈ {a, b}+ }
where sr denotes the reverse of string s, that is the string spelled
right-to-left.
2. Consider the following grammar
S ::=
|
A ::=
|
(A)
a
A, S
S
(a) Show parse trees for sentences (a, a) and ((a, a), a, a).
(b) Construct a leftmost and a rightmost derivation for sentence
((a, a), a, a).
(c) Is the grammar ambiguous? Justify your answer.
(d) Eliminate the left recursion from the grammar.
2
3. Consider the LR(1) parsing table for the grammar above.
(
s3
0
1
2
3 s4
4 s4
5
6
7
8
9
10
11 s4
12
a
s2
)
,
$
S
1
A
5
5
7
9
a
r2
s6
s6
r4
r2
s8
r4
r2
s11
r1
s10
r1
s11
r1
r3
r3
s6
12
Show the shift-reduce parse of sentence ((a, a), a, a).
Vasco Thudichum Vasconcelos
February 27, 2015
3