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

CD QB

The document outlines the syllabus and key concepts for a Compiler Design course, focusing on the introduction to compilers and lexical analysis. It covers definitions, phases of compilation, the role of lexical analyzers, and various related topics such as tokens, patterns, and finite automata. Additionally, it includes questions and explanations regarding compiler construction tools, error recovery mechanisms, and the use of regular expressions.

Uploaded by

J.A.S SANTHOSH
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)
3 views

CD QB

The document outlines the syllabus and key concepts for a Compiler Design course, focusing on the introduction to compilers and lexical analysis. It covers definitions, phases of compilation, the role of lexical analyzers, and various related topics such as tokens, patterns, and finite automata. Additionally, it includes questions and explanations regarding compiler construction tools, error recovery mechanisms, and the use of regular expressions.

Uploaded by

J.A.S SANTHOSH
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/ 27

CS1601 – Compiler Design Department of CSE 2023-2024

UNIT I INTRODUCTION TO COMPILERS AND LEXICAL ANALYSIS


Translators – Compilation and Interpretation - Language processors - Compiler Construction Tools
-Structure of a compiler – Lexical Analysis – Role of Lexical Analyzer – Specification of Tokens –
Recognition of Tokens – Lex – Design of Lexical Analyzer for a sample Language – Finite Automata
– Regular Expressions to Automata – Minimizing DFA.
PART-A
1. Define Compiler (or) What does translator mean?
Compiler is a program that reads a program written in one language –the source language- and
translates it into an equivalent program in another language- the target language. In this
translation process, the compiler reports to its user the presence of the errors in the source
program.
2. What is Cross complier? (Nov 21)
There may be a complier which run on one machine and produces the target code for another
machine. Such a complier is called cross complier. Thus by using cross complier technique
platform independency can be achieved.
3. Illustrate diagrammatically how a language is processed. (May 16)

4. What advantages are there to a language-processing system in which the compiler


produces assembly language rather than machine language? (Nov 20)
The compiler may produce an assembly-language program as its output, because assembly
language is easier to produce as output and is easier to debug.
5. State the two main parts of compilation and its function. (May 18) (May 23)
What are the two parts of a compilation? Explain briefly. (May 16, 17)
The two parts of compilation are:
✓ Analysis: The analysis part breaks up the source program into constituent pieces and
creates an intermediate representation of the source program. The analysis part is called
the front end of the compiler.
✓ Synthesis: The synthesis part constructs the desired target program from the
intermediate representation. The synthesis part is called the back end of the compiler.
6. What are the phases included in front end of a compiler? What does the front end
produce? (May 22)
The analysis part is called the front end of the compiler; it includes the phases:
✓ Lexical analysis
✓ Syntax analysis
✓ Semantic analysis
✓ Intermediate code generation
The analysis part collects information about the source program and stores it in a data
structure called a symbol table, which is passed along with the intermediate representation
to the synthesis part.

St. Joseph’s College of Engineering Page 1 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
7. What are the Phases of a compiler?
✓ Lexical analysis
✓ Syntax analysis
✓ Semantic analysis
✓ Intermediate code generation
✓ Code optimization
✓ Code generation
8. What is Lexical Analysis?
The first phase of compiler is Lexical Analysis or linear analysis in which the stream of
characters making up the source program is read from left-to-right and grouped into tokens
that are sequences of characters having a collective meaning.
9. What is meant by Semantic Analysis?
The semantic analysis phase checks the source program for semantic errors and gathers type
information for the subsequent code generation phase. It uses the hierarchical structure
determined by the syntax-analysis phase to identify the operators and operand of
expressions and statements.
10. What is Symbol Table or Write the purpose of symbol table. (May 14)
List the attributes stored in symbol table. (May 19)
Symbol table is a data structure containing a record for each identifier, with fields for the
attributes of the identifier. These attributes may provide information about the storage
allocated for a name, its type, its scope (where in the program its value may be used), and in
the case of procedure names, such things as the number and types of its arguments, the
method of passing each argument (for example, by value or by reference), and the type
returned. The data structure allows us to find the record for each identifier quickly and to
store or retrieve data from that record quickly.
11. Define Passes.
In an implementation of a compiler, portion of one or more phases are combined into a
module called pass. A pass reads the source program or the output of the previous pass,
makes the transformation specified by its phases and writes output into an intermediate file,
which is read by subsequent pass.
12. List of some Compiler Construction Tools.
Some commonly used Compiler-Construction Tools include:
✓ Parser generators ✓ Code generator generators
✓ Scanner generators ✓ Data-flow analysis engines
✓ Syntax-directed translation engines ✓ Compiler-construction toolkits
13. With a neat block diagram specify the interactions between the lexical analyser and the
parser. (Nov 20)
State the interaction between the lexical analyser and the parser. (Nov 21)

The interaction is implemented by having the parser call the lexical analyzer. The call,
suggested by the getNextToken command, causes the lexical analyzer to read characters from
its input until it can identify the next lexeme and produce for it the next token, which it
returns to the parser.

St. Joseph’s College of Engineering Page 2 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
14. What is the input to lexical analyzer generator? What is its output? (May 22)
The input to lexical analyzer generator is the source program. The main task of the lexicalanalyzer
is to read the input characters of the source program, group them into lexemes, and produce as
output a sequence of tokens for each lexeme in the source program. The stream of
tokens is sent to the parser for syntax analysis; which is the output of the lexical analyzer.
15. What is the need for separating the analysis phase into lexical analysis and parsing?
✓ Separation of lexical analysis from syntax allows the simplified design.
✓ Efficiency of complier gets increased. Reading of input source file is a time consuming
process and if it is been done once in lexical analyzer then efficiency get increased. Lexical
analyzer uses buffering techniques to improve the performances.
✓ Input alphabet peculiarities and other device specific anomalies can be restricted to
the lexical analyzer.
16. Define Tokens, Patterns and Lexemes. (May 13, 14, 19)
Tokens: A token is a pair consisting of a token name and an optional attribute value. The token
name is an abstract symbol representing a kind of lexical unit, e.g., a particular keyword, or a
sequence of input characters denoting an identifier.
Patterns: A pattern is a description of the form that the lexemes of a token may take. In the
case of a keyword as a token, the pattern is just the sequence of characters that form the
keyword. For identifiers and some other tokens, the pattern is a more complex structure that
is matched by many strings.
Lexemes: A lexeme is a sequence of characters in the source program that matches the pattern
for a token and is identified by the lexical analyzer as an instance of that token.
17. What are the possible error recovery actions in a lexical analyzer? (May 12, 15, 18,23)
✓ Panic mode recovery: Delete successive characters from the remaining input until the
lexical analyzer can find a well-formed token. This technique may confuse the parser.
✓ Other possible error recovery actions:
➢ Deleting an extraneous characters
➢ Inserting missing characters.
➢ Replacing an incorrect character by a correct character.
➢ Transposing two adjacent characters
18. Why is buffering used in lexical analysis? What are the commonly used buffering methods?
Lexical analyzer scans the sources program line by line. For storing the input string, which is to
be read, the lexical analyzer makes use of input buffer. The lexical analyzer maintains two
pointers: forward and backward pointers for scanning the input string. There are two types
of input buffering schemes- one buffer scheme and two buffer scheme.

19. List the operations on languages. (May 16)


