0% found this document useful (0 votes)
99 views56 pages

CD Question Bank

1. This document provides information about compiler design from Mahendra College of Engineering. It includes definitions of terms related to compilers such as compilers, analysis and synthesis in compilation, symbol tables, and phases of a compiler. 2. The document lists rules for defining expressions and statements. It also defines terms like tokens, patterns, lexemes, regular expressions, and transition diagrams. 3. The final sections compare syntax trees to parse trees and mention issues in lexical analysis like designing for simplicity and separating it from syntax analysis.

Uploaded by

sambavam channel
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)
99 views56 pages

CD Question Bank

1. This document provides information about compiler design from Mahendra College of Engineering. It includes definitions of terms related to compilers such as compilers, analysis and synthesis in compilation, symbol tables, and phases of a compiler. 2. The document lists rules for defining expressions and statements. It also defines terms like tokens, patterns, lexemes, regular expressions, and transition diagrams. 3. The final sections compare syntax trees to parse trees and mention issues in lexical analysis like designing for simplicity and separating it from syntax analysis.

Uploaded by

sambavam channel
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/ 56

MAHENDRA COLLEGE OF ENGINEERING

Salem-Campus, Attur Main Road, Minnampalli, Salem -636 106.

CS8602 – COMPILER DESIGN


UNIT – I
PART A
1. What is a compiler? (Remember)
A 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. The compiler reports to its user the presence
of errors in the source program.

2. What are the two main parts of a compilation? Explain briefly.


(May/June‟ 16, Apr/May ‟17 , Apr/May ‟18 , Nov/Dec „18 )
(Remember)
Analysis and Synthesis are the two parts of compilation.
The analysis part breaks up the source program into constituent
pieces and creates an intermediate representation of the source program.
The synthesis part constructs the desired target program from the
intermediate representation.

3. List some software tools. (Remember)


1) Structure editors.
2) Pretty printers.
3) Static checkers.
4) Interpreters

4. What are the three phases in analysis? (Remember)


Analysis consists of three phases:
1) Linear Analysis.
2) Hierarchical Analysis.
3) Semantic Analysis

5. What are rules for definition of expressions? (Remember)


The rules for definition of expressions are
1) Any identifier is an expression.
2) Any number is an expression.
3) If expression 1 and experssion 2 are expressions, then
Expression1+ expression2
expression1* expression2
(expression1)
Rules (1) and (2) are basis rules, while (3) defines expressions in terms of
operators applied to other expressions.
6. What are rules for definition of statements? (Remember)
The rules for definition of statements
1) If identifier1 is an identifier and expression2 is an expression, then
identifier1:= expression2 is a statement.
1
2) If expression1 is an expression and statement2 is a statement then
while (expression) do statement2
If (expression1) then statement2 are statements
7. What is a symbol table? (Nov/Dec „16, Apr/May „17) (Remember)
A symbol table is a data structure containing a record for each
identifier, with fields for the attributes of the identifier. The data
structure allows us to find the record for each identifier quickly and tostore or retrieve
data from that record quickly.
8. List the attributes stored in symbol table (Apr/May „19) (Remember)
 Variable names and constants
 Procedure and function names
 Literal constants and strings
 Compiler generated temporaries
 Labels in source languages
9. What are the cousins of the compiler? (May/June ‟12, Apr/May „17)
(Apr/May „22 )(Remember)
Preprocessors, Assemblers, Loaders and Link editors

10. What are the phases involved in the front end and the back end of
the compiler? (Remember)
Front end:
Lexical analyzer
Syntax analyzer
Semantic analyzer
Intermediate code generator
Back end:
Intermediate code generator
Code optimizer
Code generator

11. List some compiler construction tools? (Nov/Dec „16) (Apr/May


„22 ) (Remember)
Parser generators
Scanner generators
Syntax directed translation engines
Automatic code generators
Data flow engines

12. What are the other names for the compiler construction tools?
(Remember)
Compiler - compilers, compiler – generators or translator - writing
systems.

13. What are the issues in the compiler construction tools?


(Remember)
 Simpler design
 Compiler efficiency is improved
 Compiler portability is enhanced

14. What do you mean by Cross-Compiler? (Nov/Dec 2017)


2
(Remember)
Cross Compilers are compilers that run on one machine and run code
for another machine.
15. What is an interpreter? (April/May‟11)( Nov/Dec 2017)
(Remember)
An interpreter is another common kind of language processor. Instead
of producing a target program as a translation, an interpreter appears to
directly execute the operations specified in the source program on inputs
supplied by the user.
16. Illustrate diagrammatically how a language is processed
(May/June 2016) (Nov/Dec 2018) (Understand)

17. How will you group the phases of compiler? (Nov/Dec2013)


(Remember)
Analysis Phase and Synthesis Phase

18. State any two reasons as to why phases of compiler should be


grouped. (May/June14) (Analyze)
1. Can use the same front end of the compiler and produce different back
ends for different machines.
2. Can make several different languages into same intermediate language
and use a common back end for different front ends

19. What is token, pattern and lexeme? (April/May ‟11 & May/June
2013) (Nov/Dec 2016) (Apr/May ‟19)(Remember)
Token is a classification of lexical units.
Ex: id, num
Patterns are rules describing the set of lexemes belonging to a token.
Ex: letter followed by letters and digits.
Lexemes are the specific character strings that make up a token.
Ex: abc and 123
20. List the various error recovery strategies for a lexical analysis.
(May/June ‟12 & May/June2015, Apr/May „18) (Apr/May „22 )
(Remember)
Possible error recovery actions are:
· Panic mode recovery
· Deleting an extraneous character
3
· Inserting a missing character

4
· Replacing an incorrect character by a correct character
· Transposing two adjacent characters
21. What is sentinel? (Remember)
A sentinel is a special character that cannot be part of source
program.

22. What are prefix, suffix and substring? (Remember)


Prefix: A string obtained by removing zero or more trailing symbols of
strings. Ex: Ban is a prefix of Banana.
Suffix: A string obtained by removing zero or more leading symbols of
strings. Ex: nana is a suffix of Banana.
Substring: A string obtained by deleting a prefix and suffix from
strings.
Ex: nan is a substring of Banana
23. What are proper prefix, proper suffix and proper substring?
(Remember)
Any non-empty string s that is respectively a prefix, suffix or substring
s such that s ≠x.
24. What is a subsequence of a string? (Remember)
Any string formed by deleting zero or more necessarily contiguous
symbols from s. Ex: baaa is a subsequence of banana.

25. What is the role of the parser?(May/June 2015) (Remember)


The parser is used to construct the parse tree according to the given
grammar.

26. What is regular expression? (Remember)


Regular expression allows us to define precisely the sets. With this
notation we can define Pascal identifiers as
letter (letter/digit)*
27. List some of the rules to define regular expression over alphabet ∑
?
Apply the rules to define a regular expression. Give example.
(Apr/May‟18) (Remember)
€ is a regular expression that denotes (€) that is the set containing empty
string
If a is a symbol in ∑ then a is a regular expression that denotes { a }, that is
the set containing the string a.
Suppose r and s are regular expressions denoting the language L{r} and L{s}
then
a. {r}| {s} is a regular expression denoting L{r} U L{s}
b. {r}{s} is a regular expression denoting L{r} L{s}
Example: Regular Expression for identifier
letter (letter|digit)*
28. What is a regular set? (Remember)
A language denoted by a regular expression is said to be a regular set.

5
29. What are character classes? (Remember)
The notation [abc] where a, b and c are alphabet symbols denoting the
regular expression a | b | c. The abbreviated character class such as [a –
z] denotes the regular expression a | b |….| z.

30. What is a non-regular set? Give example. (Remember)


Some languages cannot be described by any regular expression.
Regular expression cannot be used to describe balanced or nested
constructs.
Ex: { wcw| w is a string of a‘s and b‘s }

31. Draw the transition for identifier and digits.(Nov/Dec‟11) (Create)


TRANSITION DIAGRAM FOR IDENTIFIERS

TRANSITION DIAGRAMS FOR DIGITS

32. What are the various parts in lex program? (Remember)


A Lex program consists of three parts:
declarations
%%
translation rules
%%
auxiliary procedures

33. Write a regular expression for an identifier and number.(Nov/Dec


‟12, Apr/May „17) (Apr/May ‟19) (Remember)
The regular expression for an identifier and numbers are given as
letter (letter | digit)*
digit + ( .digit +) ? ( E( + | - )? digit + ) ?

6
34. Compare syntax tree and parse tree.(Nov/Dec ‟12, Nov/Dec „17)
(Analyse)
SYNTAX TREE PARSE TREE
1 interior nodes are ―operators‖, interior nodes are non-
leaves are terminals, leaves are terminals
operands
2 when representing a program in rarely constructed as a data
a tree structure usually use a structure
syntax tree
3 Represents the abstract syntax Represents the concrete syntax
of a program (the semantics) of a program
35. Mention the issues in Lexical Analyzer (May/June 2013)
(Remember)
1. Simpler design is perhaps the most important consideration. The
separation of lexical analysis from syntax analysis often allows us
to simplify one or the other of these phases. For example, a parser
embodying the conventions for comments and white space is
significantly more complex than one that can assume comments
and white space have already been removed by a lexical analyzer.
2. Compiler efficiency is improved. A separate lexical analyzer
allows us to construct a specialized and potentially more efficient
processor for the task. It can significantly speed up the
performance of a compiler.
3. Compiler portability is enhanced. Input alphabet peculiarities
and other device-specific anomalies can be restricted to the lexical
analyzer.
36. Write the regular expression for white space and identifier
(Nov/Dec2013) (Remember)
Identifier: letter (letter/digit)*
White Space : delim -> blank | tab | newline
ws -> delim

37. Write the regular definition to represent date in the following


