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

2 Compiler Design Notes

The document discusses compiler design and the various phases of a compiler. It begins by defining key terms like compilers, translators, and interpreters. It then describes the main phases of a compiler as lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. The document also discusses related topics like symbol tables, passes, preprocessors, assemblers, and loaders.

Uploaded by

Padmavathi B
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

2 Compiler Design Notes

The document discusses compiler design and the various phases of a compiler. It begins by defining key terms like compilers, translators, and interpreters. It then describes the main phases of a compiler as lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. The document also discusses related topics like symbol tables, passes, preprocessors, assemblers, and loaders.

Uploaded by

Padmavathi B
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Compiler Design

Unit-I Introduction To Compilers


Translators-Compilation and Interpretation-Language processors -The Phases of Compiler-Errors Encountered in Different Phases-The Grouping of Phases-
Compiler Construction Tools -Programming Language basics.
Part-A
1. Define Compiler (or) What does translator mean? (May/June,2006)
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 complier reports to its
user the presence of the errors in the source program.
2. What are the classifications of a compiler?
The classifications of compiler are: Single-pass compiler, Multi-pass compiler, Load and go compiler, Debugging
compiler, Optimizing compiler.
3. Define linker.
Linker is a program that combines (multiple) objects files to make an executable. It converts names of variables
and functions to numbers (machine addresses).
4. Define Loader.
Loader is a program that performs the functions of loading and linkage editing. The process of loading consists of
taking re-locatable machine code, altering the re-locatable address and placing the altered instruction and data in
memory at the proper location.
5. Define interpreter. Interpreter is a language processor program that translates and executes source code
directly, without compiling it to machine code.
6. Define editor.
Editors may operate on plain text, or they may be wired into the rest of the complier, highlighting syntax errors as
you go, or allowing you to insert or delete entire syntax constructed at a time.
7.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.
8.What are the phases of a compiler? or How will you group the phases of compiler ?(Nov/Dec ,2013)
 Lexical analysis
 Syntax analysis
 Semantic analysis
 Intermediate code generation
 Code optimization
 Code generation
9.Mention few cousins of the compiler?(May/June,2012)
 Pre-processors.
 Assemblers.
 Two pass assembly.
 Loader and link editors.
10.What is front-end and back-end of the compiler?
Often the phases of a compiler are collected into a front-end and back-end.
Front-end consists of those phases that depend primarily on the source language and largely independent of the
target machine. Back-end consists of those phases that depend on the target machine language and generally
those portions do not depend on the source language, just the intermediate language. In back end we use aspects
of code optimization, code generation, along with error handling and symbol table operations.
11.List of some compiler construction tools.
 Parser generators
 Scanner generators
 Syntax-directed translation engines
 Automatic code generators
 Data flow engines.
12.Define Pre-processor. What are its functions?
Pre-processors are the programs that allow user to use macros in the sources program. Macro means some set of
instructions which can be repeatedly used in the sources program . The output of pre-processor may be given as
input to complier.
The functions of a pre-processor are:
 Macro processing.
 File inclusion.
 Rational pre-processors
 Language extensions
13.What is linear analysis?
The stream of characters making up the source program is read from left-to-right and grouped into tokens that are
sequence of characters having a collective meaning.
14.What is cross complier? (May/June, 2014)
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.
15.What is Symbol Table or Write the purpose of symbol table.(May/June, 2014)
Symbol table is a data structures 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 to store or retrieve data
from that record quickly.
16.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.
17. Difference Between Assembler, Compiler and Interpreter.
Assembler: It is the Computer Program which takes the Computer Instructions and converts them in to the bits
that the Computer can understand and performs by certain Operations.
Compiler:
 It Converts High Level Language to Low Level Language.
 It considers the entire code and converts it in to the Executable Code and runs the code.
 C, C++ are Compiler based Languages.
Interpreter:
 It Converts the higher Level language to low level language or Assembly level language. It converts to the
language of 0' and 1's.
 It considers single line of code and converts it to the binary language and runs the code on the machine.
 It it founds the error, the programs need to run from the beginning.
 BASIC is the Interpreter based Language.
18.State some software tools that manipulate source program?(May/June ,2006)
i. Structure editors
ii. Pretty printers
iii. Static checkers
iv. Interpreters.
19. Define compiler-compiler.
Systems to help with the compiler-writing process are often been referred to as compiler-compilers,
compiler-generators or translator-writing systems.
Largely they are oriented around a particular model of languages , and they are suitable for generating
compilers of languages similar model.
20.Draw the diagram of the language processing system.(Nov/Dec,2006)

21.Difference between phase and pass.


Phase:
The compilation process into a series of sub processes are called as phase
Pass:
The collection of phase are called as pass.
PART-B
1.What is a compiler? State various phases of a compiler and explain them in detail.(16)

Lexical Analysis
The first phase of scanner works as a text scanner. This phase scans the source code as a stream of
characters and converts it into meaningful lexemes. Lexical analyzer represents these lexemes in the form of
tokens as:

<token-name, attribute-value>

Syntax Analysis
The next phase is called the syntax analysis or parsing. It takes the token produced by lexical analysis as
input and generates a parse tree (or syntax tree). In this phase, token arrangements are checked against the
source code grammar, i.e. the parser checks if the expression made by the tokens is syntactically correct.

Semantic Analysis
Semantic analysis checks whether the parse tree constructed follows the rules of language. For example,
assignment of values is between compatible data types, and adding string to an integer. Also, the semantic
analyzer keeps track of identifiers, their types and expressions; whether identifiers are declared before use or
not etc. The semantic analyzer produces an annotated syntax tree as an output.

Intermediate Code Generation


After semantic analysis the compiler generates an intermediate code of the source code for the target
machine. It represents a program for some abstract machine. It is in between the high-level language and the
machine language. This intermediate code should be generated in such a way that it makes it easier to be
translated into the target machine code.

Code Optimization
The next phase does code optimization of the intermediate code. Optimization can be assumed as something
that removes unnecessary code lines, and arranges the sequence of statements in order to speed up the
program execution without wasting resources (CPU, memory).

Code Generation
In this phase, the code generator takes the optimized representation of the intermediate code and maps it to
the target machine language. The code generator translates the intermediate code into a sequence of
(generally) re-locatable machine code. Sequence of instructions of machine code performs the task as the
intermediate code would do.

Symbol Table
It is a data-structure maintained throughout all the phases of a compiler. All the identifier's names along with
their types are stored here. The symbol table makes it easier for the compiler to quickly search the identifier
record and retrieve it. The symbol table is also used for scope management.

2.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.(16)
3.Write in detail about the analysis cousins of the compiler.(16) (May/June-2013)

