11. Automata X Compiler Design Questions
11. Automata X Compiler Design Questions
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.
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.
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)
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.
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}
20. Given the two graphs G1 and G2 shown below, answer the following questions from A-Z
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
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
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).
0 1, λ
q q q
1, λ 0
0 1 2
1
0, 1, λ 0
q
3
0 1
1 0, λ
q 1, λ q q
1 2 3
0
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
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:
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
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
2,5,8 2,5,8
1,4,7
4.
0,3,6,9
III. Self-test Questions
0 1
1 0
q q q
1 1 2 0 3
Answer key a
a a
1 2 3
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
S →SbS|ScS|a
E→T | E + T | E – T
T→F | T * F | T / F
F→a | b | c | (E)
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
S→a | aA | B | C
A→aB | λ
B→Aa
C→cCD
D→ddd
S→ABa |ABS
A→aab | abc
B→Ac
S→abSb | aa
10. Define a context free grammar for declaration statement for a C++ programming language
V. Self-test Questions
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)
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.
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.
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?
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.
4. List three additional tasks performed by a scanner other than identifying tokens.
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
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 ==.
15. Give notations used in extensions of regular expressions in specifying one or more
instances, zero or one instance, and character classes.
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)
E→TE'
E'→+TE' | λ
T→FT'
T'→*FT' | λ
F→(E) | id
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.
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.
22. Define goto(I, X), for an LR(0) item I and a grammar symbol X.
27. Define goto(I, X), for an LR(1) item I and a grammar symbol X.
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
10. What is the difference between attributes in an L-attributed grammar and inherited
attributes?
X. Self-test Questions
1. List some of the activities that a compiler performs during type checking.
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.
integer
else
type_error}
t
else
type_error}
else
type_error}
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.
E.code := E1.code
t := a – b
u := a – c
v := t + u
d := v + u