0% found this document useful (0 votes)
1 views

11. Automata X Compiler Design Questions

The document contains a series of multiple-choice questions related to finite automata, languages, grammars, and regular expressions. It covers topics such as the properties of deterministic and nondeterministic finite automata, types of grammars, and the characteristics of context-free languages. The questions also explore the concepts of ambiguity in grammars, the closure properties of languages, and the structure of regular expressions.

Uploaded by

Teddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

11. Automata X Compiler Design Questions

The document contains a series of multiple-choice questions related to finite automata, languages, grammars, and regular expressions. It covers topics such as the properties of deterministic and nondeterministic finite automata, types of grammars, and the characteristics of context-free languages. The questions also explore the concepts of ambiguity in grammars, the closure properties of languages, and the structure of regular expressions.

Uploaded by

Teddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

FINITE AUTOMATA

1. A language L from a grammar G = {VN, ,P, S} is


a) Set of symbols over VN
b) Set of symbols over 
c) Set of symbols over P
d) Set of symbols over S
2. Which is true for δ(q, ab)
a) δ(q, a) ∪ δ(q, b)
b) δ(δ(q, a), b)
c) δ(q, a), b
d) δ(q, a) ∩ δ(q, b)
3. The transition function of a DFA is
a) Q × Σ → Q
b) Q × Σ → 2Q
c) Q × Σ → 2n
d) Q × Σ → Qn
4. The transition function of an NFA is
a) Q × Σ → Q
b) Q × Σ → 2Q
c) Q × Σ → 2n
d) Q × Σ → Qn
5. The maximum number of states of a DFA converted from an NFA with n states is
a) n
b) n2
c) 2n
d) None of these
6. A string after full traversal is called not accepted by an NFA if it results in
a) Some non-final states
b) All non-final states
c) A single non-final state
d) Some final states
7. An NFA with a set of states Q is converted to an equivalent DFA with a set of states Q′.
Find which is true.
a) Q′ = Q
b) Q′ ⊆ Q
c) Q ⊆ Q′
d) None of these
8. The basic limitations of a finite state machine is
a) It cannot remember arbitrarily large amount of information
b) It cannot remember state transitions
c) It cannot remember grammar for a language
d) It cannot remember language generated from a grammar
9. The string WWR is not recognized by any FSM because
a) An FSM cannot remember arbitrarily large amount of information
b) An FSM cannot fix the mid-point
c) An FSM cannot match W with WR
d) An FSM cannot remember the first and last inputs.
10. A finite automata recognizes
a) Any language
b) Context sensitive language
c) Context-free language
d) Regular language
11. Which is true for a dead state?
a) It cannot be reached anytime
b) There is no necessity of the state
c) If control enters, there is no way to come out from the state
d) If control enters, FA is dead
12. The language accepted by the given FA is a :

a) (ab)*
b) bb*a
c) b(ba)*a
d) Null
13. In the previous FA, B is called
a) Dead state
b) Inaccessible state
c) Both a and b
d) None of these
14. Which is true for a Moore machine?
a) Output depends on the present state
b) Output depends on the present input
c) Output depends on the present state and the present input
d) Output depends on the present state and the past input
15. Which is true for the Mealy machine?
a) Output depends on the present state
b) Output depends on the present input
c) Output depends on the present state and the present input
d) Output depends on the present state and the past input
16. Which is true for the inaccessible state?
a) It cannot be reached anytime
b) There is no necessity of the state
c) If control enters, there is no way to come out from the state
d) If control enters, FA is dead
17. Which type of string is accepted by the following finite automata?