1. Preprocessor.
2. Assembler.
3. Loader and Link-editor.

PREPROCESSOR
A preprocessor is a program that processes its input data to produce output that is used as input to another
program. The output is said to be a preprocessed form of the input data, which is often used by some
subsequent programs like compilers. The preprocessor is executed before the actual compilation of code
begins, therefore the preprocessor digests all these directives before any code is generated by the statements.

They may perform the following functions


1. Macro processing
2. File Inclusion
3."Rational Preprocessors
4. Language extension

1. Macro processing:
A macro is a rule or pattern that specifies how a certain input sequence (often a sequence of characters)
should be mapped to an output sequence (also often a sequence of characters) according to a defined
procedure. The mapping processes that instantiates (transforms) a macro into a specific output sequence is
known as macro expansion.  macro definitions (#define, #undef)To define preprocessor macros we can use
#define. Its format is: 

#define identifier replacement


int table2[100]; 

2.File Inclusion:
Preprocessor includes header files into the program text. When the preprocessor finds an #include directive it
replaces it by the entire content of the specified file. There are two ways to specify a file to be included: 

#include "file"
#include <file>
3."Rational Preprocessors:
These processors augment older languages with more modern flow of control and data structuring facilities.
For example, such a preprocessor might provide the user with built-in macros for constructs like while-
statements or if-statements,where none exist in the programming language itself.
4.Language extension :
These processors attempt to add capabilities to the language by what amounts to built-in macros. For
example, the language equal is a database query language embedded in C. Statements begging with ## are
taken by the preprocessor to be database access statements unrelated to C and are translated into procedure
calls on routines that perform the database access.
ASSEMBLER
Typically a modern assembler creates object code by translating assembly instruction mnemonics into
opcodes, and by resolving symbolic names for memory locations and other entities. The use of symbolic
references is a key feature of assemblers, saving tedious calculations and manual address updates after
program modifications. Most assemblers also include macro facilities for performing textual substitution-e.g.,
to generate common short sequences of instructions as inline, instead of called subroutines, or even generate
entire programs or program suites.
There are two types of assemblers based on how many passes through the source are needed to produce the
executable program.

 One-pass assemblers go through the source code once and assumes that all symbols will be defined
before any instruction that references them.
 Two-pass assemblers create a table with all symbols and their values in the first pass, then use the
table in a second pass to generate code. The assembler must at least be able to determine the length of
each instruction on the first pass so that the addresses of symbols can be calculated.

LINKERS AND LOADERS


A linker or link editor is a program that takes one or more objects generated by a compiler and combines
them into a single executable program.
Three tasks
1. Searches the program to find library routines used by program, e.g. printf(), math routines.
2. Determines the memory locations that code from each module will occupy and relocates its instructions by
adjusting absolute references
3. Resolves references among files Loader
A loader is the part of an operating system that is responsible for loading programs, one of the essential
stages in the process of starting a program. Loading a program involves reading the contents of executable
file, the file containing the program text, into memory, and then carrying out other required preparatory tasks
to prepare the executable for running. Once loading is complete, the operating system starts the program by
passing control to the loaded program code.

4.Describe how various phases could be combined as a pass in a compiler? April/May 2008
Also briefly explain Compiler construction tools . (April/May 2015)
 Lexical Analyzer
 Syntax Analyzer
 Semantic Analyzer
 Intermediate Code Generator
 Code Optimizer
 Code Generator
5.For the following expression
Position:=initial+ rate*60
Write down the output after each phase

6. Describe the following software tools i. Structure Editors ii. Pretty printers iii. Interpreters (8)
 1. Structure Editors: takes input a sequence of commands to build a source program.
  Performs text creation and modification, analyzes the program text – hierarchical structure
(check the i/p is correctly formed).
2 . Pretty Printers: analyzes the program and prints the structure of the program becomes clearly
visible.
    3.  Interpreters: Instead of producing a target program as a translation, it performs the operations
implied by the source program.

7.Elaborate on grouping of phases in a compiler.


The Grouping of Phases:
• Phases deals with logical organisation of compiler.
• In an implementation, activities from more than one phase are often grouped together.
• Front and Back Ends:
- The phases are collected into a front and a back end.
-The front end consists of phases or part of phases that depends primarily
on source language and is largely independent of the target machine.
 These normally include lexical and syntactic analysis, the creation of symbol table,
semantic analysis and intermediate code generation.
- The back end includes portions of the compiler that depend on the target machine.
 This includes part of code optimization and code generation.
8.What is difference between a phase and pass of a compiler? Explain machine dependent and machine
independent phase of compiler.
phase and pass of a compiler
Phase and Pass are two terms used in the area of compilers. A pass is a single time the compiler passes
over (goes through) the sources code or some other representation of it. Typically, most compilers have at
least two phases called front end and back end, while they could be either one-pass or multi-pass. Phase is
used to classify compilers according to the construction, while pass is used to classify compilers according to
how they operate.
Unit-II lexical analysis
Need and Role of Lexical Analyzer-Lexical Errors-Expressing Tokens by Regular Expressions-Converting Regular Expression to DFA- Minimization of DFA-
Language for Specifying Lexical Analyzers-LEX-Design of Lexical Analyzer for a sample Language.
PART-A
1. What is Lexical Analysis?
The first phase of compiler is Lexical Analysis. This is also known as 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.
2.What is a lexeme? Define a regular set. (Nov/Dec 2006)
 A Lexeme is a sequence of characters in the source program that is matched by the pattern for a token.
 A language denoted by a regular expression is said to be a regular set
2.What is a sentinel?What is its usage? (April/May 2004)
A Sentinel is a special character that cannot be part of the source program. Normally we
use ‘eof’ as the sentinel. This is used for speeding-up the lexical analyzer.
3.Mention the issues in a lexical analyzer.(May/June-2013)
There are several reason for separating the analysis phase of compiling into lexical analysis and
parsing.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. 2. Compiler efficiency is
improved. 3. Compiler portability is enhanced.
4.What is the role of lexical analysis phase?
The lexical analyzer breaks a sentence into a sequence of words or tokens and ignores white spaces and
comments. It generates a stream of tokens from the input. This is modeled through regular expressions and
the structure is recognized through finite state automata.
5. What are the possible error recovery actions in a lexical analyzer?(May/june-2012),(April/May 2015)
i. 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.
ii. Other possible error recovery actions:
 Deleting an extraneous characters
 Inserting missing characters.
 Replacing an incorrect character by a correct character.
 Transposing two adjacent characters
6.Define Tokens, Patterns and Lexemes (or) Define Lexeme.(May/June 2013&2014)
Tokens: A token is a syntactic category. Sentences consist of a string of tokens. For example number,
identifier, keyword, string etc are tokens
Lexemes: Lexeme: Sequence of characters in a token is a lexeme. For example 100.01, counter, const, "How
are you?" etc are lexemes.
Patterns: Rule of description is a pattern. For example letter (letter | digit)* is a pattern to symbolize a set of
strings which consist of a letter followed by a letter or digit.
7. Write short note on input buffering. (or).Why is buffering used in lexical analysis ? What are the
commonly used buffering methods?(May/June 2014)
Lexical analyzer scan 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 pointer forward and backward
pointer for scanning the input string. There are two types of input buffering schemes- one buffer scheme and
two buffer scheme.
8.What are the drawbacks of using buffer pairs?
i. This buffering scheme works quite well most of the time but with it amount of
look ahead is limited.
ii. Limited look ahead makes it impossible to recognize tokens in situations
Where the distance, forward pointer must travel is more than the length of buffer.
9.Define regular expressions.(or) Write a regular expression for an identifier.
Regular expression is used to define precisely the statements and expressions in the source language. For e.g.
in Pascal the identifiers is denotes in the form of regular expression as letter letter(letter|digit)*.
10. 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 r1|r2=r2|r1 | is commutative
2 r1|(r1|r3)=(r1|r2)|r3 | is associative
3 (r1 r2)r3=r1(r2 r3) Concatenation is associative
4 r1(r2|r3)=r1 r2|r1 r3 Concatenation is distributive
(r2|r3) r1=r2 r1|r3 r1 over |
5 Єr=rє=r Є is identity
6 r*=(r|є)* Relation between є and *
7 r**=r* *is idempotent

11. What is meant by recognizer?


It is a part of LEX analyzer that identifies the presence of a token on the input is a recognizer for the
language defining that token. It is the program, which automatically recognizes the tokens. A recognizer for a
language L is a program that takes an input string “x” and response “yes” if “x” is sentence of L and “no”
otherwise.
12. List the operations on languages.
 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)
