Pascal - Exercise
Pascal - Exercise
(2) Let A = {a, b} be the alphabet of the language L. Strings w E A* that are
accepted by L must fulfil the following conditions:
i) length(w) ~ 3;
ii) the number of a's in ware twice as much than the number of b's.
Design
(3) Let the language L be defined as L = { On1 n-1 I n ~ 1 }. E.g., 0, 001, and
00011 are elements of L. Design a context-free grammar G (T, N, P, S) which
generates L and where INI = 1 and IPI = 2.
160 Exercises
T: {0,1 }
N: {A, B}
P: {A -+ OBB
B -+ 1A I OA I0 }
S: A
Show the different syntax trees (parse trees) for a suitable word.
(7) Given the following part of the ALGOL60 grammar for statements describing
the syntax of conditional statements:
Derive the sentence 010011011100 from the start symbol of the grammar
and analyze that sentence with the automaton.
(9) Design a finite automaton which recognizes real numbers of the form "123,
123., 12.3, 12. E3, .123, .12E3, 12.3E3, 123E4." .
162 Exercises
STATES INPUT
0 1
I A B
A E B
B A E
E E E
0101010
0100101
(12) Design a scanner for PUO in PASCAL which also accepts decimal numbers
as defined in Exercise (11), which allows a PASCAL-like usage of
comments, and which produces a compiler listing.
T {a, b, c, d}
N {A, B, C}
P {A ~ CB
B ~ bCB I E
C ~ a I cAd}
S { A}
(14) Draw a diagram of a hash symbol table that would result when compiling the
following example for the declaration of variable names:
assume a table size N = 13, and use direct chaining to generate secondary
indices. (Note: Reserved words should not be included in this symbol table).
(15) DeSign a PASCAL-program handling hash symbol tables using the hash
function of Exercise (14). Simulate a dynamic array, so that the address
space can interactively be changed. Consider the names given in the
variable names declaration example of Exercise (14) for different address
spaces.
Make your program as simple as possible!
(16) Let G be a grammar, where none of the nonterminals can be derived to the
empty string E. Show that G is a LL(1)-grammar, if each alternative of a
production starts with a different terminal.
L(G) =
164 Exercises
S ~ AB I POx
A ~ xy I m
B ~ bC
C ~ bC I £
P ~ pP I £
0 ~ qO I £
S (A)
A CB
B ;AI£
C x IS
Construct the parse table for that grammar and analyze the sentence (x; (x))
using the parse table. Show the contents of the stack and the input buffer. as
well as any production extracted from the parse table.
(23) Let A = { a1. a2 •...• an} be a set of n elements. Then. the set of lists E A*
can be defined as a formal language as follows:
(24) Build an LL(1) parsing table for the language defined by the LL(1 )-grammar
having the productions (PROGRAM is the axiom)
(25) Why is the grammar with the following productions not LL(1) (E is the
axiom)? Convert this grammar into an LL(1 )-grammar.
E -+ E+T
E -+ T
T -+ T*F
T -+ F
F -+ (E)
F -+ x
F -+ Y
S -+ ASlb
A -+ SAla
T { (, ), a, $}
N {A, B}
P {A -+ $B$
B -+ al(Ba)}
S {A}
T { a, f, z, [, ], (, ) }
N {E,I}
P {E -+ I(E)II[E]II
I -+ alflz}
S {E}
(30) The generation of the action table can lead to multiple entries indicating that
the grammar is not SLR(1).
Show that the following grammar is not SLR(1):
S -+ E$
E -+ bEalaEblba
a) postfix notation,
b) three-address code.
Parse different sentences of L(G) and thereby generate the postfix notation
and the three-address code for the selected sentences.
Exercises 167
T { a, b, c, d, e, f}
N {X, Y,Z}
P {X ~ YIXbZ
Y ~ ZIYaZ
Z ~ fldXelcZ}
S {X}
Generate the parser tables for G (thereby proof that G is an SLR(1)-
grammar). Analyze the sentence fad f b c f e .
x+y-x·y
-x-y+x·y
u·v·w - x/y
- ( u • v • w - x / y)
u • (v • w + x / w)
Translate the expressions into postfix notation, three-address code, and two-
address code.
(34) Design a compiler, which analyzes the syntax of arithmetic expressions and
which generates postfix notation of these expressions.
(35) What is meant by strength and frequency reduction? Explain and give
examples.
(36) Given the following code sequences. What are the corresponding arithmetic
expressions?
a) Two-address code:
1 DIV u v
2 SUB z (1 )
3 MUL Y (2)
4 ADD x (3)
5 MUL 4 (4)
6 ADD (5) z
168 Exercises
b) Postfix notation:
LOD 10
LOD A
MUL
LOD B
LOD C
MUL
ADD
LOD 3
LOD A
MUL
LOD B
DIV
SUB
(37) Indirect triples are often used to perform code optimization. In this case a
separate table is used to indicate the order of execution for a sequence of
triples. Optimization can then be performed by changing the order of the
entries in the table. For example,
z:= x+y-x·y
w := x·y
0E!erations TriE!les
1. (1 ) (1 ) ADD x Y
2. (2) (2) MUL x Y
3. (3) (3) SUB (1 ) (2)
4. (4) (4) STO (3) z
5. (2) (5) STO (2) w
6. ~5l
Design an algorithm to detect and delete useless indirect triples.
(39) Given the following set of productions to generate "IF THEN" and "IF THEN
ELSE" statements in MODULA-2:
(40) Given the following augmented grammar G(T, N, P, S) (the numbering of the
productions will be used in the action-table):
T { +, x, (, )}
N {E, T}
P { (0) S' -+ E
(1 ) E -+ T
(2) E -+ E+T
(3) T -+ (E)
(4) T -+ x }
S {E}
Action-table Goto-table
states + ( ) x $ E T
0 s3 s4 s1 s2
1 s5 accept
2 p1 p1 p1 p1
3 s3 s4 s6 s2
4 p4 p4 p4 p4
5 s3 s4 s7
6 s5 s8
7 p2 p2 p2 p2
8 p3 p3 p3 p3
Parse the sentence (x + x) + x using the LR-parse algorithm and the above
given parse tables; describe for each step of the analysis the contents of
stack and input buffer, as well as the executed actions.
References
[BACK 78] J. Backus. Can programming be liberated from the von Neumann
style? A functional style and its algebra of programs. CACM 21
(1978),613 - 641.
[CICH 80] R. J. Cichelli. Minimal perfect hash functions made simple. CACM
23 (1980),17 - 19.
[MAUR 68] W. D. Maurer. An Improved Hash Code for Scatter Storage. CACM
11 (1968), 35 - 38.
unambiguous grammar 20