0% found this document useful (0 votes)
143 views

Pascal - Exercise

The document contains 28 exercises related to formal language theory and parsing. The exercises cover topics such as designing grammars for specific languages, analyzing ambiguity and grammatical properties like LL(1), constructing parse tables and syntax graphs, and writing recursive descent parsers.

Uploaded by

walkrog
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views

Pascal - Exercise

The document contains 28 exercises related to formal language theory and parsing. The exercises cover topics such as designing grammars for specific languages, analyzing ambiguity and grammatical properties like LL(1), constructing parse tables and syntax graphs, and writing recursive descent parsers.

Uploaded by

walkrog
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Exercises

(1) Design three regular grammars which generate


(a) PASCAL names;
(b) odd numbers;
(c) the language L = {sn I s E {01, 10} 1\ n > 0 }.
Check the correctness of the production set.

(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.

E.g., aabaabaab E L, abab E L.

Design

(a) a context-free grammar


(b) a context-sensitive grammar
which generate the language L. Check the correctness of the production set.

(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

(4) Given the ambiguous grammar G (T, N, P, S):

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.

(5) Given the grammar G (T, N, P, S) :


T {0,1 }
N {A, B, C}
P {A -+ OABC I 01 C
CB -+ BC
1 B -+ 11
1 C -+ 1 }
S { A}

(a) What type of grammar is G (Chomsky-Hierarchy)? Proof.


(b) Generate the smallest, the 2nd smallest, and the 3rd smallest word of
the language L (G).
(c) Give a set-definition of L(G).
(d) Design a grammar G' (T, N', P', S) which generates L and where
INI = 1.

(6) Given the grammar G (T, N, P, S) :


T { id, +, (, ) }
N {E, T}
P {E -+ T {+ T}
T -+ id I (E)}
S {E}
Show the syntax graphs of G.

~: Syntax graphs can be understood as analyzing algorithms. A sentence


is correct, when the elements of the sentence describe a complete path
through the graph.
Exercises 161

(7) Given the following part of the ALGOL60 grammar for statements describing
the syntax of conditional statements:

stmt -+ uncond. stmt I condo stmt I


condo stmt -+ IFstmt [ ELSE stmt]
uncond. stmt -+ basic stmt I compound stmt
IFstmt -+ IF log. expr. THEN uncond. stmt
basicstmt -+ assignment I goto stmt I proc stmt
compound stmt -+ BEGIN stmt END

(a) Generate the syntax graphs for this grammar.

(b) Generate a syntax tree for the following sentence:

IF log. expr. THEN BEGIN IF log. expr.


THEN assignment
ELSE assginment
END;

(c) Why is the following sentence not correct?

IF log. expr. THEN IF log. expr.


THEN assignment
ELSE assginment

(8) Design a regular grammar G which generates all sequences of 0 and 1,


where the number of O's and the number of 1's is even, and the length of the
derivable strings is greater 1.

Design a finite deterministic automaton which accepts L(G).

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

(10) Given the following finite automaton:

STATES INPUT
0 1
I A B
A E B
B A E
E E E

where I is the initial state and { A, B } is the set of final states.

Generate the transition diagram of this automaton.


Which language will be accepted by the automaton?
Design a regular grammar for that language!
Analyze the following strings with the automaton:

0101010
0100101

(11) Design a finite deterministic automaton which accepts decimal numbers of


the form "m.n or .n". Use the automaton to expand the lexical analysis of
PUO.

(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.

(13) Given the grammar G (T, N, P, S) :

T {a, b, c, d}
N {A, B, C}
P {A ~ CB
B ~ bCB I E
C ~ a I cAd}
S { A}

Show that G is a LL(1 )-grammar.


Exercises 163

(14) Draw a diagram of a hash symbol table that would result when compiling the
following example for the declaration of variable names:

REAL x, y, z1, z2, z3;


INTEGER i, j, k, last1;
STRING Message;
BOOLEAN ReadlS, Valve_Open;
ARRAY REAL Valve (20);
ARRAY INTEGER Max_Val (20);

Use the following hash function to code a word


w=w1 w2··· wk:

H(w) = (ORD(w1) + ORD(wk) + 16*k) MOD N + 1 ,

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.

(17) Design a LL(1 )-grammar for the following language:

L(G) =
164 Exercises

(18) Given a grammar G with the following productions:

S ~ AB I POx
A ~ xy I m
B ~ bC
C ~ bC I £
P ~ pP I £
0 ~ qO I £

Is G a LL(l )-grammar? Give a reason for your answer.

(1 9) Explain why LL(l) grammars are not ambiguous.

(20) What properties of a grammar prevent the writing of a recursive descent


parser from being straightforward?

(21) Write a recursive descent parser for simple expressions in Modula-2.

(22) Consider a grammar which is given by the following set of productions:

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:

ois a list. i.e. the empty list;


each a E A is a list;
if L1 • L2 •...• Lk are lists. then the concatenation (L1. L2 •...• lk) is
also a list.

Design an LL(l )-grammar which defines lists on A. Write a recursive descent


parser for this grammar.
Exercises 165

(24) Build an LL(1) parsing table for the language defined by the LL(1 )-grammar
having the productions (PROGRAM is the axiom)

PROGRAM -+ begin d semi X end


X -+ d semi X
X -+ sY
y -+ £
Y -+ semi s Y

(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

(26) Given the contex1-free grammar G (N, T, P, S) which generates logical


expressions:

T { and, or, not, (, ), b}


N {A, B, C}
P (A -+ BIAorC
B -+ C I Band C
C -+ b I (A) I not C }
S {A}

Why is G not a LL(1 )-grammar?


Generate the parser tables for G (thereby proof that G is an SLR(1)-
grammar).

(27) Consider the grammar

S -+ ASlb
A -+ SAla

List all the LR(O)-items for the above grammar.


166 Exercises

(28) Given the following grammar G (N, T, P, S):

T { (, ), a, $}
N {A, B}
P {A -+ $B$
B -+ al(Ba)}
S {A}

Which language is generated by G?


Generate the parser tables for G.
Analyze the following sentences:
(())()
( (() )

(29) Given the following grammar G (N, T, P, S):

T { a, f, z, [, ], (, ) }
N {E,I}
P {E -+ I(E)II[E]II
I -+ alflz}
S {E}

Generate the parser tables for G.


Analyze the following sentences:
f(a[z])
a[f[z]]

(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

(31) Given the context-free grammar G (N, T, P, S) of Exercise (26). Define


semantic actions for G's set of productions to generate

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

(32) Given the context-free grammar G (N, T, P, S) which generates logical


expressions:

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 .

(33) Given the following arithmetic expressions:

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

can be represented by indirect triples as follows:

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.

(38) Expand the PUO grammar to allow an ELSE-part in conditional statements


similar to the PASCAL-syntax. The semi-colon errors of Exercise (39) might
also occur in this extension of PUO. Legalize these errors by adding error
production to the grammar.
Exercises 169

(39) Given the following set of productions to generate "IF THEN" and "IF THEN
ELSE" statements in MODULA-2:

stmt sequence -+ stmt


stmt sequence -+ stmt sequence ; stmt
stmt -+ IF log. expr. THEN stmt sequence END
stmt -+ IF log. expr. THEN stmt sequence
ELSE stmt sequence END

A common syntax error in MODULA-2 is the usage of a semi-colon


immediately followed by ELSE or END. Legalize those errors by expanding
the above given set of productions by error productions.

(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}

The parse tables are given as follows:

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

[AHOS 86] A. V. Aho, R. Sethi, J. D. Ullman. Compi/ers Principles,


Techniques and Tools. Addison-Wesley, Reading (1986).

[AHOU 73] A. V. Aho, J. D. Ullman. The Theory of Parsing, Translation, and


Compiling. Volume I + II. Prentice Hall, Englewood Cliffs (1973).

[ALLE 76] F. E. Allen, J. Cocke. A program data flow analysis procedure.


CACM 19 (1976), 137 - 147.

[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.

[BELA 66] L. A. Belady. A study of replacement algorithms for a virtual storage


computer. IBM Systems JournalS (1966).

[BELL 73] J. R. Bell. Threaded Code. CACM 16 (1973), 370 - 372.

[BRIN 85] P. Brinch Hansen. Brinch Hansen on Pascal Compi/ers. Prentice-


Hall, Englewood Cliffs (1985).

[CICH 80] R. J. Cichelli. Minimal perfect hash functions made simple. CACM
23 (1980),17 - 19.

[FISC 88] C. N. Fischer, R. J. LeBlanc. Crafting a Compiler.


Benjamin/Cummings Publishing, Menlo Park (1988).

[FURB 89] S. B. Furber. VLSI RISC architecture and organization. Marcel


Dekker, New York (1989).
References 171

[JOHN 75] S. C. Johnson. Yacc - Yet Another Compiler-Compiler. Compo Sci.


Tech. Report 32, AT&T Bell Labs, Murray Hill (1975).

[KUIC 86] W. Kuich, A. Salomaa. Semirings, Automata, Languages. Springer-


Verlag, Berlin (1986).

[LOEC 87] J. Loeckx, K. Sieber. The Foundations of Program Verification. B. G.


Teubner, Stuttgart, and John Wiley & Sons, New York (1987).

[MAUR 68] W. D. Maurer. An Improved Hash Code for Scatter Storage. CACM
11 (1968), 35 - 38.

[MCKE 74] W. M. McKeeman. Symbol Table Access. In: F. L. Bauer, J. Eickel


(eds). Compiler Construction. Lecture Notes in Computer Science,
Springer Verlag, Berlin (1974), 253 - 301.

[MCKE 89] B. J. McKenzie. Fast Peephole Optimization Techniques. Software -


Practice and Experience 19 (1989), 1151 - 1162.

[MILU 86] V. Milutinovic (ed.). Tutorial on Advanced Microprocessors and


High-level Language Computer Architecture. IEEE Computer
Society Press, Los Alamitos (1986).

[MINS 67] M. L. Minsky. Computation: Finite and Infinite Machines. Prentice-


Hall, Englewood-Cliffs (1967).

[MORR 68] R. Morris. Scatter Storage Techniques. CACM 11 (1968).


39 - 43.

[NAUR 63] P. Naur (ed.). Revised Report on the Algorithmic Language


ALGOL60. CACM6 (1963), 1 - 17; Compo J.5 (1962/63), 349 - 367;
Num. Math. 4 (1963), 420 - 453.

[NORI81] K. V. Nori, U. Amman, K. Jensen, H. H. Nageli, Ch. Jacobi. Pascal-P


Implementation Notes. In: D. W. Barron (ed.). Pascal - The
Language and Its Implementation. John Wiley & Sons, New York
(1981).

[PATT 85] D. A. Patterson. Reduced instruction set computers. CACM 28


(1985),8 - 21.

[RIPL 78] D. G. Ripley, F. C. Druseikis. A Statistical Analysis of Syntax Errors.


Computer Languages 3 (1978), 227 - 240.

[SALO 73] A. K. Salomaa. Formal Languages. Academic Press, New York


(1973).
172 References

[SUEN 79] C. Y. Suen. n-Gram Statistics for Natural Language Understanding


and Text Processing. IEEE Trans. on Pattern Analysis and Machine
Intelligence PAMI-1 (1979),164 - 172.

[TANE 82] A. S. Tanenbaum, H. van Staveren, J. W. Stevenson. Using


Peephole Optimization on Intermediate Code. Trans. on Program-
ming Languages and Systems 4 (1982), 21 - 36.

[TEUF 84] T. Teufel. A Hardware Architecture of an Optimal BCD-Floating-


Point Processor. In: M. Feilmeier, J. Joubert, U. Schnedel (eds.):
Parallel Computing '83, North-Holland, Amsterdam (1984).

[TEUF 86] T. Teufel, G. Bohlender. A Bit-slice Processor Unit for Optimal


Arithmetic. In: M. Ruschitzka (ed.): Computer Systems: Performance
and Simulation, North-Holland, Amsterdam (1986).

[TEUF 91] B. Teufel. Organization of Programming Languages. Springer-


Verlag, New York (1991).

[TREM 85] J. P. Tremblay, P. G. Sorenson. The Theory and Practice of


Compiler Writing. McGraw-Hili, New York (1985).

[WAIT 84] W. Waite, G. Goos. Compiler Construction. Springer-Verlag, New


York (1984).

[WILK 51] M. Wilkes. The Best Way to Design an Automatic Calculating


Machine. Proc. Manchester University Computer Inaugural
Con ference (1951).

[WIRT 86] N. Wirth. Compilerbau. B. G. Teubner Verlag, Stuttgart (1986).

[WIRT 76] N. Wirth. Algorithms + Data Structures = Programs. Prentice-Hall,


Englewood-Cliffs (1976).

[YAMA 81] M. Yamamoto. A Survey of High-level Language Machines in


Japan. IEEE Computer 14(7) (1981),68 - 77.
Index

absolute code 130 code generator 4, 8, 100, 101,


abstract syntax tree 104,147 130,131,142,143,147
action-table 82, 84 code motion 149
activation record 134, 135, 136 code optimization 146, 147
address descriptor 144, 146 compiler 3
alphabet 9, 10 constant folding 147, 148, 151
ambiguous grammar 15, 21, 22 context-free grammar 15, 16, 17,
assembler 3 18,20,59,78,105,125
assembly languages 1 context-sensitive grammar 16
augmented grammar 84, 89, 90, context-sensitive rule 100
91,125

derivation 11, 15, 16, 18, 19


Backus-Naur form 10, 11 direct chaining 52
basic block 143, 144, 146, 147 display 136, 137
bigram indexing 49, 50, 51, 52 dynamic allocation 131
BNF 10, 11, 12, 15, 27 dynamic check 111
bottom-up analysis 78, 86, 93 dynamic link 135
bottom-up parsing 22, 25, 26, 57,
80,82
errors 118

canonical collection 89, 90, 91, 92


Chomsky-hierarchy 16 FDA 34, 42, 43
closure 88, 89, 90 finite automata 33, 35, 37, 40, 45
code elimination 148, 150 finite deterministic automata 34,
code generation 7, 46, 113, 125, 43
130,131,142,143,144,147
174 Index

finite nondeterministic automata lexical error 120, 122, 123


42,43 linker 5
finite-state acceptor 35 LL(1) grammar 27
finite-state grammar 16 LL(1)-grammar 59,63,64,67,68,
FIRST 26, 58, 59 75
first generation languages 1 LL-grammar 57
FNA42,43 LL-parsing 77, 83
FOLLOW 26, 27, 58, 59 loop optimization 148, 149, 150
formal language 9,10, 12 LR(O)-item 87, 88
frequency reduction 148, 149, 150 LA(O)-set 88, 89, 90, 91
LA(k)-grammar 78, 79, 80
LR-grammar 78
goto-function 91 LA-parser 78, 82, 120, 125
goto-table 82, 84, 87, 92 LR-parsing 82, 83, 93
grammar 9,10,13,15,16,17,18,
21,22,64,88,125
grammar expansion 127 no-restriction grammar 16
nonterminal symbol 10, 13, 15,
20,26,58
handle 78, 79, 80
hashing 50, 52
heap allocation 131, 137 open addressing 52
hierarchical analysis 7
hierarchy of grammars 16
high level languages 1, 2 P-code 104
panic recovery 124, 125, 126
parameter passing 138, 158
Intermediary representation 3, 4, parse table 64, 73, 74, 75, 82, 83,
8, 143 84,86,93
intermediate code 100, 101, 104, parse tree 7, 15, 18, 19, 20, 21,
105,106,113,147,152 22,23,24,25,26,57,60,64,
internal representation 100 65,66,67,68,80,86,104
interpreter 4 parser43,57, 120, 123, 124, 125
invisible error 119 parsing 7,15,16,18,20,22
peephole optimization 148, 152
PLJO 9, 29
left-linear grammar 15 postfix notation 101, 104, 107,
left-recursive grammar 16 108,109,113
leftmost derivation 15, 16, 19,59, predictive parSing 64
63,64, n,80 prefix notation 104
letter indexing 49 preprocessor 4
lexical analysis 7, 16, 17, 32, 37, production 10, 11, 13, 14, 15, 16,
43,52,57,93,122 18,21
Index 175

static link 135, 136


storage allocation 131, 132, 133,
quadruple 103, 104, 105, 143 137,138,141,142
strength reduction 148, 149
string 10
rearrangement 148, 151, 152
subset construction 43, 88
recognizer 35
symbol 9, 10
recovery by grammar expansion
symbol table 3,7, 18,32,45,46,
124
47,48,50,52, 100, 103, 105,
recovery by insertion, deletion,
111, 112, 125
and replacement 124
syntax 7, 9,10,16,17,27
recursive descent parser 64, 68,
syntax analysis 7, 8, 20, 22, 32,
93,113,126
43,57,100,124
recursive descent parsing 64, 67
syntax error 120, 121, 123, 124,
reduction 16
125, 126
register descriptor 143, 144, 146
syntax graph 27, 69
~gu~grnmm~16,17,18,3~
syntax tree 7, 80
37
syntax-directed translation 105
relocatable code 130
right-linear grammar 16
right-recursive grammar 16 tabular parsing 73, 82
rightmost derivation 15, 16, 78, terminal symbol 10, 13, 14,20,26,
82,86 27,58
run-time check 111 third generation languages 1
run-time error 120 threaded code 104
three-address code 102, 103,
104, 105
scanner 32, 33, 37, 43, 45, 52,
token 9
122
top-down parsing 22, 24, 57, 64,
second generation languages 1
77,82
semantic action 105, 106, 107,
transition diagram 36, 37, 38, 39,
108, 113
40,52
semantic analysis 4, 7, 8, 100,
transition table 36, 37, 38, 39, 40
105, 112
triple 103, 104
semantic error 120, 125
two-address code 103, 104
sentence 9, 10
type 0 grammar 16
shift-reduce analysis 78, 80, 81,
type 1 grammar 16
83
type 2 grammar 16
SLR parser table 78
type 3 grammar 17
stack allocation 131, 133
type analysis 8, 100
start symbol 13, 14, 16, 18, 20
type checking 111, 112
static allocation 131
static check 111
176 Index

unambiguous grammar 20

variable addressing 139. 140


visible error 119. 122
vocabulary 9.10

You might also like