13. Mention the various notational shorthands for representing regular expressions.
 One or more instances (+)
 Zero or one instance (?)
 Character classes ([abc] where a,b,c are alphabet symbols denotes the regular
expressions a | b | c.)
 Non regular sets
14. List the rules for constructing regular expressions?
The rules are divided into two major classifications(i) Basic rules (ii) Induction rules
Basic rules:
i) ε is a regular expression that denotes {ε} (i.e.) the language contains only an empty string.
ii) 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:
i) If R and S are regular expressions denoting languages LR and LS respectively then,
ii)(R)/(S) is a regular expression denoting LR U LS
iii)( R ).(S) is a regular expression denoting LR . LS
iv)(R )* is a regular expression denoting LR*.

15. Define DFA.


A DFA is an acceptor for which any state and input character has utmost one transition state that the
acceptor changes to. If no transition state is specified the input string is rejected.
16 . What is DFA ?
(i) It has no e-transitions.
(ii)For each state “S” and input symbol ‘a’, there is at most one edge labeled ‘a’ leaving
from “S”
(iii) DFA is easy to simulate.
17. What are the conditions to satisfy for NFA?
(i) NFA should have one start state.
(ii) NFA may have one or more accepting states.
(iii) ε-Transitions may present on NFA.
(iv) There can be more than transitions, on same input symbol from any one state.
(v) In NFA, from any state “S”
A. There can be at most 2 outgoing e-transitions.
B.There can be only one transition on real input symbol.
C.On real input transition it should reach the new state only.
18. Write a short note on LEX.
A LEX source program is a specification of lexical analyzer consisting of set of regular expressions together
with an action for each regular expression. The action is a piece of code, which is to be executed whenever a
token specified by the corresponding regular expression is recognized. The output of a LEX is a lexical
analyzer program constructed from the LEX source specification.
19. Define є -closure?
є -Closure is a set of states of an NFA with e-transitions, such that  -closure for some state includes all
states attainable from that state by making state transitions with  -transitions.
This is denoted by e-closure (state).
20. How to recognize tokens
Consider the following grammar and try to construct an analyzer that will return <token, attribute> pairs.
relop   < | = | = | <> | = | >
id   letter (letter | digit)*
num   digit+ ('.' digit+)? (E ('+' | '-')? digit+)?
delim   blank | tab | newline
ws   delim+
21. What is finite automate ?
A finite automate is a mathematical model that consists of-
i)Input set Σ
ii)A finite set of states S
iii)A transition function to move from one state to other.
iv)A special state called start state.
v)A special state called accept state.
The finite automate can be diagrammatically represented by a directed graph called transition graph.

22. What is the need for separating the analysis phase into lexical analysis and parsing?
i)Separation of lexical analysis from syntax allows the simplified design.
ii)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.
iii)Input alphabet peculiarities and other device specific anomalies can be restricted to the lexical analyzer.
23.Draw a transition diagram to represent relational operators.
The relational operators are:<,<=,>,>=,=,!=

24. State any reasons as to why phases of compiler should be grouped.(May/June 2014)
1.By keeping the same front end and attaching different back ends one can produce a compiler for dame
source language on different machine.
2.By keeping different fronts ends and same back end one can compile several different language on the same
machine.
25. Define a context free grammar.
A context free grammar G is a collection of the following
 V is a set of non terminals
 T is a set of terminals
 S is a start symbol
 P is a set of production rules
 G can be represented as G = (V,T,S,P)
 Production rules are given in the following form
 Non terminal → (V U T)*
26.Draw a NFA for a*|b*.(April/May-2004,Nov/Dec-2005)

27.Difference between Deterministic FA and Non Deterministic FA.


S.N NFA DFA
o
1. E-Transition is present No E-Transition
2. More than one transition for real variable Only one transition for real symbol
3. It’s not easy implemented It’s easily implemented
4. Compare to DFA not fast Can load to Faster recognizer.
28.What is the time and space complexity of NFA and DFA.
Automation Space Time
NFA O(|r|) O(|r|*|r|)
DFA O(2|r|) O(|x|)
29.Write a regular definition to represent date and time in the following format: MONTH DAY YEAR.
(April/May-2015)
MONTH--> [Jan-Dec]
DAY-->[1-31]
YEAR-->[1900-2025]
PART-B
1. Explain in detail about lexical analyzer generator.
Hints:
Definition
Typical program style
Transition diagram.
2. .Explain about input buffering.(8)
3.Explain in detail about the role of Lexical analyzer with the possible error recovery actions. (May/June-
2013) (16)
4.Describe the specification of tokens and how to recognize the tokens (16) (May/June-2013)
5.Describe the language for specifying lexical Analyzer.(16)
Hint: There is a wide range of tools for constructing lexical analyzers.
 Lex
 YACC