format : JAN 5th 2014 (May/June2015) (Remember)
Date -> MON DATE YEAR
MON->JAN
DATE->5th
YEAR->2014

38. List the operations on languages. (May/June 2016) (Remember)


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)

39. Define Lexeme. (May/June14) (Nov/Dec 2017) (Remember)


Lexemes are the specific character strings that make up a token. Ex:
abc and 123.

7
40. Write a grammar for branching statement? (May/June 2016)
(Remember)
stmt  if expr then stmt
| if expr then stmt else stmt
| other
41. List the rule that form the BASIS (Nov/Dec 2016) (Remember)
The rules that form the BASIS are
Any identifier is an expression
Any number is an expression

42. Draw a syntax tree for the expression a:=b*-c + b*-c (Nov/Dec ‟12,
Nov/Dec ‟11, Nov/Dec ‟17) (Create)

assign

a +

* *
uminus b uminus
b
b b
Syntax Tree

43. Construct a parse tree for –(id+id) ( Nov/Dec ‟16) (Create)

Uminus
+

id id

44. Compare the features of DFA and NFA. (May/June14, Nov/Dec


„17) (Analyse)

DFA NFA
DFA only has one state transition NFA is allowed to have more than
for every symbol of the alphabet one state transition for an input
symbol of the alphabet
DFA cannot use empty string NFA can use empty string
transition transition

8
PART B
1. Explain the various phases of a compiler and trace the program segment
a: =b+c* a-4. (April /May ‟11,Nov/Dec ‟11, May/June‟12, Nov/Dec
‟16, Nov/Dec ‟17) (Understand)
2. Explain the various phases of a compiler in detail .Write the output for
each phases of the compiler for the expression c:=a + b * 12.(Apr/May
‟19) (Understand)
3. Explain briefly about compiler construction tools and Write note on each
compiler construction tool. Outline the construction tools can be used to
implement various phases of a compiler. (May/June 2015, Nov/Dec
‟16,Nov/Dec‟17,Apr/May‟18,Nov/Dec‟18, Apr/May ‟19) (Understand)
4. Explain in detail the various phases of compilers with an example
(Nov/Dec ‟17) (Understand)
5. Describe the following software tools (Understand)
i. Structure Editors, ii. Pretty printers iii. Interpreters
6. Elaborate on grouping of phases in a compiler. (Nov/Dec ‟16) (create)
7. What are the phases of the compiler? Explain the phases in detail. Write
down the output of each phase for the expression a: = b + c *50
(Nov/Dec‟12 & May/June„13) (Remember)
8. Describe the various phases of compiler and trace it with the program
segment (position= initial + rate * 60) (Apr/May „22 ). (May/June 2016
.2018) (Understand)
9. Explain the need for grouping of phases. (May /June 2016)
(Understand)
10. Explain various Error encountered in different phases of compiler
(May /June 2016, Nov/Dec ‟16) (Understand)
11. Draw a diagram for the compilation of a machine language processing
system. (Apr/May ‟18) (create)
12. Write note on front and back end of compiler (Remember)
13. Explain Symbol table management and error handling (Understand)
14. Compare NFA and DFA. Construct a DFA directly from an augmented
regular expression ((ε| a)b*)*. (Nov/Dec‟12) (Analyse)
15. a.)What are the issues in Lexical analysis? (May/June„12) (Remember)
b.)Elaborate in detail recognition of tokens (Create)
16. Construct the minimized DFA for a Regular expression (0+1)* (0+1)10
(Nov/Dec ‟11) (Apr/May „22 ) (Apply)
17. Draw the DFA for the augmented regular expression (a|b)*# directly
using syntax tree. (Nov/Dec 2013) (Create)
18. Define the following terms : (May/June 2014)
i. Compiler, Interpreter, Translator and differentiate
between them. (Remember)
ii. Differentiate between lexeme, token and
pattern.(Apr/May‟17) (Analyse)
iii. What are the issues in lexical analysis? .(Apr/May‟17)
Remember)
iv. Elaborate specification of tokens (May/June‟13, Nov/Dec
‟18) (create)

9
19. Write notes on regular expression to NFA. Construct Regular
expression to NFA for the sentence (a/ b)* a and construct DFA to the
above expression (May/June 2016). (Remember) (Create)
20. Discuss how finite automata used to represent tokens and perform LA
with example (Nov/Dec 2016) (Create)
21. a) Conversion of a regular expression (a/b) * abb to NFA (Nov/Dec
2016) (Create)
b) Write an algorithm for minimizing the number of states of a DFA
(Nov/Dec 2016) (Create)
22. Explain in detail about the role of Lexical analyzer with the possible
error recovery actions. (May/June„13)(May/June 2015, Nov/Dec ‟16,
Nov/Dec ‟18 ) (Understand)
23. Draw the transition diagram for relational operators and unsigned
numbers. (Apr/May ‟17, Apr/May ‟18, Apr/May ‟19) (Create)
24. Convert the regular expression abb(a|b)* to DFA using direct method
and minimize it. (Apr/May ‟17) (Create)
25. 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*
(Apr/May ‟18) (Apply)

26. Illustrate how does LEX work? * (Apr/May ‟18) (Remember)

PART C
1. Illustrate the output of each phase of compilation for input a:=b*c-d.
(Understand)

2. Divide the following C program into appropriate lexemes:


float limitedSquare(x)
{
float x;
/* returns x-squared, but never more than 100 */
return (x <= -10.0 || x >= 10.0) ? 100 : x*x;
}
Which lexemes should get associated lexical values and draw the
symbol table. (Analyze)

3. Construct NFA for the following Regular Expressions (a|b) *abb(a|b)*


and convert it into DFA (Evaluate)

4. Write the subset construction algorithm.Using the subset construction


algorithm ,convert the regular expression (a|b)*abb to DFA((Apr/May
‟19) (Remember)
5. Construct the minimized DFA for the regular expression
(0+1)*(0+1)01. (Nov/Dec ‟18) (Apply)

10
ASSIGNMENT

1. Discuss in detail about the operations of compiler which transforms


the source program from one representation into another .Illustrate
the output for the input: a = (b + c) * (b + c)*2 (Nov/Dec ‟18)
(Understand)
2. Analyze the role of lexical analyzer with suitable examples (Apr/May
‟19) (Apply)
3. Prove that the following two regular expressions are equivalent by
showing that the minimum state DFA‘s are same (Evaluate)
(i). (a/b)*, (ii) (a* /b*)*
4. Convert the regular expression (a|b)*abb to DFA using direct method
and minimize it. (Apr/May ‟17) (Create)
5. Design a Lexical Analyzer generator (Analyze)
6. Consider the regular expression below which can be used as part of a
specification of the definition of exponents in floating point numbers.
Assume the alphabet consists of numeric digits (‗0‘ through ‗9‘) and
alphanumeric characters (‗a‘ through ‗z‘ and ‗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 character ‗.‘ denotes
concatenation. Exponent =(+|-| ε).(E | e).(digit)+
i) Derive an NFA capable of recognizing its language using Thompson
construction.
ii) Derive the DFA for the NFA found in i) above using the subset
construction.
iii) Minimize the DFA found in (ii) above using the interactive refinement
algorithm described in class. (Apr/May ‟18) (Apply)

11
UNIT – II
SYNTAX ANALYSIS
PART -A

1. What are the significant advantages using grammars in syntax


analyzer? (Remember)
 It is very precise and easy to understand.
 Using certain class of grammar we can automatically construct
parsers that help in checking the syntactic structure.

2. List some universal parsing methods. (Remember)


Top down parsing, bottom up parsing and LR parsers.

3. List some of the errors that may occur at different levels of


compilation. (Remember)
I. Lexical, such as misspelling an identifier, key word or operator.
II. Syntactic, such as an arithmetic expression with unbalanced
parenthesis.
III. Semantic, such as an operator applied to an incompatible
operand.
IV. Logical, such as an infinitely recursive call.

4. What is a context free grammar? (Remember)


Many programming language constructs have an inherently recursive
structure that can be defined by Context-Free Grammars. A Context
Free Grammar consists of terminals, non-terminals, a start symbol and
productions.

5. Write some error recovery strategies in syntax analyzer.(Nov/Dec


‟18) (Remember)
Panic mode recovery, phrase level recovery, error productions and
global productions.

6. Write the notational conventions for terminals and non – terminals.


(Remember)
1. The symbols for terminals:
(i) Lower-case letters early in the alphabet a,b,c.
(ii) Operator symbols such as +,-, etc.
(iii) Punctuation symbols such as parentheses, comma, etc.
(iv) The Digits 0,1, ... 9
(v) Bold-face strings such as id or if
2. The symbols for non-terminals:
(i) Upper-case letters early in the alphabet A,B,C.
(ii) The letter S (start symbol)
(iii) Lower case italic letters such as expr or stmt.

7. What is a grammar symbol? (Remember)


Upper case letters late in the alphabet, such as X,Y,Z represent
grammar symbols, that is either non-terminals or terminals.

12
8. How to represent strings of terminals? (Remember)
Lower case letters late in the alphabet, chiefly u,v, …z , represent
strings of terminals.

9. How to represent strings of grammar symbols? (Remember)


Lower case Greek letters α,β,γ represent strings of grammar symbols.

10. Define a language generated by grammar. (Remember)


A language that can be generated by a grammar is said to be a
context-free language.

11. What is a parse tree? (Remember)


A parse tree may be viewed as a graphical representation for a
derivation that filters out the choice regarding replacement order. Each
interior node of a parse tree is labeled by some non-terminal A and that
the children of the node are labeled, from left to right, by the symbols in
the right side of the production by which this A was replaced in the
derivation

12. Why we use a regular expression to define the lexical syntax of a