Union L U M ={s | s is in L or s is in M}
Concatenation LM ={st | s is in L and t is in M}
Kleene Closure L* (zero or more concatenations of L)
Positive Closure L+ ( one or more concatenations of L)
20. Draw a transition diagram to represent relational operators.
The relational operators are: <, <=, >, >=, =, !=

St. Joseph’s College of Engineering Page 3 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
21. List the rules for constructing regular expressions?
The rules are divided into two major classifications(i) Basic rules (ii) Induction rules
Basic rules:
✓ ε is a regular expression that denotes {ε} (i.e.) the language contains only an empty
string.
✓ For each a in ∑, then a is a regular expression denoting {a}, the language with only one
string, which consists of a single symbol a.
Induction rules:
If R and S are regular expressions denoting languages L(R) and L(S) respectively then,
✓ (R)|(S) is a regular expression denoting L(R) U L(S)
✓ (R).(S) is a regular expression denoting L(R). L(S)
✓ (R)* is a regular expression denoting L(R)*
22. What are algebraic properties of regular expressions?
The algebraic law obeyed by regular expressions are called algebraic properties of regular
expression. The algebraic properties are used to check equivalence of two regular expressions.
S.No Properties Meaning
1 r|s=s|r | is commutative
2 r|(s|t)=(r|s)|t | is associative
3 r(st)=(rs)t Concatenation is associative
4 r(s|t)=rs|rt; (s|t) r=sr|tr Concatenation is distributive over |
5 єr=rє=r є is the identity for concatenation
6 r*=(r|є)* є is guaranteed in a closure
7 r*' =r* *is idempotent
23. Write a short note on LEX.
An input file, which we call lex.1, is written in the Lex language and describes the lexical
analyzer to be generated. The Lex compiler transforms lex. 1 to a C program, in a file that is
always named lex. yy . c. The latter file is compiled by the C compiler into a file called a. out, as
always. The C-compiler output is a working lexical analyzer that can take a stream of input
characters and produce a stream of tokens.

24. What are finite automata?


Finite automata are recognizers; they simply say "yes" or "no" about each possible input string.
Finite automata come in two flavors:
✓ Nondeterministic finite automata (NFA) have no restrictions on the labels of their edges.
A symbol can label several edges out of the same state, and E, the empty string, is a possible
label.
✓ Deterministic finite automata (DFA) have, for each state, and for each symbol of its input
alphabet exactly one edge with that symbol leaving that state.
St. Joseph’s College of Engineering Page 4 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
25. Define Nondeterministic Finite Automata.
A nondeterministic finite automaton (NFA) consists of:
✓ A finite set of states S.
✓ A set of input symbols , the input alphabet. We assume that , which stands for the
empty string, is never a member of .
✓ A transition function that gives, for each state, and for each symbol in  U () a set of
next states.
✓ A state s0from S that is distinguished as the start state (or initial state).
✓ A set of states F, a subset of S, that is distinguished as the accepting states (or final
states).
26. Give the formal definition of DFA?
Formally a DFA consists of 5-tuples M = (Q,,,q0, F),Where
Q Finite set of states
 Finite set of input symbols called alphabet
 Transition function defined as  : Q → Q
qo Initial state (q0 Є Q)
F Set of accepting states ' r final ' tates (F is subset of Q)
27. Difference between Deterministic FA and Non Deterministic FA.
DFA NFA
On each input there is one and only one On each input the automaton can be in
state to which the automata' on move from several states at once
its current state
The transition function returns only one The transition function returns zero, one or
state.(i.e) : Q x  → Q more states. (i.e) : Q x  → 2Q
Construction of DFA is difficult compared Construction of NFA is easy compared to
to NFA (Take more space) DFA. (Takes less space)
Implementation of DFA is easy compared to Implementation of NFA is difficult
NFA. (speeds up computation) compared to DFA. (slow computation
process)
PART-B
1. Explain the various phases of a compiler in detail. Also write down the output for the
following expression after each phase a:= b*c-d.
Describe the various phases of a compiler and tract it with the program segment
(position=initial + rate * 60)(May 16)
What is difference between a phase and pass of a compiler? Explain machine dependent and
machine independent phase of compiler.
Explain various phases of a compiler in detail. Write the output of each phase of the compiler
for the expression c := a + b * 12. (May 19) (May 23)
What is a compiler? Describe the various phases of compiler with suitable example. (Nov 21,
May 22)
2. i. Explain language processing system with neat diagram. (May 16)
ii. Explain the need for grouping of phases.
3. i. Write notes on compiler construction tools. (May 15)
ii. Explain the various errors encountered in different phases of compiler.
4. i. Explain in detail about lexical analyzer generator.
ii. Explain about input buffering.
5. i. Differentiate between lexeme, token and pattern. (May 16)
ii. What are the issues in lexical analysis?
iii. Write notes on regular expressions.
St. Joseph’s College of Engineering Page 5 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
6. Show the transition diagram for relational operators and unsigned numbers.
7. What is a transition diagrams? Explain briefly how the keywords and identifiers are
recognized using a running example. (Nov 20)
8. What are Lexical errors? What are the possible recovery mechanisms? Divide the following
C++ program :
float limited Square(x) float x;
/* returns x-squared, but never more than 100 */
return (x<= –10.0||x>=10.0)?100 : x*x
into appropriate lexemes. Which lexemes should get associated lexical values?
What should those values be? (Nov 20)
9. Explain in detail about the role of Lexical analyzer with the possible error recovery actions.
10. Describe the specification of tokens and how to recognize the tokens (May 13)
11. Describe the language for specifying lexical Analyzer.
12. Write LEX specifications and necessary C code that reads English words from a text file and
response every occurrence of the sub string „abc‟ with „ABC‟. The program should also
compute number of characters, words and lines read. It should not consider and count any
lines(s) that begin with a symbol „#‟
13. i. How finite automation is used to represent tokens and perform lexical analysis with
examples.
ii. Compare and contrast NFA and DFA. (Nov 21)
14. i. Considering the alphabet ∑={0,1}. Construct a Non-Deterministic Finite Automata (NFA)
using the Thompson construction that is able to recognize the sentences generated by the
regular expression (1*01*0)*1*.
ii. Illustrate how does LEX work? (5 ) (May 18)
15. Write an algorithm to construct an NFA into a regular expression. (Nov 10)
16. Construct the NFA from the (a|b)*a(a|b) using Thompson‟s construction algorithm.
17. Write the subset construction algorithm. Using the subset construction algorithm, convert the
regular expression (a|b)*abb to DFA. (13) (May 19) (May 23)
18. i. Write notes on regular expression to NFA. Construct Regular expression to NFA for the
sentence (a|b)*a. (May 16)
ii. Construct DFA to recognize the language (a|b)*ab.
19. Consider the regular expression below which can be used as part of a specification of the definition
of exponents in floating-point numbers. Assume that the alphabet consists of numeric digits („0‟
through „9‟) and alphanumeric characters („a „through „z‟) with the addition of a selected small
set of punctuation and special characters.(say in this example only the characters „+‟ and „-„
are relevant). Also ,in this representation of regular expressions the characters „.‟ Denotes
concatenation.
Exponent=(+|-|ᵋ).(E|e).(digit)+
i. Drive an NFA capable of recognizing its language using the Thompson construction.
ii. Derive the DFA for the NFA found above using the subset construction.
iii. Minimize the DFA found above using the interactive refinement algorithm described
in class. (May 18) (May 23)
20. Prove that the following two regular expressions are equivalent by showing that minimum
state DFA‟s are same. (May 15)
(i)(a|b)*#
(ii)(a*|b*)*#
21. Give the minimized DFA for the following expression. (a|b)*abb (May 22)
St. Joseph’s College of Engineering Page 6 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
UNIT II SYNTAX ANALYSIS
Need and Role of Parser – Context-Free Grammars – Top Down Parsing - Recursive Descent Parser
- Predictive Parser-LL(1) Parser- Shift Reduce Parser-LR Parser-LR(0) Item - Construction of SLR
Parsing Table - LALR Parser - Error Handling and Recovery in Syntax Analyzer-YACC.