6.prove that the following two regular expressions are equivalent by showing that minimum state DFA’s
are same.
(i)(a|b)*#
(ii)(a*|b*)*#
7.Describe the error recovery schemes in the lexical phase of a compiler.(8) (April/May 2015)
8.Write short note on token specification.(10) Nov/Dec 2005.
9.Draw the transition diagram for unsigned numbers.(6) Nov/Dec,2006,2007
10.construct the NFA from the (a/b)*a(a/b) using Thompson’s construction algorithm.(10) May/June 2007
11.Give the minimized DFA for the following expression.(10) Nov/Dec,2006,2007
(a/b)*abb
Hints:
NFA
Є-closure computation
DFA diagram
Minimization steps
Minimized DFA
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.Write a algorithm to construct an NFA into a regular expression.(8) Nov/Dec 2010
14.a) Prove that the following two regular expressions are equivalent by showing that the minimum state
DFA’s are same.
(i) (a/b)*
(ii)(a*/b*) (April/May 2015)
Unit-III Syntax Analysis
Need and Role of the Parser-Context Free Grammars -Top Down Parsing -General Strategies-Recursive Descent Parser Predictive Parser-LL(1) Parser-Shift Reduce
Parser-LR Parser-LR (0)Item-Construction of SLR Parsing Table -Introduction to LALR Parser - Error Handling and Recovery in Syntax Analyzer-YACC-Design
of a syntax Analyzer for a Sample Language .
PART-A
1.Define parser.(or) What is the role of passer ?(April/May 2015)
A parsing or syntax analysis is a process which takes the input string w and produces either a parse
tree(syntactic structure) or generates the syntactic errors.
2.Mention the basic issues in parsing.
There are two important issues in parsing:
1) Specification of syntax.
2) Representation of input after parsing.
3. Why lexical and syntax analyzers are separated out?
The reason for separating the lexical and syntax analyzer.
 Simpler design.
 Compiler efficiency is improved.
 Compiler portability is enhanced.
4. Define a context free grammar.
A context free grammar G is a collection of the following
 V is a set of non terminals
 T is a set of terminals
 S is a start symbol
 P is a set of production rules
 G can be represented as G = (V,T,S,P)
5.Briefly explain the concept of derivation.
Derivation from S means generation of string w from S. For constructing derivation two things are important.
i. Choice of non-terminal from several others.
ii. Choice of rule from production rules for corresponding non terminal
Instead of choosing the arbitrary rules for corresponding non terminal
i. Either leftmost derivation-leftmost non terminal in a sentinel form
ii. Or rightmost derivation -rightmost non terminal in a sentinel form
6. Define ambiguous grammar.
A grammar G is said to be ambiguous if it generates more than one parse tree for some sentence of language
L (G). i.e. both leftmost and rightmost derivations are same for the given sentence.
7.What is operator precedence parser?
A grammar is said to be operator precedence if ti posses the following properties:
i. No production on the right side is є
ii. There should not be any production rule possessing two adjacent terminals at the right hand side.
8. Define the rule to develop an unambiguous grammar from ambiguous grammar.
If S->αSβSα1 | α2 | α3 | α4 |. ……| αn is an ambiguous grammar than Unambiguous grammar
becomes
S-> αSβS’S’
S’ ->α1 | α2 | α3 | α4 |. ……| αn
9.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.
Ex:-
E → E +T | T
T→T*F|F
F → (E) | id
10.Write the algorithm to eliminate left recursion from a grammar?
1. Arrange the non terminals in some order A1,A2… .An
2 for i := 1 to n do begin
for j := 1 to i-1 do
begin
replace each production of the form Ai →AjƳ by
the productions
Aiδ1γ| δ2γ| …….| δkγ,
where Ajδ1| δ2|…….| δk
are all the current Aj –productions;
end
eliminate the immediate left recursion among the Ai-- productions
end.
11.What is meant by left factoring?
Left factoring is a grammar transformation that is useful for producing a grammar suitable for predictive
parsing. The basic idea is that when it is not clear which of two alternative productions to use to expand a non
terminal A, we may be able to rewrite the A production to defer the decision until we have seen enough of the
input to make the right choice.
12.What is LR Parsers?
LR(k) parsers scan the input from (L) left to right and construct a (R) rightmost derivation in
reverse. LR parsers consist of a 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.
13. Mention the properties of parse tree?
 The root is labeled by the start symbol.
 Each leaf is labeled by a token.
 Each interior node is labeled by a non-terminal .
 If A is the Non terminal, labeling some interior node and x1, x2, x3 … .xn are the labels of the
