0% found this document useful (0 votes)
25 views15 pages

CC LL

The document provides an overview of compiler design, including definitions and roles of translators, compilers, and interpreters. It discusses phases of compilation, lexical analysis, parsing techniques, syntax-directed definitions, and code generation and optimization. Key concepts such as tokens, grammar, and various parsing methods like top-down and bottom-up parsing are also explained.

Uploaded by

asmitananaware02
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)
25 views15 pages

CC LL

The document provides an overview of compiler design, including definitions and roles of translators, compilers, and interpreters. It discusses phases of compilation, lexical analysis, parsing techniques, syntax-directed definitions, and code generation and optimization. Key concepts such as tokens, grammar, and various parsing methods like top-down and bottom-up parsing are also explained.

Uploaded by

asmitananaware02
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/ 15

1 Introduction

1. What is translator? Give an example.


It is a system program that take as input a program written in one
programming language and produces as output a program in another language
Ex, Compiler
2.Define Compiler.
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. In
this translation process, the compiler reports to its user the presence of the
error in the source program.
3. Interpreter.
Interpreter is a language processor program that translates and execute source
code directly, without compiling it into machine code.
4. What do you mean by phase of compiler?
A phase is a logically cohesive operation that takes as input one representation
of the source program and produce as output another representation.
5. What is front-End and Back-End of compiler?
Front-End consist of those phase that depend primarily on the source program
and largely independent of the target machine.
Back-End consists of those phase that depend on the target machine language
and generally those portions do not depend on the source language, just the
intermediate language.
6. What is the main difference between phase and pass of a compiler?
A phase is a sub process of the compilation process.
combination of one or more phase into a module is called pass.
7. Define syntax of semantics.
The rules and regulation used to form a language are known as syntax. The
meaning given to a programming construct is known as semantics.
2 Lexical Analysis
# Role of Lexical Analyzer
i.Lexical analyzer is the first phase of compiler.
ii.It's main task is to read the input characters and produce as output a
sequence of tokens that the parser uses for syntax analysis, as shown in
diagram below.
iii.Lexical analyzers can be a subroutine of the parser. It receives "get next
token" command from parser, then it reads input characters until it identifies
next token.
iv. Lexical analyzers also skips the comments, white spaces in the form of
blanks, new lines, tabs.
# Lexeme : -
A lexeme is a sequence of character in the source program that is matched by
the pattern for a token.
# Input Buffering :
Here we study a two buffer input scheme that is useful when look ahead on
the input is necessary to identify tokens. Then to speed up the lexical analyzer
sentinels are used to mark the buffer end.

Working
i. Instead of invoking one read command per character, N input characters are
read into each half of the buffer with one read command.
ii. If less than N characters are left in the input stream, then those characters
are read and a special character eof is read into the buffer after the input
characters.
iii. Two pointers to the input buffer are maintained. The string of characters
between the two pointers is the current lexeme.
# Write a program to find the area of circle.

%{

#include<stdio.h>

#define Pi 3.1415

int r;

float, area;

%}

%}

[0 -9]+{

r = atoi(yytext);

area = pi* r* r;

printf("area of circle = %f", area);

return (0);}

%%

Main(){

Printf("\n enter radius \n");

yylex ();}

# Write a LEX program to find factorial of a number.

#include<stdio.h>

int fact = 1, num;

%}

%%

[0-9]+ {

num= atoi(yytext);

while (num>1)

printf ("Factorial = %d", fact);

fact num --;

return(0);}

main(){

printf("enter number");

yylex ();}
3 Parser
1. Define Parser.
Syntax analysis means to check syntax of the input statement with the help of
stream of stokes from the lexical analysis & produce parse string to sympatric
analysis which perform this task is called as parse.
2. Grammar: - Grammar is used in parser which stores syntax of statements
used in source code.
3. CFG: - Is collection of 4 topples (VTPS)
V - Variable or non-terminals (Uppercase)
T - Terminals (Lowercase)
P - set of production
S - start symbol

