Regular Expression - Midterm Paper - Programming Languages | CSCI 4430, Exams of Programming Languages

Material Type: Exam; Professor: Milanova; Class: PROGRAMMING LANGUAGES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 08/09/2009

koofers-user-7m4
koofers-user-7m4 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name: __________________________
CSCI 4430 Exam 1, Fall 2008 2
1. Regular Expressions (30 points)
Consider the following regular expression which has been divided into three parts, 1,2,3.
------1-------- -------2------ -------3-------
(x | y)* (x | z) (w* | z*)+
(5 points each) For each string below, either write that it is not generated by the regular expression (i.e.,
NOT GENERATED) or circle and label the sections of each string generated by the regular expression
parts 1,2,3. The following example shows what we mean.
x x x z w
-------------1--------- --2-- --3โ€”
a) y x w z
b) y y y y y y z
c) x z w x
d) x x x x x x x w
e) x z x z z w
f) x z z z z z
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Regular Expression - Midterm Paper - Programming Languages | CSCI 4430 and more Exams Programming Languages in PDF only on Docsity!

1. Regular Expressions (30 points)

Consider the following regular expression which has been divided into three parts , 1,2,.

(x | y) (x | z) (w | z)+*

(5 points each) For each string below, either write that it is not generated by the regular expression (i.e., NOT GENERATED ) or circle and label the sections of each string generated by the regular expression parts 1,2,3. The following example shows what we mean.

x x x z w -------------1--------- --2-- --3โ€”

a) y x w z

b) y y y y y y z

c) x z w x

d) x x x x x x x w

e) x z x z z w

f) x z z z z z

2. Grammars, Ambiguity, Precedence (60 points) Below is a grammar with two operators:

S โ†’โ†’โ†’โ†’ E

E โ†’โ†’โ†’โ†’ - E | E + E | id

a) (10 points) This grammar is ambiguous. Prove that the grammar is ambiguous.

b) (5 points) What is the precedence of unary - wrt +? (choose one)

  • higher precedence than +_______________
  • equal precedence to +_______________
  • lower precedence than +_______________

**c) (10 points)**** Give evidence to support your answer to part b.

3. LL Parsing (60 points) Consider the following grammar over terminals {c,d,e}****. S is the starting symbol of the grammar.

S โ†’โ†’โ†’โ†’ TS | [S]S | ฮต

T โ†’โ†’ โ†’โ†’ (X)

X โ†’โ†’โ†’โ†’ TX | [X]X | ฮต

a) **(30pts)**** Fill in the table below with the FIRST and FOLLOW sets for the nonterminals in this grammar:

FISRT FOLLOW S

T

X

b) **(20 points)**** Fill in the column headings and the row corresponding to A in the LL(1) parsing table for this grammar:

( [ ] ) $

S

T

X

c) (10 points) Is this grammar LL(1)? Explain briefly why or why not.

4. LR Parsing (60 Points)

Consider the following grammar:

S โ†’โ†’โ†’โ†’ A

A โ†’โ†’ โ†’โ†’ A + A

| B + + // each plus is a separate token

B โ†’โ†’ โ†’โ†’ y

Below is a partial DFA (CSFM) for the grammar above. Notice we did not have to add a new start state as we usually do with LR parsers.

a) (5 points) Complete state 0 by performing closure on the item listed.

b) (10 points) Fill in all elements of states 2, 4 and 7.

c) (10 points) Fill in the missing transition labels (7 of them).

S โ†’ โ€ข A

A โ†’ B โ€ข + +

A โ†’ B + โ€ข +

A โ†’ B + + โ€ข

B โ†’ y โ€ข

A

y

5. Attribute Grammars (30 points)

Consider the following context-free grammar for floating-point constants, without exponential notation. Here C , digits, digit, and more_digits are nonterminals and 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and. are terminals.

C โ†’ digits. digits

digits โ†’ digit more_digits

more_digits โ†’ digits

| ฮต

digit โ†’ 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Augment this grammar with attribute rules that will accumulate the value of the constant into a val attribute of the root of the parse tree. Your answer should be S-attributed.

6. Scoping (30 points) The interesting thing about this language is that procedures whose names are in upper - case use dynamic scoping to lookup non-local variables, whereas procedures whose names are in lower-case use static (also called lexical) scoping to lookup non-local variables. You will trace the execution of this program and answer the following 2 questions:

a) (20 points) ** What is the output of this program?

b) (10 points) Show the run-time stack when execution reaches /***/ using the frames at the right. You may not need to use all the frames.

main() { integer a, b, c := 0; // all initially 0 procedure P() { integer a := 1; print โ€œ#1โ€, a, b, c; a++; b++; if (b mod 2 = 0) then q(); else P(); print โ€œ#2โ€, a, b, c; }--end P;

procedure q() { integer b := 0; a++; b++; c++; print โ€œ#3โ€, a, b, c;/****/ }---end q

P(); print โ€œ#4โ€, a, b, c; }--end main

procedure ___main_____ lexical-link xxxx dynamic-link xxxx

procedure ________ lexical-link dynamic-link

procedure ________ lexical-link dynamic-link

procedure ________ lexical-link dynamic-link

procedure ________ lexical-link dynamic-link