a) All string
b) Null string
c) No string
d) All of the above
LANGUAGE AND GRAMMAR
1. Which is correct
a) a+ = a*. a*
b) a* = a+. a+
c) a+ = a*. a
d) a* = a+. a*
2. (a, b)* means
a) Any combination of a, b including null
b) Any combination of a, b excluding null
c) Any combination of a, b, but ‘a’ will come first
d) None of these
3. (a, b)+ means
a) Any combination of a, b including null
b) Any combination of a, b excluding null
c) Any combqination of a, b, but ‘a’ will come first
d) None of these
4. What is the language generated by the grammar S → aSb, S → A, A → aA
a) ambm
b) ∅
c) anbm
d) ambm
5. Which type of grammar is the following in particular S → aSb S →ab
a) Unrestricted
b) Context-sensitive grammar
c) Context-free grammar
d) Regular grammar
6. Which type of grammar is the following in particular S → aS/bA A → aA/a
a) Unrestricted
b) Context-sensitive grammar
c) Context-free grammar
d) Regular grammar
7. The language generated by the grammar S → 0S0, S → 1S1, S → 0, S → 1 is
a) Even palindrome of 0 and 1
b) Odd palindrome of 0 and 1
c) Any combination of 0 and 1
d) None of these
8. What is the highest type number to the grammar given by the following production
rules S → Aa, A → c|Ba, B → abc.
a) Zero
b) One
c) Two
d) Three
9. The language {ambncm + n | m, n >=1} is
a) Regular
b) Context-free but not regular
c) Context-sensitive but not context-free
d) Type 0 but not context-sensitive
10. Which type of grammar is the following in particular A → aB, B → ac, C → bC/aD, D → bA/b
a) Context-sensitive grammar
b) Context-free grammar
c) Context-free but regular in particular
d) Not type0 but context-free
11. The machine format of context-sensitive grammar is
a) Finite automata
b) Push down automata
c) Linear bounded automata
d) All of the above
12. Which of the following grammar generates strings with any number of 1’s?
a) S→ 1A, A → ε
b) S → 1S, S→ ε
c) S →S1, S→ ε
d) (b) & (c)
13. Which of the following is true?
a) (ab)2 = a2b2
b) (ab)2 = abab
c) (ab)2 = aabb
d) None of these
14. If ∑ = {1}, then ∑* – ∑+ is
a) 1+
b) {1}
c) { e}
d) {e, 1, 11….. }
15. A language set contains
a) Alphabet
b) String
c) Set
d) None of these
16. In automata, why it is called formal language?
a) Some alphabet forms a string
b) Some strings form a language
c) Well-defined use of symbols I significant
d) Only the form of the string generated by alphabets is significant.

17. The language constructed from the grammar S → aSbb/aAbb, A → a is


a) an+1 b2n
a) anb2n
b) an b2n-1
c) an+1 b2n+1

18. The language generated by the grammar S →aSa/aBa, A→ Ba/b is


a) anban
b) anban+1
c) a2n
d) Ф
Regular Expression
1. The machine format of regular expression is
a) Finite automata
b) Push down automata
c) Turing machine
d) All of the above
2. The regular expression is accepted by
a) Finite automata
b) Push down automata
c) Turing machine
d) All of the above
3. The language of all words with at least 2 a’s can be described by the regular expression
a) (ab)*a
b) (a + b)*ab*a(a + b)*
c) b*ab*a(a + b)*
d) All of the above
4. The set of all strings of {0, 1} having exactly two 0’s is
a) 1*01*01*
b) {0 + 1)*1
c) {11 + 0}*
d) {00 + 11}*
5. The set of all strings of {0, 1} where 0 is followed by 1 and 1 is followed by 0 is
a) (01)*
b) 0*1*
c) 0*1 + 1*0
d) 0*1* + 1*0*
6. Which of the strings do not belong to the regular expression (ba + baa)*aaba
a) baaaba
b) babaabaaaba
c) babababa
d) baaaaba
7. Which of the following type of language is regular expression?
a) Type 0
b) Type 1
c) Type 2
d) Type 3
8. Which of the following RE over {0,1} denotes the set of all strings not containing
100 as substring?
a) (1 + 0)*0*
b) 0*1010*
c) 0*1*01
d) All of the above
9. ∧ + RR* = ?
a) R
b) R*
c) R +
d) ∧
10. a/b} denotes the set
a) {ab}
b) {a, b}
c) {a, b}*
d) {ab}*
11. What is the solution for the equation R = Q + RP (If P and Q are RE and P does not
contain ∧)?
a) R = QP*
b) R = QP
c) R = PQ*
d) R = P*Q*
12. Which of the languages is accepted by the following FA

a) b(a + bba*)*a*
b) a(a + bba*)*b*
c) a(a + bb*a*)*b*
d) None of these
13. Which is true for ∈-closure of a state?
a) All the states reachable from the state with input null excluding the state
b) The state only
c) All the other states
d) All the states reachable from the state with input null
14. The pumping lemma for regular expression is used to prove that
a) Certain sets are regular
b) Certain sets are not regular
c) Certain regular grammar produce RE
d) Certain regular grammar does not produce RE
15. Regular sets are closed under
a) Union
b) Concatenation
c) Kleene closure
d) All of the above