language instead of a grammar? (Remember)
1. The lexical rules of a language are frequently quite simple, and to
describe them we do not need a notation as powerful as grammars.
2. Regular expressions generally provide a more concise and easier to
understand notation for tokens than grammars.
3. More efficient lexical analyzers can be constructed automatically from
regular expressions than from arbitrary grammars.
4. Separating the syntactic structure of a language into lexical and non
lexical parts provides a convenient way of modularizing the front end of
a compiler into two manageable-sized components

13. How to verify the language generated by a grammar? (Remember)


A proof that a grammar G generates a language L has two parts:
1. We must show that every string generated by G is in L, and
2. Conversely that every string in L can indeed be generated by G.

14. Eliminate the left recursion for the grammar A-> Ac | Aad | bd| ε
(May/June‟13, Apr/May „17) (Analyse)
A->bdA‘
A‘->cA‘| adA‘| ε
15. Perform the left factor for the grammar
stmtif expr then stmt else stmt | if expr then stmt
Soln:
stmtif expr then stmt stmt‘
stmt‘else stmt | 
16. Give examples for non context free languages. (Remember)
L1={wcw | w is in (a|b)*}
L2={anbmcnd m | n>=1 and m>=1}
L3={anbncn | n>=0}

13
17. What do you mean by top-down parsing? (Remember)
This is the parsing technique were the parse tree is created from root to
leaf.
Two techniques of top-down parsing are
1. Recursive Descent Parsing
2. Predictive Parsing.

18. What are the conditions to be checked before performing


predictive parsing? (Remember)
The conditions to be checked before performing predictive parsing are
1. Left recursion.
2. Left Factoring.

19. What is an LL(1) Grammar? (Remember)


Predictive parsers, that is, recursive-descent parsers needing no
backtracking, can be constructed for a class of grammars called LL(1). A
grammar is said to be LL(1) if it contains at most one production in the
entries of the parsing table.

20. What do you mean by bottom-down parsing? (Remember)


A bottom up parse corresponds to the construction of a parse tree for
an input string beginning at the leaves(bottom) and working up towards
the root(top).

21. What is an handle? (Remember)


A handle is a substring that matches the body of a production, and
whose reduction represents one step along the reverse of a right most
derivation.

22. What is handle pruning? (April/May‟11, Nov/Dec‟11, Nov/Dec ‟16,


Apr/May ‟18, Apr/May ‟19) (Apr/May „22 ) (Remember)
The right most derivation in reverse is called as handle pruning.

23. What are four operations in shift reduce parsing? (Remember)


The four operations in shift reduce parsing are
1. Shift
2. Reduce
3. Accept and
4. Error

24. What are the various conflicts that occur during shift reduce
parsing? (Apr/May „17) (Remember)
The various conflicts that occur during shift reduce parsing are
1. Shift/Reduce conflict. 2.Reduce/Reduce conflict.

25. What is an LR Parsing? (Remember)


LR Parsing is used to large class of context-free grammar. In this
technique LR(k) parsing, ―L‖ is left-to-right scanning of the input, the ―R‖

14
for constructing right most derivation in reverse, and ―k‖ for the number of
symbols of look ahead that are used in making parsing decisions.

26. List some LR Parsing Techniques. (Remember)


The various LR Parsing Techniques are
1. Simple LR (SLR)Parsing.
2. Canonical LR(CLR)Parsing.
3. Look Ahead LR (LALR) Parsing.

27. Write the algorithm for FIRST and FOLLOW in parser (May/June
2016) (Remember)
FIRST
1. If X is terminal, then FIRST(X) IS {X}.
2. If X → ε is a production, then add ε to FIRST(X).
3. 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);
FOLLOW
1. Place $ in FOLLOW(S),where S is the start symbol and $ is the
input right endmarker.
2. If there is a production A → αBβ, then everything in FIRST(β)
except for ε is placed in FOLLOW(B).
3. If there is a production A → αB, or a production A→ αBβ where FIRST(β)
contains ε ,then everything in FOLLOW(A) is in FOLLOW(B).

28. Write the rule to eliminate left recursion in a grammar. (Nov/Dec


‟12) (Remember)
Let us consider a simple left recursive grammar, AA |  where  does
not start with A.
An equivalent non-left-recursive grammar is
A  A‘
A‘  A‘ | ε

29. What is an ambiguous grammar? (May/June ‟12) (June 16)


(Remember)
A Grammar that produces more than one parse tree for some
sentence is said to be ambiguous. An Ambiguous grammar is one that
produces more than one leftmost or more than one rightmost derivation
for the same sentence. Ex: E  E+E|E*E

30. Eliminate the left recursion from the grammar (Nov/Dec 2013)
(Analyse) SAa|b AAc | Sd | e

Ans: S Aa|b
A SdA‘ | eA‘
A‘  cA‘ | 

15
31. Summarize the merits and demerits of LALR Parser?
(Apr/May‟18) (Remember)
Merits:
● Maintains lookahead information
● Keeps automaton small
● Resulting automaton has same size as LR(0) automaton
Demerits:
● Takes time to construct the LALR Parsing table.

32. Define LR (0) item. (Nov/Dec ‟18) (Remember)


 An LR (0) item is a production G with dot at some position on the
right side of the production.
 LR(0) items is useful to indicate that how much of the input has
been scanned up to a given point in the process of parsing.
Eg: A→.XYZ
A→X.YZ
A→XY.Z
A→XYZ.
33. Mention the purpose of YACC. (Apr/May‟19) (Remember)
 YACC is an automatic tool for generating the parser program.
 YACC stands for yet another Compiler which is basically the utility
available from UNIX.
 Basically YACC is LALR parser generator.
 It can report conflict or ambiguities in the form of error messages

PART B
1. Construct predictive parsing table for the grammar (May/June‟13&
May/June„12) (Create)
S (L)/a LL,S/S.
and check whether the following sentences belong to that grammar or
not.
(a,a)
(a, (a , a))
(a, (a , (a , a))) (Apr/May ‟17) (Evaluate)

2. What are the different strategies that a parser can employ to recover from
syntax errors?
3. Explain in detail the conflicts that may occur during shift- reduce parsing
(May/June„12) (Understand)
4. Construct the SLR parsing table from SAA A Aa/b.
5. What is FIRST and FOLLOW? Explain in detail with an example. Write
down the necessary algorithm. (Remember)
6. Check whether the following grammar is SLR (1) or not. Explain your
answer with reasons.

a. S L=R
b. SR

16
c. L*R
d. Lid
e. RL (Create)
7. Check whether the following grammar is a LL (1) grammar(May/June
2016) (Apr/May‟ 22) (Analyze)
S  iEtS | iEtSeS | a
E  b Also define the FIRST and FOLLOW procedures.
8. Construct an SLR Parsing table for the following grammar. (April/May
‟11, Nov/Dec ‟11) (Create)
EE+T
ET
TT*F
TF
F (E)
Fid
9. Explain the error recovery strategies in syntax analysis (Understand)
10. List LR(0) items for the following grammar (May/June„13) (Evaluate)
S -> AS/b A -> SA/a.
11. Construct a non recursive Predictive Parsing table for the following
grammar: (Nov/Dec‟12) (Create)
1. E -> E or E=E and E | not E | (E) | 0 | 1
12. Construct SLR Parsing Table for the grammar (Nov/Dec‟12, Nov/Dec
‟16, Apr/May „17) (Create)
(i) EE+T | T
(ii) TTF | F
(iii) F F* | a | b
13. Design an LALR Parser for the following grammar and parse the input
id=id. (Nov/Dec 2013) (Create)
SL=R|R
L*R|id
RL
14. Construct the SLR parse table for the grammar .Show the actions of
the parser for the input string ―abab‖ (May/June 2014) (Create)
15. What is an ambiguous grammar? Is the following grammar
ambiguous? Prove EE+| E(E)|id.The grammar should be moved to the
next line ,centered. (May/June 2014) (Remember)
16. Draw NFA for the regular expression ab*/ab.(May/June 2014)
((Analyse))
17. Construct Stack implementation of shift reduce parsing for the
grammar
(i) E E+E
(ii) E  E*E
(iii) E  (E)
(iv) E  id and the input string idl + id2 *id3 (June 2016)
(Create)
18. Write an algorithm for Non recursive predictive parsing. (May/June
2016) (Understand)
19. Explain Context free grammars with examples (May/June 2016)
(Understand)

17
20. Explain LR parsing algorithm with an example (Nov/Dec ‟17)
(Understand)
21. Explain the non-recursive implementation of predictive parsers with
the help of the grammar (Nov/Dec ‟17) (Understand)
EE+T |T
TT*F | F
F (E) | id.
22. Construct parse tree for the input string w=cad using top down
parsing
i. S  cAD
ii. A  ab | a (Nov/Dec ‟16) (Create)

23. Construct parsing table for the grammar and find the moves made by
predictive parser on input id+id*id and find FIRST and FOLLOW
(Nov/Dec ‟16) (Create)
24. Explain ambiguous grammar G: E->E+E | E*E | (E) | id for the
sentence id+id*id (Nov/Dec ‟16) (Understand)
25. 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. (Apr/May ‟18) (Apply)
stat  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
iii)
26. 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 that are nullable. (Apr/May ‟18) (Apply)
27. Consider the following CFG grammar (Apr/May ‟18) (Apply)
S  aABe
A  Abc | b
B d
Where a,b,c,d,e are terminals , ‗S‘ (Start symbol) , A, B are non-
terminals.
a) Parse the sentence ―abbcde‖ using right most derivation
b) Parse the sentence ―abbcde‖ using left most derivation
c) Draw the parse tree.

18
28. Construct a predictive parser for the grammar. Verify whether the
input string id+id*id is accepted by the grammar or not

E TE‘
E‘ +TE|
T FT‘
T‘*FT‘|
F  (E)| id (APR/MAY ‟22)
PART C
1. Consider the following grammar (Apr/May ‟18) (Apply)
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

