CompilerDesignLabManual PDF
CompilerDesignLabManual PDF
Faculty:
Mr. Bhaskar Mondal
Contents
1
.
.
.
.
2
2
4
4
8
.
.
.
.
8
8
8
8
9
9
9
9
9
Advanced Problems
Books to Follow
.
.
.
.
Parser
2.1 Day: 5: Exercise using C . . .
2.2 Introduction to YACC . . . . .
2.3 Day: 6: Exercise using YACC
2.4 Day 7: Exercise Using C . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Instructions
Maintain Index/ content properly.
Brief descriptions including algorithm used and flowchart of the work you did for each exercise.
Copies of the main C, Lex and Yacc you used for the exercises, along with the numerical results.
You must calculate and mention computational Complexity of each experiment.
You must provide Test Cases/sample Input and Output at the end of exercise, Print the plot
files (.jpg) corresponding with the different exercises(if any).
Explanations of anything unusual or interesting, or points of confusion that you were unable to
resolve outside lab.
If you believe I have an error in a lab, please inform me of it. Explain why you think it is an
error and, if you like, suggest a correction.
1.1
Introduction
Figure 1
The patterns in the input are written using an extended set of regular expressions. These are:
r0
r$
< s > r0
< > r0
<< EOF >>0
Note that inside of a character class, all regular expression operators lose their special meaning
except escape (0 \0 ) and the character class operators, -, ], and, at the beginning of the class, . What
is a token
1.2
token
IDENT
ASSIGN_OP
NUMBER
ADD_OP
NUMBER
SEMICOLON
Day 1: Using C
1. Write a C Program to Design Lexical Analyzer which will identify keywords, identifiers, sentinels, special characters, operators, number of lines in code.
2. Write down a program in C to identify a input as id/ keywords/ number, the program should able
to take a line of instruction (int rate = 50;) and recognize all the small parts of the instruction.
1.3
(in ubuntu)
http://rpmfind.net/linux/rpm2html/search.php?query=flex
Installation of f lex on Windows Down load and Install Cygwin from https://www.cygwin.com/
Cygwin is:
a large collection of GNU and Open Source tools which provide functionality similar to a Linux
distribution on Windows.
a DLL (cygwin1.dll) which provides substantial POSIX API functionality.
Use Command Install gcc, make, gdb, flex ... package
Installation of f lex on Windows Installation Instructions
Step 1: Download FLEX
Step 2: Download DevC++
Step 3: Install FLEX in "C:\GnuWin32"
Step 4: Install DevC++ in "C:\Dev-Cpp"
Step 5: Open Environment Varibles (Steps on how to get to environment variables is given below)
Step 6: Add this "C:\GnuWin32\bin;C:\Dev-Cpp\bin;" to PATH.
Step 7: Stop
How to set to Environment Variables in Windows
Step 1: Click Start
Step 2: Right Click "Computer"
Step 3: Click Properties
Step 4: When the window opens, Click on the Advanced Settings in the left pane
Step 5: Click on Environment Variables in the bottom
Step 6: Select path in the 2nd window and click edit and add the lines mentioned above
Compiling lex programs Lets assume that you have a lex program written under name first.l
Step 1: Open Command prompt
Step2: Type "flex first.l"
Step3: Type "cc lex.yy.x"
Step 4: type "a"
Version: 1.0 Page 4
Lex.yy.c
Lex Compiler
C/ C++ Compiler
Input Stream
a.out
Lex.yy.c
a.out
Tokens
Figure 2
Compilation Here is the step by step method of compiling a LEX program.
% flex example.l (output f i l e lex .yy.c)
% gcc lex.yy.c -lfl
(-lfl : to link flex library )
or
%gcc lex.yy.c -ll
(-lfl : to link lex library )
Execution:
% cat input | ./a.out (in linux)
\$ cat input | ./a.exe (in windows)
input
);
Declarations Lex copies the material between %{ and %} directly to the generated C file, so
you may write any valid C codes here E.g.
%{
#define A 100
%}
WS [ \t]+
letter [A-Za-z]
digit [0-9]
op_plus "+"
Lex executes the action for the longest possible match for the current input
It can consists of any legal C code,
Lex copies it to the C file after the end of the Lex generated code
[\t ]+
{op_plus}
[a-zA-Z]+
.|\n
%%
Special Variables/Procedures:
yytext
yyleng
yylineno
yywrap
int yywrap(void)
int yylex(void)
char *yytext
yyleng
yylval
FILE *yyout
FILE *yyin
INITIAL
BEGIN
ECHO
Create a Lex file, lets say "exp.l" and open it up in your favorite text editor (read: Notepad++)
%{
#define A 100
%}
WS [\t]+
letter [A-Za-z]
digit [0-9]
op_plus "+"
%%
[0-9]+ {printf ("digit");};
%%
main(){
yylex();
}
yywrap(void)
{
return 0;
}
Example 2:
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 );
}
[0-9]
[a-z][a-z0-9]*
%%
{DIGIT}+
{
printf( "An integer: %s (%d)\n", yytext,
atoi( yytext ) );
}
{DIGIT}+"."{DIGIT}*
{
printf( "A float: %s (%g)\n", yytext,
atof( yytext ) );
}
if|then|begin|end|procedure|function
{
printf( "A keyword: %s\n", yytext );
}
{ID}
"+"|"-"|"*"|"/"
"{"[^}\n]*"}"
[ \t\n]+
/* eat up whitespace */
%%
main( argc, argv )
int argc;
char **argv;
{
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
yylex();
}
1.4
1. Program using LEX to recognize digit, number, words, operators, command lines, spaces.
2. Program using LEX to count the number of characters, words, spaces and lines in a given input
file.
3. Program using LEX to count the numbers of comment lines in a given C program. Also eliminate
them and copy the resulting program into separate file.
4. Program using LEX to recognize a valid arithmetic expression and to recognize the identifiers
and operators present. Print them separately.
5. Program using LEX to recognize whether a given sentence is simple or compound.
6. Program using LEX to recognize and count the number of identifiers in a given input file.
Parser
2.1
2.2
Introduction to YACC
Grammer Rules
Lex
Yacc
yylex
yyparse
Input
Parsed input
Figure 3
2.3
1. Convert The BNF rules into Yacc form and write code to generate abstract syntax tree.
2. YACC program to recognize a valid arithmetic expression that uses operators +, -, * and /.
Version: 1.0 Page 8
3. YACC program to recognize a valid variable, which starts with a letter, followed by any number
of letters or digits.
4. YACC program to evaluate an arithmetic expression involving operators +, -, * and /.
5. YACC program to recognize strings aaab, abbb, ab and a using the grammar (anbn, n
0).
6. Program to recognize the grammar (anb, n = 10).
2.4
3.1
4.1
1. Write a program for generating for various intermediate code forms: A Program to Generate
Machine Code.
(a) Three address code
(b) Quadruple
2. Write a program to generate the intermediate code in the form of Polish Notation
Advanced Problems
1.
2.
3.
4.
Books to Follow
1 John R. Levine, Tony Mason, Doug Brown; Lex & Yacc, OReilly & Associates1992. ISBN:
9781565920002, Online: https://books.google.co.in/books?id=fMPxfWfe67EC
Version: 1.0 Page 9
2 Charles N. Fischer, Richard J. LeBlanc Jr., Ron K. Cytron, Crafting A Compiler 2011, Pearson Education. ISBN: 9780133001570, Online:https://books.google.co.in/books?id=GSYrAAAAQBAJ
Evaluation Scheme
EC
No.
Evaluation
Component
Duration
Weightage
Data &
Time
Nature of
Component