Context-free Grammar
1. Context-free language is language.
a) Type 0
b) Type 1
c) Type 2
d) Type 3
2. Parsing a string from a given grammar means
a) Finding a derivation
b) Finding a leftmost derivation
c) Finding a rightmost derivation
d) Finding a derivation tree.
3. A grammar is called ambiguous if
a) It generates more than one string
b) It generates both leftmost and rightmost derivation for a given string
c) It generates more than one parse tree for a given string
d) It fulfills both (b) and (c)
4. Which is not true for ambiguous grammar?
a) Ambiguity creates problem in generating a language from a given grammar
b) All ambiguity can be removed.
c) Inherent ambiguity cannot be removed
d) Some ambiguity can be removed by hand
5. Non-generating symbols are those symbols which
a) Do not generate any string of nonterminals
b) Do not generate any null string
c) Do not generate any string of terminal and non-terminals
d) Go not generate any string of terminals
6. Useless symbols in CFG are
a) Non-generating symbols and nonreachable symbols
b) Null alphabets and null string
c) Non-terminal symbols
d) All of these
7. Which of the following is a unit production?
a) (String of NT) → (String of NT)
b) (Single NT) → (String of NT)
c) (Single NT) → (Single NT)
d) (String of NT) → (Single NT)
8. Which is true for the following CFG?
S → aA/∈
A → bA/a
a) Null production can be removed
b) Null production cannot be removed
c) As A does not produce null, null cannot be removed
d) Both (b) and (c)
9. Which of the following production is in CNF? (more specific)
a) (NT) → (String of NT)
b) (NT) → (String of terminal and nonterminal)
c) (NT) → (String of terminal)
d) (NT) → (String of exactly two NT)
10. Which of the following production is in GNF? (more specific)
a) (NT) → (Single T)(String of NT)
b) (NT) → (Single NT)(String of T)
c) (NT) → (String of terminal and nonterminal)
d) (NT) → (String of NT)
11. Which of the following is common in both CNF and GNF?
a) (NT) → (Single T)(String of NT)
b) (NT) → (String of exactly two NT)
c) (NT) → (String of NT)
d) (NT) → (Single T)
12. Context-free language is not closed under
a) Union
b) Concatenation
c) Complementation
d) Star closure

Study the automaton given below and choose the correct answers to Questions 1-3:

