CSE382-compiler Design Lab Manual
CSE382-compiler Design Lab Manual
Engineering
KALASALINGAM UNIVERSITY
(Kalasalingam Academy of Research and Education)
Anand Nagar,Krishnankoil-626126
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LAB MANUAL
COMPILER DESIGN LAB
(CSE382)
Course Coordinator
Programme Coordinator
Module Coordinator
HOD/CSE
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
UNIVERSITY
VISION
To be a Center of Excellence of International Repute in Education and Research.
MISSION
To Produce Technically Competent, Socially Committed Technocrats and
Administrators through Quality Education and Research.
DEPARTMENT
VISION
To become a Centre of Excellence in Teaching and Research in the field of
Computer Science and Engineering.
MISSION
To prepare the students for a prospective career in IT industry and for higher
learning by imparting sound technical knowledge.
To carry out research in cutting edge technologies in computer engineering to
meet the requirement of the industry and society.
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
KALASALINGAM UNIVERSITY
(Kalasalingam Academy of Research and Education)
Anand Nagar, Krishnankoil - 626126
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
COURSE PLAN EVEN SEMESTER 2014-2015
Subject with code
Course
Semester / Sec
Course Credit
Course Coordinator
Module Coordinator
Programme Coordinator
Prerequisites:
Course Description
This course provides students with a comprehensive study of how to design compilers.
The students will be able to design the different phases that include lexical analyzer,
syntax analyzer, semantic analyzer, code optimizer and code generator.
This course also emphasis on problem solving and implementation of code and
to optimize the code using a universal subset of the C programming language. Students
will also learn how to construct their own compiler using the model tools .
Career Opportunities:
The student completing the course should have gained an understanding of the
major ideas and techniques in compiler writing and a further development of
programming skills .To participate actively in the group term project. To construct a
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
program that act as a recognizer for the set of strings defined by a regular expression or
context-free grammar.
Course Outcomes(COs):
CO1:
CO2:
CO3:
CO4:
PO3: Capability to design and develop computing systems to meet the requirement of
industry and society with due consideration for public health, safety and environment.
PO4: Ability to apply knowledge of design of experiment and data analysis to derive
solutions in complex computing problems.
PO5: Ability to develop and apply modeling, simulation and prediction tools and
techniques to engineering problems.
PO6: Assess and understand the professional, legal,
responsibilities relevant to computer engineering practice.
security
and
societal
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
PO12
S
S
S
CO and PO Mapping
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 S
CO2 S
S
S
CO3 S
S
S
S
CO4
M
L
S- Strong Correlation M- Medium Correlation L Low Correlation
Content Delivery / Instructional Methodology
1.
2.
List of Experiments
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Cumulative
Number of
Periods
S.No
Experiment Details
Number
of Periods
12
18
21
24
27
4
5
6
7
8
9
36
10
39
11
42
12
45
ASSESSMENT METHODOLOGIES:
Direct
Indirect
Mini Projects
ASSESSMENT
Internal assessment = 30 marks
Mini Project
= 20 marks
External assessment = 50 marks
--------------Total = 100 marks
--------------Evaluation Procedure for Internal and External Assessment:
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Modules
Very good
Level
Of Very
Clear
understanding the understanding
Formal language , (20 - 25)
regular expression
and LEX tool
Satisfied
Poor
Able to convert Poor
into
understanding
RE to NFA and (0 - 9)
NFA to RE
(10 - 19)
Optimization
techniques
Acceptable
Poor Level
level of
time (0 - 10)
and
space
complexity in
optimization
(11 - 29)
Output
Viva questions
With
good
time
and
space
complexity
in
optimization
(30 - 40)
Output
as
expected
(15 - 25)
Output is not
upto
the
expectation
level (6 - 14)
Answered for Answered for
more
than 70% - 90% of
90% of the the questions.
questions
(5 - 8)
(9 - 10)
Partial
(0 - 5)
Modules
Level
Of
understanding
the
Formal
language
,
regular
expression
and LEX tool
Optimization
techniques
Out put
S.No Experiments
1
2
3
Implementation
lexical analyzer
of
Efficiency of Efficiency
Algorithm
of program
Output
Viva
Voice
20
50
25
50
20
50
20
Simulation of Symbol
25
table Management
Construction of a NFA
from
a
regular 25
expression
5
5
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
R
egular Expression to
NFA
Convert NFA to
DFA
Find the
first and
follow of a given
context free grammar
Use JFLAP simulator to
construct a LR Parser
table and Predictive
parser table for a
grammar.
Implementation of shift
reduce
parsing
Algorithm
Implementation
of
Operator
precedence
Parsing Algorithm
Generation of a code for
a given intermediate
code
5
6
7
8
9
10
11
12
20
45
25
25
50
20
20
55
15
10
25
50
20
25
40
25
10
25
50
20
55
15
10
55
15
10
55
15
10
10
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Ex.no .1
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Algorithm:
Step 1: Declare the necessary variables.
Step 2: Declare an array and store the keywords in that array
Step 3: Open the input file in read open
Step 4: read the string from the file till the end of file.
o If the first character in the string is # then print that string as header file
o If the string matches with any of the keywords print that string is a
keyword
o If the string matches with operator and special symbols print the
corresponding message
o If the string is not a keyword then print that as an identifier.
Input:
#include<stdio.h>
void main()
{
int a;
double b;
char c;
printf("%d %b %c",a,b,c);
}
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Output:
#include<stdio.h>
header file
void
Keyword
main
Keyword
(
Left Parenthesis
)
Right Parenthesis
{
Open Brace
int
Keyword
a
Identifier
;
Semicolon
b
Identifier
;
Semicolon
char
Keyword
c
Identifier
(
Left Parenthesis
%c
Control string
,
Comma
a
Identifier
,
Comma
a
Identifier
,
Comma
)
Right Parenthesis
;
Semicolon
}
Close Brace
Out come:
After completing these exercise, student can able to know about the lexical analysis task.
Sample Viva-Voce questions:
1.
2.
3.
4.
5.
6.
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
The experiments mentioned above (1) address the following COs : CO1,CO2
Ex.no .2
Symbol table Management
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Aim
To create and print a symbol table that contains the name, type and size of the identifier
from a C file.
Theory:
Symbol table is a data structure used by a language translator such as a compiler or
interpreter, where each identifier in a program's source code is associated with
information relating to its declaration or appearance in the source, such as its type, scope
level and sometimes its location.
Algorithm
Step 1: Declare the necessary variables.
Step 2: Open the file that contains the simple C program.
Step 3: Read word by word (using fscanf function) from the file and perform the
following operations until the end of file is reached.
a)
Compare the word with the data types that are supported by C.
b)
Then store the type, size (according to the data types in C) and name of the
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
SYMBOL TABLE
SYMBOL NAME
TYPE
first
second
c
int
double
char
Outcome:
After completing these exercises student can able to solve the problems using file.
Sample Viva-Voce questions:
1.
2.
3.
4.
5.
6.
Define typedef.
7.
Ex.no .3
Regular Expression to NFA Conversion
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Aim:
To convert the given regular expression to NFA
Theory
Regular expression:
For lexical analysis, specifications are traditionally written using regular expressions: An
algebraic notation for describing sets of strings.
Figure shows the constructions used to build regular expressions and the languages they
describe:
Regular
expression
a
Language
(set of strings)
{{}a}
{}
s|t
st
L(s) [ L(t)
{vw | v 2 L(s),w 2 L(t)}
s*
Informal description
The set consisting of the oneletter
string a.
The set containing the empty
string.
Strings from both languages
Strings constructed by concatenating
a string from the
first language with a string
from the second language.
It consists of the empty string plus
whatever can be obtained by concatenating
a string from L(s) to a string from L(s_).
This is equivalent to saying that
L(s_) consists of strings that can be
obtained by concatenating zero or more
(possibly different) strings from L(s)
NFA
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Start:
Apply the following rules to the current transition diagram until all the edges are labeled
by characters from or . The nodes on the left sides of the rules are identified with
nodes in the current transition diagram. All newly occurring nodes on the right side of a
rule correspond to newly created nodes and thus to new states.
Input:
Enter the regular expression : (a/b) abb
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Output
e
Start
a
3
1
e
6
4
a
7
b
8
Outcome:
After completing these exercise the student can able to solve the problem using regular
expression
Sample Viva-Voce questions:
1. Draw a NFA for a*|b*.
2. Comment Handle Pruning?
3. Comment viable prefixes?
4. Define handle.
5. List the algebraic properties of regular expressions?
6. Comment finite automata?
7. List the goals of error handler in a parser?
Experiments addressing COs:
The experiments mentioned above (3) address the following COs : CO2,CO3
Ex.no .4
Regular Expression to NFA
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Aim
Using JFLAP simulator convert Regular Expression to NFA
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Theory
The JFLAP (Java Formal Language and Automata Package) is a visual tool used to create
and simulate various types of robots, and convert different representations of
languages.
Regular languages - create
DFA
NFA
regular grammar
regular expression
push-down automaton
context-free grammar
parser
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Definition
A regular expression is another representation of a regular language, and is defined over
an alphabet (defined as ). The simplest regular expressions are symbols from , , and
symbols from . Regular expressions can be built from these simple regular expressions
with parenthesis, in addition to union, Kleene star and concatenation operators. In FLAP,
the concatenation symbol is implicit whenever two items are next to each other, and it is
not explicitly stated. Thus, if one wishes to concatenate the strings grass and hopper,
simply input grasshopper. The following is how JFLAP implements other special
symbols for regular expressions:
( , ) are used to help define the order of operations
* is the Kleene star
+ is the union operator
! is used to represent the empty string.
The following are a few examples of regular expressions and the languages generated
using these operators:
1.
b, c}
a+b+c = {a,
4.
abc = {abc}
5.
...)
2.
3.
abc}
(!+a)bc = {bc, 6.
(a+b)* = (, a, b, aa, ab, ba,
bb, aaa, ...)
7.
a+b* = (a, , b, bb,
bbb, ...)
8.
a+!* = (a, )
9.
(a+!)* = (, a, aa,
aaa, aaaa, ...)
Since all regular languages accept finite acceptors, a regular expression must also be able
to accept a finite automaton. There is a feature in JFLAP that allows the conversion of a
regular expression to an NFA.
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Converting to a NFA
After typing in an expression, there is nothing else that can be done in this editor window
besides converting it to an NFA, so let's proceed to that. Click on the Convert
Convert to NFA menu option. If one uses the example provided earlier, this screen
should come up (after resizing the window a little).
Now, click on the (D)e-expressionify Transition button (third from the left, to the
immediate left of the Do Step button). Then, click on the transition from q0 to q1.
You should now see, perhaps with a little resizing, a screen like this...
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
You're probably wondering what exactly you just did. Basically, you broke the regular
expression into three sub-expressions, which were united into one expression by the
implicit concatenation operator. Whenever you click on an expression or sub-expression
through this button, it will subdivide according to the last unique operation in the order of
operations. Thus, since concatenations are the last operation to be performed on the given
expression, the expression is divided according to that operator.
Let's continue. Click on the second button from the left, the (T)ransition Creator
button. Now, let's make our first transition. Create a transition from q0 to q2. You
will not be prompted by a label, as in this mode only transitions are created. These
types of transitions are all that are needed to create a nondeterministic automaton. When
finished, you should see a transition between q0 and q2. Now, try to create a
transition from q0 to q4. You will be notified that such a transition is invalid. This is
because, due to the concatenation operation between sub-expressions a* and b, any
b must first process the a* part of the NFA. While it is possible to go to q4 without
processing any input, that will have to wait until the a* expression is decomposed.
Thus, in order to get to q4, we need to go through q3, the final state of the a*
expression. If you create a transition from q3 to q4, it will be accepted.
Since (a+b) is the last expression, we will get to the final state after processing it.
Because of this, try to establish a transition from q7 to q1. However, you should be
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
warned that although the transition may be correct, you must create the transitions in the
correct order. Thus, add the transition from q5 to q6 before adding the q7 to q1
transition. When done, your screen should resemble the one below.
Now, decompose the a* expression. Two new states should be created, q8 and q9,
and you should add transitions from q2 to q8, q9 to q3, q2 to q3, and q3 to
q2. You may wonder why we cannot just add a transition from q0 to q3. While
such a transition is legal, JFLAP forces the transition to go through q2, because that is
the starting state for the a* expression. Continue decomposing the expression using the
tools provided. If you are stuck, feel free to use the Do Step button, which will perform
the next step in the decomposition. If you wish to simply see the completed NFA, press
the Do All button. You will probably have to move the states around in the screen
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
and/or resize the screen so the decomposition looks good, whichever option you choose.
In any event, when done, you should see a screen resembling the one below. Notice the
indeterminate fork through q6, representing the union operator, where one can process
either an a or a b transition. When finished, click the "Export" button, and you now
have a nondeterministic finite automaton that you may use as you see fit.
Outcome:
After completing these exercise the student can able to solve the problem using JFLAP
tools.
Sample Viva-Voce questions:
1. Define JFLAP.
2 Comment viable prefixes?
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
3. Define handle.
4.List the algebraic properties of regular expressions?
5.Comment finite automata?
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Ex.no .5
Find the FIRST and FOLLOW of a given context free grammar
Aim
Write a C Program to find first and follow of a given context free grammar
Theory
Computing the Function FIRST
To compute FIRST( X ) for all grammar symbols X apply the following rules until no
more terminals or can be added to any FIRST set:
is in all of
place a in
FIRST(Y1 ),
place $ in FOLLOW( S ) where S is the start symbol and $ is the input right
marker;
is placed in FOLLOW( B );
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
contains ,
Example: Find the FIRST and FOLLOW for the following grammar:
P bSe
S AR
R AR |
A id = E ;
E FT
T +FT |
F (E) | id
The computation of the functions FIRST and FOLLOW:
FIRST( P ) = { b }
FOLLOW( P ) = { $ }
/ according to rule 1
FIRST( S ) = { id }
FOLLOW( S ) = { e }
/ according to rule 2
FIRST( R ) = { id, }
FOLLOW( R ) = { e }
/ according to rule 3 /
FIRST( A ) = { id }
FOLLOW( A ) = { id, }
/ according to rule 3 /
FOLLOW( E ) = { ; , ) }
/ according to rule 2 /
FIRST( T ) = { +, }
FOLLOW( T ) = { ; , ) }
/ according to rule 3 /
FOLLOW( F ) = { +, ; , ) }
/ according to rule 3 /
Outcome:
After completing these exercise the student can able to solve the problem for finding first
and follow of grammar.
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
2.
3.
4.
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Ex.no .6
Predictive parsing
Aim
Write a C program to implement predictive parsing table
Theory
Algorithm for Construction of Predictive Parsing Tables
Repeat: for each production A of the grammar do
for each terminal a in FIRST( )
add A to M[ A, a ]
if FIRST( ) contains
add A to M[ A, b ] for each b in FOLLOW( A )
if is in FIRST( ) and $ is in FOLLOW( A )
add A to M[ A, $ ]
make each undefined entry of M be error
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
F (E) | id | INT
The parsing table for this grammar then becomes:
id
INT
P
S
R
A
E
S
AR
R
AR
A id
=E;
E
FT
E
FT
E
FT
T
+FT
F id
T
F
b
P
bSe
F
INT
T
F
(E)
Using the above parsing table a simple string will be parsed as follows:
Stack
$P
$ eSb
$eS
$eRA
$eR;E=id
$eR;E=
$eR;E
$eR;TF
$eR;Tid
$eR;T
$eR;TF+
$eR;TF
$eR;TINT
Input
b id = id + 1 ; e $
b id = id + 1 ; e $
id = id + 1 ; e $
id = id + 1 ; e $
id = id + 1 ; e $
= id + 1 ; e $
id + 1 ; e $
id + 1 ; e $
id + 1 ; e $
+1;e$
+1;e$
1;e$
1;e$
Output
P bSe
S AR
A id = E ;
E FT
F id
T +FT
F INT
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
$eR;T
$eR;
$eR
$e
$
;e$
;e$
e$
e$
$
T
R
Outcome:
After completing these exercise the student can able to construct the predictive parsing
table.
Sample Viva-Voce questions:
1. Comment on predictive parser?
2. Eliminate left recursion from the following grammar
A->Ac/Aad/bd/c.
3. State LL (1) grammar? Give the properties of LL (1) grammar.
4. Give the algorithm for Left Factoring a Grammar.
5. Comment on Left Recursion? Give an example for eliminating the same.
Experiments addressing COs:
The experiments mentioned above (6) address the following COs : CO1,CO2
Ex.no .7
Shift-Reduce Parsing
Aim
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Handles
A handle of a string is a substring that matches the right side of a production whose
reduction to the nonterminal on the left represents one step along the reverse of a
rightmost derivation.
A handle of a right-sentential form is a production A and a position in where
the string may be found and replaced by A to produce the previous sentential form in
the rightmost derivation of .
in
S *rm Aw w
the handle of
Example:
w is
in position
EE+E
EE*E
E(E)
E id
Rightmost derivation:
rm E + E
rm E + E * E
rm E + E * id3
rm E + id2 * id3
rm id1 + id2 * id3
rm E * E
rm E * id3
rm E + E * id3
rm E + id2 * id3
rm id1 + id2 * id3
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
shift - the next input symbol is shifted onto the top of the stack;
reduce - the parser knows the right end of the handle is at the top of thestack. It
must then locate the left end of the handle within the stack and decide withwhat
nonterminal to replace the handle;
Input
1)
yz$
2)
$ B
yz$
3)
$ By
z$
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Input
1)
2)
$ Bxy
xyz$
z$
Input
id1 + id2 * id3 $
+ id2 * id3 $
$E
$ E+
$ E+id2
+ id2 * id3 $
id2 * id3 $
* id3 $
$ E+E
$ E+E*
$ E+E* id3
* id3 $
id3 $
$
$ E+E*E
$ E+E
$E
Action
shift
reduce by E id
shift
shift
reduce by E id
shift
shift
reduce by E id
reduce by E E*E
reduce by E E+E
accept
Outcome:
After completing these exercise the student can able to solve the problem using parsing
functions.
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Ex.no .8
Operator Precedence Parsing
Aim
To Write a C program to implement operator precedence parsing
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Theory
Precedence Relations
Bottom-up parsers for a large class of context-free grammars can be easily developed
using operator grammars.
Operator grammars have the property that no production right side is empty or has two
adjacent nonterminals. This property enables the implementation of efficient operatorprecedence parsers. Theseparser rely on the following three precedence relations:
Relation
a < b
a = b
a > b
Meaning
a yields precedence to b
a has the same precedence as b
a takes precedence over b
These operator precedence relations allow delimiting the handles in the right sentential
forms: < marks the left end, = appears in the interior of the handle, and > marks the
right end.
Let assume that between the symbols ai and ai+1 there is exactly one precedence relation.
Suppose that $ is the end of the string. Then for all terminals we can write: $ < b and b
> $. If weremove all nonterminals and place the correct precedence relation:<, =, >
between the remaining terminals, there remain strings that can be analyzed by easily
developed parser.
For example, the following operator precedence relations canbe introduced for simple
expressions:
id
id
+
*
$
<
<
<
+
>
>
>
<
*
>
<
>
<
$
>
>
>
>
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
The operator precedence parsers usually do not store the precedence table with the
relations, rather they are implemented in a special way.
Operator precedence parsers use precedence functions that map terminal symbols to
integers, and so the precedence relations between the symbols are implemented by
numerical comparison.
Not every table of precedence relations has precedence functions but in practice for most
grammars such functions can be designed.
Algorithm for Constructing Precedence Functions
1. Create functions fa for each grammar terminal a and for the end of string symbol;
2. Partition the symbols in groups so that fa and gb are in the same group if a = b
( there can be symbols in the same group even if they are not connected by this
relation);
3. Create a directed graph whose nodes are in the groups, next for each symbols a
and b do: place an edge from the group of gb to the group of fa if a < b, otherwise
if a > b place an edge from the group of fa to that of gb;
4. If the constructed graph has a cycle then no precedence functions exist. When
there are no cycles collect the length of the longest paths from the groups of fa
and gb respectively.
<
<
<
+
>
>
>
<
*
>
<
>
<
$
>
>
>
>
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
gid
fid
f*
g*
g+
f+
f$
g$
id
4
5
+
2
1
*
4
3
$
0
0
Outcome:
After completing these exercise the student can able to solve the problem using bottom
up parsing functions.
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Ex.no .9
Generation of a code for a given intermediate code
Aim
To convert an Arithmetic expression given in postfix notation to two address assembly
language code
Algorithm
Step 1: Declare the necessary variables.
Step 2: Read the expression from the user.
Step 3: Read character by character and do the following until end of the expression is
reached.
a) If the character is an alphabet, push it in the identifier stack.
b) Else if the character is an operator, temporarily store the corresponding
instruction based upon the operator.
c) Then pop the second identifier from the stack and then the first from the stack
and store them in temporary variable, say a, b respectively.
d) Then print the current instruction asLDA with the operand stored in variable
b.
e) Then print the arithmetic instruction with the operand stored in the variable a.
f) Then print the print the instruction STA with a temporary operand.
g) Then push the temporary operand in the stack for future use.
Step 4: Stop the program execution.
Example Output
Enter the Postfix Expression: ABC*+
LDA
MUL
STA
LDA
B
C
T1
A
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
ADD T1
STA T2
Outcome:
After completing these exercises student can able to generation of a code for a givin
intermediate code
Sample Viva-Voce questions:
1.How would you represent the following equation using the DAG,
a: =b*-c + b*-c. What is the purpose of DAG?
2. Illustrate the intermediate code representation for the expression a or b and not c ?
3.How would you map names to Values?
4.List the various methods of implementing three address statements?
5.List the methods of representing a syntax tree?
6. State the Syntax directed definition of if-else statement.
7. State quadruples preferred over triples in an optimizing Complier?
8.Give the triple representation of a ternary operation x:= y[i]
Experiments addressing COs:
The experiments mentioned above (9) address the following COs : CO2,CO3,CO4
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Ex.no .10
Scanner using lex/flex tool
Aim
Construct a scanner for the simple C-like language using the lex/flex tool. Given a
legal input (valid program in the language) the scanner should print a stream of two
tuples in the following format <tokenid, tokenname>.
For the following program segment
int main()
{
int first, second, third;
third=first+second;
}
The lexer should output something like
<260, int>
<265, main>
<270, ident>
...
In the above example, the token-id for int is 260 and the token-id for ident ("identifier")
is 270. These numbers can be assigned according to your wish. Consider that the
language only defines main and supports the declaration part and statements involving
arithmetic operations.
Theory
NAME
flex - fast lexical analyzer generator
SYNOPSIS
flex [-bcdfhilnpstvwBFILTV78+? -C[aefFmr] -ooutput -Pprefix -Sskeleton] [--help --version] [filename ...]
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
DESCRIPTION
flex is a tool for generating scanners: programs which recognized lexical
patterns in text. flex reads the given input files, or its standard input if no
file names
are given, for a description of a scanner to generate. The description is in the form of
pairs of regular expressions and C code, called rules. flex generates as output a C source
file, lex.yy.c, which defines a routine yylex(). This file is compiled and linked with the
-lfl library to produce an executable. When the executable is run, it analyzes itts input
for occurrences of the regular expressions. Whenever it finds one, it executes the
corresponding C code.
SOME SIMPLE EXAMPLES
First some simple examples to get the flavor of how one uses flex. The
following flex input specifies a scanner which whenever it encounters the string
"username" will replace it with the user's login name:
%%
username printf( "%s", getlogin() );
By default, any text not matched by a flex scanner is copied to the output, so the net
effect of this scanner is to copy its input file to its output with each occurrence of
"username" expanded. In this input, there is just one rule. "username" is the pattern and
the "printf" is the action. The "%%" marks the beginning of the rules.
Here's another simple example:
int num_lines = 0, num_chars = 0;
%%
\n
.
++num_lines; ++num_chars;
++num_chars;
%%
main()
{
yylex();
printf( "# of lines = %d, # of chars = %d\n",
num_lines, num_chars );
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
}
This scanner counts the number of characters and the number of lines in its input (it
produces no output other than the final report on the counts). The first line declares two
globals, "num_lines" and num_chars", which are accessible both inside yylex() and in
the main() routine declared after the second "%%". There are two rules, one which
matches a newline ("\n") and increments both the line count and the character count, and
one which matches any character other than a newline (indicated by the "." regular
expression).
FORMAT OF THE INPUT FILE
The flex input file consists of three sections, separated by a line with just %% in it:
definitions
%%
rules
%%
user code
Name definitions have the form:
name definition
The "name" is a word beginning with a letter or an underscore ('_') followed by zero or
more letters, digits, '_', or '-' (dash). The definition is taken to begin at the first non
white-space character following the name and continuing to the end of the line. The
definition can subsequently be referred to using "{name}", which will expand to
"(definition)". For example,
DIGIT [0-9]
ID
[a-z][a-z0-9]*
defines "DIGIT" to be a regular expression which matches a single digit, and "ID" to be a
regular expression which matchesa letter followed by zero-or-more letters-or-digits. A
subsequent reference to
{DIGIT}+"."{DIGIT}*
is identical to
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
([0-9])+"."([0-9])*
and matches one-or-more digits followed by a '.' followed by zero-or-more digits.
Sample Program
%{
#include <stdio.h>
#define INT
#define MAIN
260
265
[m][a][i][n]"()"
"return"|"if"|"else"
[a-zA-Z][a-zA-Z0-9]*
"+"|"-"|"*"|"/"
\"
[0-9]+
\n
\;
\{
\}
\=
[ \t]+
%%
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Test.c
int main()
{
int first,second,third;
third=first+second ;
}
RESULT:
[root@localhost ~]# lex prob2.l
[root@localhost ~]# cc lex.yy.c -o prob2 -ll
[root@localhost ~]# ./prob2<test.c
<260,int>
<265,main>
<260,int>
<270,ident>
, <270,ident>
, <270,ident>
<270,ident>
<270,ident>
<280,operator>
<270,ident>
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Outcome:
After completing these excercises student can able to solve the problems using lex.
Sample Viva-Voce questions:
1. Comment lex tool
2. Define lex Compiler
3. Mention the lex Specification.
Experiments addressing COs:
The experiments mentioned above (10) address the following COs : CO4
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Ex.no .11
Parser using LEX and YACC
Aim
Construct a parser for a C-like language. The parser should work with the lexer that has
been developed already. The parser should give warning messages when any error occurs,
such as when a variable is used without a preceding definition. Description of the
language is given below:
The keywords of the language are: else if int return void
Special symbols are: + - * / < > = : . ( ) { } /**/
Other tokens are ID and NUM. defined by the following regular expressions:
ID = letter letter*
NUM = digit digit*
letter = a||z|A||Z
digit = 0||9
White spaces (blanks, newlines, tabs) must be ignored. Comments must be skipped by the
lexer. A BNF grammar for the language is as follows:
program declaration-list
declaration-list declaration-list declaration | declaration
declaration var-declaration | fun-declaration
var-declaration type ID ;
type int | void
fun-declaration type ID ( params ) compound-stmt
params type ID
compound stmt { local declarations statement-list }
local declarations local declarations var-declarations | empty
statement-list statement-list statement | empty
statement expression-stmt | compound-stmt | selection-stmt | return-stmt
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
expression-stmt expression : | :
selection-stmt if ( expression ) statement | if ( expression ) statement else
statement
return-stmt return ;
expression var = expression | simple-expression
var = ID
simple-expression exp relop exp | exp
relop < | >
exp exp addop term | term
addop + | term term mulop factor | factor
mulop * | /
factor ( expression ) | var | NUM
Theory
Introduction
Yacc provides a general tool for describing the input to a computer program. The Yacc
user specifies the structures of his input, together with code to be invoked as each such
structure is recognized. Yacc turns such a specification into a subroutine that handles the
input process; frequently, it is convenient and appropriate to have most of the flow of
control in the user's application handled by this subroutine.
The input subroutine produced by Yacc calls a user-supplied routine to return the next
basic input item. Thus, the user can specify his input in terms of individual input
characters or in terms of higher level constructs such as names and numbers. The user
supplied routine may also handle idiomatic features such as comment and continuation
conventions, which typically defy easy grammatical specification. Yacc is written in
portable C.
Yacc provides a general tool for imposing structure on the input to a computer program.
User prepares a specification of the input process; this includes rules describing the input
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
structure, code to be invoked when these rules are recognized, and a low-level routine to
do the basic input. Yacc then generates a function to control the input process. This
function, called a parser, calls the user-supplied low-level input routine (the lexical
analyzer) to pick up the basic items (called tokens) from the input stream. These tokens
are organized according to the input structure rules, called grammar rules; when one of
these rules has been recognized, then user code supplied for this rule, an action, is
invoked; actions have the ability to return values and make use of the values of other
actions.
Yacc is written in a portable dialect of C and the actions, and output subroutine, are in C
as well. Moreover, many of the syntactic conventions of Yacc follow C.
The heart of the input specification is a collection of grammar rules. Each rule describes
an allowable structure and gives it a name. For example, one grammar rule might be
date : month_name day ',' year
Here, date, month_name, day, and year represent structures of interest in the input
process; presumably, month_name, day, and year are defined elsewhere. The comma ``,''
is enclosed in single quotes; this implies that the comma is to appear literally in the input.
The colon and semicolon merely serve as punctuation in the rule, and have no
significance in controlling the input. Thus, with proper definitions, the input
July 4, 1776
might be matched by the above rule.
An important part of the input process is carried out by the lexical analyzer. This user
routine reads the input stream, recognizing the lower level structures, and communicates
these tokens to the parser. For historical reasons, a structure recognized by the lexical
analyzer is called a terminal symbol, while the structure recognized by the parser is called
a nonterminal symbol. To avoid confusion, terminal symbols will usually be referred to
as tokens.
There is considerable leeway (flexibility) in deciding whether to recognize structures
using the lexical analyzer or grammar rules. For example, the rules
month_name : 'J' 'a' 'n' ;
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
1. Basic Specifications
Every specification file consists of three sections: the declarations, (grammar) rules, and
programs. The sections are separated by double percent ``%%'' marks. (The percent ``
%'' is generally used in Yacc specifications as an escape character.)
In other words, a full specification file looks like
declarations
%%
rules
%%
programs
The declaration section may be empty. Moreover, if the programs section is omitted, the
second %% mark may be omitted also; thus, the smallest legal Yacc specification is
%%
rules
Blanks, tabs, and newlines are ignored except that they may not appear in names or
multi-character reserved symbols. Comments may appear wherever a name is legal; they
are enclosed in /* . . . */, as in C and PL/I.
The rules section is made up of one or more grammar rules.
A grammar rule has the form:
A : BODY ;
A represents a nonterminal name, and BODY represents a sequence of zero or more
names and literals. The colon and the semicolon are Yacc punctuation.
Names may be of arbitrary length, and may be made up of letters, dot ``.'', underscore
``_'', and non-initial digits. Upper and lower case letters are distinct. The names used in
the body of a grammar rule may represent tokens or nonterminal symbols.
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
A literal consists of a character enclosed in single quotes ``'''. As in C, the backslash ``\''
is an escape character within literals, and all the C escapes are recognized. Thus
'\n' newline
'\r'
return
'\''
'\\'
backslash ``\''
'\t'
tab
'\b'
backspace
E F ;
G ;
B C D
E F
;
It is not necessary that all grammar rules with the same left side appear together in the
grammar rules section, although it makes the input much more readable, and easier to
change.
If a nonterminal symbol matches the empty string, this can be indicated in the obvious
way:
empty : ;
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Names representing tokens must be declared; this is most simply done by writing
%token name1, name2 . . .
in the declarations section. Every name not defined in the declarations section is
assumed to represent a non-terminal symbol. Every non-terminal symbol must appear on
the left side of at least one rule.
Of all the nonterminal symbols, one, called the start symbol, has particular importance.
The parser is designed to recognize the start symbol; thus, this symbol represents the
largest, most general structure described by the grammar rules. By default, the start
symbol is taken to be the left hand side of the first grammar rule in the rules section. It is
possible, and in fact desirable, to declare the start symbol explicitly in the declarations
section using the % start keyword:
%start symbol
The end of the input to the parser is signaled by a special token, called the endmarker. If
the tokens up to, but not including, the endmarker form a structure which matches the
start symbol, the parser function returns to its caller after the end-marker is seen; it
accepts the input. If the endmarker is seen in any other context, it is an error.
It is the job of the user-supplied lexical analyzer to return the endmarker when
appropriate; see section 3, below. Usually the endmarker represents some reasonably
obvious I/O status, such as ``end-of-file'' or ``end-of-record''.
2: Actions: With each grammar rule, the user may associate actions to be Yacc: Yet
Another Compiler-Compiler performed each time the rule is recognized in the input
process. These actions may return values, and may obtain the values returned by previous
actions. Moreover, the lexical analyzer can return values for tokens, if desired.
An action is an arbitrary C statement, and as such can do input and output, call
subprograms, and alter external vectors and variables. An action is specified by one or
more statements, enclosed in curly braces ``{'' and ``}''. For example,
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
A : '(' B ')'
{ hello( 1, "abc" ); }
and
XXX : YYY ZZZ
{ printf("a message\n");
flag = 25; }
are grammar rules with actions.
To facilitate easy communication between the actions and the parser, the action
statements are altered slightly. The symbol ``dollar sign'' ``$'' is used as a signal to Yacc
in this context.
To return a value, the action normally sets the pseudo-variable ``$$'' to some value. For
example, an action that does nothing but return the value 1 is
{ $$ = 1; }
To obtain the values returned by previous actions and the lexical analyzer, the action may
use the pseudo-variables $1, $2, . . ., which refer to the values returned by the
components of the right side of a rule, reading from left to right. Thus, if the rule is
A
B C D ;
for example, then $2 has the value returned by C, and $3 the value returned by D.
As a more concrete example, consider the rule
expr :
The value returned by this rule is usually the value of the expr in parentheses. This can
be indicated by
expr :
{ $$ = $2 ; }
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
By default, the value of a rule is the value of the first element in it ($1). Thus,
grammar rules of the form
A
: B
{ $$ = 1; }
{ x = $2; y = $3; }
;
the effect is to set x to 1, and y to the value returned by C. Actions that do not terminate a
rule are actually handled by Yacc by manufacturing a new nonterminal symbol name, and
a new rule matching this name to the empty string. The interior action is the action
triggered off by recognizing this added rule. Yacc actually treats the above example as if
it had been written:
$ACT :
/* empty */
{ $$ = 1; }
;
A
B $ACT C
{ x = $2; y = $3; }
In many applications, output is not done directly by the actions; rather, a data
structure, such as a parse tree, is constructed in memory, and transformations are applied
to it before output is generated. Parse trees are particularly easy to construct, given
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
routines to build and maintain the tree structure desired. For example, suppose there is a
C function node, written so that the call
node( L, n1, n2 )
creates a node with label L, and descendants n1 and n2, and returns the index of the
newly created node. Then parse tree can be built by supplying actions such as:
expr :
in the specification.
The user may define other variables to be used by the actions. Declarations and
definitions can appear in the declarations section, enclosed in the marks ``%{'' and ``
%}''. These declarations and definitions have global scope, so they are known to the
action statements and the lexical analyzer. For example,
%{ int variable = 0; %}
could be placed in the declarations section, making variable accessible to all of the
actions. The Yacc parser uses only names beginning in ``yy''; the user should avoid such
names.
In these examples, all the values are integers: a discussion of values of other types will be
found in Section 10.
3: Lexical Analysis
The user must supply a lexical analyzer to read the input stream and communicate tokens
(with values, if desired) to the parser. The lexical analyzer is an integer-valued function
called yylex. The user must supply a lexical analyzer to read the input stream and
communicate tokens (with values, if desired) to the parser. The lexical analyzer is an
integer-valued function called yylex. The parser and the lexical analyzer must agree on
these token numbers in order for communication between them to take place. The
numbers may be chosen by Yacc, or chosen by the user. In either case, the ``# define''
mechanism of C is used to allow the lexical analyzer to return these numbers
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
symbolically. For example, suppose that the token name DIGIT has been defined in the
declarations section of the Yacc specification file. The relevant portion of the lexical
analyzer might look like:
yylex(){
extern int yylval;
int c;
...
c = getchar();
...
switch( c ) {
...
case '0':
case '1':
...
case '9':
yylval = c-'0';
return( DIGIT );
...
}
...
The intent is to return a token number of DIGIT, and a value equal to the numerical value
of the digit. Provided that the lexical analyzer code is placed in the programs section of
the specification file, the identifier DIGIT will be defined as the token number associated
with the token DIGIT.
This mechanism leads to clear, easily modified lexical analyzers; the only pitfall is
the need to avoid using any token names in the grammar that are reserved or significant
in C or the parser; for example, the use of token names if or while will almost
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
certainly cause severe difficulties when the lexical analyzer is compiled. The token
name error is reserved for error handling, and should not be used naively.
As mentioned above, the token numbers may be chosen by Yacc or by the user. In the
default situation, the numbers are chosen by Yacc. The default token number for a literal
character is the numerical value of the character in the local character set. Other names
are assigned token numbers starting at 257.
To assign a token number to a token (including literals), the first appearance of the
token name or literal in the declarations section can be immediately followed by a
nonnegative integer. This integer is taken to be the token number of the name or literal.
Names and literals not defined by this mechanism retain their default definition. It is
important that all token numbers be distinct.
For historical reasons, the end marker must have token number 0 or negative. This token
number cannot be redefined by the user; thus, all lexical analyzers should be prepared to
return 0 or negative as a token number upon reaching the end of their input.
A very useful tool for constructing lexical analyzers is the Lex program developed by
Mike Lesk. These lexical analyzers are designed to work in close harmony with Yacc
parsers. The specifications for these lexical analyzers use regular expressions instead of
grammar rules. Lex can be easily used to produce quite complicated lexical analyzers,
but there remain some languages (such as FORTRAN) which do not fit any theoretical
framework, and whose lexical analyzers must be crafted by hand.
Program for Simple Parser
NewLex.l
%{
#include <stdlib.h>
#include "y.tab.h"
void yyerror(char *);
extern int yylval;
%}
digit
num
[0-9]
{digit}{digit}*
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
%%
{num}
{ yylval=atoi(yytext);
return(NUM);}
{return yytext[0];}
"+"|"*"|"("|")"|"\n"
[ \t]
;
/*Anything else is treated as error*/
.
yyerror("Invalid character\n");
%%
Newyacc.y
%{
#include <ctype.h>
#include <stdio.h>
%}
%token NUM
%%
lines : lines exp '\n'
| lines '\n'
|
;
{printf("value : %d \n",$2);}
exp
{$$=$1+$3;}
{$$=$1*$3;}
{$$=$2;}
int main(void) {
yyparse();
return 0;
}
void yyerror(char *s) {
fprintf(stderr,"%s\n",s);
}
yywrap()
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
{
return(1);
}
LFile.l
%{
#include<stdio.h>
#include "y.tab.h"
#define TableSize 29
struct SYMTAB{
char symb[16];
int kind;
int type;
int empty;
}symbol[TableSize];
extern int yylval;
int full=0;
%}
/* regular deifinitions*/
delim [ \t\n]
ws
{delim}+
letter [A-Za-z]
digit
[0-9]
id
{letter}{letter}*
num
{digit}{digit}*
%%
{ws}
{/*no action*/}
if
{yylval=IF; return(IF);}
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
else
{yylval=ELSE; return(ELSE);}
int
{yylval=INT; return(INT);}
{yylval=VOID; return(VOID);}
{id}
{ yylval=install_id(yytext); return(ID); }
{num}
{ yylval=atoi(yytext); return(NUM);}
{/*ignore comment*/}
"//".*
{/*ignore comment*/}
%%
unsigned int Hash(char *sym)
{
unsigned int HashVal = 0;
while(*sym!='\0')
HashVal += *sym++;
return HashVal%TableSize;
}
void enter_symbol(unsigned int HashVal,char *sym)
{
strcpy(symbol[HashVal].symb,sym);
symbol[HashVal].kind=-1;
symbol[HashVal].type=-1;
symbol[HashVal].empty= 0;
}
unsigned int linear_probe_hash(char *key)
{
unsigned int HashVal,i;
i=0;
HashVal=Hash(key);
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
do{
HashVal = (HashVal+i)%TableSize;
i++;
}while(symbol[HashVal].empty!=-1);
if(strcmp(symbol[HashVal].symb,key)==0)
{full=1; return HashVal;}
else
return HashVal;
}
int install_id(char *s)
{
int error=0,hashval;
hashval=Hash(s);
if(strcmp(symbol[hashval].symb,s)==0)
{
error=1;
return(hashval);
}
if((!error)&&(symbol[hashval].empty!=-1))
{
hashval=linear_probe_hash(s);
if(full==1){
error=1;
full=0;
}
}
if(!error)
enter_symbol(hashval,s);
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
return(hashval);
}
YaccFile.y
%{
#include<stdio.h>
#define TableSize 29
extern struct SYMTAB{
char symb[16];
int kind;
int type;
int empty;
}symbol[TableSize];
%}
%token ID
%token NUM IF ELSE RETURN VOID INT
%%
program
: declist ;
declist
: declist dec
| dec ;
dec
: vardec
| fundec ;
{symbol[$2].kind=0; symbol[$2].type=$1;}
: INT
{$$ = $1;}
| VOID ;
{$$= $1;}
: type ID
{symbol[$2].kind=0; symbol[$2].type=$1;}
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
: localdec vardec
|;
: expstmt
| compoundstmt
| selectstmt
| returnstmt ;
expstmt
: exp ';'
| ';' ;
selectstmt
returnstmt
: RETURN ;
exp
var
simpleexp
relop
: '<'
| '>' ;
expr
addop
: '+'
| '-' ;
term
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
mulop
: '*'
| '/' ;
factor
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
Ex.no .12
Calculator using LEX and YACC
Aim
To implement a desktop calculator using LEX and YACC tool
Algorithm
To create the desk calculator example program, do the following:
1.Process the yacc grammar file using the -d optional flag (which informs the yacc
command to create a file that defines the tokens used in addition to the C language source
code):
yacc -d calc.yacc
1.Use the ls command to verify that the following files were created:
y.tab.c
The C language source file that the yacc command created for the parser
y.tab.h
A header file containing define statements for the tokens used by the parser
2.Process the lex specification file:
lex calc.lex
3.Use the ls command to verify that the following file was created:
lex.yy.c
The C language source file that the lex command created for the lexical analyzer
4.Compile and link the two C language source files:
cc y.tab.c lex.yy.c
5.Use the ls command to verify that the following files were created:
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
y.tab.o
The object file for the y.tab.c source file
lex.yy.o
The object file for the lex.yy.c source file
a.out
The executable program file
To run the program directly from the a.out file, type:
$ a.out
In either case, after you start the program, the cursor moves to the line below the $
(command prompt). Then, enter numbers and operators as you would on a calculator.
When you press the Enter key, the program displays the result of the operation. After you
assign a value to a variable, as follows, the cursor moves to the next line.
m=4 <enter>
When you use the variable in subsequent calculations, it will have the assigned value:
m+5 <enter>
9
Parser Source Code
The following example shows the contents of the calc.yacc file. This file has entries in all
three sections of a yacc grammar file: declarations, rules, and programs.
%{
#include <stdio.h>
int regs[26];
int base;
%}
%start list
%token DIGIT LETTER
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS /*supplies precedence for unary minus */
%%
list:
/*empty */
|
list stat '\n'
|
list error '\n'
{
yyerrok;
}
;
stat:
expr
{
printf("%d\n",$1);
}
|
LETTER '=' expr
{
regs[$1] = $3;
}
;
expr:
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
{
$$ = $1 / $3;
}
|
expr '%' expr
{
$$ = $1 % $3;
}
|
expr '+' expr
{
$$ = $1 + $3;
}
|
expr '-' expr
{
$$ = $1 - $3;
}
|
expr '&' expr
{
$$ = $1 & $3;
}
|
expr '|' expr
{
$$ = $1 | $3;
}
|
'-' expr %prec UMINUS
{
$$ = -$2;
}
|
LETTER
{
$$ = regs[$1];
}
|
number
number: DIGIT
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
{
$$ = $1;
base = ($1==0) ? 8 : 10;
}
|
number DIGIT
{
$$ = base * $1 + $2;
}
;
%%
main()
{
return(yyparse());
}
yyerror(s)
char *s;
{
fprintf(stderr, "%s\n",s);
}
yywrap()
{
return(1);
}
The file contains the following sections:
o
o
o
o
o
Because these subroutines are included in this file, you do not need to use the yacc
library when processing this file.
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
main
yyerror(s)
yywrap
[a-z]
{
c = yytext[0];
yylval = c - 'a';
return(LETTER);
}
[0-9]
{
c = yytext[0];
yylval = c - '0';
return(DIGIT);
}
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society
Vision: To become a Centre of Excellence in Teaching and Research in the field of Computer Science and
Engineering
[^a-z0-9\b]
c = yytext[0];
return(c);
Outcome:
After completing these excercises student can able to generate a calculator using LEX
and YACC .
Sample Viva-Voce questions:
1) Comment lex tool
2) Define lex Compiler
3) List the basic Specification of lex compiler.
4) List the Specification for YACC compiler.
5) List the advantage of lex compiler and yacc compiler
Experiments addressing COs:
The experiments mentioned above (12) address the following COs : CO3,CO4
Mission: To prepare the students for a prospective career in IT industry and for higher learning by
imparting sound technical knowledge. To carry out research in cutting edge technologies in computer
engineering to meet the requirement of the industry and society