2. Show the following grammar


SAa|bAc|dc|bda
Aa
is LALR(1),but not SLR(1).(Nov/Dec ‟18)(Evaluate)

3. For the following grammar construct FIRST and FOLLOW set


SA | BCD
ABBA | EB
BbEc| BC| BDc|
Cc| 
Da|BDb
Ea | bE |  (Evaluate)

4. Construct the transition diagram for Predictive parser using the


grammar
EE+T |T
TT*F | F
F (E) | id. (Evaluate)

5. Construct CLR parsing table for the grammar


SCC
CcC
Cd (Apr/May‟22)(Evaluate)

19
ASSIGNMENT

1) Construct a predictive parsing table (or) Check whether given grammar


is LL(1) or not
AAaAb/BaBb
A
B (Evaluate)

2) For the following grammar construct FIRST and FOLLOW set


SA|BCD
ABBA|EB (Evaluate)

3) Show the following grammar

SAa|bAc|Bc|bBa
Ad
Bd
is LR(1),but not LALR(1). (Nov/Dec ‟18) (Understand)
4) 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
S‘S
SaAd|bBd|aBe|bAe
Ac
Bc (Apr/May ‟19) (Remember)

5) Write the algorithm for construction of LR parsing table for a given


grammar.Construct the LR parsing table for the following grammar:
EE+T |T
TT*F | F
F (E) | id. (Apr/May ‟19) (Remember)

UNIT-3
INTERMEDIATE CODE GENERATION
PART A

1. What are the benefits of using a machine independent intermediate


form?(Remember) (Apr / May ‟11, May/Jun „14)
The benefits of using a machine-independent intermediate forms are
1) Retargeting is facilitated; a compiler for a different machine can be
created by attaching a back end for the new machine to an existing
front end.
2) A machine-independent code optimizer can be applied to the
intermediate representation

2. What are the various intermediate representation? (Remember)


(May/June„13)

20
The various intermediate representation are syntax trees, postfix
notation and three address code.

3. What is a syntax tree? (Remember) (Apr / May ‟11)


A syntax tree depicts the natural hierarchical structure of a source
program.

4. What is a Dag? (Remember)


A dag gives the same information like syntax tree but in more compact
way because common subexpression are identified.

5. How can you generate three-address code? (Remember)


The three-address code is generated using semantic rules that are
similar to those for constructing syntax trees for generating postfix notation.

6. What is postfix notation? (Remember)


A Postfix notation is a linearized representation of a syntax tree. It is a
list of nodes of the tree in which a node appears immediately after its
children.

7. Draw a syntax tree and dag for a:=b*-c + b*-c . (Understand)


(Nov/Dec ‟12, Nov/Dec ‟11)
assign assign

a + a +

* * *
uminus b uminus
uminus b
b b
b b

(a) Syntax Tree (b) Dag

8. What is the use of mknode and mkleaf functions? (Remember)


mknode(op,left,right) return a pointer to an existing node whenever
possible, instead of constructing new nodes.
mkleaf(num,val) creates a number node with label num and a field
containing val,the value of the number.

9. Write the three address code for a:=b*-c + b*-c (Understand)


Three-address code sequences as below
t1 := -c t1 := -c
t2 := b * t1 t2 := b * t1
t3 := -c t5 := t2 + t2
t4 := t2 + t4 a := t5
a := t5 a := t5
(a) Code for the syntax tree (b) Code for the dag

21
10. Write any four types of three address statement? (Remember)
(Nov/Dec ‟11)
1) Assignment statements
2) Assignment Instructions
3) Copy statements
4) Unconditional jump
5) Conditional jump
6) Indexed assignments
7) Address and pointer assignments
8) Procedure calls and return

11. What is meant by Shot-Circuit or jumping code? (Remember)


We can also translate a Boolean expression into three-address code
without generating code for any of the Boolean operators and without having
the code necessarily evaluate the entire expression. This style of evaluation
is sometimes called ―short-circuit‖ or ―jumping‖ code

12. What is the three address code? (Remember)


Three Address code is a sequence of statements of the general form
x := y op z
where x, y, z are names, constants, or compiler-generated
temporaries; op stands for any operator, such as a fixed-or-floating point
arithmetic operator, or a logical operator on Boolean-valued data

13. What are the various implementations of three address code?


(Remember) (May/June14)
1) Quadruples
2) Triples
3) Indirect Triples

14. What is the use of enterproc and enter in declarations?


(Remember)
1) enter(table, name, type, offset) creates a new entry for name name
in the symbol table pointed to by table. Again, enter places type type
and relative address offset in fields within the entry.
2) enterproc (table, name, newtable) creates a new entry for procedure
name in the symbol table pointed to by table. The argument newtable
points to the symbol table for this procedure name.

15. What are the various operations used in processing the


declarations in nested procedures?(Remember)
1. mktable(previous)
2. enter(table, name, type, offset)
3. addwidth(table, width)
4. enterproc (table, name, newtable)

22
16. How to address the ith element in a single dimensional array?
(Understand)
If the width of each array element is w, then the ith element of array A
begins in location
base + ( i-low) * w

17. What are the two forms to store the two dimensional array?
(Remember)
A two-dimensional array is normally stored in one of two forms, either
row-major (row-by-row) or column-major (column-by-column).
In the case of a two-dimensional array stored in row-major form, the
relative address of A[ i1 ,i2] can be calculated by the formula
base + (( i1 - low1) x n2 + i2 – low2 ) x w

18. What are the various Boolean operators? (Remember)


Boolean expressions are composed of the boolean operators and, or,
and not

19. What are the methods of translating Boolean expressions?


(Remember)
There are two principal methods of representing the value of a Boolean
expression. To encode true and false numerically and to evaluate a boolean
expression analogously to an arithmetic expression. Implementing boolean
expressions is by flow of control, that is, representing the value of a
boolean expression by a position reached in a program

20. What is backpatching? (Remember) (Nov/Dec ‟12 ,May/June‟13,


Nov/Dec 2013)
Backpatching is the activity of filling up unspecified information of
labels using appropriate semantic actions during the code generation
process. In the semantic actions the functions used are
mklist(i),merge_list(p1,p2) and backpatch(p,i)

21. What are the three functions used to manipulate the list of labels
in backpatching? (Remember)
 mklist(i) creates the new list. The index i is passed as an argument to
this function where I is an index to the array of quadruple.
 merge_list(p1,p2) this function concatenates two lists pointed by p1
and p2. It returns the pointer to the concatenated list.
 backpatch(p,i) inserts i as target label for the statement pointed by
pointer p.

22. What is a calling sequence in procedure calls? (Remember)


Calling sequence, a sequence of actions taken on entry to and exit
from each procedure.

23. Write the steps for translation of a case statement? (Remember)


1. Evaluate the expression.

23
2. Find which value in the list of cases is the same as the value of the
expression. The default value matches the expression if none of the
values explicitly mentioned in cases.
3. Execute the statement associated with the value found.

24. Why are quadruples preferred over triples in an optimizing


compiler? (Remember) (May/June ‟12)
In quadruples, temporary names are entered into the symbol table.
But in triples we might
refer to a temporary value by the position of the statement that computes it.
If we do so, three-address statements can be represented by records with
only three fields: op, arg1 and arg2

25. List out the motivations of back patching ? (Remember)


(May/June ‟12)
Back patching usually refers to the process of resolving forward branches
that have been planted
in the code, e.g. at 'if' statements, when the value of the target becomes
unknown, e.g. when the closing brace or matching 'else' is encountered.

26. Translate the arithmetic expression a*-(b+c) into syntax tree and
postfix notation. (Understand) (Nov/Dec 2013)
Postfix notation abc+-*
Syntax tree
*
a uminus

b c
27. Write down the syntax directed definition of a simple desk
calculator? (Remember) (Nov/Dec „16)

Production Semantic rules


L ---> En L.val = E.val
E ---> E1+ T E.val = E1.val+ T.val
E ---> T E.val = T.val
T---> T1*F T.val = Ti.val x F.val
T ---> F T.val = F.val
F ---> (E) F.val = E.val
F ---> digit F.val = digit.lexval

24
PART B
1. Give the syntax-directed definition for flow of control statements.
(Remember)
2. How Back patching can be used to generate code for Boolean expressions
and flow of control statements. (Remember) (Nov/Dec ‟11)

3. How would you generate the intermediate code for the flow of control
statements? Explain with examples. (Remember) (May/June„13)
4. What are the various ways of calling procedures? Explain in detail.
(Remember)
5. Describe the method of generating syntax-directed definition for Control
statements. (Remember)
6. Give the semantic rules for declarations in a procedure. (Remember)
7. Explain about back patching with an example. (Remember)
(May/June‟13, May/June 2014)
8. Explain how the types and relative addresses of declared names are
computed and how scope information is dealt with. (Remember)
9. Describe in detail the syntax-directed translation of case statements.
(Remember)
10. Write down the translation scheme to generate three address code for
assignment statements. (Remember) (April/May ‟11, Nov/Dec ‟11 &
May/June„13) (Apr/May‟ 22)
11. (i) Translate the following switch statement into intermediate code.
(Understand) (Nov/Dec‟12)
Switch E
begin
case V1:S1
case V2:S2
case Vn-1:Sn-1
default:Sn
end
(ii) Generate three address code for the Boolean expression a<b or c<d
and e<f.
12. Translate the executable statements of the following C program into
three address code
(Understand) (Nov/Dec‟12)
main()
{
int i,a[10];
i=1;
while (i<=10){
a[i]=0;i=i+I;}
}
13. Define three-address code? Describe the various methods of
implementing the three address statements with an example. (Remember)
(April/May ‟11, Nov/Dec ‟11, May/June„13)
14. Discuss the various methods for translating Boolean expression
(Remember) (April/May‟11, May/June‟12)