children
14. Define Handles.
A handle of a right-sentential form Ƴ is a production A → β and a position of Ƴ where the string β may be
found and replaced by A to produce the previous right-sentential form in a rightmost derivation of Ƴ
15.What are the problems in top down parsing?
a) Left recursion.
b) Backtracking.
c) The order in which alternates are tried can affect the language accepted.
16.Define recursive-descent parser.
A parser that uses a set of recursive procedures to recognize its input with no backtracking is called
a recursive-descent parser. The recursive procedures can be
quite easy to write.
17.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.
18.What is non recursive predictive parsing?
Non recursive predictive parser can be maintained by a stack explicitly, rather than implicitly via recursive
calls. The key problem during predictive parsing is that determining the production to e applied for a non-
terminal. The non recursive parser looks up the production to be applied in a parsing table.
19.Write the algorithm for FIRST? (Or) Define FIRST in predictive parsing.
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);
20.Write the algorithm for FOLLOW? (Or) Define FOLLOW in predictive parsing.
1. Place $ in FOLLOW(S), where S is the start symbol and $ is the input right end marker.
2. If there is a production A  aBß, then everything in FIRST(ß) except for Є is placed in FOLLOW(B).
3. If there is a production A  aB, or a production AaBß where FIRST(ß) contains Є, then everything in
FOLLOW(A) is in FOLLOW(B).
21.Write the algorithm for the construction of a predictive parsing table?
Input : Grammar G
Output : Parsing table M
Method :
a) For each production A →α of the grammar, do steps b and c.
b) For each terminal a in FIRST(α) ,add A→α to M [A , a]
c) If ε is in FIRST(α) , add A→α to M[A, b] for each terminal b is FOLLOW(A). if ε is in FIRST(α)
and $ is in FOLLOW(A), and A→α to M[A , $]
d) Make each undefined entry of M be error.
22.What is LL(1) grammar?
A grammar whose parsing table has no multiply-defined entries is said to be LL(1).
23.Define handle pruning.
A technique to obtain the rightmost derivation in reverse (called canonical reduction sequence) is known as
handle pruning (i.e.) starting with a string of terminals w to be parsed. If w is the sentence of the grammar
then w=ϒn, where ϒn is the nth right sentential form of unknown right most derivation.
24.What is shift reduce parsing?
The bottom up style of parsing is called shift reduce parsing. 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.
25.What are the four possible action of a shift reduce parser?
a) Shift action – the next input symbol is shifted to the top of the stack.
b) Reduce action – replace handle.
c) Accept action – successful completion of parsing.
d) Error action- find syntax error.
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.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.
28.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.
Eg: A .XYZ AX.YZ AXY.Z AXYZ.
29.What is augmented grammar? (or) How will you change the given grammar to an augmented
grammar?
If G is a grammar with start symbol S, then G’, the augmented grammar for G, is G with a new start
symbol S’ and production S’→ S. It is to indicate the parser when it should stop and announce acceptance of
the input.
30.Left factor the following grammar:
S → iEtS | iEtSeS |a
E → b.
Ans:
The left factored grammar is,
S → iEtSS′ | a
S′ → eS | ε
E→b
31.Explain the need of augmentation.
To indicate to the parser that, when it should stop parsing and when to announce acceptance of the input.
Acceptance occurs when the parser is about to reduce by S‘→S.
32.What are the rules for “Closure operation” in SLR parsing?
If ‘I’ is a set of items for grammar G then Closure (I) is the set of items constructed from I by the following 2 rules.
(i) Initially every item in I is added to Closure (I)
(ii) If A→α .is in Closure(I) and B → to I , then add the item B → to I, if it is not already there.
Apply this until no more new items can be added to Closure (I).
33.How to make an ACTION and GOTO entry in SLR parsing table?
i. 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.
ii. 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‘.
iii. If S‘→S is in Ii then set ACTION[I, $] to “accept”‖.
34. Explain the rules for GOTO operation in LR parsing.
Goto (I, X) where I is a set of items and X is a grammar symbol.goto (I,X) is defined to be the closure of the set of all
items [A → α X ] such that [A → . X] is in I.
35. What are the rules to apply Closure function in CLR parser?
Let ‘L’ is a set of LR (1) items for grammar then 2 steps can compute Closure of I.
I. i.Initially every item in I is added to Closure (I).
ii. Consider,
A → X. BY, a
B→Z
are 2 productions and X, Y,Z are grammar symbols. Then add B → .Z ,FIRST(Ya) as the new
LR(1) item ,if it is not already there. This rule has to be applied till no new items can be added to
Closure (I).
36. Find the item I0 for the following grammar using CLR parsing method.
G: S → AS
S→b
A → SA
A→a
I0:
S→. S, $
S → .AS , $
S → .b , $
A → .SA , a|b
A → .a , a|b
S → .AS , a|b
S → .b , a|b
37. Specify the advantages of LALR.
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.
38. Mention the demerits of LALR parser.
 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.
39.What is the syntax for YACC source specification program?
Declarations
%%
Translation rules
%%
Supporting C-routines
40.Define terminal.
Terminals are the basic symbols from which the strings are formed.
41. Define Nonterminal
Nonterminal are syntactic variables that denote set of strings.
42.What are the possible actions of a shift reduce parser?
 Shift.
 Reduce.
 Accept.
 Error.
43.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.
44. Differentiate Kernel and non-Kernel items.
Kernel items, which include the initial item, S’ -> .S and all items whose dots are not at the left end. Whereas
the nonkernel items have their dots at the left end.
45. What are the components of LR parser?
 An input.
 An output.
 A stack.
 A driver program.
 A parsing table.
46. List the different techniques to construct an LR parsing table?
 Simple LR(SLR).
 Canonical LR.
 Lookahead LR (LALR).
47. Elininate left recursion from the following grammar AAc/Aad/bd/є . (May/June 2013)
AbdA'
A' aAcA' / adA' /є
48.Write the regular expression for identifier and whitespace.(Nov/Dec 2013)
An Identifier (or Name) 
/[a-zA-Z_][0-9a-zA-Z_]*/
1. Begin with one letters or underscore, followed by zero or more digits, letters and underscore.
2. You can use meta character \w (word character) for [a-zA-Z0-9_]; \d (digit) for [0-9]. Hence, it can be written
as /[a-zA-Z_]\w*/.
3. To include dash (-) in the identifier, use /[a-zA-Z_][\w-]*/. Nonetheless, dash conflicts with subtraction and is
often excluded from identifier.
An Whitespace
/\s\s/ # Matches two whitespaces
/\S\S\s/ # Two non-whitespaces followed by a whitespace
/\s+/ # one or more whitespaces
/\S+\s\S+/ # two words (non-whitespaces) separated by a whitespace
49. Eliminate the left recursion for the grammar (Nov/Dec 2013)
SAa|b
AAc|Sd|є
Sol:
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' | ε
50. Compare the feature of DFA and NFA.(May/June 2014)
1.Both are transition functions of automata. In DFA the next possible state is distinctly set while in NFA each pair of
state and input symbol can have many possible next states.
2.NFA can use empty string transition while DFA cannot use empty string transition.
3.NFA is easier to construct while it is more difficult to construct DFA.
4.Backtracking is allowed in DFA while in NFA it may or may not be allowed.
5.DFA requires more space while NFA requires less space.
51. Eliminate left recursion for the grammar.
EE+T/T, TT*F/F,F(E)/id (April/May-08,Marks 2)

AAα/β then convert it to


AβA’
A’αA’
A’є

