


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!



First and Follow Sets (15 points) Consider the following three grammars
A -> BC B -> Ax | x C -> yC | y
A -> BC B -> Ax | x | epsilon C -> yC | y
A -> BC B -> Ax | x | epsilon C -> yC | y | epsilon For each of the following statements about First and Follow sets, indicate for which grammars(s) (1, 2, or 3) the statement is correct. Each statement is correct for one or more grammars-be sure to list all the grammars for which a statement is correct. Note that grammar 3 is a small extension of grammar 2, which is a small extension of grammar 1. You may find it helpful to compute the First and Follow sets beginning with the simplest grammar.
CS 164, Fall 2000 Midterm #1Professor not listed 1
Regular Expressions and Context-Free Grammar (15 points) In this problem we will show that every regular language is also a context-free language. The proof is to construct for every regular expression an equivalent context-free grammar. A regular expression is equivalent to a context-free grammar if they generate the same language. We will do the first case for you; the problem is to show how to do two other cases. Consider the regular expression AB. Assume that we have already converted A to an equivalent context-free grammar with start symbol SA and that we have converted B to an equivalent context-free grammar with start symbol SB. Then a context free grammar for the language AB with start symbol SAB is
(We assume that the grammars for Sa and SB share no non-terminals.) (a) In the same style as above, what is the context-free grammar for A + B? (b) What is the context-free grammar for A*?
LR Parsing (25 points) In these problems we describe a DFA for accepting viable prefixes of a grammar. Your assignment is to give a grammar whose DFA recognizing viable prefixes satisfies the description. Assume the DFA's we describe contain LR(0) items-no lookaheads are involved. Do not augment your grammar with an extra production S -> S'. Just find any grammar that works. (a) The DFA consists of one state and no transitions. (b) The DFA consists of two states s1 and s2 and there is one transition from the start state s1 to s2. (c) The DFA consists for three states s1, s2, and s3. There is one transition from the start state s1 to s2, one transition from s2 to itself, and one transition from s2 to s3.
Syntax-Directed Translation (10 points) For the following grammar for arithmetic expression define a syntax-directed translation that records the sign (POS or NEG) of each subexpression E in the attribute E sign; Write your semantic actions to the right of the corresponding production. you may assume that all integers are non-zero (so you don't need to worry about
(b) What language does your augmented grammar generate?