PART-A
1. Define parser (or) What is the role of parser? (May 15)
The parser obtains a string of tokens from the lexical analyzer, and verifies that the string of
token names can be generated by the grammar for the source language. The parser to reports
any syntax errors in an intelligible fashion and to recover from commonly occurring errors to
continue processing the remainder of the program. The parser constructs a parse tree and
passes it to the rest of the compiler for further processing.
2. State the various error recovery strategies used in a parser to correct the errors. (Nov 20)
✓ Panic Mode
✓ Standard Mode
✓ Error Productions
✓ Global Corrections
3. Define a Context Free Grammar.
A CFG, G is formally defined as a 4-tuple G = (V, T, P,S) where
V Finite set of Variables or non-terminals
T Finite set of Terminals
P Finite set of Production rule
S Start symbol, S Є V
4. Briefly discuss about the concept of derivation.
The sequence of substitutions, used to obtain a string is called a derivation. Derivation refers
to replace an instance of a given string‟s non-terminal, by the RHS of the production rule,
whose LHS contains the non-terminals to be replaced.
5. Define Ambiguous Grammar. (May 16, Nov 21)
A CFG G such that some word has two parse trees is said to be ambiguous grammar. In
other words a CFG G has more than one leftmost (or rightmost) derivation for given word
w is said to be ambiguous grammar.
6. What is meant by left recursion?
A grammar is left recursive if it has a non-terminal A such that there is derivation A=>Aα
for some string α. Top down parsing methods cannot handle left-recursion grammars, so a
transformation that eliminates left recursion in needed.
E → E +T | T
T→T*F|F
F → (E) | id
7. Eliminate the left recursion for the grammar (Nov 13)(May 13)
S → Aa|b
A → Ac|Sd|є
Let's use the ordering S, A (S = A1, A = A2).
✓ When i = 1, we skip the "for j" loop and remove immediate left recursion from the S
productions (there is none).
✓ When i = 2 and j = 1, we substitute the S-productions in A → Sd to obtain the A-
productions A → Ac | Aad | bd | ε
✓ Eliminating immediate left recursion from the A productions yields the grammar:
S → Aa | b
A → bdA' | A'
A' → cA' | adA' | ε
St. Joseph’s College of Engineering Page 7 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
8. Eliminate Left Recursion for the grammar.
E → E+T | T
T → T*F | F
F → (E) | id.
General Rule:
A → Aα | β then convert it to
A → βA‟
A‟ → αA‟
A‟ → є
After Left Recursion Elimination the grammar is:
E → TE‟
E‟ → +TE‟ | є
T → FT‟
T‟ → *FT‟ | є
F → (E) | id
9. Elininate left recursion from the following grammar A→Ac|Aad|bd|є . (May 13)
A → bdA'
A' → aAcA' | adA' |є
10. Write the algorithm to eliminate left recursion from a grammar?
INPUT: Grammar G with no cycles or -productions.
OUTPUT: An equivalent grammar with no left recursion.
METHOD: Apply the algorithm to G. Note that the resulting non-left-recursive grammar may
have -productions.
1) arrange the nonterminals in some order A1,A2… .An
2) for ( each i from 1 to n ) {
3) for ( each j from 1 to i - 1 ) {
4) replace each production of the form Ai →Aj γ by the
productions Ai→δ1γ| δ2γ| …….| δkγ, where
Aj→δ1| δ2|…….| δk are all current Aj-productions
5 }
6) eliminate the immediate left recursion among the Ai-productions
7) }
11. What is meant by Left Factoring?
Left factoring is removing the common left factor that appears in two productions of the same
non-terminal. It is done to avoid back-tracing by the parser. Suppose the parser has a look-ahead.
Consider this example A → qB | qC where A,B,C are non-terminals and q is a sentence. In
this case, the parser will be confused as to which of the two productions to choose and it might
have to back-trace. After left factoring, the grammar is converted to
A → qD
D→B|C
In this case, a parser with a look-ahead will always choose the right production.

12. Eliminate the Left factor in the following grammar:


S → iEtS | iEtSeS |a
E → b.
The left factored grammar is,
S → iEtSS′ | a
S′ → eS | ε
E→b

St. Joseph’s College of Engineering Page 8 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
13. Define recursive-descent parser.
A recursive-descent parsing program consists of a set of procedures, one for each
nonterminal. Execution begins with the procedure for the start symbol, which halts and
announces success if its procedure body scans the entire input string.
14. Write the algorithm for FIRST and FOLLOW? (May 16)
FIRST:
✓ If X is terminal, then FIRST(X) is {X}.
✓ If X is non-terminal and X →Y1,Y2..Yk is a production, then place a in FIRST(X) if for
some i , a is in FIRST(Yi) , and Є is in all of FIRST(Y1),…FIRST(Yi-1);
✓ If X → Є is a production, then add Є to FIRST(X).
FOLLOW
✓ Place $ in FOLLOW(S), where S is the start symbol and $ is the input right end
marker.
✓ If there is a production A → B, then everything in FIRST() except for Є is placed in
FOLLOW(B).
✓ If there is a production A → B, or a production A→ B where FIRST() contains Є,
then everything in FOLLOW(A) is in FOLLOW(B).
15. Define predictive parsers.
A predictive parser is an efficient way of implementing recursive-descent parsing by handling
the stack of activation records explicitly. The predictive parser has an input, a stack, a parsing
table and an output.

16. What are the problems in top down parsing?


✓ Left Recursion.
✓ Back Tracking.
✓ The order in which alternates are tried can affect the language accepted.
17. What is handle pruning? (May 22)
A technique to obtain the rightmost derivation in reverse (called canonical reduction
sequence) is known as handle pruning
✓ Start with a string of terminals w we wish to parse.
✓ If w is a sentence of the grammar at hand, then w = γn, where γn is the n – th right
sentential from of some as yet unknown rightmost derivation.
✓ S = γ0  γ1  γ2  …. γn – 1  γn = w
18. What is Bottom up parsing and shift reduce parsing? (Nov 20)
The bottom up style of parsing is called shift reduce parsing. Shift-reduce parsing is a form
of bottom-up parsing in which a stack holds grammar symbols and an input buffer holds the
rest of the string to be parsed. This parsing method is bottom up because it attempts to
construct a parse tree for an input string beginning at the leaves and working up towards
the root.
19. What are the four possible action of a shift reduce parser?
✓ Shift action: the next input symbol is shifted to the top of the stack
✓ Reduce action: replace handle
✓ Accept action: successful completion of parsing
✓ Error action: find syntax error

