Docsity

Semantic Actions - Programming Languages and Compilers - Solved Exams, Exams of Programming Languages

Toggle sidebar
bg1
CS 164 Programming Languages and Compilers Handout 9
Midterm I Solution
โ€ขPlease read all instructions (including these) carefully.
โ€ขThere are 5 questions on the exam, some with multiple parts. You have 1 hour and 20 minutes
to work on the exam.
โ€ขThe exam is closed book, but you may refer to your four sheets of prepared notes.
โ€ขPlease write your answers in the space provided on the exam, and clearly mark your solutions.
You may use the backs of the exam pages as scratch paper. Please do not use any additional
scratch paper.
โ€ขSolutions will be graded on correctness and clarity. Each problem has a relatively simple and
straightforward solution. You may get as few as 0 points for a question if your solution is far
more complicated than necessary. Partial solutions will be graded for partial credit.
NAME: Sam P. L. Solution
SID or SS#:
Problem Max points Points
1 15
2 25
3 20
4 25
5 15
TOTAL 100
Fall 2000 page 1 of 10
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Semantic Actions - Programming Languages and Compilers - Solved Exams and more Exams Programming Languages in PDF only on Docsity!

Midterm I Solution

  • Please read all instructions (including these) carefully.
  • There are 5 questions on the exam, some with multiple parts. You have 1 hour and 20 minutes to work on the exam.
  • The exam is closed book, but you may refer to your four sheets of prepared notes.
  • Please write your answers in the space provided on the exam, and clearly mark your solutions. You may use the backs of the exam pages as scratch paper. Please do not use any additional scratch paper.
  • Solutions will be graded on correctness and clarity. Each problem has a relatively simple and straightforward solution. You may get as few as 0 points for a question if your solution is far more complicated than necessary. Partial solutions will be graded for partial credit.

NAME: Sam P. L. Solution

SID or SS#:

Problem Max points Points 1 15 2 25 3 20 4 25 5 15 TOTAL 100

  1. Regular Expressions and Finite Automata (15 points)

Consider the design of a small language using only the letters โ€œzโ€, โ€œoโ€, and the slash character โ€œ/โ€. A comment in this language starts with โ€œ/oโ€ and ends after the very next โ€œo/โ€. Comments do not nest. Give a single regular expression that matches exactly one complete comment and nothing else. For full credit, use only the core regular expression notations A + B, AB, Aโˆ—, A+, , and โ€ฒโ€ฒabcโ€ฒโ€ฒ.

This is a classic regular expression puzzle, usually stated as describing C comments. We departed slightly from the usual โ€œ/ โˆ—... โˆ— /โ€ syntax to avoid confusion between * and ?. Regular expressions are not unique for a given language, so there are multiple correct answers. Here are just three of the many equally valid approaches:

/o (o?^ z + /)?^ o+/ /o /?^ (o?^ z /?)?^ o+/ /o (/?^ o?^ z)?^ /?^ o+/

The key feature of each of these solutions is the parenthesized expression in the middle. In each case, this parenthesized expression is designed to prevent an o from ever appearing immediately before a /. They all use a โ€œo?^ zโ€ pattern to represent an optional sequence of o followed by a mandatory z. That is really the heart of the solution; the rest just takes care of letting slashes appear elsewhere in the body, and is not difficult to fill in after some careful thought. One other subtle point is worth noticing. Because โ€œo?^ zโ€ always requires a z after each sequence of o in the comment body, we need some other way of dealing with a sequence of o that runs up to the very end of the comment. For this reason, all three solutions end with โ€œo+/โ€ rather than simply โ€œo/โ€. If we had ended our patterns with โ€œo/โ€, it would have been impossible to match a comment like โ€œ/ooo/โ€. Common errors:

  • matching too long a string: the obvious โ€œ/o (o + z + /)?^ o/โ€ pattern will incorrectly match โ€œ/oo/zzzz/oo/โ€ as one comment
  • trying to prevent โ€œo/โ€, but missing cases where we loop through some โ€œ(... )?โ€ expression multiple times: for example, โ€œ/o (/ + oz + oo)?^ o+/โ€
  • not allowing a comment that has one or more โ€œ/โ€ immediately after the opening โ€œ/oโ€
  • not allowing a comment that has one or more โ€œoโ€ immediately before the closing โ€œo/โ€
  • forbidding all โ€œoโ€ or all โ€œ/โ€ from the body of a comment
  • not allowing โ€œ/oโ€ in the body of a comment
  • not allowing comments of the form โ€œ/oooo... oooo/โ€
  • accepting โ€œ/o/โ€ as a comment
  1. Semantic Actions (25 points)

Consider the following grammars and associated semantic actions. In the actions, the operations And, Or, and Not are constructors for an abstract syntax tree data type. For each grammar, answer three things.

  • Say whether each attribute of a non-terminal is inherited or synthesized and why.
  • Show the value of the attributes of G after parsing ยฌ(A โˆง (A โ‡’ B)).

