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

Midterm Solutions for Regular Expressions and Automata Theory, Exams of Programming Languages

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

2012/2013

Uploaded on 04/02/2013

shaila_210h
shaila_210h 🇮🇳

4.3

(36)

184 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Midterm Solutions
Brevity was not needed for the regular expressions, just correctness.
Problem 1a
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)
Problem 1b
Regular Expression:
(0|1)(0|1)(0|1)(0|1)00
Similarly with macros:
regular expression = XXXX00
Problem 1c
DFAs:
Part a.
1 1 0,1 0,1 0,1 0,1
0
0,1 1
1
0
0,1 1
1
0
0
0,1 0,1 0,1 0,1 0 0
Part b.
Problem 1d
NFA for the union:
1
pf3
pf4
pf5

Partial preview of the text

Download Midterm Solutions for Regular Expressions and Automata Theory and more Exams Programming Languages in PDF only on Docsity!

Midterm Solutions

Brevity was not needed for the regular expressions, just correctness.

Problem 1a

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)

Problem 1b

Regular Expression:

(0|1)(0|1)(0|1)(0|1)

Similarly with macros:

regular expression = XXXX

Problem 1c

DFAs:

Part a.

1 1 0,1 0,1 0,1 0, 0 0,1 1

0 0,1 (^1)

0

Part b.

Problem 1d

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:

DFA for 1b.

DFA for 1a.

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.

Problem 1e

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:

POSTINCREXPR -> IDENTIFIER ++

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.

Problem 3c

i Parse tree:

E

T plus E

id plus plus

plus plus id

T

ii The grammar is ambiguous because there are two parse trees for x+++x:

E

T plus E

id

id

E

T E

id

id T

plus plus

plus

plus

T plus

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.

Problem 4

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);