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

Lista3 de Comp

Lista 3 de compiladores
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views

Lista3 de Comp

Lista 3 de compiladores
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Bottom-Up Parsing & Semantics

(Compilers Course by Alex Aiken)

QUESTION 1

Consider the following grammar:

S→A(S)B∣ϵ
A→S∣SB∣x∣ϵ
B→SB∣y

What are the first and follow sets of S?

A First: {x, y, '(', ϵ}, Follow: {$, '(', y}


B First: {x, ϵ}, Follow: {$, y, x, '(', ')'}
C First: {x, y, '(', ϵ}, Follow: {y, x, '(', ')'}
D First: {x, '('}, Follow: {$, y, x}
E First: {x, y, '('}, Follow: {$, y, x, '(', ')'}
F First: {x, y, '(', ϵ}, Follow: {$, y, x, '(', ')'}

QUESTION 2

What are the items in the initial state of the SLR(1) parsing automaton for the grammar in the previous question (Question 1)?
[Choose all that apply]

A B C D E F
S→.A ( S ) B S→. A→.S A→.SB A→.x A→S.B
G H I J
A→. B→.SB B→.y B→.

QUESTION 3

Which of the following are true of the initial state of the SLR(1) parsing automaton from the last question (Question 2)? [Choose all that apply]

A The state has a reduce-reduce conflict on end-of-input.


B The state has a reduce-reduce conflict on input '('.
C The state has a reduce-reduce conflict on input x.
D The state has a reduce-reduce conflict on transition S.
E This state has a shift-reduce conflict on end-of-input.
F This state has a shift-reduce conflict on input '('.
G This state has a shift-reduce conflict on input x.
H This state has a shift-reduce conflict on transition S.

QUESTION 4

Consider grammars G1, G2, and G3.

G1: E → idT ∣ (E)T


T → + id ∣ * id
G2: S → bSb ∣ A ∣ ϵ
A → aA ∣ ϵ
G3: R → aR' ∣ (R)R'
R' → ϵ ∣ XR'
X→.R∣+R∣*

The number of symbols in the first sets for the *non-terminals* are:

A
G1: E = 2; T = 2
G2: S = 3; A = 2
G3: R = 2; R' = 4; X = 3
B
G1: E = 2; T = 2
G2: S = 3; A = 2
G3: R = 2; R' = 3; X = 3
C
G1: E = 2; T = 2
G2: S = 3; A = 1
G3: R = 2; R' = 3; X = 3
D
G1: E = 4; T = 2
G2: S = 2; A = 2
G3: R = 2; R' = 4; X = 3
E
G1: E = 4; T = 2
G2: S = 2; A = 2
G3: R = 2; R' = 3; X = 3

QUESTION 5

Given the following grammar,

stmt → var | if_stmt


if_stmt → if var then stmt | if var then stmt else stmt
var → a | b | win | loss

Which of the following series is a valid bottom-up parsing for the string [Choose all that apply]:
if a then if b then win else loss

if a then if b then win else loss


if var then if b then win else loss
if var then if var then win else loss
if var then if var then var else loss
if var then if var then var else var
if var then if var then stmt else var
if var then if var then stmt else stmt
if var then if_stmt
if var then stmt
if_stmt
stmt

if a then if b then win else loss


if var then if b then win else loss
if var then if var then win else loss
if var then if var then var else loss
if var then if var then stmt else loss
if var then if_stmt else loss
if var then stmt else loss
if var then stmt else var
if var then stmt else stmt
if_stmt
stmt

if a then if b then win else loss


if var then if b then win else loss
if var then if b then win else loss
if var then if b then var else loss
if var then if b then stmt else var
if var then if var then stmt else var
if var then if var then stmt else stmt
if var then if_stmt
if var then stmt
if_stmt
stmt

if a then if b then win else loss


if var then if b then win else loss
if var then if var then win else loss
if var then if var then var else loss
if var then if var then stmt else loss
if var then if var then stmt else var
if var then if var then stmt else stmt
if var then if_stmt
if var then stmt
if_stmt
stmt

if a then if b then win else loss


if var then if b then win else loss
if var then if var then win else loss
if var then if var then var else loss
if var then if var then stmt else var
if var then if var then stmt else stmt
if var then if_stmt
if var then stmt
if_stmt
stmt

QUESTION 6

For the grammar in the last question (Question 5), when applying shift-reduce parsing to the same string:

if a then if b then win else loss

What kind of conflicts will we have?

A B C D
No conflict Reduce-reduce conflict Both conflicts Shift-reduce conflict

QUESTION 7

Consider the following grammar:

E→T*E∣T
T → int + T ∣ int ∣ (E)

Using shift-reduce parsing, how many shift and how many reduce moves does it take to accept the input string:
((int + int)*int)

A B C
shift = 10; reduce = 6 shift = 9; reduce = 9 shift = 10; reduce = 7
D E
shift = 9; reduce = 8 shift = 9; reduce = 7

QUESTION 8

Consider the following grammar:

S → Sb ∣ a

This grammar is:

A B C
not SLR(1) SLR(1) but not LL(1) LL(1)

QUESTION 9

Consider the following grammar:

S → SbS ∣ a

This grammar is:

A B C
not SLR(1) SLR(1) but not LL(1) LL(1)
QUESTION 10

Consider the following grammar:

S → bS ∣ a

This grammar is:

A B C
not SLR(1) SLR(1) but not LL(1) LL(1)

QUESTION 11

Which of the following statements are true about this grammar:

S → aTUb ∣ ϵ
T → cUc ∣ bUb ∣ aUa
U → Sb ∣ cc

A The first set of S is {ϵ, a, b}


B The first set of U is {a, b, c}
C The follow set of S is {$, b}
D The follow set of T is {a, b, c}

QUESTION 12

Choose whether or not each variable use binds to the name on the given line.

1 Class Foo {
2 f(x: Int): Int {
3 {
4 let x: Int <- 4 in {
6 x;
7 let x: Int <- 7 in
8 x;
9 x;
10 };
11 x;
12 };
13 };
14 x: Int <- 14;
15 }

A Line 6 binds to line 2


B Line 9 binds to line 7
C Line 11 binds to line 2
D Line 11 binds to line 14

QUESTION 13

If we have the following program:

Class Main inherits IO {


x : Int ← 5;
foo(z:Int) : Int {
x+z
};
bar(y:Int) : Int {{
let x : Int ← 1 in
let z : Int ← 2 in
foo(y);
}};
main(): Object {{
let x : Int ← 7 in
out_int(foo(bar(3)));
}};
}
What would the the program print assuming it is statically scoped?
A B C D E
13 5 11 9 15

QUESTION 14

For the program in Question 13, what would the the program print assuming it is dynamically scoped?
A B C D E
13 5 11 9 15

QUESTION 15
Choose the type rules that are sound.

[Sequence]

_____________________________
{ }

[Divide]

________________________

[Compare]

________________________

[Isvoid]

___________________
e1: bool

QUESTION 16
A

© Stanford University, Stanford, California 94305

You might also like