1. M is a
a) nondeterministic automaton
b) deterministic automaton accepting {0. I}*
c) deterministic automaton accepting all strings over {0, I} having 3m 0's and 3n 1's, m., n >= 1
d) deterministic automaton
2. M accepts
a) 01110
b) 10001
c) 01010
d) 11111
3. If q2 is also made a final state. then At accepts
(a) 01110 and 01100
(b) 10001 and 10000
(c) 0110 but not 0111101
(d) 03n , n >= 1 but not 13n , n>=1
4. For a grammar G with productions S → SS, S → aSb, S → bSa, S → ,
(a) S => abba
(b) S =>* abba
(c) abba ∉ L(G)
(d) S =>* aaa
5. If α ⇒ β in a grammar G, then*
(a) α = β
(b) β ⇒* α
(c) β ⇒ α
(d) none of these
6. If α → β is a production in a grammar G, then
(a) αα ⇒* ββ
(b) ααβ ⇒* ββα
(c) αα ⇒* βα
(d) ααα ⇒* βββ
7. If a grammar G has three productions S → aSa | bSb | c, then
(a) abcba and bacab ∈ L(G)
(b) abcba and abcab ∈ L(G)
(c) accca and bcccb ∈ L(G)
(d) acccb and becca ∈ L(G)
8. The minimum number of productions for a grammar G = ({S}, {0, 1, 2, ..., 9}, P, S) for generating
{0, 1, 2, ..., 9} is
(a) 9
(b) 10
(c) 1
(d) 2
9. If G₁ = (N, T, P₁, S) and G₂ = (N, T, P₂, S) and P₁ ⊆ P₂, then
(a) L(G₁) ⊆ L(G₂)
(b) L(G₂) ⊆ L(G₁)
(c) L(G₁) = L(G₂)
(d) none of these.
10. The regular grammar generating {aⁿ : n ≥ 1} is
(a) ({S}, {a}, {SaS}, S)
(b) ({S}, {a}, {S → SS, S → a})
(c) ({S}, {a}, {S → aS}, S)
(d) ({S}, {a}, {SaS, S → a, S)
11. L = {theory, of, computer, science} can be generated by
(a) a regular grammar
(b) a context-free grammar but not a regular grammar
(c) a context-sensitive grammar but not a context-free grammar
(d) only by a1 type 0 grammar.
12. {aⁿ : n ≥ 1} is
(a) regular
(b) context-free but not regular
(c) context-sensitive but not context-free
(d) none of these.
13. {a^n b^n | n ≥ 1} is
(a) regular
(b) context-free but not regular
(c) context-sensitive but not context-free
(d) none of these.
14. {a^n b^n c^n | n ≥ 1} is
(a) regular
(b) context-free but not regular
(c) context-sensitive but not context-free
(d) none of these.
15. {a^n b^n c^n | n, m ≥ 1} is
(a) regular
(b) context-free but not regular
(c) context-sensitive but not context-free
(d) none of these.
16. The set of all strings over {a, b} of even length is represented by the regular expression
(a) (ab + aa + bb + ba)*
(b) (a + b)(a + b*)
(c) (aa + bb)*
(d) (ab + ba)*
17. The set of all strings over {a, b} of length 4, starting with an a is represented by the regular
expression
(a) a(a + b)*
(b) a(ab)*
(c) (ab + ba)(aa + bb)
(d) a(a + b)(a + b)(a + b)
18. If L is the set of all strings over {a, b} containing at least one a, then it is not represented by the
regular expression
(a) ba(a2 + b)
(b) (a + b)a(b + a)
(c) (a + b)ab
(d) (a + b)*a
19. {a^(2n) | n ≥ 1} is represented by the regular expression
(a) (aa)*
(b) a*
(c) aa*a (d) a*a*
(d) (aa)*
20. The set of strings over {a, b} having exactly 3 b's is represented by the regular expression
(a) a*bbb
(b) a*ba*ba*b
(c) ba*ba*b
(d) a*ba*ba*ba*
21. The set of all strings over {a, b} having abab as a substring is represented by
(a) a*ababb*
(b) (a + b)*abab(a + b)*
(c) a*b*ababa*b*
(d) (a + b)*abab
22. (a + a) is equivalent to
(a) a(a*)*
(b) a*
(c) aa*
(d) none of these.
23. a(a + b) is equivalent to
(a) a* + b*
(b) (ab)*
(c) ab
(d) none of these.
24. ab + b represents all strings w over {a, b}
(a) starting with an a and having no other a's or having no a's but only b's
(b) starting with an a followed2 by b's
(c) having no a's but only b's3
(d) none of these.

I. Self-test Questions without answer key

Attempt all of these questions and make sure that you answer them correctly.

1. Give an example of a set that can be represented using complete listing method

2. Give an example of a set that can be represented using partial listing method

3. Give an example of a set that can be represented using set builder method

4. An infinite set can be represented using complete listing method (TRUE or FALSE)

5. A  B, if and only if all elements of A are also elements of B (TRUE or FALSE)

6. A  B, if and only if all elements of A are also elements of B (TRUE or FALSE)

7. Two sets A and B are said to be equivalent if and only if all elements of A are in B (TRUE
or FALSE)

8. Given two set A and B each having N and M elements each, what are the maximum and
minimum number of elements in A  B.

9. Given two set A and B each having N and M elements each, what are the maximum and
minimum number of elements in A  B.

10. What will be A  B and A  B given A = {1,2,3,8, 11} and B={3, 2, 1, 9, 8}

11. What will be A - B given A = {1,2,3,8, 11} and B={3, 2, 1, 9, 8}

12. What will be the complements of A given A = {1,2,3,8, 11} and U={1,2,3, …, 15}

13. Proof by deduction that the roots of the equation x 2 -5x + 6 = 0 are +2 and +3
14. Proof by deduction that the number of vertices with odd number of degree in a graph are
even

15. Proof by induction that the sum of the integers from 1 to N equals (N+1)*N/2

16. The set of ASCII characters of a computer defines an alphabet in formal language theory
(TRUE or FALSE)

17. Any string in a natural language are defined from the Latin alphabets{a, b, c, …z, A, B, C,
… Z)

18. Define at least three different formal languages from the alphabet {a, b, c, d, e}

19. Differentiate {}, {} and  from formal language perspective

20. Given the two graphs G1 and G2 shown below, answer the following questions from A-Z

a. Identify the degree of all the vertices of G1 and G2

b. Identify the in-degree and out-degree of G2

c. Identify the degree of the graph G1 and G2

d. Distinguish order of a graph and size of a graph

e. Find cycles from G1 and G2

f. Find a walk from G1 which is not a path


g. Is there a loop in G1 and G2

h. How many connected component G1 has

i. Represent G1 and G2 using Adjacency matrix

j. Represent G1 and G2 using Adjacency list

I.I. Questions with answer key

1. Given a set A = {C++, C, C#}, list all the power set of A (P(A)). How many elements does
P(A) has?

2. According to the De Morgan’s law, given A as the set of all even integers, B as the set of
integers which are multiple of three and U as the set of all integers answer the following
questions

a. Describe what the complement of A  B be verbally as well as using partial listing


method

b. Describe what the complement of A  B be verbally as well as using partial listing


method

3. Proof by induction that the sum of the degrees of a graph is twice the number of edges

Answer key

1. The number of elements P(A) will have is exactly 2n where n is the number of elements of
A. Hence A has 8 elements in its power set. These are

{,{C++},{C},{C#}, {C++, C}, {C++,C#}, {C,C#}, {C++, C, C#}}

2.

a. A  B is the set of all even integers and all odd integers which are multiple of 3.
Hence, the complement of A  B becomes a set of non-even integer which are not
also multiple of three. Therefore, (A  B)  The set of odd integers which are not
multiple of three
b. A  B is the set of integers which are both even and multiple of three. That means,
the complement of A  B becomes a set of all non-even integer or integers which
are not multiple of three. Hence, (A  B)  The set of odd integers or integers
which are not multiple of three

3. Proof by induction that the sum of the degrees of a graph is twice the number of edges

Base phase: assume there is only one edge in the graph. A single edge connects two
vertices only say vertices A and B. Hence, the degree of all vertices becomes 0 other than
A abd B which have a degree of 1 each. Hence the sum of all the degrees becomes 1 + 1
+ 0 + 0 + … + 0 = 2. Hence it is true for N =1.

Induction phase: Assume it is true for any N such that N  m, we want to show that it is
also true for N= m+1 (i.e the sum of all the degrees of the vertices becomes 2(m+1) = 2m
+ 2)?

Let’s assume we have a graph G of m+1 edge where one of the edges is from A to B. If we
ignore this edge, we will have a Graph G with m edge. As G has m edges, then the sum
of the degrees of all the vertices become 2m (induction assumption above). If we add
back the removed edge that links A with B, G become modified into G and degree of A
and B will increase by one each. Hence, the total sum of degrees of all vertices of G
becomes the total sum of degrees of G plus 2 (i.e. 2m + 2 = 2(m+1).

II. Self-test Questions

1. Define the language defined by the DFSA shown above


2. Find the DFSA that accept a string w of the alphabet {a,b} where Na mod 3 > 1. Na is the
number of the symbol a in w.
3. Answer the following questions based on the following FSA.
0 1

0 1, λ
q q q
1, λ 0
0 1 2

1
0, 1, λ 0

q
3

A. What are Q, ∑ and F?


B. Is the string 0000111 accepted by the FSA? Justify your answer.
C. Convert the FSA to an NFSA without -transition and then check the acceptance of the
string 0101.
D. Convert the NFSA without -transition moves you obtained above to the
corresponding DFSA and then check the acceptance of the string 1100.
E. Define the language determined by the machine
4. Construct a DFA or NFA that generates a string over the alphabet {a,b} which has m a’s
followed by n b’s for all n and m greater than or equals to 0
5. Discuss the similarities and differences between DFSA and NFSA.
6. Find an equivalent NFSA without λ corresponding to the NFSA shown in the figure below.

0 1
1 0, λ
q 1, λ q q
1 2 3
0

7. Find an equivalent DFSA for the following NFSA.


b a
a b
A B C
b a

8. Define the language determined by the NFSA machine above


9. Find a regular grammar that generates a language that is accepted by the automaton given
in question 11
10. Construct a minimal DFSA for the following DFSA.

11. Given an FSA that accepts binary representation of an integer divisible by three.
12. Given an FSA that accepts binary representation of an integer that ends with 01.
13. Define extended transitions for a DFA machine
14. Describe acceptance a string w by DFS using extended transitions concept
15. Define extended transitions for a NFA machine
16. Describe acceptance a string w by NFS using extended transitions concept
17. Prove that any DFA machine can be converted into NFA machine
18. Prove that any NFA machine can be converted into DFA machine

II.I. Questions with answer key

1. Find a DFSA that accept a string w of the alphabet {a} where |w| mod 3 |w| mod 2.
2. Show that the language L = {an: n ≥ 3} is regular.

3. Design a DFA that accept only a number (in base 10) which is multiple of 3.

Answer key
1. To answer this question, we may start by defining a state as an indicator that
indicates the remainder for the modulo for the current length when divided by
two and three. Hence, I define states as follows:

a. State 0 as a state when length modulo 2 = 0 and length modulo 3 = 0

b. State 1 as a state when length modulo 2 = 1 and length modulo 3 = 0

c. State 2 as a state when length modulo 2 = 0 and length modulo 3 = 1

d. State 3 as a state when length modulo 2 = 1 and length modulo 3 = 1

e. State 4 as a state when length modulo 2 = 0 and length modulo 3 = 2

f. State 5 as a state when length modulo 2 = 1 and length modulo 3 = 2

Hence, except state 0 and 3 are accepting states as per the description.

a
3 4
a a

0 1
a a a
5 2

2. In order to show the language L = {an: n ≥ 3} is regular, we need to construct a DFA that

will accept all strings of L and reject others. The following DFA fulfill the requirement
a

a a
1 2 3

3. ={0,1,2,3,4,5,6,7,8,9}. A digit 0, 3, 6 and 9 are all multiple of 3 hence their


modulo to 3 is 0. When we compute modulo of the digit 1, 4, 7, 10 to 3, it will be
1 and the remaining digits (2, 5, and 8) has module of 2. If X mod 3 is M for any
integer X then Y mod 3 becomes N where Y is X concatenated with some digit d.
This relations is shown in the table below for a given X
M=X mod 3 Possible d value N = Y mod 3

0 0,3,6,9 0

0 1,4,7 1

0 2,5,8 2

1 0,3,6,9 1

1 1,4,7 2

1 2,5,8 0

2 0,3,6,9 2

2 1,4,7 0

2 2,5,8 1

DFA:

2,5,8

1,4,7 1,4,7

0,3,6,9 M=0 M=1 M=2 0,3,6,9

2,5,8 2,5,8

1,4,7
4.
0,3,6,9
III. Self-test Questions

1. Find an NFSA that accepts L(r) where r = aa*(a + b)


2. Find a regular expression corresponding to the NFSA shown in the figure below.

0 1

1 0
q q q
1 1 2 0 3

3. State pumping lemma for regular languages


4. Using pumping lemma show that the language L = {anb2n | n > 0} is not regular.
5. Write regular expression for the languages all strings ending in 01 given = {0, 1}.
6. Write regular expression for the languages all strings not ending in 01 given = {0, 1}.
7. Find regular expressions for the languages on L = {w: na(w) mod 3 = 0}given ={a, b}.
Find regular expressions for the languages on L = {w: na(w) mod 5 > 0}given ={a, b}.
8. State the precedence rules for the regular expression operators
9. Construct a regular expression r for C++ identifiers (i.e x  L(r)  x is a C++ identifier)
10. Convert the regular expressions (0+1)01 into NFA with  transitions
11. Convert the regular expressions 00(0+1)* into NFA with  transitions
12. Prove the following theorems
a. The union of two regular language is regular
b. The intersection of two regular language is regular
c. The complement of a regular language is regular
d. The difference of two regular language is regular
e. The reversal of a regular language is regular
f. The closure (star) of a regular language is regular
g. The concatenation of two regular language is regular
III.I. Questions with answer key

1. Convert the regular expressions 01* into NFA with  transitions


2. Find regular expressions for the languages on L = {w: |w| mod 3 = 0} given ={a, b}.
3. Write regular expression for the languages all strings containing even number of zeros given
= {0, 1}.

Answer key a

a a
1 2 3

IV. Self-test Questions

1. Describe the following grammars using the different grammar characteristics (context free or
not, the language it defines, linearity, ambiguity, single step or not, etc)

B: S →aB|A
B. S → aSb | λ
aA →aA|a|CBA

C: S →aB B →λ

B →bA|b D: A→aB C →c

A.
B→cA|C
A →a
C→cC|c
C →c
2. Given a grammar G = (N, T, P, S) with productions:
C →c
C.
S →AB D.

A →aA|a

B →bB|b

And a string x = aaabbb


A) find a left most and right most derivations for x.

B) draw the parse tree for x.

C) is the grammar ambigious?

3. Given a grammar G = (N, T, P, S) with productions:

S →SbS|ScS|a

and a string x = abaca

A) find a left most and right most derivations for x.

B) draw the parse tree for x.