St. Joseph’s College of Engineering Page 9 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
20. List down the conflicts during shift-reduce parsing.
✓ Shift/reduce conflict: Parser cannot decide whether to shift or reduce.
✓ Reduce/reduce conflict: Parser cannot decide which of the several reductions to
make.
21. What is LR Parsers? Mention the components of LR parser.
LR(k) parsers scan the input from (L) left to right and construct a (R) rightmost derivation
in reverse. LR parsers consist of an input, output, stack, driver routine and a parsing table.
The k is for the number of input symbols of look ahead that are used in
making parsing decisions. When k is omitted, k is assumed to be 1.
22. Define LR (0) item.
An LR(0) item of a grammar G is a production of G with a dot at some position of the right
side.
A→.XYZ
A→X.YZ
A→XY.Z
A→XYZ.
23. What are Kernel and non-Kernel items? (Nov 21)
Kernel items: the initial item, S' → .S, and all items whose dots are not at the left end.
Nonkernel items: all items with their dots at the left end, except for S' → .S.
24. List the different techniques to construct an LR parsing table?
✓ Simple LR(SLR).
✓ Canonical LR.
✓ Lookahead LR (LALR).
25. How to make an ACTION and GOTO entry in SLR parsing table?
✓ If [A→α.aβ] is in Ii and goto (Ii, a)=Ij then set ACTION[I, a] to =shift j. Here ‗a„ must
be a terminal.
✓ If [A→a.] is in Ii then set ACTION [I, a] to “reduce AA→a” for all a in FOLLOW (A),
here A should not be S„.
✓ If S„→S is in Ii then set ACTION[I, $] to “accept”‖.
26. Define viable prefixes.
The set of prefixes of right sentential forms that can appear on the stack of a shift-reduce
parser are called viable prefixes. An equivalent definition of a viable prefix is that it is a prefix
of a right sentential form that does not continue past the right end of the rightmost
handle of that sentential form.
27. Summarize the merits and demerits of LALR parser. (May 18)(May 23)
Merits
✓ Merging of states with common cores can never produce a shift/reduce conflict that
was not present in any one of the original states. Because shift actions depends only
one core, not the look ahead.
Demerits
✓ Merger will produce reduce / reduce conflict.
✓ On erroneous input, LALR parser may proceed to do some reductions after the LR
parser has declared an error, but LALR parser never shift a symbol after the LR parser
declares an error.
28. What is the syntax for YACC source specification program?
Declarations
%%
Translation rules
%%
Supporting C-routines

St. Joseph’s College of Engineering Page 10 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
29. What is the purpose of YACC. (May 19, 22)
YACC then generates a function to control the input process. This function, called a parser,
calls the user-supplied low-level input routine (the lexical analyzer) to pick up the basic items
(called tokens) from the input stream. It shows how a parser generator can be used to
facilitate the construction of the front end of a compiler.
PART-B
1. Explain Context free grammars with examples.
2. i. Prove the grammar is ambiguous.
E → E + E | E * E | (E) | id
ii. Specify the demerits of ambiguous grammar.
iii. What are the rules to convert an unambiguous grammar from ambiguous grammar. Write
necessary steps for the above ambiguous grammar.
iv. Using unambiguous grammar, write Leftmost derivation ,draw parse tree for the string
id*id*id+id*id
3. A grammar symbol X (terminal or nonterminal) is useless if there is no derivation of the form
S wXy wxy That is, X can never appear in the derivation of any sentence. Elaborate on the algorithm
that is used to eliminate from a grammar all productions containing useless symbols. Apply
your algorithm to the grammar :
S→0|A
A→ AB
B → 1 (Nov 20)
4. Write the algorithm to eliminate left-recursion and left-factoring and apply both to the
following grammar.
E-→E+T|E-T|T
T-→a|b|(E) (May 15)
5. Write in detail about
i. Recursive descent parsing with algorithm.
ii. Top down parsing with algorithm.
6. Consider the following CFG grammar over the non-terminals{X,Y,Z} and terminals {a,c,d}
with the productions below and start symbol Z.
X→a
X→Y
Z→d
Z→XYZ
Y→c
Y→ᵋ
Compute the FIRST and FOLLOW sets of every non-terminal and the set of non-terminals are
nullable. (May 18)
7. Check whether the following grammar is a LL(1) grammar
S →iEtS | iEtSeS | a
E→ b
Also define the FIRST and FOLLOW procedures. Explain in detail with an example. Write
down the necessary algorithm.
8. Show that the following grammar is LL(1). (May 22)
S → AaAb | BbBa
A→
B→
St. Joseph’s College of Engineering Page 11 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
9. Construct predictive parsing table and parse the string NOT(true OR false)(May 23)
bexpr→bexpr OR bterm | bterm
bterm→bterm AND bfactor | bfactor
bfactor→NOT bfactor | (bexpr) | true | false
10. Construct non recursion predictive parsing table for the following grammar. E→E
or E | E and E| not E | (E) | 0 | 1. (Nov 12)
11. Describe the conflicts that may occur during shift reduce parsing. (May 12)
12. Construct stack implementation of shift reduce parsing for the grammar (May 16)
E→ E+E
E → E*E
E → (E)
E → id and the input string id1+id2*id3.
13. i. What is SLR(1) parser. Describe the steps for the SLR parser. (Nov 21)
ii. Give a rightmost derivation for (a, (a, a)) and show the handle of each right-sentential
form.
14. Consider the following grammar. (Nov 12) (May 23)
E→E+T | T
T→TF | F
F→F* | a | b
Construct the SLR parsing table for this grammar. Also parse the input a*b+a .
15. Generate SLR Parsing table for the following grammar.
S→Aa|bAc|Bc|bBa
A→d
B→d
And parse the sentences “bdc”and “dd”. (May 15)
16. Construct SLR parsing table for the following grammar (Nov 12)
S→ L=R|R
L→*R| id
R→L
17. Construct the SLR(1) parsing table for
E → E + T|T,
T →T * F|F,
F → (E) |id. (Nov 20, May 22)
18. Parse the string (a, a) using SLR parsing table.
S→ (L) | a
L→L , S | S
19. Describe the LR parsing algorithm with an example. (Nov 21)
20. Consider the grammar given below:
E → E+T
E→T
T → T*F
T→F
F → (E)
F → id
Construct an LR parsing side for the above grammar. Give the moves of LR parser on
id*id+id

