CS 451 - Programming Languages - Fall 2007
Exam #2 Practice Problem Solutions


Loyola College > Department of Computer Science > Dr. James Glenn > CS 451 > Homework > Exam #2 Practice Problems > Solutions

Problem 1: Is the following grammar LR(0)?

S &rarr aS
S &rarr a

StateItems
0 S &rarr · a S
S &rarr · a
1 S &rarr a · S
S &rarr a ·
S &rarr · a S
S &rarr · a

State 1 has a shift/reduce conflict, so the grammar is not LR(0).


Problem 2: Construct a CFG for the language a(ba)nca(ba)n. Construct the parse table for your CFG and show a trace of parsing ababacababa.

1) S &rarr a c a
2) S &rarr a b S b a

StateItems
0 S &rarr · a c a
S &rarr · a b S b a
1 S &rarr a · c a
S &rarr a · b S b a
2 S &rarr a c · a
3 S &rarr a b · S b a
S &rarr · a c a
S &rarr · a b S b a
4 S &rarr a b S · b a
5 S &rarr a b S b · a

 Sabc
0accepts1  
1  s3s2
2 s/r1  
3s4s1  
4  s5 
5 s/r2  

StackInputAction
0 a shift, goto 1
0 a 1 b shift, goto 3
0 a 1 b 3 a shift, goto 1
0 a 1 b 3 a 1 b shift, goto 3
0 a 1 b 3 a 1 b 3 a shift, goto 1
0 a 1 b 3 a 1 b 3 a 1 c shift, goto 2
0 a 1 b 3 a 1 b 3 a 1 c 2 a shift, reduce by rule 1
0 a 1 b 3 a 1 b 3 S shift, goto 4
0 a 1 b 3 a 1 b 3 S 4 b shift, goto 5
0 a 1 b 3 a 1 b 3 S 4 b 5 a shift, reduce by rule 2
0 a 1 b 3 S shift, goto 4
0 a 1 b 3 S 4 b shift, goto 5
0 a 1 b 3 S 4 b 5 a shift, reduce by rule 2
0 S accept!


Problem 3: Given the following attribute grammar, compute the values of the attributes of the nonterminals in the parse tree of pi(ab,i(b,b))a. Assume that p.s is initialized to 0 by the scanner.

P &rarr p B B.s = p.s
B1 &rarr S B2 S.s = B1.s; B1.l = S.l + B2.l; B2.s = B1.s + S.l
B1 &rarr &epsilon B1.l = 0
S &rarr a S.n = S.s; S.l = 1
S &rarr b S.n = S.s; S.l = 1
S &rarr I I.s = S.s; S.l = I.l
I &rarr i ( B ) I.l = B.l + 1; B.s = I.s + 1
I &rarr i ( B1 , B2 ) I.l = B1.l + B2.l + 1; B1.s = I.s + 1; B2.s = B1.s + B1.l

Since l is a purely synthesized attribute, compute that first using a bottom-up approach. Next, compute s and then n.


Problem 4: Some languages allow multi-way assignments such as a,b = c,d, which would simulaneously copy the value from c to a and from d to b. Write an attribute grammar for such assignment statements that includes an attribute that can be checked to see if the assignment is balanced (that is, if there are the same number of terms on both sides of the assignment operator). Assume that all of the terms are identifiers.

A &rarr L1 = L2 ; A.ok = (L1.count == L2.count)
L1 &rarr id , L2 L1.count = id.count + L2.count
L &rarr id L.count = id.count
(Assume that the scanner initializes id.count to 1 for each id terminal.)

Problem 5: Show the output of the following code fragment assuming that dynamic binding is used. Assume that execution begins in procedure Z.

procedure A
  print x

procedure B
  int x = 1
  call A

procedure C
  int x = 2
  call B

procedure Z
  int x = 3;
  call A
  call B
  call C
  call A

3 1 1 3


Problem 6: What is the output of the following code fragment if deep binding is used? What is the output if shallow binding is used? In both cases, assume that execution begins in the outermost procedure.

procedure A
  int m

  procedure B(procedure X, int y)
    print X(y)

  procedure C(procedure X)
    m = 10
    B(X, m)

  procedure D(int x) : int
    return x * m

  m = 1
  C(D)

Shallow binding: 100
Deep binding: 10


Problem 7: Consider the following code fragment in a language that allows nested procedures. What is the output, assuming that execution begins in the main body of procedure A? What machine instructions would be generated to perform the call of procedure D from procedure C? What would be generated to perform the call of procedure D from procedure A?

procedure A
  int x

  procedure B
    int y

    procedure C
      int x

      x = 2
      print x, y
      call D

    y = 8
    print x, y
    call C

  procedure D
    int z

    procedure E
      z = 9
      call F

    procedure F
      int y

      y = 5
      print x, y, z


    x++
    z = 4
    call E

  x = 10
  call B
  call D

Output: 10 8 2 8 11 5 9 12 5 9

Code for individual procedure calls can be extracted from the complete code.