C) is the grammar ambigious?

4. Consider the following grammar:

E→T | E + T | E – T

T→F | T * F | T / F

F→a | b | c | (E)

Draw parse trees for the following strings

a) a*b+c b) a+b*c c) (a+b)*c d) a-b-c

5. Given CFG grammar G with productions

S → aS | AB,

A → λ,

B → λ,

D → b,

construct a grammar G’ without null productions such that L(G’) = L(G) - {λ}.
6. Remove all unit productions from the following CFG.

S→Aa | B

B→A | bb

A→a | bc | B

7. Eliminate useless productions from

S→a | aA | B | C

A→aB | λ

B→Aa

C→cCD

D→ddd

8. Convert the grammar with productions

S→ABa |ABS

A→aab | abc

B→Ac

to Chomsky normal form.

9. Convert the grammar with productions

S→abSb | aa

to Greibach normal form.

10. Define a context free grammar for declaration statement for a C++ programming language
V. Self-test Questions

1. Construct an DPDA that accepts L = {anbn: n≥0}

2. Construct an NPDA that accepts L = {a2nbn: n≥0}

3. Construct an NPDA that accepts L = {anb2n: n≥0}

4. Using instantaneous description show that whether the string aaaabb is accepted in question
2

5. Construct an NPDA that accepts the language generated by the following CFG