ETE’
E’+TE’/є
TFT’
T’*FT’/є
F(E)/id
PART-B
1.(i) Construct Predictive Parser for the following grammar:(May/June-2012&13)(10)
(ii) Describe the conflicts that may occur during shift reduce parsing. (may/june-2012).(6)
2.What is FIRST and FOLLOW? Explain in detail with an example. Write down the necessary algorithm.(16)
3. 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
4. Consider the following grammar(16). (Nov/Dec-2012)
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 .
5. (i)Construct SLR parsing table for the following grammar (10). (Nov/Dec-2012,April/May-04,Marks 8)
S L=R|R L*R| id RL
(ii) Write down the rules for FIRST and FOLLOW (6). (Nov/Dec-2012)
6. . Check whether the following grammar is a LL(1) grammar
S  iEtS | iEtSeS | a
E  b
Also define the FIRST and FOLLOW procedures. (16)
7. Construct non recursion predictive parsing table for the following grammar.
EE or E /E and E/ not E / (E) / 0 /1. (Dec-12,Marks 16).
8. . i. Find the language from (4)
S → 0S1 | 0A1 A→1A0 | 10
ii.Define Parse tree , Regular Expression , Left most derivation , Right most (4)
derivation , and write example for each.
iii.Write algorithm to convert NFA from Regular expression. (4)
iv.Find the language from (4)
S→0S1 | 0A | 0 |1B | 1 A→0A | 0 B→1B|1
Hints:
i. Refer rules.
ii.Definition of parse tree, an regular expression and LMD,RMDwith examples
iii.Algorithm
9.i.Prove the grammar is ambiguous. (4)
E→E+E | E*E | (E) | id
ii. Specify the demerits of ambiguous grammar. (2)
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
Hints:
Draw more than parse tree for any expression.
It is difficult to find which parse tree is correct for evaluation
Refer rules.
Apply rules for conversion.
10. Write in detail about
i.Recursive descent parsing with algorithm.
ii.Top down parsing with algorithm.
Hints:
 Definition of recursive descent and algorithm.
 Definition of top down parser , and algorithm for top down parsing table , parsing method of top
down parser.
11. Construct predictive parsing table and parse the string NOT(true OR false)
bexpr→bexpr OR bterm | bterm
bterm→bterm AND bfactor | bfactor
bfactor→NOT bfactor | (bexpr) | true | false
Hints:
 Find FIRST and FOLLOW and
 construct table and
 parse the string.
12. . Construct CLR parsing table to parse the sentence id=id*id for the following grammar.
S→ L=R | R
L→*R | id
R→L
Hints:
 Find LR(1) items;
 Construct CLR table.
 Parse the string.
13. Parse the string (a,a) using SLR parsing table.
S→ (L) | a
L→L , S | S
Hints:
 Find LR(0) items.
 Construct SLR parsing table.
 Parse the string.
14. Construct LALR parsing table for the grammar.
E→ E+T | T
T→ T*F | F
F→ (E) | id
Hints:
 Find LR(1) items.
 Find same core items different second component .them merge it
 After merging construct LALR parsing table.
15. Write algorithms for SLR and CLR string parsing algorithm.
Hints:
 Brief introduction about SLR and CLR parsers.
 Algorithm
16.(i) Generate SLR Parsing table for the following grammar. (12)
S-->Aa|bAc|Bc|bBa
A-->d
B-->d
And parse the sentences “bdc”and “dd”.(April/May 2015)
17.(i) Write the algorithm to eliminate left-recursion and left-factoring and apply both to the following
grammar. (8)
E-->E+T|E-T|T
T-->a|b|(E) ( April/May 2015)

Unit IV Syntax Directed Translation & Run Time Environment


Syntax directed Definitions-Construction of Syntax Tree-Bottom-up Evaluation of S-Attribute Definitions- Design of predictive translator - Type Systems-
Specification of a simple type checker-Equivalence of Type Expressions-Type Conversions.

RUN-TIME ENVIRONMENT: Source Language Issues-Storage Organization-Storage Allocation-Parameter Passing-Symbol Tables-Dynamic Storage Allocation-
Storage Allocation in FORTAN.
PART-A
1.Define syntax directed definition.
Syntax directed definition is a generalization of context free grammar in which each grammar production
X→α is a associated with it a set of semantic rules of the form a:=f(b1,b2,...bk) , where a is an attribute
obtained from the function f.
2.What is mean by syntax directed definition.
It is a generalization of a CFG in which each grammar symbol has an associated set of attributes like,
synthesized attribute and inherited attribute
3.How the value of synthesized attribute is computed?
It was computed from the values of attributes at the children of that node in the parse tree.
4.How the value of inherited attribute is computed?
It was computed from the value of attributes at the siblings and parent of that node.
5.What is mean by construction of syntax tree for expression.
Construction syntax tree for an expression means translation of expression into postfix form. The nodes for
each operator and operand is created. Each node can implemented as a record with multiple fields.Following
are the function used in syntax tree for expression.
1. Mknode(op,left,right)
2. Mknode(id,entry)
3. Mkleaf(num,val)
6.What are the function of construction of syntax tree for expression? Explain.
1. Mknode(op,left,right)
This function creates a node with field operator having operator as label, and the two pointer
to left and right.
2. Mknode(id,entry)
This function creates identifier node with label id and a pointer to symbol table is given by
‘entry’.
3. Mkleaf(num,val)
This function creates node for number with label num and val is for value of that number.
7.What do you mean by DAG?
It is Directed Acyclic Graph. In this common sub expressions are eliminated. So it is a compact way of
representation. Like syntax tree DAG has nodes representing the subexpressions in the expression. These
nodes have operator, operand1 and operand2 where operands are the children of that node.

The Difference between DAG and syntax tree is that common subexpressions has more than one parent and
in syntax tree the common subexpression would be represented as duplicated subtree.
8.Construct a syntax tree and DAG for k:=k+5.

9.What is S-attributed definition?