25
15. (i) Describe the various types three-address statements. (Remember)
(May/June‟12) (Apr/May‟22)
(ii) How names can be looked up in the symbol table? Discuss.
(Remember)
16. Explain the following grammar for a simple procedure call.
(Understand) (May/June‟12)
statement S - > call id(Elist)
17. Write the translation scheme for the flow of control statements.
(Remember) (Nov/Dec 2013)
18. (i) Write the grammar and translation scheme for procedure call
statements. (Remember) (Nov/Dec 2013)
(ii) Draw the DAG for the following three address code. (Understand)
d=b*c; e=a+b; b=b*c; a=e-d
19. How would you convert the following into intermediate code?Give a
suitable example.
1) Assignment Statements. 2)Case Statements (Remember)
(May/June 2014)
20. Explain the sequence of stack allocation process for a function call.
(Remember) (May/June 2014)
21. For the given program segment a[i,j]=B[i,k] do the following (Create
and Analyse) (May/June 2015)
(i) Draw the annotated parse tree with the translation scheme to
convert to the three address code.
(ii) Write the 3-address code.
(iii) Determine the address of a[3,5] where all are integer arrays with
size 10 * 10 each.
22. What are the various inherited and synthesized attributes, explain.
(Remember)
23. Explain the evaluation order for SDD‘s with dependency graphs and
attributes. (Understand)
24. Explain the applications of syntax directed translations. (Understand)
25. Give syntax directed definition to differentiate expressions formed by
applying the arithmetic operators + and * to the variable x and
constants; expression : x*(3*x+x*x). (Remember) (May/June 2015)
26. What are the various syntax directed translation schemes.
(Remember)
PART C
1. Given the Syntax-Directed Definition below construct the annotated
parse tree for the input expression: ―int a, b, c‖. (Create)
2. Construct a syntax directed definition for constructing a syntax tree for
assignment statements. (Create)
S->id :=E
E->El +E2
E->El *E2
E -> -EI
E ->(El)
E -> id
3. Construct a Syntax-Directed Translation scheme that translates
arithmetic expressions from infix into postfix notation.Using the semantic

26
attributes for each of the grammar symbols and semantic rules, Evaluate
the input ―3*4+5*2‖. (Create) (Apr/May‟18)

ASSIGNMENT

1. Given the Syntax-Directed Definition below with the synthesized attribute


val, draw the annotated parse tree for the expression (3+4) * (5+6).
(Create)
2. Explain in detail about bottom up evaluation of S-Attributed definitions
(Knowledge)

27
UNIT-IV
PART - A
1. What do you mean by binding of names? (Apr/May „17) (Remember)
Even if each name is declared once in a program, the same name may
denote different data objects at run time. The informal term ―data object‖
corresponds to a storage location that can hold values.

2. Draw the diagram for storage organization. (Create)


Subdivision of run time memory into code and data area
Code
Static
Data
Stack


Heap

3. Draw the diagram for general activation record. (Apr/May‟ 22)


(Create)
A GENERAL ACTIVATION RECORD
Returned values
Actual parameters
Optional control link
Optional access link
Saved machine status
Local data
Temporaries

4. List various storage allocation strategies. (Nov/Dec ‟17)


Name different storage allocation strategies used in run time
environment. (Apr/May ‟18) (Remember)
The various storage allocation strategies are
Static allocation. Stack allocation, Heap allocation.

5. What is a dangling reference? (May/June ‟12) (May/June 16)


(Apr/May‟ 22) (Remember)
A dangling reference occurs when there is a reference to storage that
has been deallocated.It is a logical error to use dangling references, since
the value of deallocated storage is undefined according to the semantics of
most languages.
main()
{
int *p;
p=dangle();

28
}
int *dangle()
{
int i=23;
return &i;
}

6. What is a static check and dynamic check? (Remember)


Static check:
A compiler must check that the source program follows both the
syntactic and semantic conventions of the source language. This checking
is called static checking.
Dynamic check:
During the execution of the target program, programming errors will be
detected and reported is called dynamic checking.

7. Give examples for static checks. (May/June„13) (Remember)


The examples for static checks includes
Type checks
Flow-of-control checks.
Uniqueness checks.
Name-related checks..

8. List dynamic storage allocation techniques (Nov/Dec „16)


(Remember)
The two general approaches to dynamic storage allocation are
 Stack allocation (hierarchical): restricted, but simple and efficient.
 Heap allocation: more general, but less efficient, more difficult to
implement.

9. What are the two standard storage allocation strategies? (Remember)

Static Allocation, Stack Allocation


10. Draw the activation tree for the following code. (Apr/May ‟18)
(Apply)
int main()
{
printf(“Enter Your Name”);
scanf(“%s”, username);
int show_data(username);
printf(“Press any key to continue…”);

int show_data(char *user)
{
printf(“Your Name is %s”,username);
return 0;
}
}

29
main()

Show_data(username)

11.List out the contents of activation records(Remember)


 Actual parameters
 Returned values
 Control link
 Access link
 Saved machine status
 Local data
 Temporaries

12.Define Calling Sequences(Remember)

Calling sequences and the layout of activation records may differ greatly,
even among implementations of the same language. The code in a calling
sequence is often divided between the calling procedure (the "caller") and
the procedure it calls (the "callee").
There is no exact division of run-time tasks between caller and callee; the
source language, the target machine, and the operating system impose
requirements that may favor one solution over another.

13. How allocation of storage for variables and access to those variables
is performed that do not allow nested procedure declarations?
(Remember)

1. Global variables are allocated static storage. The locations of these


variables
remain fixed and are known at compile time. So to access any variable that
is not local to the currently executing procedure, we simply use the
statically determined address.
2. Any other name must be local to the activation at the top of the stack.
We may access these variables through the top sp pointer of the stack.

14. Mention some issues with nested procedures.

Access becomes far more complicated when a language allows procedure


declarations to be nested and also uses the normal static scoping rule;
that is, a procedure can access variables of the procedures whose
declarations surround its own declaration, following the nested scoping
rule described for blocks.

15.Define Access Links.(Remember)

30
 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.
 The nesting depth of q must be exactly one less than the nesting depth
of p. Access links form a chain from the activation record at the top of
the stack to a sequence of activations at progressively lower nesting
depths. Along this chain are all the activations whose data and
procedures are accessible to the currently executing procedure.

16. Define memory manager(Remember)


The subsystem that allocates and deallocates space within the heap; it
serves as an interface between application programs and the operating
system. For languages like C or C ++ that deallocate chunks of storage
manually (i.e., by explicit statements of the program, such as f r e e or d e
l e t e ) , the memory manager is also responsible for implementing
deallocation.

17. Mention the properties of memory manager(Remember)


Space Efficiency., Program Efficiency and Low Overhead.

18.Give the typical memory hierarchy configurations(Understand)

19.What is temporal and spatial locality ? (Remember)

Most programs exhibit a high degree of locality; that is, they spend
most of their time executing a relatively small fraction of the code and
touching only a small fraction of the data.
We say that a program has temporal locality if the memory locations it
accesses are likely to be accessed again within a short period of time. We
say that a program has spatial locality if memory locations close to the

31
location accessed are likely also to be accessed within a short period of
time.

20. The conventional wisdom shows that programs spend 90% of


their time executing 10% of the code. Justify . (Remember)

Programs often contain many instructions that are never executed.


Programs built with components and libraries use only a small fraction of
the provided functionality. Also as requirements change and programs
evolve, legacy systems often contain many instructions that are no longer
used.

21.What is set associativity? (Remember)

It would be too expensive to check every single line in the cache, so it


is common practice to restrict the placement of a cache line within the
cache. This restriction is known as set associativity.
A cache is k-way set associative if a cache line can reside only in k
locations. The simplest cache is a 1-way associative cache, also known as
a direct-mapped cache.

22. How to manage and coalesce free space ? (Remember)

When an object is deallocated manually, the memory manager must


make its chunk free, so it can be allocated again. In some circumstances,
it may also be possible to combine (coalesce) that chunk with adjacent
chunks of the heap, to form a larger chunk.
There is an advantage to doing so, since we can always use a large
chunk to do the work of small chunks of equal total size, but many small
chunks cannot hold one large object, as the combined chunk could.

23. What are the two data structures that are useful to support
coalescing of adjacent free blocks? (Remember)
Boundary Tags, A Doubly Linked, Embedded Free List

24. Mention some popular conventions and tools developed to help


programmers cope with the complexity in managing memory.
(Remember)

Object ownership, Reference counting, Region-based allocation

25.What are the two sub problems in use of registers? (Remember)


1. During register allocation, we select the set of variables that will reside
in registers at a point in the program.
2. During a subsequent register assignment phase, we pick the specific
register that a variable will reside in.
26. What are the two descriptors used in the simple code generation?
(Remember)(apr/may 2019)
1. Register descriptor

32
2. Address descriptor
27. What is the use of function getreg? (Remember)
The function getreg returns the location L to hold the value of x for
the
I. performing the assignment of the form x := y unless and until
it is a must.
28. What are the uses of register and address descriptors in code
generation? ( Nov/Dec‟12) (Remember)
The code-generation algorithm uses descriptors to keep track of
register contents and addresses for names.
1. A register descriptor keeps track of what is currently in each
register. It is consulted whenever a new register is needed. We assume that
initially the register descriptor shows that all registers are empty. As the
code generation for the block progresses, each register will hold the value of
zero or more names at any given time.
2. An address descriptor keeps track of the location where the current
value of the name can be found at run time. The location might
be a register, a stack location, a memory address. This information can be
stored in the symbol table and is used to determine the accessing method for
a name.

29. What is meant by input to the Code Generator? (Remember)


The input to the code generator is the intermediate representation of
the source program produced by the front end, along with information in
the symbol table that is used to determine the run-time addresses of the
data objects denoted by the names in the IR.