E→T | E + T | E – T

T→F | T * F | T / F

F→a | b | c | (E)

6. Construct an NPDA that accepts L = {wwR: w {a, b}+}

7. Construct an NPDA for the language L = {w {a, b}+: na(w) = nb(w)}

Compiler Design
VI. Self-test Questions

1. ___________ is a program that can read a program written in high-level language and
converts it into an equivalent machine language.

2. ___________ is a program that translates and then executes a high-level instruction


before translating the next instruction.

3. ___________ is a program whose task is collecting modules of a program stored in


separate files and expanding macros into source language statements.

4. ___________ processes assembly language program and produces relocatable machine


code.

5. ___________ is a program that links together relocatable machine codes with other
relocatable object and library files into the code actually runs on the machine.
6. ___________ puts all executable programs into memory for execution.

7. List the various phases of a compiler

8. List the phases of a compiler that are termed as front-end of a compiler.

9. List the phases of a compiler that are termed as back-end of a compiler.

10. What are the input to and the output of lexical analysis phase?

11. What are the input to and the output of syntax analysis phase?

12. What are the input to and the output of semantic analysis phase?

13. What are the input to and the output of intermediate code generation phase?

14. What are the input to and the output of code generation phase?

15. What is the purpose of symbol table?

16. Give an example of lexical analyzer generator.