St. Joseph’s College of Engineering Page 12 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
21. Consider the Context-Free Grammar(CFG) depicted below where “begin”,”end”and “x” are all
terminal symbols of the grammar and stat is considered the starting symbol for this grammar.
Productions are numbered in parenthesis and you can abbreviate “begin” to “b” and “end” to
“e” respectively.
Start→Block
Block→begin Block end
Block→Body
Body→x
i. Compute the set of LR(1) items for this grammar and draw the corresponding DFA. Do
not forget to augment the grammar with the initial production S->Start$ as the
production(0).
ii. Construct the corresponding LR parsing table. . (May 18)
22. Write the algorithm for construction of LR parsing table for a given grammar. Construct the
LR parsing table for the following grammar: (May 19)
E→E+T
E→T
E→T*F
T→F
F→(E)
F → id
23. Construct CLR parsing table to parse the sentence id=id*id for the following grammar.
S→ L=R | R L→*R | id R→L
24. Construct LALR parsing table for the grammar.
E→ E+T | T T→ T*F | F F→ (E) | id
25. Write the algorithm for construction of LALR parsing table for a given grammar. Using the
algorithm for construction of LALR parsing table construct the LALR parsing table for the following
grammar. (May 19)
S‟ → S
S → aAd | bBd | aBe | bAe
A→c
B→c
UNIT III INTERMEDIATE CODE GENERATION
Syntax Directed Definitions, Evaluation Orders for Syntax Directed Definitions, Intermediate
Languages: Syntax Tree, Three Address Code, Types and Declarations, Translation of Expressions,
Type Checking.
PART-A
1. Define syntax directed definition.
A syntax-directed definition (SDD) is a context-free grammar together with, attributes and
rules. Attributes are associated with grammar symbols and rules are associated with
productions. If X is a symbol and a is one of its attributes, then we write X.a to denote the
value of a at a particular parse-tree node labeled X.
2. What are Inherited and Synthesized attributes? (Nov 20)
An inherited attribute for a nonterminal B at a parse-tree node N is defined by a semantic
rule associated with the production at the parent of N. An inherited attribute at node N is
defined only in terms of attribute values at N's parent, N itself, and N's siblings.
A synthesized attribute for a nonterminal A at a parse-tree node N is defined by a semantic
rule associated with the production at N. A synthesized attribute at node N is defined only in
terms of attribute values at the children of N and at N itself.
3. What is S-attributed definition?
S-Attributed Grammars are a class of attribute grammars characterized by having
no inherited attributes, but only synthesized attributes.

St. Joseph’s College of Engineering Page 13 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
4. What is L-attributed definition?
L-attributed grammars are a special type of attribute grammars. They allow the attributes to
be evaluated in one left-to-right traversal of the abstract syntax tree. Each attribute must be
either:
✓ Synthesized, or
✓ Inherited, but with the rules limited as follows. Suppose that there is a production A
→ X1.. Xn,
a. Inherited attributes associated with the head A.
b. Either inherited or synthesized attributes associated with the occurrences of
symbols X1, X2,. . . , Xi-1 located to the left of Xi.
c. Inherited or synthesized attributes associated with this occurrence of X i itself,
but only in such a way that there are no cycles in a dependency graph formed
by the attributes of this Xi.
5. What is a translation scheme? (May 22)
A translation scheme is a context-free grammar in which semantic rules are embedded within
the right sides of the productions. So a translation scheme is like a syntax- directed
definition, except that the order of evaluation of the semantic rules is explicitly shown. The
position at which an action is to be executed.
6. What are the advantages of SDT?
Syntax directed translation is a scheme that indicates the order in which semantic rules are to
be evaluated. The main advantage of SDT is that it helps in deciding evaluation order. The
evaluation of semantic actions associated with SDT may generate code, save information in
symbol table, or may issue error messages.
7. What is meant by construction of syntax tree for expression?
A syntax-tree node representing an expression El + E2 has label + and two children representing
the subexpressions El and E2. The nodes of a syntax tree are implemented by objects with a
suitable number of fields. Each object will have an op field that is the label ofthe node. The
objects will have additional fields as follows:
✓ If the node is a leaf, an additional field holds the lexical value for the leaf. A constructor
function Leaf ( op, val) creates a leaf object.
✓ If the node is an interior node, there are as many additional fields as the node has children
in the syntax tree. A constructor function Node takes two or more arguments: Node(op, c1,
c2, . , ck) creates an object with first field op and k additional fields for the
k children c1, c2, . . . , ck
8. Constructed a decorated parse tree according to the syntax directed definition, for the
following input statement ( 4+7.5*3)/2. (May 15)

St. Joseph’s College of Engineering Page 14 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
9. What do you mean by DAG? (May 16)
Like the syntax tree for an expression, a DAG has leaves corresponding to atomic operands
and interior codes corresponding to operators. The difference is that a node N in a DAG has
more than one parent if N represents a common subexpression. A DAG not only represents
expressions more briefly, it gives the compiler important clues regarding the generation of
efficient code to evaluate the expressions.
10. Construct a syntax tree and DAG for k:=k+5.

11. Construct the DAG and identify the value numbers for the sub expressions of the
following expressions, assuming + associates from the left. (Nov 20)
i. a*b + (a*b)
ii. a*b*a*b

12. What are the different ways of implementing the three address code? (May 22)
The three address code can be implemented as:
✓ Quadruples
✓ Triples
✓ Indirect Triples
✓ Static Single-Assignment Form
13. Place the code x=*y ; a=&x in Triplets and indirect Triplets. (May 15)
Triple:
op arg1 arg2
0 * y
1 = x (0)
Indirect:
op arg1 arg2 instruction
0 * y 11 (0)
1 = x (0) 12 (1)

Triple:
op arg1 arg2
0 & x
1 = a (0)
Indirect:
op arg1 arg2 instruction
0 & x 11 (0)
1 = a (0) 12 (1)
St. Joseph’s College of Engineering Page 15 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
14. Write a 3-address code for; x=*y ; a=&x. (May 15) (May 23)
t1:=*y
x:=t1
t1:=&x
a:=t1
15. Define type systems.
Type system of a language is a collection of rules depicting the type expression assignments
to program objects. An implementation of a type system is called a type checker.
16. What is type checking? (May 23)
Type checking uses logical rules to reason about the behavior of a program at run time.
Specifically, it ensures that the types of the operands match the type expected by an operator.
For example, the && operator in Java expects its two operands to be booleans; the result is
also of type boolean.
17. State the type expressions. (Nov 21)
Types have structure, which we shall represent using type expressions: a type expression is either
a basic type or is formed by applying an operator called a type constructor to a type expression.
The sets of basic types and constructors depend on the language to be checked
18. What are static and dynamic errors?
✓ Static error: It can be detected at compile time. Eg: Undeclared identifiers.
✓ Dynamic errors: It can be detected at run time. Eg: Type checking
19. What are the advantages of compile time checking?
✓ It can catch many common errors.
✓ Static checking is desired when speed is important, since it can result faster code that
does not perform any type checking during execution.
20. What are the advantages of the dynamic checking?
✓ It usually permits the programmer to be less concerned with types. Thus, if frees the
programmer.
✓ It may be required in some cases like array bounds check, which can be performed
only during execution.
✓ It can give in clearer code.
✓ It may rise to in more robust code by ensuring thorough checking of values for the
program identifiers during execution.
21. Give an account on Static vs. Dynamic Type Checking.
✓ Static: Done at compile time (e.g., Java)
✓ Dynamic: Done at run time (e.g., Scheme)
✓ Strong type system is one where any program that passes the static type checker
cannot contain run-time type errors. Such languages are said to be strongly typed.
22. Express the rule for checking the type of a function. (Nov 21)
Type checking can take on two forms: synthesis and inference. A typical rule for type
synthesis
has the form
if f has type s → t and x has type s,
then expression f (x) has type t
Here, f and x denote expressions, and s -+ t denotes a function from s to t. This rule for
functions with one argument carries over to functions with several arguments.
A typical rule for type inference has the form
if f (x) is an expression,
then for some  and , f has type  →  and x has type 
PART –B
1. Explain the concept of syntax directed definition. Explain Synthesized attribute and Inherited
attribute with suitable examples. (May 23)

