






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







NAME: Sam P. L. Solution
SID or SS#:
Problem Max points Points 1 15 2 25 3 20 4 25 5 15 TOTAL 100
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:
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.
(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
bb b "" " F
A
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.
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
(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.