Compiler Mid-Term Exam (20 %) Briefly explain the terms below

advertisement
Compiler Mid-Term Exam
1. (20 %) Briefly explain the terms below:
1) scanner, 2) token, 3) parser, 4) abstract syntax tree (AST), 5) code
generator
Sol:
1 scanner: The scanner begins the analysis of the source program by
reading the input text (character by character) and grouping
individual characters into tokens
2 token: The token is generated by scanner and has its type and value,
such as an identifier “a”; the type is identifier and value is “a”.
3 parser: The parser is based on formal syntax specification and
analyzes the syntax by reading a sequence of tokens.
4 abstract syntax tree (AST): The AST serves as a common, concise
representation for a program for all phases after syntax
analysis.
5 code generator: It traverses the AST to generate the code of target
language.
2. (20 %) Based on the grammar below:
1) Prog → Dcls Stmts $
2) Dcls → Dcl Dcls
3)
| λ
4) Dcl → floatdcl id
5)
6)
7)
| intdcl id
Stmts → Stmt Stmts
| λ
8)
Stmt → id assign Val Expr
9)
| print d
10) Expr → plus Val Expr
11)
| minus Val Expr
12)
| λ
13) Val → id
14)
| num
Construct the parse tree and the AST for the source code below:
ia
a=5
Sol:
Prog
$
Dcls
Dcl
Stmts
Dcls
Stmts
Stmt
intdcl
λ
λ
id
assign
id
i
Val
Expr
num
λ
a
a
=
5
Prog
a
=
a
5
3. Write non-deterministic finite automata (NFA) and then deterministic finite
automata DFA that recognizes the tokens defined by the following regular
expression:
( (bc)|d) + (30 %)
Sol:
d
6
λ
λ
7
λ
λ
1
2
8
λ
λ
b
3
A {1, 2, 3, 6}
B {4}
4
C {2, 3, 5, 6, 8, 9}
d
D
d
b
d
b
B
A
b
c
C
c
5
D {2, 3, 6, 7, 8, 9}
9
4. (a) (10 %) Find the first set and the follow set of the grammar below:
1) S → AB
2) A → Aa
3) | a
4) B → Bb
5) | b
(b) (10%) Find the first set and the follow set of the grammar
in Problem No. (2).
(a) Sol:
First (S) = {a }
First (A) = {a }
First (B) = {b}
Follow (S) = {$}
Follow (A) = {a, b}
Follow (B) = {$, b}
(b) First (Prog) = {λ, f, i }
First (Dcls) = {λ, f, i }
First (Dcl) = { f, i }
First (Stmts) = {λ,
id, p }
First (Stmt) = {
id, p }
First (Expr) = {+, -,λ}
First (Val) = { id, num }
Follow (Prog) = { $ }
Follow (Dcls) = { $, id, p }
Follow (Dcl) = { $, id, p, f, i }
Follow (Stmts) = { $ }
Follow (Stmt) = { $, id, p }
Follow (Expr) = { $, id, p }
Follow (Val) = { $, id, p, +, - }
5. (10 %) Consider the following grammar:
1) Start → E $
2) E
→ E ( plus E
3)
| num
Is the grammar ambiguous? If so, show two distinct derivations of some
input string for the ambiguous grammar.
Sol:
The grammar is ambiguous
Two distinct derivations of the source code 1 ( + 1 ( + 1
1) Start
E$
E ( plus E
E ( plus E ( plus E
1 ( plus E ( plus E
1 ( + E ( plus E
1 ( + 1 ( plus E
1(+1(+E
1(+1(+1
2) Start
E$
E ( plus E
1 ( plus E
1(+E
1 ( + E ( plus E
1 ( + 1 ( plus E
1(+1(+E
1(+1(+1
Download