# Basic Terminology: -
1) Sentence: - A string of terminal derived from grammar is called as sentence.
2) Sentential Form: - A string of terminal as well as non-terminal is called as
sentential form.
3) Derivation: - This process starts from starting non-terminal It is replacement
of non-terminal by the RHS of its production rule.
4) Reduction: - This process starts from sentence. It finds out string of the
sentence which matches with LHS of any production rule. Its replacement is
called as random.
5) Syntax Free: - This is also called as parse string or derivation string. It is a
graphical representation of a sentence.
6) Ambiguous Grammar: - If there are many derivation processes which can
evaluate sentence from starting non terminal Then this grammar is called as
ambiguous grammar.
4. Top-down Parser: -
Top-down parsing is an attempt to find the leftmost derivation for an input
string. It constructs a parse tree for the input starting from the root (start
symbol) and creates the nodes of the parse tree in pre-order.
1. Top-down parser user derivation process.
2. It will not accept ambiguous grammar.
3. It will not accept left-recursive grammar.
5. Bottom-Up Parser
As we have seen in the definition part of bottom-up parser, it is constructed
beginning at the leaves (bottom) and working up towards the root (top). It
reduces an input string "w" to the start symbol of the grammar. During every
reduction step, a particular sub-string matching the right side of the
production is replaced by a symbol on the left of that production.
6. Left Recursive & left Factoring grammar: -
The given grammar is called as left recursive grammar because, when left most
symbol in RHS of production rule of any non-terminal is that non-terminal itself
then it is called as a left recursive grammar.
7. Predictive Parser (LL (1) Parser)
In many cases after writing a grammar, eliminating left recursion and after left
factoring the resulting grammar, we get the grammar that can be parsed by a
Recursive Descent Parser but this parser does not need backtracking. This type
of parser is called as Predictive Parser.
Advantages
i. It is simple and easy to implement parsing technique.
ii. The operator precedence parser is constructed by hand after understanding
the grammar. It is simple to debug.
Disadvantage: - i. It is hard to handle tokens like minus (-) which has two
different values of precedence depending on whether it is being used as binary
or unary.
ii. This technique does not take the grammar as the input and generate a
parser. Any addition or deletion of production rules would require a rewrite of
the parser.
8. LR Parser
It is the most powerful shift-reduce parsing method.
It is used to parse large class of context free grammars.
Advantages of LR Parsing
i. LR parsers can be constructed to recognize virtually all programming
language constructs for which context free grammars are written.
ii. LR parsing is most general non-backtracking shift-reduce parsing, yet it is still
efficient.
iii. The class of grammars that can be parsed using LR methods is a proper
superset of the class of grammars that can be parsed with predictive parsers.
9. YACC
i. YACC stands for Yet Another Compiler Compiler.
ii. It is a automatic parser generator utility provided by UNIX/LINUX.
4 Syntax Directed Definition
1. Define SSD
A syntax directed definition specifies the values of attributes, by associating
semantic rule with the grammar production. An attribute is any property of a
symbol.
EX. E→E1 + T E.code = E1.code ll T.code ll ‘+’

2. Synthesized attribute:
It is computed from its children or associated with the meaning of the tokens.
i.e., a synthesized attribute for a nonterminal A at a parse tree node N, is
defined by a semantic rule associated with the production at N. A synthesized
attribute at node N is defined only in terms of attribute value at the children of
N and at N itself.

3. Inherited attributes:
The attribute value of a parse-tree node is determined from attribute values of
its parent and siblings. i.e. an inherited attribute for a non-terminal A at a
parse tree node N is defined by a semantic rule associated with the production
at the parent of N. An inherited attribute at node N is defined only in terms of
attribute values at N's parent, N itself and N's siblings.