30. How mapping is performed in code generator? (Remember)

The complexity of performing mapping is determined by a factors such as


• the level of the IR
• the nature of the instruction-set architecture
• the desired quality of the generated code.

31. Define register allocation and assignment(Remember)

1. Register allocation, during which we select the set of variables that


will
reside in registers at each point in the program.
2. Register assignment, during which we pick the specific register that a
variable will reside in.

32. What is a basic block? (May/June‟13, Nov/Dec ‟11, Apr/May „17)


(Remember)
A basic block is a sequence of consecutive statements in which flow
of control enters at the beginning and leaves at the end without halt or
possibility of branching except at the end.

33
33. Write the steps to find the leader of a basic block. (Remember)
1. The first statement is a leader.
2. Any statement that is the target of a conditional or unconditional
goto is a leader.
3. Any statement that immediately follows a got o or conditional goto
statement is a leader

PART B

1. What are the different storage allocation strategies? (OR) Elaborate


storage organization (April/May ‟11 , May/June„12 May/June‟13,
Nov/Dec ‟11, Apr/May „17) (Nov/Dec 2013 ) (May/June 2015,
Nov/Dec ‟17) (Apr/may 2019)(Remember)
2. What is an activation record? Explain how its relevant to the
intermediate code generation phase with respect to procedure
declaration. (Remember)
3. Distinguish between the source text of a procedure and its activation at
run time. (April/May‟11) (Analyse)

4. Discuss the runtime storage management in detail (Create)


5. Describe about the contents of activation record. (Apr/May‟18)
(Remember)
6. Discuss in detail about Calling sequences (Create).
7. Explain the procedures for accessing non local data on the
stack(Analyse)
8. Discuss in detail about access links for finding non local data.
(Analyse).
9. Discuss in detail on Heap management (Analyse).
10. Write in Detail about the issues in the design of a code generator.
(Analyse).(nov/dec 2018,)
11. Illustrate the basic block structure of quicksort algorithm with three
address code representation.(Apr/May‟22)

PART C
1. Explain the simple code generator algorithm with a suitable example.
(Analyse) (Apr/may 2019)
2. Illustrate the concept of reduction of fragmentation using B e s t - F i t
and N e x t - F i t Object Placement techniques.(Create)
3. Explain auxiliary array called display for accessing and
maintaining the nonlocal data with suitable diagram.
(Analyse)

ASSIGNMENT

1.Explain in detail about static and dynamic RAM (Knowledge)


2.Write short notes on Instruction selection in Code
generation(Knowledge)

34
UNIT-V
PART – A
1. What is an Optimizing compiler (Nov/Dec 2013) (June 16)
(Remember)
Optimization is the process of transforming a piece of code to make more
efficient(either in terms of time or space) without changing its output or
side-effects. The only difference visible to the code‘s user should be that it
runs faster and/or consumes less memory. It is really a misnomer that
the name implies you are finding an ―optimal‖ solution— in truth,
optimization aims to improve, not perfect; the result.There should not be
any unreachable code. Dead code should be completely removed from
source language
2. What are the properties of optimizing compiler? (May/June‟13,
Nov/Dec‟17) (Remember)
The source code should be such that it should produce minimum amount
of target code.
There should not be any unreachable code.
Dead code should be completely removed from source language.
The optimizing compilers should apply following code improving
transformations on source language.
i) common sub expression elimination
ii) dead code elimination
iii) code movement
iv) strength reduction
3. What are the four structure preserving transformations on basic
block? (Apr / May ‟11, Apr/May „18) (Remember)
1. Common sub expression elimination
2. Dead – code elimination
3. Renaming of temporary variables
4. Interchange of two independent adjacent statements
4. Define Common sub expression (Nov/Dec ‟18) (Understand)
An occurrence of an expression E is called a common sub expression if
E was previously computed; and the values of variables in E have not
changed since the previous computation. We can avoid recomputing the
expression if we can use the previously computed value.
5. What is the criteria for code improving transformation? (Nov/Dec
‟11) (Remember)
The transformations provided by an optimizing compiler should have several
properties.
1. A transformation must preserve the meaning of programs.
2. A transformation must, on the average, speed up programs by a
measurable amount.
3. A transformation must be worth the effort.
6. What do you mean by copy propagation? (Apr/May „17) (Remember)
One concerns assignments of the form f:=g call copy statements, or
copies for short. The idea behind the copy-propagation transformation is to
use g for f, whenever possible after the copy statement f:=g.

35
7. Define constant folding. (May/June„13) (Remember)
Deducing at compile time that the value of an expression is a constant
and using the constant instead is known as constant folding.
8. What is an induction variable? (Remember)
Consider block B3
j:=j-1
t4=4*j
The values of j and t4 remain in lock-step; every time the value of j
decreases by 1, that of t4 decreases by 4 because 4*j is assigned to t 4.Such
identifiers are called induction variables.
9. Define dead-code elimination.(April/May‟11) (Remember)
A variable is live at a point in a program if its value can be used
subsequently; otherwise, it is dead at that point. The dead code may appear
as the result of previous transformations.
Consider debug := false

if (debug)
print …
By data flow analysis, each time the program reaches the if statement,
the value of debug is false.
10. What is loop optimization? Name the techniques in loop
optimization (April/May‟11) (May/June14, Apr/May „18) (Remember)
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. Three techniques are important for loop optimization:
(1) Code motion,
(2) Induction variable elimination,
(3) Reduction in strength
11. Define loop unrolling with example (Nov/Dec 2013) (Remember)
Loop unrolling, also known as loop unwinding, is a loop transformation
technique that attempts to optimize a program's execution speed at the
expense of its binary size. The transformation can be undertaken manually
by the programmer or by an optimizing compiler.
Normal loop After loop unrolling
int x;
int x; for (x = 0; x < 100; x+=5) {
for (x = 0; x < 100; x++) delete(x);
{ delete(x+1);
delete(x); delete(x+2);
} delete(x+3);
delete(x+4); }
12. What is a basic block? (May/June‟13, Nov/Dec ‟11, Apr/May „17)
(Remember)
A basic block is a sequence of consecutive statements in which flow
of control enters at the beginning and leaves at the end without halt or
possibility of branching except at the end.

36
13. Write the steps to find the leader of a basic block. (Remember)
1. The first statement is a leader.
2. Any statement that is the target of a conditional or unconditional
goto is a leader.
3. Any statement that immediately follows a got o or conditional goto
statement is a leader
14. What do you mean by algebraic transformation? (Remember)
Countless algebraic transformations can be used to change the
set of expressions computed by a basic block into an algebraically
equivalent set. The useful ones are those that simplify expressions or
replace expensive operations by cheaper ones. Ex: x := x + 0 or x := x * 1
can be eliminated from a basic block without changing the set of
expressions t computes.
15. What is a flow graph? (May/June‟13 & May/June ‟12, Nov/Dec
‟11) (Remember)
We can add the flow of control information to the set of basic
blocks by constructing a directed graph called a flow graph. The nodes of
the flow graph are the basic blocks. One node is distinguished as initial;
it is t he block whose leader is the first statement.
16. What is a loop? (Remember)
A loop is a collection of nodes in a flow graph such that
1. All nodes in the collection are strongly connected
2. The collection of nodes has a unique entry
17. Give any four applications /advantages of DAG. (Nov/Dec ‟12
&May/June„13) (Remember)
II. Determining the common sub-expressions.
III. Determining which names are used inside the block and
computed outside the block.
IV. Determining which statements of the block could have their
computed value outside the block.
V. Simplifying the list of quadruples by eliminating the common
su-expressions and not performing the assignment of the
form x := y unless and until it is a must.
18. What is the purpose of next use information? (Apr / May ‟11)
(Remember)
We need to know, for each use of a variable in a basic block, whether
the value contained in the variable will be used again later in the block. If a
variable has no next-use we can reuse the register allocated to the variable.
We also need to know whether a variable used in a basic block
Is live-on-exit , i.e. if the value contained in the variable has a use
outside the block.
19. How to perform register assignment for outer loops (May/June‟12)
(Remember)
If an outer loop L1 contains an inner loop L2, the allocated registers in
L2 need not be allocated registers in L1 – L2· However, if name X is allocated
a register in loop L1 but not L2, we must store x on entrance to L2 and load
x if we leave L2 and enter a block L1 – L2. Similarly, if we choose to allocate

37
x a register in L2 but not L1 we must load x on entrance to L2 and store x
on exit from L2.
20. What is DAG? (Nov/Dec ‟11)(June 16) (Remember)
Directed acyclic graphs (dags) are useful data structures for
implementing transformations on basic blocks. A dag gives a picture of how
the value computed by each statement in a basic block is used in
subsequent statements of the block.
21. What is the use of next-use information (Nov/Dec 2013)
(Remember)
We need to know, for each use of a variable in a basic block, whether the
value contained in the variable will be used again later in the block. If a
variable has no next-use we can reuse the register allocated to the variable.
We also need to know whether a variable used in a basic block
Is live-on-exit, i.e. if the value contained in the variable has a use outside
the block.
22. Define symbol table. (May/June14) (Remember)
Symbol table provides the associations between all occurrences of
each name given in the program. It provides a link between each name and
it declaration.
23. Define a natural loop. (Remember)
One important application of dominator information is in determining the
loops of a flow graph suitable for improvement. There are two essentials
properties of such loops.
1. A loop must have a single entry point called header.
2. There must be at least one way to iterate the loop i.e., at least
one path back to the header.
24. What is a pre-header? (Remember)
Pre-Header is also a header that appears before the header. The
preheader has only the header as the successor, and all edges which
formerly entered the header of L from outside L instead enter the pre-header.
25. When a graph is reducible? (Remember)
Exclusive use of structured flow-of-control statements such as if-then-
else, while-do, continue, and break statements produces programs whose
flow graphs are always reducible.
26. Write a general data flow equation for global data flow analysis.
(Remember)
The general data flow equation for global data flow analysis is given as
out[S]=gen[S]U(in[S]-kill[S])
27. Define point and path. (Remember)
Point: Point between two statements as the point before the first statement
and after the last statement.
Path: A path from p1 to pn is a sequence of points p1,p2,…,pn such that for
each i between 1 and n-1,either
1. pi is the point immediately preceding a statement and pi+1 is the
point immediately following that statement in the same block, or
2. pi is the end of some block and pi+1is the beginning of a
successor block.
28. What is live variable?(Nov/Dec‟12) (Remember)