St. Joseph’s College of Engineering Page 16 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
2. Give a Syntax directed Definitions to differentiate expressions formed by applying the
arithmetic operators + and * to the variable X and constants 3 expression: X*(3*X+X*X) ( May
15)
3. (i) Given the Syntax-Directed Definition below construct the annotated parse tree for the input
expression: “int a, b, c”. (May 23)
D → T L L.inh = T.type T
→ intT.type = integer T
→ float T.type = float
L → L1, id L1.inh = L.inhaddType(id.entry,L.inh)
L → id addType(id.entry,L.inh)
(ii) Given the Syntax-Directed Definition below with the synthesized attribute val, draw the
annotated parse tree for the expression (3+4) * (5+6). (May 23)
L → E L.val = E.val
E → T E.val = T.val
E → E1 + T E.val = E1.val + T.val
T → F T.val = F.val
T → T1 * F T.val = T1.val * F.val
F → ( E ) F.val = E.val
F → digit F.val = digit.lexval
4. Construct a syntax directed definition for constructing a syntax tree for assignment statements
(May 2016)
S → id: = E
E → E1+E2
E → E1*E2
E → - E1
E → ( E1)
E → id
5. Describe how SDD can be evaluated at the nodes of a parse tree using dependency graphs.
(Nov 20, May 22)
6. Explain about the Implementing of L-Attributed SDD's
7. Describe syntax-directed translation schemes with appropriate examples.
Explain how type conversion is performed with suitable examples. (May 19)
8. Elucidate the variants of Syntax tree with suitable examples. (Nov 21)
9. Construct parse tree, syntax tree and annotated parse tree for the input string is 5*6+7.
10. For the given program fragment A[i,j]=B[i,k] do the following:
i. Draw the annotated parse tree with the translation scheme to convert to three address
code
ii. Write the 3-address code
iii. Determine the address of A[3,5] where , all are integer arrays with size of A as 10*10
and B as 10*10 with k=2 and the start index position of all arrays is at 1.(assume the
base addresses) (May 15)
11. Explain type checking and type conversion. Explain with an example of converting the
operands the same type. (Nov 20, May 22)
12. Specify a type checker which can handle expressions, statements and functions.
13. For the following production rule give the SDT for translation of expression to three address code
and show for an example. (Apr 23) (May 23)
x= (a+b)*(c+d)
St. Joseph’s College of Engineering Page 17 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
S → id: = E
E → E1+E2
E → E1*E2
E → - E1
E → ( E1)
E → id

14. i. Write an algorithm for unification with its operation.


ii. Discuss in detail about Translation of array reference. (Nov 21)

St. Joseph’s College of Engineering Page 18 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
UNIT IV RUN-TIME ENVIRONMENT AND CODE GENERATION
Storage Organization, Stack Allocation Space, Access to Non-local Data on the Stack, Heap
Management - Basic blocks and flow graphs - Issues in Code Generation - Design of a simple Code
Generator.
1. Differentiate between static and dynamic storage allocation. (Nov 20)
Static Storage Allocation: A storage-allocation decision is static, if it can be made by the compiler
looking only at the text of the program, not at what the program does when it executes.
Dynamic Storage Allocation: A decision is dynamic if it can be decided only while the
program is running.

2. List the different storage allocation strategies. (Nov 21)


Enlist the storage allocation strategies used in runtime environment. (May 22)
✓ Static Storage Allocation
✓ Dynamic Storage Allocation
Many compilers use some combination of the following two strategies for dynamic storage
allocation:
✓ Stack storage. Names local to a procedure are allocated space on a stack. The stack
supports the normal call/return policy for procedures.
✓ Heap storage. Data that may outlive the call to the procedure that created it is
usually allocated on a "heap" of reusable storage.
3. What are the limitations of static allocation? (Nov 12)
✓ The size of a data object and constraints on its position in memory must be known at
compile time.
✓ Recursive procedure is restricted.
✓ Data structures cannot be created dynamically.
4. What is the use of run time storage?
The run time storage might be subdivided to hold:
✓ The generated target code
✓ Data objects, and
✓ A counterpart of the control stack to keep track of procedure activation.
5. Draw the activation tree for the following code. (May 18)
printf(“Enter Your Name: “);
scanf(“%s”, username);
show_data(username);
printf(“Press any key to continue…”);
...
int show_data(char *user)
{
printf(“Your name is %s”, username);
return 0;}

St. Joseph’s College of Engineering Page 19 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
6. Define activation trees.
✓ Each node represents an activation of a procedure,
✓ The root represents the activation of the main program
✓ The node for a is the parent of the node for b if and only if control flows
from activation a to b,
✓ The node for a is to the left of the node for b if and only if the lifetime of a occurs
before the lifetime of b.
7. Write notes on control stack?
Procedure calls and returns are usually managed by a run-time stack called the control stack. Each
live activation has an activation record (sometimes called a frame) on the control stack, with the
root of the activation tree at the bottom, and the entire sequence of activation recordson the
stack corresponding to the path in the activation tree to the activation where control
currently resides..
8. Mention the fields in activation record. (Nov 14, 21)(May 23)
What is an activation record? (or) What is frame?
Information needed by a single execution of a procedure is managed using a contiguous block
of storage called an activation record or frame, consisting of the collection of fields such
as
✓ Actual parameters
✓ Returned Values
✓ Control link
✓ Access link
✓ Saved machine status
✓ Local data
✓ Temporaries
9. Write the scope of a declaration?
A portion of the program to which a declaration applies is called the scope of that
declaration. An occurrence of a name in a procedure is said to be local to the procedure
if it is in the scope of a declaration within the procedure; otherwise, the
occurrence is said to be nonlocal.
10. Define binding of names.
When an environment associates storage location s with a name x, we say that x is
bound to s; the association itself is referred to as a binding of x. A binding is the
dynamic counterpart of a declaring.
11. What is stack allocation?
Stack allocation is based on the idea of a control stack; storage is organized as a
stack, and activation records are pushed and popped as activations begin and end
respectively.
12. What are the various ways to pass a parameter in a function?
✓ Call-by-value
✓ Call-by-reference
✓ Call-by-value-result (copy-restore) :this method is a hybrid between call by value and
call by references.
✓ Call-by-name
13. What is dangling references? (May 2016)
Whenever storage can be de-allocated, the problem of dangling references arises. A
dangling reference occurs when there is a reference to storage that has been de
allocated.
14. Define address descriptor. (May 22)
An address descriptor is used to store the location where current value of the name can be
found at run time.

