





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 instructions and questions for the cs164 midterm i exam in computer science, held in fall 2003. The exam covers topics such as regular expressions, finite automata, deterministic and non-deterministic finite-state machines, context-free grammars, and ll(1) parsing. Students are required to write their answers in the provided space and are allowed to use handwritten notes.
Typology: Exams
1 / 9
This page cannot be seen from the preview
Don't miss anything!






NAME and LOGIN:
Your SSN or Student ID:
Circle the time of your section: 9:00 10:00 11:00 2:00 3:
Q1 (30 points) Q2 (30 points) Q3 (30 points) Q4 (10 points) Total (100 points)
1 Regular Expressions/Finite Automata
Part (a)
Consider a language over the alphabet { 0 , 1 } consisting of the strings that meet the following conditions:
For example, 110000, 000011, and 101111 are all in the language; 00000, 001010, and 100110 are not.
Write a regular expression that defines this language:
Part (b)
Consider a language over the alphabet { 0 , 1 } consisting of the strings that meet the following conditions:
For example, 110000, 001100, and 001100 are all in the language; 000011, 001010, and 111001 are not.
Write a regular expression that defines this language:
Part (e).
Draw a deterministic finite-state machine for the language that is the union of the languages de- fined in Parts (a) and (b). Also, write a string that is not accepted by your finite-state machine.
2 First/Follow/LL(1)
This question concerns the context-free grammar given below (where capital letters denote non- terminals, small letters denote terminals).
S → A B | B A B A → S A | y x B → x |
Part (a)
Fill in the following table with First and Follow sets for the grammar.
y x
x
Part (b)
In the LL(1) parsing table below, fill in the column headings, and the row corresponding to the non-terminal A:
Part (c)
Is the grammar LL(1)? Justify your answer.
(i) Was the error found in the lexer? If yes , clearly explain the cause of the lexical error, and move on to Part (c) on the next page. If not , write the output of the lexer for this input expression and continue to (ii). (Assume that the lexer uses three kinds of tokens: the + operator, the ++ operator, and an identifier.)
(ii) Was the error found in the parser? If yes , clearly explain the cause of the syntactic error, and move on to Part (c) on the next page. If not , write the output of the parser (i.e., an AST) for this input expression and continue to (iii).
(iii) Was the error found in the semantic checker? If yes, clearly explain the cause of the semantic error. (Clearly, since you got all the way to this part, the answer must be yes .) To show the error, all you need to do is evaluate the AST from (ii) under the assumption that the initial value of x is 1, and show where and why the evaluation would fail. Evaluate the AST just like you did in your PA1 interpreter.
Part (c) Let’s assume that when the programmer wrote the expression x+++++x, he really meant to compute the expression x++ + ++x. Unfortunately, the first Java expression is not equivalent to the second expression with the whitespaces (as discussed above, the first expression is not even a legal Java expression). Clearly, in Java, white spaces are sometimes very important.
One solution how to make the compiler recognize x+++++x as equivalent to x++ + ++x without having to insert any whitespaces into the expression is to move part of the functionality of the lexer to the parser.
Specifically, let us propose that the ++ operator won’t be recognized in the lexer; instead, the lexer would simply turn each + character into a plus token. So, the lexer recognizes only two kinds of tokens: id and plus. As usual, the lexer discards any white space from the input.
It would then be up to the parser to group plus tokens into ++ operators. Below is a grammar for such a parser. This grammar describes a tiny subset of the language of arithmetic expressions (this language includes additions, pre-increments and post-increments):
E → T plus E | T T → id | id plus plus | plus plus id
(i) Show the parse tree for the input string x+++++x.
(ii) Let’s now show that the proposed solution unfortunately doesn’t work — because the gram- mar is ambiguous. Use the input x+++x to show that the grammar is ambiguous.
Why does ambiguity in a grammar for a programming language pose a problem?