



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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Solutions to midterm problems on regular expressions and automata theory. It includes regular expressions for specific problems, dfa and nfa diagrams, and discussions on ambiguous grammars and parsing errors.
Typology: Exams
1 / 5
This page cannot be seen from the preview
Don't miss anything!
Brevity was not needed for the regular expressions, just correctness.
Regular Expression:
(11(0|1)(0|1)(0|1)(0|1)) | ((0|1)(0|1)11(0|1)(0|1)) | ((0|1)(0|1)(0|1)(0|1)11)
You could have used macros to represent (0|1), which would have simplified your answer significantly:
let X = (0|1), then regular expression = (11XXXX) | (XX11XX) | (XXXX11)
Regular Expression:
(0|1)(0|1)(0|1)(0|1)
Similarly with macros:
regular expression = XXXX
DFAs:
Part a.
1 1 0,1 0,1 0,1 0, 0 0,1 1
0 0,1 (^1)
0
Part b.
NFA for the union:
e e e e
e = epsilon
Many people noticed that you could simply OR the previous DFA’s together to create the union:
e
e
The epsilon transitions from the start state point to the start states of the DFA’s in 1a and 1b, and we preserve the same final states in both.
DFA for the union:
1 1 0,1 0,1 0,1 0, 0 0,1 1
1
0 0,
1
1 0
0 0
0
Strings of length not equal to 6 are not accepted, along with some examples of length 6 which do not match the criteria above: 101010 , 010110 , etc. There were a lot of different answers for this question.
(ii): Two answers were possible. One is that the error is detected in the parser, because of a suitably restrictive grammar production for post-increment expressions, eg:
This would disallow the input x++++, since x++ is not an identifier.
The other answer is that the parser does not detect the error, yielding the following AST:
(iii): If you answered this question, the error must have been detected in the semantic checker. To see why, consider an evaluation of the AST with x having initial value 1. First, we evaluate x++, which yields the value 1 (the increment happens at the very end of the evaluation). Then, we attempt to evaluate 1++. But, this causes an error, since ++ must be applied to an argument whose value can be updated, like a variable.
i Parse tree:
T plus E
id plus plus
plus plus id
ii The grammar is ambiguous because there are two parse trees for x+++x:
Ambiguity is bad because if the program is parsed two different ways, it can mean two different things. The programmer can’t be certain about the meaning of the program.
start = new NFAState(); end = new NFAState(); start.addTransition(NFAState.EPSILON, childStart); start.addTransition(NFAState.EPSILON, end); childEnd.addTransition(NFAState.EPSILON, childStart); childEnd.addTransition(NFAState.EPSILON, end);