St. Joseph’s College of Engineering Page 20 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
15. What are the various ways of passing a parameter to a function? (May 19)
There are different ways in which parameter data can be passed into and out
of methods and functions. It is beyond the scope of these notes to describe all such schemes,
so we will consider only the two most common methods used in C++ and Java:
"pass by value" and "pass by reference".
16. What are access links?
A direct implementation of the normal static scope rule for nested functions is obtained by
adding a pointer called the access link to each activation record. If procedure p is nested
immediately within procedure q in the source code, then the access link in any activation of p
points to the most recent activation of q.
17. What are the basic functions performed by the memory manager?
The memory manager keeps track of all the free space in heap storage at all times. It performs
two basic functions:
✓ Allocation: When a program requests memory for a variable or object, the memory
manager produces a chunk of contiguous heap memory of the requested size.
✓ Deallocation: The memory manager returns deallocated space to the pool of free space,
so it can reuse the space to satisfy other allocation requests.
18. Write any four issues that should be clear before writing a code generator. (May 22)
✓ Input to the code generator ✓ Register allocation
✓ The target program ✓ Evaluation order
✓ Instruction selection
19. State the tasks of a code generator. (Nov 20)
✓ Instruction selection: which instructions to use.
✓ Instruction scheduling: in which order to put those instructions. Scheduling is a speed
optimization that can have a critical effect on pipelined machines.
✓ Register allocation: the allocation of variables to processor registers.
✓ Debug data generation: if required so the code can be debugged.
20. Define code generation. (or) What role does the target machine play on the code generation
phase of the compiler? (May 15).
The code generation is the final phase of the compiler. It takes an intermediate
representation of the source program as the input and produces an equivalent target
program as the output.
21. Write the object code sequence for t:=a+b produced by a typical code generator.(May 19,23)
MOV R0,a
MOV R1,b
ADD R0,R0,R1
MOV t,R0
PART –B
1. What are different storage allocation strategies? Explain. (May 13, 19)
Explain about static and stack allocation in storage allocation strategies.
2. Explain the organization of runtime storage in detail.
3. Explain any four issues in storage allocation. (May 15)
4. Describe about the content of activation record.
5. Discuss in detail about stack allocation space of memory and the usage of stack in the
memory allocation. (Nov 21) (May 23)
6. Briefly explain about Access to Nonlocal Data on the Stack.
7. What is the Memory Hierarchy configuration of a computer? Discuss the memory manager
subsystem that is responsible for allocating and deallocating space within the heap. (Nov 20,
May 22)
St. Joseph’s College of Engineering Page 21 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
8. i. Explain about various ways to pass a parameter in a function with example.
ii. Construct a Syntax-Directed Translation scheme that translates arithmetic expressions
from infix into postfix notation. Use semantic attributes for each of the grammar symbols
and semantic rules. Evaluate the input: 3 * 4 + 5 * 2. (May 18)
9. Elaborate the various issues in code generation with examples. (Nov 21)
10. Illustrate the algorithm that generates code for a single basic block with three address
instructions. (Nov 20, May 19, 22,23)
UNIT V CODE OPTIMIZATION
Principal Sources of Optimization – Peep-hole optimization - DAG- Optimization of Basic
Blocks-Global Data Flow Analysis - Efficient Data Flow Algorithm.
PART-A
1. Why is compiler optimization essential? (May 22)
Compilers that apply code-improving transformations are called optimizing compilers.
Comipler optimization is essential to make the code produced by compiling algorithms run
faster or takes less space.
2. What are the properties of optimizing compiler? (May 16)
An optimizing compiler should provide the following properties:
✓ The transformations should preserve the semantics of the programs, that is the changes
should guarantee that the same input produces the same outputs (and shouldnot cause
errors);
✓ The transformations should speed up considerably the compiler on the average
(although occasionally on some inputs this may not be demonstrated, on most of the inputs
it should become faster);
✓ The transformation should be worth the intellectual effort
3. What is meant by optimization?
It is a program transformation that made the code produced by compiling algorithms run
faster or takes less space.
4. What are the principle sources of optimization?
The principle sources of optimization are optimization consists of detecting patterns in the
program and replacing these patterns by equivalent but more efficient constructs. The richest
source of optimization is the efficient utilization of the registers and instruction set of a
machine.
5. Mention some of the major optimization techniques.
✓ Local optimization ✓ Global optimization
✓ Loop optimization ✓ Function preserving transformations
✓ Data flow analysis ✓ Algorithm optimization.
6. What is meant by loop optimization?
The running time of a program may be improved if we decrease the number of instructions
in an inner loop even if we increase the amount of code outside that loop.
7. Mention various techniques used for loop optimization.(May 18)
✓ Induction variable. ✓ Loop interchange.
✓ Strength reduction. ✓ Loop-invariant code motion.
✓ Loop fusion. ✓ Loop nest optimization
✓ Loop inversion. ✓ .Loop unrolling.

St. Joseph’s College of Engineering Page 22 of 27


