Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Main points of this past exam are: State Machine, Regular Expressions, Finite Automata, Language, Strings, Odd Position, Deterministic Finite-State Machines, Language Defined, State Machines, Appropriate Notation

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shaila_210h
shaila_210h 🇮🇳

4.3

(36)

190 documents

1 / 10

Toggle sidebar

Related documents


Partial preview of the text

Download Semantic Actions - Programming Languages and Compilers - Solved Exams and more Exams Programming Languages in PDF only on Docsity! 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 CS 164 Programming Languages and Compilers Handout 9 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 Fall 2000 page 2 of 10 CS 164 Programming Languages and Compilers Handout 9 (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 → F1 ∧ F2 F.q = F.b ? And(F1.q, F2.q) : Or(F1.q, F2.q) F1.b = F.b F2.b = F.b F → F1 ∨ F2 F.q = F.b ? Or(F1.q, F2.q) : And(F1.q, F2.q) F1.b = F.b F2.b = F.b F → ¬F1 F.q = F1.q F1.b = ¬F.b F → F1 ⇒ F2 F.q = F.b ? Or(F1.q, F2.q) : And(F1.q, F2.q) F1.b = ¬F.b F2.b = F.b F → (F1) F.q = F1.q F1.b = F.b F → id F.q = F.b ? id.lexeme : Neg(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))). Fall 2000 page 5 of 10 CS 164 Programming Languages and Compilers Handout 9 3. First and Follow Sets (20 points) Give a grammar with the following First and Follow sets. Your grammar should have exactly two productions per non-terminal and no epsilon productions. The non-terminals are X, Y, Z and the terminals are a, b, c, d, e, f . First(X) = {b, d, f} Follow(X) = {$} First(Y ) = {b, d} Follow(Y ) = {c, e} First(Z) = {c, e} Follow(Z) = {a} Follow(d) = {c, e} Follow(a) = {$} Follow(e) = {a} Follow(b) = {b, d} Follow(f) = {$} Follow(c) = {c, e} There are many ways to solve this problem. In our solution, we began by using the First sets to generate the obvious productions that have the correct first (and only) terminal: X -> b | d | f Y -> b | d Z-> c | e We are only allowed two productions per non-terminal, so we need to eliminate an X production. Noticing that b and d are in the first of Y, we can try X -> Y | f Y -> b | d Z -> c | e At this point we have the First sets correct for the non-terminals; now we try to modify the productions to have the correct Follow sets as well. Starting arbitrarily with the terminal c, we notice that both c and e are in Follow(C) and in First(Z), so we can add Z after c in some production. There is only one choice: X -> Y | f Y -> b | d Z -> cZ | e Similar reasoning about Follow(b) leads to: X -> Y | f Y -> bY | d Z -> cZ | e Fall 2000 page 6 of 10 CS 164 Programming Languages and Compilers Handout 9 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. Fall 2000 page 7 of 10
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved