Name:
Entry:
COL226: Programming Languages
II semester 2015-16
Minor 1
60 minutes
Sat 13 Feb 2016
Gp No:
Max Marks 40
1. [7 marks] Prove that for any two functions f : 0 a − > 0 b, g : 0 b − > 0 c and any list L : 0 a list
map (g o f ) L = (map g) (map f L)
where map f [ ] = [ ] and map f (h :: t) = (f h) :: (map f t)
Solution.
The proof is by structural induction on list L.
Basis
map (g o f ) [ ] = [ ], using base case of map f unction def inition, i.e., map f [ ] = [ ].
(1)
(map g)(map f [ ]) = (map g) [ ],
= [ ],
using map f [ ] = [ ]
(2)
using map f [ ] = [ ].
(3)
Induction Hypothesis
Assume for any two functions f : 0 a− >0 b, g : 0 b− >0 c, and a list T : 0 a list of length k ≥ 0,
map (g o f ) T = (map g)(map f T )
Induction Step
Let L = x :: T be any list of length k + 1.
=
=
=
=
=
=
map (g o f ) L
map (g o f ) (x :: T )
((g o f ) x) :: (map (g o f ) T )
{definition of map}
(g (f x)) :: (map (g o f ) T )
{definition of o}
(g (f x)) :: (map g (map f T )) {Induction Hypothesis}
(map g)(f x :: map f T )
{definition of map}
(map g)(map f L)
{definition of map}
1
Name:
Entry:
Gp No:
2. [5+5 = 10 marks] A ‘wise man’ once claimed, “A picture is worth a thousand words”. However
it is not clear what a million words are worth. Consider the language
L = {{a, b}k c{a, b}k | 1 ≤ k ≤ 1000000}
over the alphabet A = {a, b, c}.
(a) Design a deterministic finite automaton which accepts the language L.
(b) Design a deterministic finite automaton which accepts the language L∗ .
Solution.
(a) Let D = hQ, A, q0 , δ, {q00 }i where Q = {qk , qk0 | 0 ≤ k ≤ 106 } ∪ {qd }, A = {a, b, c}, δ : Q × A →
Q with δ(qi , a) = δ(qi , b) = qi+1 , for 0 ≤ i < 106 , δ(qi , c) = qi0 , for 1 ≤ i ≤ 106 and
0
δ(qi0 , a) = δ(qi0 , b) = qi−1
, for 1 < i ≤ 106 , δ(q00 , a) = δ(q00 , b) = δ(q00 , c) = qd ,
δ(qd , a) = δ(qd , b) = δ(qd , c) = qd , and δ(q0 , c) = δ(q106 , a) = δ(q106 , b) = qd
If you desperately want a state diagram for the above DFA, here it is:
start
q0
a,b
q1
a,b
c
q10
c
a,b
q00
q2
a,b
c
a,b
c
a,b,c
q20
c
q3
c
a,b
c
q30
q1000000
c
0
q1000000
c
a,b
qd
(b) Let D∗ = hQ, A, q0 , δ, {q0 }i where Q = {qk , qk0 | 1 ≤ k ≤ 106 } ∪ {q0 , qd }, δ : Q × A → Q
with δ(qi , a) = δ(qi , b) = qi+1 , for 0 ≤ i < 106 , δ(qi , c) = qi0 , for 1 ≤ i ≤ 106 and
0
0
δ(qi+1
, a) = δ(qi+1
, b) = qi0 , for 1 < i < 106 , δ(q10 , a) = δ(q10 , b) = q0 and all other cases
go to qd a dead state. Note that q0 is both start state and accepting state.
If you desperately want a state diagram for the above DFA, here it is:
start
q0
a,b
a,b
q1
c
q10
c
a,b
q2
a,b
c
a,b
c
q20
c
qd
q3
c
a,b
c
q30
q1000000
c
0
q1000000
c
a,b
2
Name:
Entry:
Gp No:
3. [10 marks] Let RE(A) denote the set of regular expressions over the alphabet A such that none of
the regular expression operators occur as symbols in A. That is, the set {ε, ∅, ∗, (, ), ., |} is disjoint
from A.
(a) Define RE(A) in EBNF.
(b) Define a context-free grammar that generates RE(A).
Solution.
(a) Assume A = {a1 , . . . , an }
RegExp
SimpleRegExp
Alphabet
N onemptyRegExp
::=
::=
::=
::=
EmptyRegExp | N onemptyRegExp
“∅” | “ε”
a1 | · · · | an
Alphabet |
N onemptyRegExp“{.”N onemptyRegExp“}” |
N onemptyRegExp“{|”N onemptyRegExp“}” |
N onemptyRegExp“*” |
“(”N onemptyRegExp“)”
(b) Let O = {ε, ∅, ∗, (, ), ., |}. Then the set of terminal symbols is given by T = A ∪ O. G =
hN, T, P, Si and the sets N , P and S are as shown below in the rewrite rules.
i. Since the question does not specify any precedence between operators the corresponding
CFG may be specified unambiguously by a fully-parenthesized grammar as follows.
S
R
A
−→ ∅ | ε | R
−→ A | (R.R) | (R|R) | (R∗)
−→ a1 | · · · | an
ii. Alternatively we may define the precedence of operators using the usual convention that
unary operators have the highest precedence followed by the binary operators. We may
assume further that all binary operators are left associative and that . has a higher precedence than |. Then we have the following rules.
S
R
D
K
F
A
−→
−→
−→
−→
−→
−→
∅|ε|R
D | D|R
K | K.D
F | F∗
A | (S)
a1 | · · · | an
3
Name:
Entry:
Gp No:
4. [7+6=13 marks] Let O = {⊕, , ⊗} be three right-associative infix binary operators in an expression language which has following precedence rules.
(a) has the highest precedence and ⊕ has the lowest precedence with ⊗ having precedence
strictly in between and ⊕.
(b) In any expression, between two neighbouring occurrences of the same operator,
the left one has higher precedence than the one on the right.
Let I = {a, b, c} be the set of identifier tokens and B = {(, )} is the set of brackets that are allowed
for grouping sub-expressions together. Then T = I ∪ O ∪ B is the set of terminal tokens.
(a) Design a context-free grammar to generate all valid expressions in this language which allows
for any expressions (with or without the use of brackets) to be unambiguously parsed in such
a way that the precedence rules are respected.
(b) Design attribute grammar rules to transform every expression into a unique expression belonging to the language generated by the rules
S
I
−→
−→
I | (S ⊕ S) | (S ⊗ S) | (S S)
a | b | c
Solution.
(a) The required grammar is G = h{S, P, M, D, I}, {a, b, c}, P, Si where the set P of productions
is as follows.
S
P
M
D
I
−→
−→
−→
−→
−→
P | P ⊕S
M | M ⊗P
D | DM
I | (S)
a|b|c
The above grammar is for right-associative binary operators with the given precedence. It
will generate many expressions with (some or no) parentheses and the rules of precedence will
determine how the grouping needs to be done.
(b) The grammar G0 given in question (4b) generates only a subset of the strings generated by the
solution G in answer (4a). The task now is to give attribute definitions to the rules in G such
that every expression (with some or no) parentheses is converted into a fully parenthesised
expression with the right kind of grouping as intended in the string. Since such a translation
fully parenthesizes every compound expression generated by G, there would be no need to use
the precedence rules when evaluating the expression. We synthesize a single attribute str for
each non-terminal symbol
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
S
S0
P
P0
M
M0
D
D
I
I
I
−→
−→
−→
−→
−→
−→
−→
−→
−→
−→
−→
P
P ⊕ S1
M
M ⊗ P1
D
D M1
I
(S)
a
b
c
S.str := P.str
S0 .str := “(”P.str ⊕ S1 .str“)”
P.str := M.str
P0 .str := “(”M.str ⊕ P1 .str“)”
M.str := D.str
M0 .str := “(”D.str ⊕ M1 .str“)”
D.str := I.str
D.str := S.str
I.str := “a”
I.str := “b”
I.str := “c”
Notice that rule 8. removes existing parentheses (whether they are redundant or not) and
the rules 2., 4., 6. ensure that there are exactly as many pairs of parentheses as there are
occurrences of binary operators in the expression.
4
                
    
            
    
                © Copyright 2025 Paperzz