(a) G โ†’ F G.p = F.p F โ†’ F 1 โˆง F 2 F.p = And(F 1 .p, F 2 .p) F โ†’ F 1 โˆจ F 2 F.p = Or(F 1 .p, F 2 .p) F โ†’ ยฌF 1 F.p = N eg(F 1 .p) F โ†’ F 1 โ‡’ F 2 F.p = Or(N ot(F 1 .p), F 2 .p) F โ†’ (F 1 ) F.p = F 1 .p F โ†’ id F.p = id.lexeme Both attributes G.p and F.p are synthesized. In every production, G.p and F.p are func- tions of attributes of their child nodes. The can be computed in a bottom-up parse. A simplified parse tree for the expression ยฌ(A โˆง (A โ‡’ B)) is shown here:

G

F HH H   ยฌ F aa %% aa !! !! F

A

โˆง F

bb b "" " F

A

โ‡’ F

B

Using the parse tree, the attribute values can be calculated from the bottom up by applying the semantic actions in the grammar. The value of G.p is Not(And(A,Or(Not(A),B))).

(b) Remember: Say whether each attribute of a non-terminal is inherited or synthesized and why. Show the value of the attributes of G after parsing ยฌ(A โˆง (A โ‡’ B)).

G โ†’ F G.q = F.q F.b = true F โ†’ F 1 โˆง F 2 F.q = F.b? And(F 1 .q, F 2 .q) : Or(F 1 .q, F 2 .q) F 1 .b = F.b F 2 .b = F.b F โ†’ F 1 โˆจ F 2 F.q = F.b? Or(F 1 .q, F 2 .q) : And(F 1 .q, F 2 .q) F 1 .b = F.b F 2 .b = F.b F โ†’ ยฌF 1 F.q = F 1 .q F 1 .b = ยฌF.b F โ†’ F 1 โ‡’ F 2 F.q = F.b? Or(F 1 .q, F 2 .q) : And(F 1 .q, F 2 .q) F 1 .b = ยฌF.b F 2 .b = F.b F โ†’ (F 1 ) F.q = F 1 .q F 1 .b = F.b F โ†’ id F.q = F.b? id.lexeme : N eg(id.lexeme)

The attributes G.q and F.q can be computed bottom-up. However, F.b is inherited, and must be computed top-down. Even though G.q andF.q can be computed bottom-up, they depend on inherited attributes, and are themselves inherited. To compute G.q, the value of F.b must be propagated down the tree. Then, F.q can be computed at each node based on the values of F.b. The value of G.q is Or(Not(A),And(A,Not(B))).

Since $ is not in the follow of Y, there must be something that comes after Y in the X -> Y production. Since First(Z) is in the Follow(Y) we try:

X -> YZ | f Y -> bY | d Z -> cZ | e

At this point we still havenโ€™t used a. Now Follow(a) = {$} and Follow(Z) = {a}. So we get

X -> YZa | f Y -> bY | d Z -> cZ | e

At this point it is routine to check that all the requirements are satisfied.

  1. LR Parsing and Error Recovery(25 points)

Consider the following grammar.

S โ†’ S a | b | error a

(a) Show the DFA for recognizing viable prefixes of this grammar. Use LR(0) items, and treat error as a terminal.

States:

I0 = { S -> .Sa, S -> .b, S -> .error a } I1 = { S -> S.a } I2 = { S -> b. } I3 = { S -> error. a } I4 = { S -> error a. } I5 = { S -> Sa.}

Transitions:

I0 -S-> I I0 -b-> I I0 -error-> I I1 -a-> I I3 -a-> I

  1. Miscellaneous Parsing (15 points)

(a) Give an unambiguous grammar that is not LR(1). There are an infinite number of unambiguous non-LR(1) grammars. Here is one of the simpler ones:

S โ†’ A x y | B x z A โ†’ a B โ†’ a

The grammar is unambiguous; it contains exactly two strings and each string has a unique derivation:

S โ‡’ A x y โ‡’ a x y S โ‡’ B x z โ‡’ a x z

However, the grammar is not LR(1). Consider an input that begins with โ€œa xโ€. After shift- ing the a, the parser must reduce to either A or B. However, based solely on the lookahead character of x, we cannot decide which of A or B is correct, leading to a reduce/reduce conflict. Parsing this grammar correctly would require SLR(2), LR(2), or LALR(2). With two characters of lookahead, we would know to reduce a to A on lookahead x y, but to reduce a to B on lookahead x z. (b) How many strings are in the language of the grammar S โ†’ aS? The language of a grammar is the set of strings derivable from the grammarโ€™s start symbol. A derivation results from applying some finite sequence of productions from the grammar; a string is a finite sequence of terminals selected from a given alphabet ฮฃ. The grammar given above can never have a finite derivation that yields a finite string. There is no string of terminals ฯ‰ such that S โ‡’+^ ฯ‰. Therefore, there are zero strings in the language of this grammar. (c) Which of LL(1), SLR(1), and LR(1) can parse strings in the following grammar, and why?

E โ†’ A | B A โ†’ a | c B โ†’ b | c

The grammar is ambiguous; there are two possible derivations of the string โ€œcโ€. Neither LL(1) nor SLR(1) nor LR(1) is able to parse any ambiguous grammar. While it is possible to work through complete LL(1), SLR(1), and LR(1) constructions to show exactly where they fail, that is not necessary. As soon as you show that a grammar is ambiguous you immediately know that none of LL(1), SLR(1), or LR(1) can possibly work.