17. Give an example of parser generator.

VII. Self-test Questions

1. _________________ reads the input characters of the source program, groups them
into lexemes, and produces as an output a sequence of tokens for each lexeme in the
source program.

2. _____________ is a description of the form that lexemes of a token may take.

3. _____________ is a sequence of characters in the source program that matches the


pattern for a token.

4. List three additional tasks performed by a scanner other than identifying tokens.

5. List some of the tokens that exist in a particular programming language.

6. What is/are the purpose/s of attributes of a token?

7. List some of the lexical errors that can occur during scanning.

8. List some of the actions that a lexical analyzer takes in recovering from lexical errors.
9. Define a regular expression for the following tokens in C++ programming

a. Identifier

b. Keywords

c. Operators

d. Symbols

e. Integer numbers

f. Floating point numbers

g. Strings

h. Character

10. For the above tokens, what attribute each token to have if the list shows the token
identity in group

11. Discuss how input buffering works with and without the use of sentinels.

12. Draw a transition diagram that recognizes the operators: +, ++, +=, --, -, -= , = and ==.

13. Distinguish regular definition and regular expression

14. Write a regular definition for integer constants in C++.

15. Give notations used in extensions of regular expressions in specifying one or more
instances, zero or one instance, and character classes.

16. Discuss how a lexical analyzer system is built

VIII. Self-test Questions

1. What are the input to and the output of a parser?

2. List the two commonly used parsing methods using in compilers.

3. List the goals that an error handler should possess.


4. List four types of error recovery modes.

5. How does a compiler that implements panic mode error recovery method recovery from
an error? Discuss

6. FIRST(α), a string of grammars symbols α, is a set of terminals that begins the strings
derived from α. (TRUE / FALSE)

7. FOLLOW(A), for a nonterminal A, is the set of terminals α that can appear immediately
to the right of A in some sentential form. (TRUE / FALSE)

8. Compute FIRST and FOLLOW of non-terminals in the following grammar:

E→TE'

E'→+TE' | λ

T→FT'

T'→*FT' | λ

F→(E) | id

9. List two types of top-down parsing techniques.

10. Construct a non recursive predictive parsing table for the grammar in question 8 and
then parse the input id + id $.

11. Parse input efef using recursive descent parsing technique based the following
grammar.
A → fAeA | eAfA| λ