S-Attributed Grammars are a class of attribute grammars characterized by having no inherited attributes, but
only synthesized attributes. Inherited attributes, which must be passed down from parent nodes to children
nodes of the abstract syntax tree during the semantic analysis of the parsing process, are a problem
for bottom-up parsing because in bottom-up parsing, the parent nodes of the abstract syntax tree are
created after creation of all of their children. Attribute evaluation in S-attributed grammars can be
incorporated conveniently in both top-down parsing and bottom-up parsing.
10.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. As a result, attribute evaluation in L-attributed grammars
can be incorporated conveniently in top-down parsing. Many programming languages are L-attributed.
Special types of compilers, the narrow compilers, are based on some form of L-attributed grammar. These are
comparable with S-attributed grammars. Used for code synthesis.
The Class of L-attributes can be evaluated in depth first order.
11.Define a translation scheme.
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.
12.What are the advantage of SDT?
Syntax directed translation is a scheme that indicate 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.
13.What is type checking?
Type checker verifies that the type of a construct (constant,variable,array,list,object) matches what is
expected in its usage context.
14.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
15.What are the advantages of compile time checking?
(i) It can catch many common errors.
(ii) Static checking is desired when speed is important, since it can result faster code that
does not perform any type checking during execution.
16.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.
17.Define type systems.
Type system of a language is a collection of rues depicting the type expression assignments to program
objects. An implementation of a type systems is called a type checker.
18.Write Static vs. Dynamic Type Checking
Static: Done at compile time (e.g., Java)
Dynamic: Done at run time (e.g., Scheme)
Sound 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.
19.Define procedure definition.
A procedure definition is a declaration that, in its simplest form, associates an identifier with a statement.
The identifier is the procedure name, and the statement body. Some of the identifiers appearing in a
‘procedure definition’ are special and are called ‘formal parameters ‘ of the procedure. Arguments,
known as ‘actual parameters’ may be passed to a called ‘procedure’; they are substituted for the formal in the
body.
20.Define activation trees.
A recursive procedure p need not call itself directly; p may call another procedure q, which may
then call p through some sequence of procedure calls. We can use a tree called an activation tree, to
depict the way control enters and leaves activation. In an activation tree
a) Each node represents an activation of a procedure,
b) The root represents the activation of the main program
c) The node for a is the parent of the node for b if an only if control flows from activation a to b,
and
d) The node for a is to the left of the node for b if an only if the lifetime of a occurs before the lifetime of b.
21.Write notes on control stack?
A control stack is to keep track of live procedure activations. The idea is to push the node for
activation onto the control stack as the activation begins and to pop the node when the activation ends.
22.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 eh procedure if it is in the cope of a
declaration within the procedure; otherwise, the occurrence is said to be nonlocal.
23.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.
24.What is the use of run time storage?
The run time storage might be subdivided to hold: a) The generated target code b) Data objects, and c) A
counterpart of the control stack to keep track of procedure activation.
25.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 a) Return value b)
Actual parameters c) Optional control link d) Optional access link e) Saved machine status f) Local data g)
Temporaries.
26.What does the runtime storage hold?
Runtime storage holds
1) The generated target code
2) Data objects
3) A counter part of the control stack to keep track of procedure activations.
27.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
28.What are the limitations of static allocation? (Nov/dec 2012)
a) The size of a data object and constraints on its position in memory must be known at compile time.
b) Recursive procedure is restricted.
c) Data structures cannot be created dynamically.
29What 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.
30.List the fields in activation record .(Nov/Dec 2014)
 Actual parameters
 Returned Values
 Control link
 Access link
 Saved machine status
 Local data
 Temporaries
31.What is dangling references?
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.
32.Constructed a decorated parse tree according to the syntax directed definition, for the following
input statement ( 4+7.5*3)/2. (April/May 2015)

33.Write a 3-address code for; x=*y ; a=&x. (April/May 2015)


t1:=*y
x:=t1

t1:=&x
a:=t1
34.Place the above code in Triplets and indirect Triplets.(April/May 2015)
Triple:
Op Arg1 Arg2
(0) * Y
(1) = X (0)
Indirect:
Op Arg1 Arg2 statement
11 * Y (0) 11
12 = (11) (1) 12
Triple:
Op Arg1 Arg2
(0) & x
(1) = a (0)
Indirect:
Op Arg1 Arg2 statement
11 & x (0) 11
12 = a (11) (1) 12

PART –B
1.Explain the concept of syntax directed definition.
2.Construct parse tree, syntax tree and annotated parse tree for the input string is 5*6+7;
3.Explain 1)Synthesized attribute 2)inherited attribute with suitable examples.
4.Write a syntax directed definition and evaluate 9*3+2 with parser stack using LR parsing method.
5.Consider the following CFG,
E→TR
R→+TR
R→-TR
R→є
T→num
With translation scheme to generate to generate postfix expression equivalent to the given infix
expression which is recognized by above grammar. All actions in the translation should be at the end of
each production
6.Explain the  Implementing L-Attributed SDD's
7.(i)Given the Syntax-Directed Definition below construct the annotated parse tree for the input
expression: “int a, b, c”.
D → T L L.inh = T.type
T → int T.type = integer
T → float T.type = float
L → L1, id L1.inh = L.inh addType(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).
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
8.Explain the various structures that are used for the symbol table constructions.(April/may 2012,2014)
9.(i)What are different storage allocation strategies? Explain.(8)(may/june-2013)
(ii)Specify a type checker which can handle expressions, statements and functions.(8)
10.Explain the organization of runtime storage in detail.
11.Explain about static and stack allocation in storage allocation strategies
12.Explain any 4 issues in storage allocation (4). (April/May 2015)
13.Give a Syntax directed Definitions to differentiate expressions formed by applying the arithmetic
operators + and * to the variable X and constants ; expression :X*(3*X+X*X). ( April/May 2015) (8)
14. 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 (6)
(ii) Write the 3-address code(6)
(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) (4) (April/May 2015)
18(i).Apply Back-patching to generate intermediate code for the following input.
x:2+y;
If x<y then x:=x+y;
repeat y:=y*2;
while x>10 do x:=x/2;
Write the semantic rule and derive the Parse tree for the given code (12)
(ii) What is an Activation Record? Explain how its relevant to the intermediate code generation phase
with respect to procedure declarations. (4) (April/May 2015)
Unit- V Code Optimization And Code Generation
Principal Sources of Optimization-DAG- Optimization of Basic Blocks-Global Data Flow Analysis- Efficient Data Flow Algorithms-Issues in Design of a Code
Generator - A Simple Code Generator Algorithm.
1.What is meant by optimization?
It is a program transformation that made the code produced by compiling algorithms run faster or takes less space.
2.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.
3.Mention some of the major optimization techniques.
 Local optimization
 Loop optimization
 Data flow analysis
 Function preserving transformations
 Algorithm optimization.
