TP 1
TP 1
numbers.
%{ %{
# include<stdio⋅h> # include<stdio⋅h>
int sum, x, a [50]; int i, x, sum = 0;
%} %}
%% %%
[0 – 9]+ { [0 – 9]+
x=atoi(yy text); {
sum=0; printf ("\n"); x=atoi (yytext);
for (i=0; i<x; i++) for (i=0; i<=x; i++)
{ printf ("\enter number"); {
scanf ("%d", &a[i]); sum=sum+i;
sum + = a[i]; printf ("%d", i);
} }
printf ("sum is /n=", sum); prinft ("The sum of first %d
return (0); numbers is %d /n"; x, sum);
} return (0);
%% }
main() %%
{ main()
printf ("enter how many {
numbers"); printf ("\enter number \n");
yylex(); yylex();
} }
yyerror() {
printf ("error"); }
yywrap() {
return (1);
}
count number of vowels and number of words per line
%{ # include <stdio·h> [ ]+ {wno ++;}
int 1cnt = 0, vcnt = 0, wno = 0; [aeiouAEIOU] {vcnt ++;}
%} ·;
%% %%
[ · ]+ {1cnt ++; wno ++; printf main() {
("1cnt = % d vowel = % d printf ("Enter input : \n");
word = % d \n", lcnt, vcnt, yylex();
wno); printf (" \n Total lines = % d",
vcnt = 0; wno = 0; } 1cnt); }
A lex program to find area of identifies tokens like id, if and
square. for.
%{ %{
# include<stdio⋅h> include <stdio.h>
int a; %}
%} %%
%% [a-zA-z] [a-zA-z0-9]*
[0 – 9]+ { return id; }
{ [iI] [fF]
s = atoi (yytext); { return if; }
a=s*s [Ff] [Oo] [Rr]
printf ("\n area of square is { return for; }
\n%d", a); %%
return (0); main()
} {
%% printf ("\n Enter word");
main() yylex();
{ }
printf ("enter side \n");
yylex();
}
%{ getchar();
#include<stdio.h> retrun 0;
int number, cube; }
%} int yywrap(void)
%% {
[0-9]+ { return 0;
number-atoi(yytext); }
cube=number* number*
number;
printf("\n Cube of given number
% is = %d", number,cube);
retum (0);
}
%%
int main()
{
printf("\n Please enter any
integer value: ");
yylex();
SLR Parser LALR Parser CLR Parser
1. SLR parser is 1. LALR parser is 1. CLR parser is
very easy and also easy and cheap expensive and
cheap to implement. to implement. difficult to
implement.
2. SLR parser is the 2. LALR and SLR
smallest in size. have the same size. 2. CLR parser is the
largest.
3. Error detection is 3. Error detection is
not immediate in not immediate in 3. Error detection
SLR parser. LALR parser. can be done
immediately in CLR
4. SLR parser fails 4. It is intermediate parser.
to produce a in power between
parsing table for a SLR and CLR i.e., 4. It is very powerful
certain class of SLR ≤ LALR ≤ CLR. and works on a
grammars. large class of
5. LALR parser grammar.
5. SLR parser requires more time
requires less time and space 5. CLR parser also
and space complexity. requires more time
complexity. and space
complexity.
6] It starts with the start symbol. 6]It ends with the start symbol.
10] Builds the parse tree top- 10] Builds the parse tree
down. bottom-up.
(a) What is cross compiler.
a compiler which can convert instructions into machine code or
low-level code for a computer other than that on which it is run.
(b) What is the purpose of flow graph?
Flow graph shows how control is flowing between basic blocks.
ii) It is also used to detect loops and can be used for global code
optimization
(c) List any two lex library functions.
yyerror(): Indicates the error in lex programs.
yylex(): It scans number of lines from the keyboard
yywrap(): If the value returned from yywrap() is 1, it indicates that
no further input is available.
(d) Define Memory Binding.
A data item is associated to some address of a memory area is
called memory binding.
(e) Define the term Dead Code.
Code which can be omitted from a program without affecting its
results is called dead code.
(f) Terminals can have synthesized attributes, but not inherited
attributes. State true or false.
True Terminals are tokens which are recognized by lexical
analyzer. So attributes of terminal is synthesized by lexical
analyzer.
(g) Define the term lexeme.
A lexeme is a sequence of characters in the source program
that is matched by the pattern for a token.
(h) Define SDD. (Syntax Directed Definitions)
A SDD is a context free grammar in which the productions are
associated with semantic rules.
(i) List the different types of conflicts that occur in LR Parser.
Shift Reduce conflict. ii )Reduce Reduce conflict
(b) List the phases of compiler.
Lexical analysis, Syntax analysis, Semantic analysis,
Intermediate code generation, Code optimization, Target code
generation.
(c) Which parser is more powerful parser in Bottom up parser ?
CLR is most powerful parser among bottom up parser
(d) What is the purpose of augmenting the grammar?
Purpose of augmenting the grammar is to indicate parser where
to stop parsing and announce acceptance of input sentence
(e) Define the term Token.
Token is a sequence of fcharacters that can be treated as a
single logical entity. Typical tokens are, 1) Identifiers 2) Keywords
3) operators 4) special symbols 5) constants
(g) State any two functions of Lex Library.
yyerror): Indicates the error in lex programs
yyles(): It scans number of lines from the keyboard.
yywrap(): If the value returned from yywrap() is 1. it indicates that
no further input is available.
(h) Define the term Bootstrapping.
Bootstrapping is the technique for producing a self-compiling
compiler i.e, it is a process in which we write a compiler for a
language L using same language L
(1) Name the conflict which is not possible in LR Parser.
Shift-Shift Conflict
(1) What is handle pruning?
Handle pruning is a parsing process where input sentence is
reduced to starting not terminal (NT) of grammar. ii )If A→B is a
production then reducing ẞ to A by the given production is called
handle pruning i.e., removing the children of A from the parse
tree.
1) What is translator? Give one example.
Translator is a Program that converts program/text from one
language to another. Example: Ccompiler, Assembler
2) What are the advantages of bootstrapping?
-To Improve quality of compiler -We need to know only 1
language as same language is used for writing compiler and
source language to be translated. - It also helps in implementing
cross compiler
3) Define cross compiler.
A compiler running on one machine and generating code for
other machine.
4) What is boot strapping?
It is a process in which we write a compiler for a language L
using same language L
5) State true or false: symbol table is not required in all phases of
compilers.
False, it can be required for all phases of compiler.
6) List all the phases of compiler in sequence.
Lexical analysis, syntax analysis, semantic analysis,
intermediate code generation, code optimization, target code
generation.
9. No conflict occur.
Single-Pass Compiler Multi-Pass Compiler
1]. A one-pass complier is that 1. A multi-pass compiler is a
passes through the source type of complier that processes
code of each compilation unit the source code of a program
only once. several times.
4
2
find factorial of a given total words, lines and characters
number. from input end with $.
%{ %{
# include<stdio⋅h> # include<stdio⋅h>
int i, fact, n; int c=0, w=0, 1=0;
%} %}
%% %%
[0 – 9]+ [\t]+
{ n = atoi (yytext); {
for (i=1; i<=n; i++) w++; c+=strlen (yytext); }
fact = fact * i; [⋅\n] {
printf ("Factorial do no. % d is l++, w++}
%d\n", n, fact); [$] {
return (0) printf ("\n\n\t total characters :
%% % d \n
main() \t Total words : \t%d\n Total
{ printf ("\n Enter number"); lines : %d
yylex(); \t \n", c, w, l);
} return (0);
}
%%
main()
{
yylex();
}
find total vowels from the input.
%{ %%
# include<stdio⋅h> main()
int w=0, vc=0; {
%} printf ("enter input \n");
%% yylex();
[\t]+ }
{w++;]
[a e i o u A E I O U] { vc ++; }
[⋅] +
{
printf ("\n\n Total vowels %d
is", vc;}
⋅;
Q] Define Directed Acyclic Graph (DAG). 12
a Directed Acyclic Graph (DAG) is a special kind of abstract
syntax tree (AST) where a unique node is present for every unique
value. ii) DAG is a directed acyclic graph used to represent
intermediate code in efficient way by eliminating common sub
expressions.
Q] What is an operator grammar?
A grammar with following characteristics is called operator
grammar: No E-transition i.e. E is not present on right hand side
of any production. ii) No production has two adjacent non-
terminals
Q] Write a short note on SDD (Syntax Directed Definitions).
A context free grammar, in which the productions are shown
along with its associated semantic rules, is called as a Syntax
Directed Definition (SDD). ii) Attributes are associated with
grammar symbols and rules are associated with productions. The
attributes can be written as the grammar symbol followed by a dot
and attribute name.e.g., the 'type' attribute of identifier list can be
written as identifier_list.type iii)The value of attribute may be
numbers, types, tables or strings. e.g., the 'type' attribute of the
identifier list can be an integer, a character or real. We can
associate a semantic rule "identifier_list.type =
type_spec.data_type". iv)Types of attributes:-
a) Inherited attributes:-
1) It is defined by the semantic rule associated with the production
at the parent of node. 2) Attributes values are confined to the
parent of node, its siblings and by itself. 3) The non-terminal
concerned must be in the body of the production.
b) Synthesized attributes:-
It is defined by the semantic rule associated with the production at
the node. 2) Attributes values are confined to the children of node
and by itself. 3) The non terminal concerned must be in the head of
production. 4) Terminals have synthesized attributes which are
the lexical values (denoted by lexval) generated by the lexical
analyzer. 5) A SDD that uses only synthesized attribute is called S-
attributed.
(j) What is the use of dynamic pointer. 10
Dynamic Pointers are sued for deallocating Activation Record
(AR).
2) Strength Reduction:-
As the name suggests, this technique involves reducing the
strength of the expressions by replacing the expensive and costly
operators with the simple and cheaper ones.
Example:
Code before optimization Code after optimization
B=Ax2 B=A+A
Here, the expression “A * 2 “ has been replaced with the
expression "A+A" because the cost of multiplication operator is
higher than the cost of addition operator.
(a) What is the output of the lex program?
Lex program generates a lexical analyzer program in file
"lex.yy.c", which is a lexical analyzer program fof your program
Synthesized Attributes Inherited Attributes
1] An attribute is said to be 1] An attribute is said to be
synthesized attribute if its inherited attribute if its parse
parse tree node value is tree node value is determined
determined by the attribute by the attribute value at parent
value at child nodes. and/or siblings node.
7) State the pattern for regular expression" set of all strings of a's
and b's of length 2", in lex language notation.
[ab][ab]
L-Attributed Definition
• L-Attributed Definitions contain both synthesized and inherited
attributes but do not need to build a dependency graph to evaluate
them. • The idea behind L-Attributed Definitions, between the
attributes associated with a production body, dependency-graph
edges can go from left to right, but not from right to left (hence "L-
attributed"). • The classes of syntax-directed definitions whose
attributes can always be evaluated in depth-first order are called
L-Attributed Definitions. • The L in the L-Attributed Definitions
stands for left because attribute information appears to flow from
left to right.
S-Attributed Definition
• The SDD is S-attributed if every attribute is synthesized. A
syntax-directed translation is called S-attributed if all its attributes
are synthesized. • For S-attributed SDD, the attributes are
evaluated in bottom-up order of the nodes of the parse tree. • The
attributes are evaluated by using postorder traversal of the parse
tree. • Since, bottom-up parsing uses postorder traversal, S-
attributed definitions can be implemented during bottom-up
parsing or LR parsing. • Synthesized attributes can be evaluated
by a bottom-up parser as the input is being parsed. • The parser
can keep the values of the synthesized attributes associated with
the grammar symbols on its stack.