Principles of Compiler Design
Principles of Compiler Design
1. Define compiler?
A compiler is a program that reads a program written in one language (source
language) and translates it into an equivalent program in another language (target
language) and the compiler reports to its user the presence of errors in the source
program.
5. Define CFG?
Many programming language has rules that prescribe the syntactic structure of
well-formed programs. The syntax of programming language constructs can be
described by CFG. Conditional statement defined by a rule such as ; If S1 and S2
are statements and E is an expression, then
“If E then S1 else S2” is a statement.
6. Define derivations. Give an example and its types?
We apply the productions of a CFG to infer that certain strings are in the language
of a certain variable. There are two approaches (a) derivations (b) recursive
inference or reduction. Then derivation uses the production from head to body. A
replacement according to a production is known as as derivation
i) Left most derivation
ii) Right most derivation or canonical derivations
E E+Eid+Eid+(E)
id+(E+E)id+(id*E)
id+(id*id)
7. Define ambiguity?
20. What are the two common ways of determining precedence relations should
hold between a pair of terminals?
a) Based on associativity and precedence of operators.
b) Construct an unambiguous grammar for the language, a grammar that
reflects the correct associativity and precedence in its parse tree.
34. What are the three techniques for constructing LR parsing table?
a) SLR (simple LR)
Replaced as AbdA’ / A’
A’cA’/ adA’/ £
Finally we obtain
SAa / b,
AbdA’ / A’,
A’cA’ / adA’ / £
.
1. What are the advantages of generating an intermediate representation?
i) Ease of conversion from the source program to the intermediate code.
ii) Ease with which subsequent processing can be performed from the
intermediate code.
- c
a 4
7. What are the functions used to create the nodes of syntax trees?
i) Mknode (op, left, right)
8. What are the functions for constructing syntax trees for expressions?
i) The construction of a syntax tree for an expression is similar to the
translation of the expression into postfix form.
ii) Each node in a syntax tree can be implemented as a record with several
fields.
P1 = mkleaf(id,a) P2 = mknum(num,4)
P3 = mkleaf(id,c) P4 = mknode(‘*’,p2,p3)
P5 = mknode(‘-‘,p1,p4)
Example: x: =y op z
22. What are the three address code for a or b and not c?
The three address sequence is
T1:= not c
T2:= b and T1
T3:= a or T2.
23. Write a three address code for the expression a < b or c < d?
100: if a<b goto 103
101: t1:=0
102: goto 104
103: t1:=1
104: if c<d goto 107
I 10
26. What are the various data structure used for implementing the symbol table?
1. Linear list
2. Binary tree
3. Hash table
33. What are the semantic rules are defined in the declarations operations?
1) Mktable(previous)
2) Enter(table, name,type,offset)
3) Addwidth(table,width)
4) Enterproc(table,namenewtable)
34. Define short circuit code?
Translate the Boolean expression into three-address code without generating code
for any of the Boolean operators and without having the code necessarily evaluate
the entire expression. This style of evaluation is sometimes is called short-circuit
or jumping code.
36. Give the 2 attributes of syntax directed translation into 3-addr code?
i) E.place, the name that will hold the value of E and
ii) E.code , the sequence of 3-addr statements evaluating E.
40. Write the 3-addr code for the statements a =b*-c + b*-c?
Three address codes are: a=b*-c + b*-c
T1 = -c
T2 = b*T1
T3 = -c
T4 = b*T3
T5 = T2+T4
a:= T5.