4.What are the methods available in loop optimization?
Code movement - Strength reduction- Loop test replacement- Induction variable elimination
5.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 this instructions by a shorter or faster sequence whenever possible. It is very useful for intermediate
representation.
6.What are the characteristics of peephole optimization?
a. Redundant instruction elimination. b. Flow of control optimization c. Algebraic simplifications d. Use of machine
idioms
7.What are the various types of optimization?
The various type of optimization is, 1)Local optimization,2)Loop optimization,3)Data flow analysis,4)Global
optimization.
8.List the criteria for selecting a code optimization technique.
The criteria for selecting a good code optimization technique are, It should capture most of the potential improvement
without an unreasonable amount of effort. It should preserve the meaning of the program. It should reduce the time or
space taken by the object program.
9.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.
10.What do you mean by induction variable elimination?
It is the process of eliminating all the induction variables , except one when there are two or more induction variables
available in a loop is called induction variable elimination.
11.List any two structure preserving transformations adopted by the optimizer?
The structure preserving transformations adopted by the optimizer are, Basic blocks.-Flow graphs.
12.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.
13.What is meant by constant folding?
Constant folding is the process of replacing expressions by their value if the value can be computed at complex time.
14.Define optimizing compilers.(Nov/Dec 2013)
Compilers that apply code-improving transformations are called optimizing compilers.
15.When do you say a transformation of a program is local?
A transformation of a program is called local, if it can be performed by looking only at the statement in a basic block.
16.Write a note on function preserving transformation.
A complier can improve a program by transformation without changing the function it compliers.
17.List the function –preserving transformation.
1)Common subexpression elimination.2)Copy propagation.3)Dead code elimination.4)Constant folding.
18.Define common subexpression.
An occurrence of an expression E is called a common subexpression if E was previously computed and the values of
variables in E have not changed since the previous computation.
19.What is meant by loop optimization?
The running time of a program may be improved if we decrease the number of instructions in a inner loop even if we
increase the amount of code outside that loop.
20.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.
State the meaning of in[s], out[s], kill[s], gen[s].
in[s]-The set of definitions reaching the beginning of S.out[s]-End of S.gen [s]-The set of definitions generated by
S.kill[s]-The set of definitions that never reach the end of S.
21.What is data flow analysis? (Nov/Dec 2012)
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.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
23.Define loop unrolling with example.(Nov/Dec 2013)
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 ();
}
23.What is constant folding?(May/June 2013)
Constant folding is the process of replacing expressions by their value if the value can be computed at complex time.
24.How would you represent the dummy blocks with no statements indicated in global data flow
nalysis?(May/June 2013)
Dummy blocks with no statements are used as technical convenience (indicated as open circles).
25.What is meant by copy propagation or variable propagation?
One concerns assignments of the form f:=g called copy statements or copies for short
Eg: 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).
26.Define busy expressions.
An expression E is busy at a program point if and only if
 An evaluation of E exits along some path P1,P2,….Pn starting at program point P1 and,
 No definition of any operand of E exists before its evaluation along the path.
27.Define code generation. or What role does the target machine play on the code generation phase of the
compiler?(April/May 2015).
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.
28.Define Target machine.
The target computer is byte-addressable machine with four bytes to a word and n-general purpose registers. R0, R1…
… … .Rn-1. It has two address instructions of the form Op, source, destination in which Op is an op-code, and source
and destination are data fields.
29.How do you calculate the cost of an instruction?
The cost of an instruction can be computed as one plus cost associated with the source and destination addressing
modes given by added cost.
MOV R0,R1 cost is 1
MOV R1,M cost is 2
SUB 5(R0),*10(R1) cost is 3
30.Define basic block. (Nov/Dec 2013)
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.
31.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.
32.Define flow graph.(Nov/Dec 2013)
Relationships between basic blocks are represented by a directed graph called flow graph.
33.What do you mean by DAG?
It is Directed Acyclic Graph. In this common sub expressions are eliminated. So it is a compact way of representation.
34.List the advantages of DAGs (Nov/Dec 2012)
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.
35.What is meant by registers and address descriptor?
Register descriptor: It contains information’ s about,1.What registers are currently in use. 2.What registers are empty.
Address descriptor: It keeps track of the location where the current value of the name can be found at run time.
36.Explain heuristic ordering for DAG?
It is based on Node Listing algorithm.
While unlisted interior nodes remain do
begin
Select an unlisted node n , all of whose parents have been listed;
list n;
While the leftmost child ‘m’ of ‘n’ has no unlisted parents and is not a
leaf do
do
{
/*since n was just listed, surely m is not yet listed */
}
begin
list m
n=m;
end
end
37.Write labeling algorithm.
if n is a leaf then
if n is the leftmost child of its parent then
LABEL(n)=1
else
LABEL(n)=0
else
begin
let n1 ,n2 ,n3,,,nk be the children of n ordered by LABEL , so
LABEL(n1)>=LABEL(n2)>=… … … LABEL(nk)
LABEL(n)=max(LABEL(ni)+i –1)
End
38.Define live variable.(Nov/Dec 2012)
A variable is live at a point in a program if its value can be used subsequently.
39.What are the different storage allocation strategies?
1) Static allocation.-It lays out storage for all data objects at compile time.
2) Stack allocation.-It manages the runtime storage as a stack.
3) Heap allocation.-It allocates and de-allocates storage as needed at runtime from a data area.
40.What is the use of Next-use information?(Nov/Dec 2013)
 If a register contains a value for a name that is no longer needed, we should re-use that register for another
name (rather than using a memory location)
 So it is useful to determine whether/when a name is used again in a block
 Definition: Given statements i, j, and variable x,
– If i assigns a value to x, and
– j has x as an operand, and
– No intervening statement assigns a value to x,
– Then j uses the value of x computed at i.
41.How liveness variable calculated.(April/May 2015)
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).
42.Write the algorithm that orders the DAG nodes for generating optimal target code?.(April/May 2015)
(1) while unlisted interior nodes remain do begin
(2) select an unlisted node n, all of whose parents have been listed ;
(3) list n;
(4) while the leftmost child m of n has no unlisted parents
and is not a leaf do
/* since n was just listed , m is not yet listed*/
begin
(5) list m;
(6) n=m
end
end
PART –B
1.Explain the principle sources of optimization in detail.
2. Discuss about the following:
i). Copy Propagation
ii) Dead-code Elimination
iii) Code motion.
3(i).Explain optimization of basic blocks.
(ii)Explain redundant common subexpression elimination.
4.Write about data flow analysis of structural programs.
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.(i)Explain the issues in design of code generator.
(ii)Explain peephole optimization.
7.Explain the simple code generator with a suitable example
8.Write detailed notes on Basic blocks and flow graphs.
9.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)
10.Explain register allocation and assignment.
11(i).Explain loops in flow graphs.
(ii).Explain Local optimization.
12.(i) Write the code generation algorithm using dynamic programming and generate code for the
statement x=a/(b-c)-s*(e+f) [Assume all instructions to be unit cost] (12)
(ii) What are the advantages of DAG representation ? Give example.(4) (April/May 2015)
13.(i)Write the procedure to perform Refister Allocation and Assignment with Graph Coloring.(8)
(ii)Construct DAG and optimal target code for the expression
X=((a+b)/(b-c))-(a+b)*(b-c)+f. (April/May 2015).(8)
14.Perform analysis of available expressions on the following code by converting into basic blocks and
compute global common sub expression elimination.
II. I:=0
III. A:=n-3
IV. If i<a then loop else end
V. Label loop
VI. B:=i_4
VII. E:=p+b
VIII. D:-m[c]
IX. E:=d-2
X. F:=I-4
XI. G:=p+f
XII. M[g]:=e
XIII. I:=i+1
XIV. A:=n-3
XV. If i<a then loop else end
XVI. Label end (April/May 2015)

15.(i) Explain Loop optimization in details and apply it to the code (10)

I:=0
A:=n-3
If i<a then loop else end
Label loop
B:=i_4
E:=p+b
D:-m[c]
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 .(6) (April/May
2015)

You might also like