4. Dependency graph :-
If an attribute b at a node in a parse tree depends on an attribute c, then the
semantic rule for b at that node must be evaluated after the semantic rule that
defines c. The interdependencies among the inherited and synthesized
attributes at the nodes in a parse tree can be depicted by a directed graph
called a dependency graph.
5. Code Generation and optimization
1 Register Descriptor
A partial result is the value of some subexpression computed while evaluating
an expression. Partial results are maintained in CPU registers. If the number of
results exceeds the number of available CPU register, some of them have to be
moved to memory
2 Write a short note on code optimization technique
Code optimization can be significantly done in loops of the program. Inner loop
is a place where program spends large amount of time. If number of
instructions are less in inner loop the running time of the program will get
decreased to a large extend. Hence loop optimization is a technique in which
code optimization performed on inner loops.
3 Explain in detail two optimization technique with example?
i) Frequency Reduction: Execution of program can be reduced by moving code
from a part of a program which is executed very frequently (in a loop) to
another part of the program which is executed fewer times.
Example,
For i: 1 to 500 do will be transformed into
Begin Z: 200*a;
x:=k For i:= 1 to 500 do
z: 200*a; Begin
y:=Z+k; x:=k;
end y = 2 + x;
ii) Strength Reduction : The strength reduction optimization replaces the
occurrences of a time consuming operation or high strength operation by
occurrences of faster operation or low strength operation.
For example,
For (i=1; i<=50; i++){…
count = i*7; …}
4) Define Directed Acyclic Graph (DAG).
In mathematics, particularly graph theory, and computer science, a directed
acyclic graph (DAG) is a directed graph with no directed cycles. That is, it
consists of vertices and edges (also called arcs), with each edge directed from
one vertex to another, such that following those directions will never form a
closed loop.

5) Define basic block.


In compiler construction, a basic block is a straight-line code sequence
with no branches in except to the entry and no branches out except at the
exit. This restricted form makes a basic block highly amenable to analysis.

6) Define Flow graph

Flow graph is a directed graph. It contains the flow of control information for
the set of basic block. A control flow graph is used to depict that how the
program control is being parsed among the blocks. It is useful in the loop
optimization.
1) Define Operand descriptors.
The operand descriptor (OD) describes the value and attributes of an
operand. A typical HISC instruction consists of an operation code, and indexes
to source and destination operands referenced by operand descriptors.
2) Define Annotated parse tree.
AN ANNOTATED PARSE TREE is a parse tree showing the values of the
attributes at each node. The process of computing the attribute values at the
nodes is called annotating or decorating the parse tree.
3) State True or False. Bottom-up parsing uses the process of derivation.
Bottom-up parsing can be defined as an attempt to reduce the input string
w to the start symbol of grammar by tracing out the rightmost derivations
of w in reverse. Eg. Classification of bottom up parsers A general shift
reduce parsing is LR parsing.
4) Define cross compiler.
A cross compiler is a compiler capable of creating executable code for a
platform other than the one on which the compiler is running. For
example, a compiler that runs on a Windows 7 PC but generates code
that runs on Android smartphone is a cross compiler.
5) State True or False. The yywrap( ) lex library function by default always
return 1.
If yywrap () returns false (zero), then it is assumed that the function has
gone ahead and set up yyin to point to another input file, and scanning
continues. If it returns true (non-zero), then the scanner terminates,
returning 0 to its caller. Note that in either case, the start condition
remains unchanged; it does not revert to INITIAL
6) List the techniques used in code optimization.
Compile Time Evaluation
Common Sub-Expression Elimination
Code Movement
Dead Code Elimination-
7) What is the purpose of augmenting the grammar?
The augmented grammar adds a new starting non-terminal S ′ with the sole
production S ′ → S. This helps in detecting acceptance: If you reduce by this
particular production (to the non-terminal S ′), you are accepting. To reduce to
the start non-terminal of the original grammar tells you nothing, it might
appear on some right hand side.
8) Define one pass & Multipass compilers
A one-pass compiler is a compiler that passes through the source code of each
compilation unit only once. A multi-pass compiler is a type of compiler that
processes the source code or abstract syntax tree of a program several times.
A one-pass compilers is faster than multi-pass compiler.
9) Give the name of the file which is obtained after compilation of the lex
program by the Lex compiler.
The lex compiler transforms lex.l to a C program known as lex.yy.c. •
lex.yy.c is compiled by the C compiler to a file called a.out.
10) What is the output of LEX program?
It takes as its input a LEX source program and produces lexical Analyzer as its
output. Lexical Analyzer will convert the input string entered by the user into
tokens as its output. LEX is a program generator designed for lexical processing
of character input/output stream.
11) List the phases of compiler.
Lexical Analyzer.
Syntax Analyzer
Semantic Analyzer.
Intermediate Code Generator.