38
A variable is live at a point in a program if its value can be used
subsequently; otherwise, it is dead at that point.
29. What is dataflow analysis? (Nov/Dec‟12) (Remember)
Data-Flow Analysis is a standard way to formulate intra-procedural program
analysis. t is the process of collecting information about the way the
variables are used, defined in the program.
30. List out two properties of reducible flow graph? (May/June„12)
(Remember)
A flow graph G is reducible if and only if we can partition the edges into two
disjoint groups, often called the forward edges and back edges, with the
following two properties:
1. The forward edges form an acyclic graph in which every node can be
reached from the initial node of G.
2. The back edges consist only of edges whose heads dominate their tails.
31. What is a Dag?(June 16) (Remember)
A dag gives the same information but in more compact way because
common subexpression are identified
32. Draw a syntax tree and dag for a:=b*-c + b*-c (Nov/Dec ‟12,
Nov/Dec ‟11) (Create)
assign assign

a + a +

* *
*
uminus b uminus
uminus b
b b
b b

(a) Syntax Tree (b) Dag


33. Write three address code sequence for the assignment statement
d:= (a-b) + (a-c) + (a-c) (June 2016) (Apply)
t1=a-b
t2=a-c
t3=a-c
t4=t1+t2
t5=t4+t3
d=t5
34. Draw the DAG for the statement a=(a*b+c)-(a*b+c) (Nov/Dec ‟17)
(Create)
assign

a -

* b

a b
39
PART B

1. Explain the structure-preserving transformations for basic blocks


(Nov/Dec ‟11) (Apr/May‟ 22) (Understand)
2. Describe the principal sources of optimization. (May/June‟13, Nov/Dec
‟11, May/June‟12, May/June 2016, Nov/Dec 17)
3. Write global common subexpression elimination algorithm with example.
(May/June‟13) (Remember)
4. Write in detail about function-preserving transformations. (April/May
‟11) (Remember)
5. What are steps needed to compute the next use information?
(Remember)
6. Explain DAG representation of the basic blocks with an example.
(Understand)
7. How to generate a code for a basic block from its dag representation?
(April/May ‟11) (Remember)
8. Discuss labeling algorithm with example. (May/June„13) (Create)
9. Define basic block. Write an algorithm to partition a sequence of three-
address statements into basic blocks (May‟11,May/June‟12) (April/ May
2019)(Remember)
10. Construct the dag for the following basic block (May/June‟12)
(Create)
d=b* c e=a+b
b=b*c a=e-d
11. For the flow graph shown below, write the three address statements &
construct the DAG (Nov/Dec‟12, Apr/May „17). (Create)
t1=4*i
t2=a[t1]
t3=4*i
t4=b[t3]
t5=t2*t4
t6:=prod +t5
prod:=t6
t7:=i+1
i:=t7
If i≤20 goto (1)
12. Construct the DAG and three address code for the following C program
(Nov/Dec 2013) (Create)
i=1;
s=0;
while (i<=10)
{
s=s+a[i][i];
i=i+1;
}
13. 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/June 2014)
14. Discuss in detail about optimization of basic blocks with example
(Nov/Dec ‟11, Apr/May „17) (Nov/Dec ‟18) (Understand)

40
15. Discuss in detail about global data flow analysis (Nov/Dec ‟11,
May/June„13) (Nov/Dec‟12) (Apr/May‟ 22) (Create)
16. Write an algorithm to construct the natural loop of a back edge.
(April/May ‟11, (Nov/Dec ‟16) (Remember)
17. Construct DAG and optimal target code for the following expression.
x=((a+b)/(b-c))-(a+b)*(b-c)+f (Create)
18. Determine the basic blocks of instructions ,Control Flow Graph(CFG)
and the CFG dominator tree for the following the code (Apr/May‟18)
(Analyze)

01 a=1
02 b=0
03 L0: a = a + 1
04 b =p + 1
05 if (a > b) goto L3
06 L1: a = 3
07 if (b > a) goto L2
08 b=b+1
09 goto L1
10 L2: a = b
11 b=p+q
12 if (a > b) goto L0
13 L3: t1= p * q
14 t2= t1 + b
15 return t2
19. Construct a code sequence and DAG for the following syntax directed
translation of the expression :(a + b) – (e –( c + d )). (Apr/May‟18)
(Create)
20. Write and explain the algorithm for construction of basic
blocks.(Apr/May‟19) (Understand)
21. Explain in detail about Peep-hole optimization(Apr/May‟22)
(Understand)
22. Construct the DAG for the following basic block
x = a[i]
a[j]=y
z=a[i] (Apr/May‟19) (Apply)

PART C
1. 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
purposes of code generation, such as its type and location at run-time .
(Apr/May ‟18) (Understand)

01: procedure main


02: integer a, b, c;
03: procedure f1 (a, b);

41
04: integer a, b;
05: call f2(b, a)
06: end;
07: procedure f2(y, z)
08: integer y, z ;
09: procedure f3(m, n);
10: integer m, n;
11: end;
12: procedure f4(m, n);
13: integer m, n;
14: end;
15: call f3(c,z);
16: call f4(c,z);
17: end;
18: …
19: call f1 (a, b);
20: end;

2. For the flow graph of shown below, compute the live variables, The gen
and kill sets for each block, (Understand)

3. A simple matrix- multiplication program is given below:


for(i=0;i<n;i++)
for(j=0;j<n;j++)
c[i][j]=0.0;

42
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.
ii. Construct the flow graph for the code from 1
iii. Identify the loops in the flow graph from 2 (April / May 2019)
(Analysis)

ASSIGNMENT
1. Explain in detail about code-improving transformations(Knowledge)
2. construct a DAG and write the sequence of instruction for the expression
a=(a*b+c)-(a*b+c). (Design)

PROGRAMS

1. Develop a lexical analyzer to recognize a few patterns in C. (Ex.


identifiers, constants, comments, operators etc.). Create a symbol
table, while recognizing identifiers.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct sym
{
int pos;
char n[20],typ[20];
int allst;
int addr;
}s[20];
int main()
{
int i=0,p=0,n,j,k;
char input[20],typ[20];
char dt[5][20]={"int","char","float","long","double"};
FILE *fp;
clrscr();
fp=fopen("H:\\input.txt","r");
fscanf(fp,"%s",input);
while(strcmp(input,"EOF")!=0)
{
for(j=0;j<5;j++)

43
{
if(strcmp(input,dt[j])==0)
{
strcpy(typ,input);
fscanf(fp,"%s",input);
while(strcmp(input,";")!=0)
{
if(strcmp(input,",")!=0)
{
p++;
s[p].pos=++i;
strcpy(s[p].n,input);
strcpy(s[p].typ,typ);
fscanf(fp,"%s",input);
}
if(strcmp(input,";")!=0)
fscanf(fp,"%s",input);
}
}
}
fscanf(fp,"%s",input);
}
n=i+1;
s[0].allst=0;
s[0].addr=1000;
for(k=1;k<n;k++)
{
s[k].addr=s[k-1].addr+s[k-1].allst;
if(strcmp(s[k].typ,"int")==0)
s[k].allst=2;
else
if((strcmp(s[k].typ,"float")==0)||(strcmp(s[k].typ,"double")==0))
s[k].allst=4;
else if(strcmp(s[k].typ,"char")==0)
s[k].allst=1;
else if(strcmp(s[k].typ,"long")==0)
s[k].allst=8;
}
printf("\n\t\t\tSymbol Table");
printf("\nPosition\tName\tType\tAllocatedStorage\tAddress");
for(k=1;k<n;k++)
printf("\n%d\t\t%s\t%s\t\t%d\t\t%d",s[k].pos,s[k].n,s[k].typ,s[k].allst,s[k].
addr);
getch();
return 0;
}

OUTPUT

44
input.txt:
void main ()
{
int a , d ;
float b ;
c = a + b;
}
EOF

2. Implement a Lexical Analyzer using Lex Tool

Program to Find the Capital letters in some string using LEX

%{
%} Declarations section

%%
[A-Z] {printf(“%s”,yytext);}
.; Transition rules section
%%

main()
{printf(“Enter Some string”);
yylex();
} Auxillary procedures
int yywrap()
{return 1;
}

Input
Enter Some string
Panimalar Engineering College
Output
PEC

2.b Program to count the number of vowels and consonants in a given string.

%{

45
#include<stdio.h>

int v=0;
int c=0;
%}
%%
\n return 0;
[aeiouAEIOU] { ++v; }
[^aeiouAEIOU] { ++c; }

%%
int yywrap()
{
return 1;
}
main ()
{
printf("Enter the string : ");
yylex();
printf("No. of Vowels = %d \n No. of Consonants=%d", v,c);
}

Input
Enter the string : Computer
Output
No. of Vowels =3
No. of consonants=5

2.c. Program to count the number of characters, words, spaces, end of lines in a given
input file.
%{
#include<stdio.h>
int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;
%}
%%

\n { lines++; words++;}
[\t ' '] words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
. spl_char++;
%%

main(void)
{
yyin= fopen("myfile.txt","r");
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" This File contains ...");

46
printf("\n\t%d lines", lines);
printf("\n\t%d words",words);
printf("\n\t%d small letters", s_letters);
printf("\n\t%d capital letters",c_letters);
printf("\n\t%d digits", num);
printf("\n\t%d special characters",spl_char);
printf("\n\tIn total %d characters.\n",total);
}