CS1601 – Compiler Design Department of CSE 2023-2024
8. Define loop unrolling with example. (Nov 13)
Loop overhead can be reduced by reducing the number of iterations and replicating the
body of the loop.
Example:
In the code fragment below, the body of the loop can be replicated once and the number of
iterations can be reduced from 100 to 50.
for (i = 0; i < 100; i++)
g ();
Below is the code fragment after loop unrolling.
for (i = 0; i < 100; i += 2)
{
g (); g (); }
9. Define code motion and loop-variant computation.
Code motion: It is the process of taking a computation that yields the same result independent
of the number of times through the loops and placing it before the loop. Loop – variant
computation: It is eliminating all the induction variables, except one when there are
two or more induction variables available in a loop
10. What is meant by constant folding? (May 13) (May 23)
Constant folding is the process of replacing expressions by their value if the value can be
computed at complex time.
11. What is meant by copy propagation or variable propagation?
One concerns assignments of the form f:=g called copy statements or copies for short, it
means the use of variable V1 in place of V2
1.V1:=V2
2.f:V1+f
3.g:=v2+f-6
In statement 2, V2 can be used in place of V1 by copy propagations. So,
1.V1=V2;
….
4.f:=V2+f
5.g:V2+f-6
This leads for common sub expression elimination further (V2+f is a common sub
expression).
12. What is the step takes place in peephole optimization?
It improves the performance of the target program by examining a short sequence of target
instructions. It is called peephole. Replace these instructions by a shorter or faster sequence
whenever possible. It is very useful for intermediate representation.
13. Point out the characteristics of peephole optimization. (Nov 21) (May 23)
✓ Redundant instruction elimination. ✓ Algebraic simplifications
✓ Flow of control optimization ✓ Use of machine idioms
14. Brief about the methodology used to locally improve the target code. (Nov 20)
The methodology used to locally improve the target code is Peephole optimization. This
technique tries to improve the performance of the target code by examining a short sequence
of target instructions known as peephole, and replacing them by a shorter and faster
sequence whenever possible.
15. List any two structure preserving transformations adopted by the optimizer?
✓ Basic blocks
✓ Flow graphs.
St. Joseph’s College of Engineering Page 23 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
16. Define basic block. Give an example. (Nov 13, Nov 20)
A basic block contains sequence of consecutive statements, which may be entered only at the
beginning and when it is entered it is executed in sequence without halt or possibility of
branch.
Three Address Code for the expression a = b + c + d is T1
= b + c, T2 = T1 + d
a = T2
17. What are the structure preserving transformations on basic blocks? ((Nov 21)
✓ Common subexpression elimination
✓ Copy propagation
✓ Dead code elimination
✓ Constant folding.
18. What are the rules to find “ leader” in basic block?
✓ It is the first statement in a basic block is a leader.
✓ Any statement which is the target of a conditional or unconditional goto is a leader.
✓ Any statement which immediately follows a conditional goto is a leader.
19. List the advantages of DAGs (Nov 12)
✓ It automatically detects common sub expression.
✓ We can determine which identifiers have their values used in the block.
✓ We can determine which statements compute values, and which could be used outside
the block. It reconstruct a simplified list of quadruples taking advantage of common sub
expressions and not performs assignments of the form a=b unless
necessary.
20. Define flow graph. (Nov 13)
Relationships between basic blocks are represented by a directed graph called flow graph.
21. What is data flow analysis? (Nov 12)
The data flow analysis is the transmission of useful relationships from all parts of the
program to the places where the information can be of use.
22. What do you mean by data flow equations?
A typical equation has the form out[s] = gen[s] U (in[s]-kill[s])
It can be read as information at the end of a statement is either generated within the
statement or enters at the beginning and is not killed as control flows through the
statement.
23. What are dominators?
A node of flow graph is said to be a dominator, i.e one node dominates the other node if
every path from the initial node of the flow graph to that node goes through the first node.(d
Dom n).when d-node dominates n-node.
24. What is meant by U-D chaining?
It is Use-Definition chaining. It is the process of gathering information about how global
data flow analysis can be used id called as use-definition (UD) chaining.
25. How would you represent the dummy blocks with no statements indicated in global data
flow analysis? (May 2013)
Dummy blocks with no statements are used as technical convenience (indicated as open
circles).
26. Define live variable. (Nov 12)
A variable is live at a point in a program if its value can be used subsequently.
St. Joseph’s College of Engineering Page 24 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
27. How liveness variable calculated. (May 15)
A variable is live if it holds a value that will/might be used in the future. The representation
of the program that we use for liveness analysis (determining which variables are live at each point
in a program), is a control flow graph. The nodes in a control flow graph are basic statements
(instructions). There is an edge from statement x to statement y if x can be
immediately followed by y (control flows from x to y).
PART –B
1. Explain principle sources of optimization with examples. (May 16)
2. What is code optimization? State its advantages. Discuss various code optimization schemes
in detail. (Nov 20, May 22)
3. Discuss about the following: (Nov 20, May 22,23)
i. Copy Propagation
ii. Dead-code Elimination
iii. Code motion.
4. i. Explain Loop optimization in details and apply it to the code
i:=0; a:=n-3; Label loop
if i<a then b:=i_4;
loop e:=p+b;
else d:-m[c];
end; e:=d-2;
f:=I-4;
g:=p+f;
m[g]:=e;
i:=i+1
a:=n-3
ii. What are the optimization technique applied on procedure calls? Explain with example.
5. Optimize the following code using various optimization techniques:
i=1,s=0;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
c[i][j]=c[i][j]+a[i][j]+b[i][j];
6. Explain peephole optimization.
7. Write detailed notes on Basic blocks and flow graphs.
8. i. Explain loops in flow graphs.
ii. Explain Local optimization.
9. i. Explain optimization of basic blocks.
ii. Explain redundant common subexpression elimination.
10. Perform analysis of available expressions on the following code by converting into basic
blocks and compute global common sub expression elimination. (May 15)
I. i:=0; a:=n-3; if i<a then loop else end;
II. Label loop;
III. b:=i_4; e:=p+b;
IV. d:-m[c]; e:=d-2; f:=I-4; g:=p+f; m[g]:=e; i:=i+1; a:=n-3;
V. if i<a then loop
VI. else end
VII. Label end
11. Define a Directed Acyclic Graph. Construct a DAG and write the sequence of instructions for
the expression a+a*(b-c)+(b-c)*d. (May 14)
St. Joseph’s College of Engineering Page 25 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
12. What are the advantages of DAG representation? Give example. (May 15)
13. i. Write the procedure to perform Register Allocation and Assignment with Graph Coloring.
(May 15)
ii. Construct DAG and optimal target code for the expression X=((a+b)/(b-c))-(a+b)*(b-c)+f.
14. Generate DAG representation of the following code and list out the applications of DAG
representation
i=1,
while(i<=10) do
sum+=a[i]
15. i. Construct the DAG for the following basic block
d:=b*c
e:=a+b
b:=b*c
a:=e-d
ii. How to trace data-flow analysis of structured program?
16. i. Determine the basic blocks of instructions, control Flow graph(CFG) and the CFG
dominator tree for the following the code.
ii. Construct a code sequence and DAG for the following syntax directed translation of the
expression: (a+b)-(e-(c+d)).(May 18)
17. Write and explain the algorithm for construction of basic blocks. (May 19)
Construct the DAG for the following basic block.
x = a[i]
a[j] =y
z=a[i]
18. Write about data flow analysis of structural programs.
19. i. Formulate the steps for efficient Data Flow algorithm. (Nov 21) (May 23)
ii. Describe the representation of array using DAG with example.
20. Summarize in detail about global dataflow analysis with example. (Nov 21)
PART C
1. In SQL , keywords and identifiers are case-insensitive. Write a Lex program that recognizes
the keywords SELECT, FROM, and WHERE (in any combination of capital and lower-case
letters), and token ID, which may be any sequence of letters and digits, beginning with a
letter. (May 19)
2. Draw the symbol tables for each of the procedures in the following PASCAL code (including
main) and show their nesting relationship by linking them via a pointer reference in the structure
( or record)used to implement them in memory. Include the entries or fields for the local variables,
arguments and any other information you find relevant for the purpose of code generation,
such as its type and locations at run-time. (May 18)
01: procedure main 08: integer y,z; 15: call f3(c,z)
02: integer a,b,c 09: procedure f3(m,n) 16: call f4(c,z)
03: procedure f1(a,b); 10: integer m,n; 17: end;
04: integer a,b; 11: end; 18: ….
05: call f2(b,a); 12: procedure f4(m,n); 19: call f1(a, b);
06: end; 13: integer m,n; 20: end;
07: procedure f2(y,z) 14: end;
3. Find transition diagrams for the following regular expression and regular definition (Nov 21)
✓ a(a|b)*a
✓ ((|a)b*)*
✓ All strings of digits with at most one repeated digit.
✓ All strings of a‟s and b‟s that do not contain the substring abb
✓ All strings of a‟s and b‟s that do not contain the subsequence abb
St. Joseph’s College of Engineering Page 26 of 27
CS1601 – Compiler Design Department of CSE 2023-2024
4. Consider the following CFG (Nov 20, May 22)
E→E or T|T
T→T and F|F
F→not F|(E)|true |false
Write the semantic rules and explain the processes converting “not (true or false)” to
intermediate form using Parser tree method.
5. Consider the grammar S →ABD, A → a | Db| ε, B → gD |dA| ε , D → e | f (Nov 20, May
22)
i. Construct FIRST and FOLLOW for each nonterminal of the above grammar.
ii. Construct the predictive parsing table for the grammar.
iii. Show the parsing action on a valid string and on an invalid string
iv. Check whether the grammar is LL(1). Give justification.
6. Consider the following grammar (May 18)
E→E+E E→E*E E→( E ) E→id
i. Find the SLR parsing table for the given grammar.
ii. Parse the sentence: (a+b)*c.
7. Apply code generation algorithm to generate a code sequence for the three address statement
for the following assignment statement. D := (a – b) * (a – c) + (a – c) (Nov 21)
8. A simple matrix-multiplication program is given below : (May 19)
For (i=0; i<n; i++)
For(j=0; j<n; j++)
C[i][j]= 0.0;
For(i=0; i<n;i++)
For(j=0;j<n;j++)
For(k=0;k<n;k++)
C[i][j]=c[i][j]+a[i][k]*b[k][j];
i. Translate the program into three address statements. Assume the matrix entries are
numbers that require 8 bytes, and that matrices are stored in row –major order. (7)
ii. Construct the flow graph for the code from 1. (6)
iii. Identify the loops in the flow graph from 2. (2)

St. Joseph’s College of Engineering Page 27 of 27

You might also like