12. What types of grammars are LL(1) grammars? What does LL(1) stand for?

13. List the procedures employed to check whether a given grammar is an LL(1) grammar or
not.

14. What are handle and handle pruning in bottom-up parsing?


15. How does a shift-reduce parser works?

16. List the conflicts that occur in shift-reduce parsing and explain briefly about the
conflicts.

17. List the four possible actions that a shift-reduce parser makes.

18. What does LR(K) stand for?

19. Discuss the LR parsing algorithm

20. Define an LR(0) item.

21. What is closure(I), for an LR(0) item I?

22. Define goto(I, X), for an LR(0) item I and a grammar symbol X.

23. Describe the sets of items construction algorithm in SLR.

24. Describe the algorithm for constructing an SLR(1) parsing table.

25. Define an LR(1) item.

26. What is closure(I), for an LR(1) item I?

27. Define goto(I, X), for an LR(1) item I and a grammar symbol X.

28. Describe the sets of items construction algorithm in LR(1).

29. Describe the algorithm for constructing an LR(1) parsing table.

IX. Self-test Questions

1. ________________ is a generalization of the CFG in which each grammar symbol has an


associated set of attributes (synthesized and inherited) and rules.

2. List the two types of attributes in syntax directed definition.

3. What is an annotated parse tree?

4. Given the following syntax directed definition, draw annotated parse tree for 1101.11
Syntax Rules Semantic Rules
N→L1.L2 N.v = L1.v + L2.v / (2L2.l)
L1→L2 B L1.v = 2 * L2.v + B.v
L1.l = L2.l + 1
L→B L.v = B.v
L.l = 1
B→0 B.v = 0
B→1 B.v = 1

5. What types of attributes are v, l, and s in question 4? (synthesized / inherited)

6. Draw a dependency graph for question 4.

7. Write at least two topological sort for question 4.

8. What is an S-attributed grammar?

9. What is an L-attributed grammar?

10. What is the difference between attributes in an L-attributed grammar and inherited
attributes?

11. Give an example of S-attributed grammar.

12. Give an example of L-attributed grammar.

X. Self-test Questions

1. List some of the activities that a compiler performs during type checking.

2. What does uniqueness check mean?

3. List some of the type expressions in C++.

4. List some of the type constructors in C++.

5. Describe the difference between structural equivalence and name equivalence in type
expressions.
6. Given the following syntax directed definition for assigning types for program parts,
draw an annotated parse tree for a mod b assuming that a and b have been declared
integers.

E→literal {E.type := char}

E→num {E.type := integer}

E→id {E .type := lookup (id.entry)}

E→E1 mod E2 {E.type := if E1.type = integer and E2.type := integer then

integer

else

type_error}

E→E1[E2] {E.type := if E2.type = integer and E1.type := array(s, t)


then

t
else
type_error}

E→^E1 {E.type := if E2.type = pointer(t) then

else

type_error}

XI. Self-test Questions

1. What are the uses of generating an intermediate code?

2. List three intermediate languages used in intermediate code generation.


3. Give the format of three-address code for function call.

4. Give the format of three-address code for conditional jump.

5. Describe the uses of the functions: newtemp, newlabel, and gen in generating three-
address code statement using syntax directed translation.

6. Given the following syntax directed definition, generate three-address code statements
for the assignment statement: a:=x+y*z.

Production Semantic Rules

S→id := E S.code := E.code || gen (id.lexeme, :=, E.place)

E→E1 + E2 E.place := newtemp;

E.code := E1.code || E2.code || gen (E.place, ‘:=’, E1.place, ‘+’,


E2.place)

E→E1 * E2 E.place := newtemp;

E.code := E1.code || E2.code || gen (E.place, ‘:=’, E1.place, ‘*’,


E2.place)

E→- E1 E.place := newtemp;

E.code := E1.code || gen (E.place, ‘:= uminus ’, E1.place)

E→(E1) E.place := newtemp;

E.code := E1.code

E→id E.place := id.lexeme;

E.code := ‘’ /* empty code */

XII. Self-Test Questions

1. What are the input to and the output of a code generator?


2. What properties are required of the generated code?

3. Give an example on how selection of instructions affects the efficiency of a generated


code.

4. Describe the differences between register allocation and register assignment.

5. Given the following three-address statements for the assignment statement d := (a – b)


+ (a – c) + (a – c), describe how the simple code generation algorithm works assuming
that d is live.

t := a – b

u := a – c

v := t + u

d := v + u

You might also like