int yywrap()
{
return(1);
}
Sample output
'myfile.txt' contains this.

This is my 1st lex program!!!


Cheers!! It works!!:)

The output will be


This file contains..
2 lines
9 words
30 small letters
3 capital letters
1 digits
9 special characters
In total 43 characters.

2.d LEXICAL ANALYZER USING LEXICAL ANALYZER GENERATING


TOOLS

PROGRAM :

%{
#include<stdio.h>
#include<string.h>
struct st
{
char LEXeme[25];
char name[25];
}ST[100];
int cnt=0;
%}
ID [a-zA-Z][a-zA-Z0-9]*
DIGIT [0-9]
%%

47
{DIGIT}+ {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"const integer
literal");cnt++;}
{DIGIT}+"."{DIGIT}+
{strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"const float
literal");cnt++;}
"#include"|"#define" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"pp
directive");cnt++;}
{ID}".h"
{strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"includefile");cnt++;}
main |void |switch |case
|continue|break|do|while|for|if|else|int|float|char
{strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"keyword");cnt++;}
"\""{ID}"\"" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"string
literal");cnt++;}
{ID} {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"identifier");cnt++;}
"+"|"-"|"#"|"/"|"%"
{strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Arithmetic OP");cnt++;}
"&"|"|"|"^"+"~" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Bitwise
OP");cnt++;}
"<<"|">>" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Bitwise SHift
OP");cnt++;}
"&&"|"||"|"!" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Logical
OP");cnt++;}
"<"|">"|"<="|">="
{strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Realational OP");cnt++;}
"=="|"!=" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Equality
OP");cnt++;}
"[" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"OSB");cnt++;}
"]" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"CSB");cnt++;}
"{" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"OCB");cnt++;}
"}" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"CCB");cnt++;}
"(" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"ORB");cnt++;}
")" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"CRB");cnt++;}
":" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Semicolon");cnt++;}
"++" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"IncOP");cnt++;}
"--" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"DecOp");cnt++;}
"?" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Ternary OP");cnt++;}
"=" {strcpy(ST[cnt].LEXeme,yytext);strcpy(ST[cnt].name,"Assignment
OP");cnt++;}
%%
main(int argc,char *argv[])
{
int i=0;

48
yyin=fopen(argv[1],"r");
yylex();
printf("\nTOKEN TABLE");
printf("\nLEXEME\t\t\tNAME\n");
printf(" \t\t \n");
for(i=0;i<cnt;i++)
{
printf("\n%s",ST[i].LEXeme);
printf("\t\t\t%s",ST[i].name);
}
printf("\n");
}
int yywrap()
{
return 1;
}
INPUT C PROGRAM:
#include<stdio.h>
main()
{
int a,b,c=0;
c=a+b;
return c;
}
OUTPUT:
TOKEN TABLE
LEXEME NAME

#include pp directive
< Realational OP
stdio.h includefile
> Realational OP
main string literal
( ORB
) CRB
{ OCB
int identifier
a identifier
b identifier
c identifier
= Assignment OP
0 const integer literal
c identifier
= Assignment OP

49
a identifier
+ Arithmetic OP
b identifier
return identifier
c identifier
} CCB
3. Implement an Arithmetic Calculator using LEX and YACC

PROGRAM
Ex4c.l

%{
%}
DIGIT [0-9]+\.?|[0-9]*\.[0-9]+

%option noyywrap
%%

[]
{DIGIT} { yylval=atof(yytext); return NUM;}
\n|. {return yytext[0];}

Ex4c.y

%{
#include<ctype.h>
#include<stdio.h>
#define YYSTYPE double
%}
%token NUM

%left '+' '-'


%left '*' '/'
%right UMINUS

%%

S : S E '\n' { printf("Answer: %g \nEnter:\n", $2); }


| S '\n'
|
| error '\n' { yyerror("Error: Enter once more.\n" ); yyerrok; }
;
E : E '+' E { $$ = $1 + $3; }
| E '-' E { $$=$1-$3; }
| E '*' E { $$=$1*$3; }
| E '/' E { $$=$1/$3; }
| '('E')' { $$=$2; }
| '-'E %prec UMINUS { $$= -$2; }
| NUM

50
;
%%

#include "lex.yy.c"

int main()
{
printf("Enter the expression: ");
yyparse();
}

yyerror (char *s)


{
printf ("%s",s);
}

Output
[csestaff@localhost csestaff]$ lex ex4c.l
[csestaff@localhost csestaff]$ yacc -d ex4.y
[csestaff@localhost csestaff]$ cc y.tab.c
[csestaff@localhost csestaff]$ ./a.out
Enter the expression: 2+3
Answer: 5
Enter:
ewe
syntax error
Error: Enter once more.

5. Implement simple code optimization techniques (Constant folding,


Strength reduction and Algebraic transformation)

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct op
{
char l;
char r[20];
}
op[10],pr[10];
void main()
{
int a,i,k,j,n,z=0,m,q;
char *p,*l;
char temp,t;

51
char *tem;
clrscr();
printf("Enter the Number of Values:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("left: ");
op[i].l=getche();
printf("\tright: ");
scanf("%s",op[i].r);
}
printf("Intermediate Code\n") ;
for(i=0;i<n;i++)
{
printf("%c=",op[i].l);
printf("%s\n",op[i].r);
}
for(i=0;i<n-1;i++)
{
temp=op[i].l;
for(j=0;j<n;j++) {
p=strchr(op[j].r,temp);
if(p) {
pr[z].l=op[i].l;
strcpy(pr[z].r,op[i].r);
z++; }}}
pr[z].l=op[n-1].l;
strcpy(pr[z].r,op[n-1].r);
z++;
printf("\nAfter Dead Code Elimination\n");
for(k=0;k<z;k++) {
printf("%c\t=",pr[k].l);
printf("%s\n",pr[k].r);
}
for(m=0;m<z;m++) {
tem=pr[m].r;
for(j=m+1;j<z;j++) {
p=strstr(tem,pr[j].r);
if(p) {
t=pr[j].l;
pr[j].l=pr[m].l;
for(i=0;i<z;i++) {
l=strchr(pr[i].r,t) ;

52
if(l) {
a=l-pr[i].r;
printf("pos: %d",a);
pr[i].r[a]=pr[m].l; }}}}}
printf("Eliminate Common Expression\n");
for(i=0;i<z;i++)
{
printf("%c\t=",pr[i].l);
printf("%s\n",pr[i].r);
}
for(i=0;i<z;i++)
{
for(j=i+1;j<z;j++)
{
q=strcmp(pr[i].r,pr[j].r);
if((pr[i].l==pr[j].l)&&!q)
{
pr[i].l='\0';
strcpy(pr[i].r,'\0');
}
}
}
printf("Optimized Code\n");
for(i=0;i<z;i++)
{
if(pr[i].l!='\0')
{
printf("%c=",pr[i].l);
printf("%s\n",pr[i].r);
}
}
getch();
}
OUTPUT

53
6. Implement back-end of the compiler for which the three address code is
given as input and the 8086 assembly language code is produced as output.

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<graphics.h>
typedef struct
{
char var[10];
int alive;
}
regist;
regist preg[10];
void substring(char exp[],int st,int end)
{
int i,j=0;
char dup[10]="";
for(i=st;i<end;i++)
dup[j++]=exp[i];
dup[j]='0';
strcpy(exp,dup); }

54
int getregister(char var[]) {
int i;
for(i=0;i<10;i++) {
if(preg[i].alive==0) {
strcpy(preg[i].var,var);
break; }}
return(i); }
void getvar(char exp[],char v[]) {
int i,j=0;
char var[10]="";
for(i=0;exp[i]!='\0';i++)
if(isalpha(exp[i]))
var[j++]=exp[i];
else
break;
strcpy(v,var); }
void main() {
char basic[10][10],var[10][10],fstr[10],op;
int i,j,k,reg,vc,flag=0;
clrscr();
printf("\nEnter the Three Address Code:\n Type exit at the end\n");
for(i=0;;i++) {
gets(basic[i]);
if(strcmp(basic[i],"exit")==0)
break; }
printf("\nThe Equivalent Assembly Code is:\n");
for(j=0;j<i;j++) {
getvar(basic[j],var[vc++]);
strcpy(fstr,var[vc
-1]);
substring(basic[j],strlen(var[vc
-1])+1,strlen(basic[j]));
getvar(basic[j],var[vc++]);
reg=getregister(var[vc-1]);
if(preg[reg].alive==0)
{
printf("\nMov R%d,%s",reg,var[vc-1]);
preg[reg].alive=1;
}
op=basic[j][strlen(var[vc-1])];
substring(basic[j],strlen(var[vc-1])+1,strlen(basic[j]));
getvar(basic[j],var[vc++]);
switch(op)
{
case '+': printf("\nAdd"); break;
case '-': printf("\nSub"); break;
case '*': printf("\nMul"); break;
case '/': printf("\nDiv"); break;
}

55
flag=1;
for(k=0;k<=reg;k++)
{
if(strcmp(preg[k].var,var[vc-1])==0)
{
printf("R%d, R%d",k,reg);
preg[k].alive=0;
flag=0;
break;
}
}
if(flag)
{
printf(" %s,R%d",var[vc-1],reg);
printf("\nMov %s,R%d",fstr,reg);
}
strcpy(preg[reg].var,var[vc-3]);
getch();
}
}

Output:
Enter the Three Address Code:
a=b+c
c=a*c
exit
The Equivalent Assembly Code is:
Mov R0,b
Add c,R0
Mov a,R0
Mov R1,a
Mul c,R1
Mov c,R1

56

You might also like