


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The solutions to problem 1 and problem 2 of the cs164 spring 1997 midterm exam. Problem 1 involves analyzing flex examples and determining the output for given inputs. Problem 2 consists of identifying whether various statements are true or false and providing justifications. Additionally, there are problems related to context-free grammars, parse tables, and regular expressions.
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Following are some small Flex examples For each one, state what Flex will print for the given input.
ab { printf("1"); } ab* { printf("2"); } %%
input: abbbab
a.
%x STATE %% ab { printf("x"); } ab* { printf("y"); } a { BEGIN(STATE2); count=1 }
input: aababbababb
b.
a { printf("a"); } a+ { printf("b"); } ab { printf("c"); } b { printf("d"); }
. { printf("%s", yytext); } %%
input: a+aa+b+ab+abb+abbbb
c.
Indicate whether each statement is true or false by circling either T or F. For each statement, give a brief explanation (one sentence or an example)justifying your answer.
T F Every language can be specified by a context-free grammar. T F If a grammar G is the smallest grammar that generates a given language, then G is reduced. T F Some reduce/reduce conflicts can be eliminated by introduction of Bison-style precedence and/or associativity rules. T F Regular expressions can derive the empty string. T F It is always the scanner's job to recognize keywords.
CS164 Spring 1997 Midterm 1 1
T F For an unambiguous grammar and a string X generated by the grammar, the leftmost nad rightmost derivations for X will always produce parse trees of the same shape. T F If a grammar has a reduce/reduce conflict, then it is ambiguous. T F Instead of using Flex to write a scanner, you could write it with Bison T F Instead of using Bison to write a parser, you could write it with Flex.
Following is a context-free grammar. This grammar describes a small subset of the English language. An example of a sentence of the language is "mice which eat fish eat mice". All sentences in the language contain a subject (S) and a predicate (P). Subjects are nouns (N) optionally followed by a list of subordinate clauses (Cs). Each subordinate clause is the keyword which followed by a verb (V) and a predicate. The only verb in the language is eat. A predicate consists of a verb followed by a subject.
a.Give a leftmost derivation of the sentence "mice eat fish"
Use the parse table below to answer parts (b) - (d)
b.Fill in the two missing stack configurations (indicated by the blank boxes):
3.([bc](aba?|ba))[cb]*
For each description below, identify each regular expression that it describes, or none of the above
a.All strings of a's, b's, and c's in which all a's occur before any c's. b.All strings of a's, b's, and c's without bc in them. c.All strings of a's, b's, and c's that begin or end with bc. d.All strings of a's, b's, and c's in which a and c are never adjacent e.All strings of a's, b's, and c's with an odd number of a's