12) Which parser is most powerful parser in Bottom up parser?


The LR parser is a non-recursive, shift-reduce, bottom-up parser. It uses a wide
class of context-free grammar which makes it the most efficient syntax analysis
technique.
13) What is the purpose of augmenting the grammar?
The augmented grammar adds a new starting non-terminal S ′ with the sole
production S ′ → S. This helps in detecting acceptance: If you reduce by this
particular production (to the non-terminal S ′), you are accepting. To reduce to
the start non-terminal of the original grammar tells you nothing, it might
appear on some right hand side.
14) Define the term token.
TOKEN is the smallest unit in a ‘C’ program. It is each and every word and
punctuation that you come across in your C program. The compiler breaks a
program into the smallest possible units (Tokens) and proceeds to the various
stages of the compilation.
15) State True or False. An SDD is S-attributed if every attribute is
synthesized.
In an S-attributed SDD, attributes all attributes are synthesized - S-attribute
definition. An L-attributed grammar is a grammar that node dependency graph
of any of its production rules has data-flow arrow pointing from an attribute to
an attribute to its left. Such grammars allow attributes to be evaluated in a
left-to-right traversal.
16) State any two functions of Lex library
1. Firstly lexical analyzer creates a program lex.1 in the Lex language. Then Lex
compiler runs the lex.1 program and produces a C program lex.yy.c.
2. Finally C compiler runs the lex.yy.c program and produces an object program
a.out.
3. a.out is lexical analyzer that transforms an input stream into a sequence of
tokens.

17) Define the term bootstrapping.


Bootstrapping describes a situation in which an entrepreneur starts a
company with little capital, relying on money other than outside
investments. An individual is said to be bootstrapping when they attempt to
found and build a company from personal finances or the operating revenues
of the new company.
18) Name the conflict which is not possible in LR parser.
If any conflict (either shift/reduce or reduce/reduce) is found during LALR (k)
construction, then the grammar is not LALR (k). State merging from LR (1)
to LALR (1) cannot produce a shift/reduce conflict, so if there is one, the
grammar isn't LR (1) either. But it can produce a reduce/reduce conflict.
19) What is handle pruning?
HANDLE PRUNING is the general approach used in shift-and-reduce parsing. A
Handle is a substring that matches the body of a production. Handler eduction
is a step in the reverse of rightmost derivation.
20) Define the term handle.
Handle In the online world, a handle is another word for a username. It can
refer to the name you use in chat rooms, web forums, and social media
services like Twitter.

1. Write a LEX Program which identifies the tokens like id, if, for and while.
%{
#include <stdio.h>
%}
^[a - z A - Z _][a - z A - Z 0 - 9 _] * printf("Valid Identifier");
^[^a - z A - Z _] printf("Invalid Identifier");
.;
%%
main()
{
yylex();}

2) What is an operator grammar?


An operator grammar is a context-free grammar in which there are no
consecutive non-terminals in any right-hand side. (Intuitively, every
production has an operator, as in the grammar for mathematical
expressions.
3) Write a LEX Program for calculating the cube of a given number.
%{

int op = 0,i;

float a, b;

%}

dig [0-9]+|([0-9]*)"."([0-9]+)

add "+"

sub "-"

mul "*"

div "/"

pow "^"

ln \n

%%

{dig} {digi();}

{add} {op=1;}

{sub} {op=2;}

{mul} {op=3;}

{div} {op=4;}

{pow} {op=5;}

{ln} {printf("\n The Answer :%f\n\n",a);}

%%

digi(){

if(op==0)

a=atof(yytext);

else{

b=atof(yytext);

switch(op){

case 1:a=a+b;

break;

case 2:a=a-b;

break;
case 3:a=a*b;

break;

case 4:a=a/b;

break;

case 5:

for(i=a;b>1;b--)

a=a*i;

break;}

op=0;}}

main(int argv,char *argc[]){

yylex();}

yywrap(){

return 1;}

4) Consider the expression a=b*(-c)+b*(-c) Give i) Triple representation


ii) Quadruple representation.
t1 = uminus c
t2 = b * t1
t3 = uminus c
t4 = b * t3
t5 = t2 + t4
a = t5

You might also like