0% found this document useful (0 votes)
19 views4 pages

Sheet

The document outlines various tasks related to compiler design, including grammar analysis, regular expressions, NFA and DFA construction, and ambiguity in grammars. It provides specific examples and exercises for generating strings, minimizing DFA, and converting grammars to LL(1). Additionally, it discusses the elimination of left recursion and common prefixes in grammars, along with the predictive parsing algorithm.
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)
19 views4 pages

Sheet

The document outlines various tasks related to compiler design, including grammar analysis, regular expressions, NFA and DFA construction, and ambiguity in grammars. It provides specific examples and exercises for generating strings, minimizing DFA, and converting grammars to LL(1). Additionally, it discusses the elimination of left recursion and common prefixes in grammars, along with the predictive parsing algorithm.
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/ 4

Compiler Design

Sheet
1. Given the following grammar:
assgstmt  identifier := expression
expression  identifier
expression  number
expression  expression + expression
expression  expression - expression
expression  expression * expression
expression  expression / expression

Apply the phases of compiler design to compile the


statement: z := 2*x + y/3 -6

2.
a. Describe the languages denoted by the following regular
expressions:
1. a(a|b)*a
2. ((ε|a)b*)*
3. (a|b)*a(a|b)(a|b)
4. a*ba*ba*ba*

b. Write regular definitions for the following languages:


i. All strings of a’s and b’s with an even number of a’s.
ii. All strings of a’s and b’s with and odd number of b’s.
iii. All strings of a’s and b’s that contain at least two b's
iv. All strings of a’s and b’s that contain just two or three b`s
c. Construct the NFA for the following regular expressions:
i. (a|b)*
ii. (a*|b*)*
iii. ((ε |a)b*)*
iv. Show the sequence of moves made by i., ii. and iii. in
processing the input string: ababbab
v. b*(ab|ba)b*. Does the sentence w = “abbb” belong to
the language generated by this regular expression?
Justify (as in iv.).
d. Convert directly to DFA's, the regular expressions in c.
e. Convert the following NFA's into DFA's.

f. Minimize the following DFA's (hint: At first remove the


unreachable and dead states if there exist):

Fig. (1)

Fig. (2)
3.
a. Given the following grammar:
S  SS+ |SS* | a
i. Show how the string "aa+a*" can be generated by this
grammar by two different ways and construct the
parse tree for this string. (hint: use leftmost derivation
and rightmost derivation).
ii. Is this grammar ambiguous? Justify by an example.
iii. Is this grammar LL(1)? why?
b. Explain the problem of ambiguity for the if-else grammar
and show how to eliminate it.
c. The Operator Precedence property can solve the ambiguity
of expression grammars, justify by an example.
d. Eliminate the (immediate and indirect) left recursion for the
following grammar(hint: Apply the algorithm):
S→AB
A→CB|b
C→Sa
B→b
e. Factor out the common prefixes of the following grammar:
ET+E|T
T  int | int * T | ( E )
4.
a. Convert the following grammar to be LL(1) (hint: Apply the
algorithm of indirect left recursion elimination and the
conditions of LL(1) grammar):

SE
E  E+T | T
T  E-T | F
F  E*F | id
b. The problem of backtracking in Recursive Descent Parser can
be handled using left factoring, justify using an example.
c. Given the following LL(1) grammar:
1 S→Aa
2 A→BD
3 B→b
4 B→ε
5 D→d
6 D→ε

i. Find the FIRST sets and FOLLOW sets for the nonterminals
of the grammar and prove that the predictive parse table
for the given grammar is as follows (hint: Also, this question
can instead give you this table empty to fill in or to find it):

ii. Apply the predictive parsing algorithm to parse the


following input string and then construct the parse
tree for each one (hint: Use the obtained actions
step by step).
a) a
b) da
c) ba
d) bda
d. Solve the assignment in the last lecture and understand
well why Homework 2 is not LL (1).

Notice: This sheet is used only as a reference.

You might also like