Compiler Design Practical
Compiler Design Practical
Page 2 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 01
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
Page 3 of 32
Objective: -
1. To recognize the following verbs:-is, am, are, were, was, be, being,
been, do, does, did, will, would, should, can, could, has, have, had,
go.
2. Input a Character.
3. Output a Character.
Source code: -
%%
[\t]+ /* ignore whitespace */;
is |
am |
are |
were |
was |
be |
being |
been |
do |
does |
did |
will |
would|
should |
can |
could |
has |
have |
had |
go {printf(“%s : is a verb\n”, yytext);}
[a-zA-Z]+ {printf(“%s : is not a verb\n”, yytext);}
.|\n {ECHO;}
%%
main()
{
yylex();
}
Page 4 of 32
Output: -
Discussion: -
Advantages:
In this program we can identify some predefined verbs and can easily
feed the output of this program to a parser program. It helps us to
understand how a scanner works and also how it interacts with the
parser.
Limitations:
This program used only some of the reserved keywords and works
on only C like statement.
Efficiency:
Under a little limitation the efficiency of this program is 100%.
Page 5 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 02
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
Page 6 of 32
Objective: -
1. To recognize the following words as different parts of speech: -is,
am, are, go, very simply, quickly, gently, to, from, behind, between,
if, then, and.
2. Input a Character.
3. Output a Character.
Source code: -
%%
[\t]+ /* ignore whitespace */;
is |
am |
are |
go {printf(“%s : is a verb”, yytext);}
very |
simply |
quickly |
gently{printf(“%s : is an adverb\n”, yytext);}
to |
from |
behind |
between {printf(“%s : is a pronoun\n”, yytext);}
if |
then |
and {printf(“%s : is a preposition\n”, yytext);}
[a-zA-Z]+ {printf(“%s : is not recognize, might be a noun\n”, yytext);}
.|\n {ECHO;}
%%
main()
{
yylex();
}
Page 7 of 32
Output: -
Discussion:
as different parts of speech and we can easily feed the output of this
Limitations: This program used only some of the reserved words and
works on only C.
Page 8 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 03
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
Page 9 of 32
Objective: -
1. To recognize different keyword.
2. Input a Character.
3. Output a Character.
Source code: -
%%
[\t]+ /* ignore whitespace */;
if |
for |
while |
do |
main {printf(“%s : is a keyword\n”, yytext);}
[a-zA-Z] + {printf(“%s : is not a keyword”, yytext);}
.|\n {ECHO;}
%%
main()
{
yylex();
}
Page 10 of 32
Output: -
Discussion: -
Advantages: This program can identify all real numbers and we can
easily feed the output of this program to a parser program. It helps
us to understand how a scanner works and also how it interacts with
the parser.
Limitations: This program can identify all real numbers but works on
only C like statements.
Page 11 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 04
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
Page 12 of 32
Objective: -
1. To recognize the identifier.
2. Input a Character.
3. Output a Character.
Source Code: -
%%
[\t]+ /* ignore whitespace */;
[a-ZA-z]([a-zA-Z] |[0-9])* {printf(“%s : is an identifier\n”, yytext);}
[0-9]([a-zA-Z] |[0-9])* {printf(“%s : is not an identifier”, yytext);}
.|\n {ECHO;}
%%
main()
{
yylex();
}
Page 13 of 32
Output: -
Discussion: -
Advantages: This program can identify all real numbers and we can
easily feed the output of this program to a parser program. It helps
us to understand how a scanner works and also how it interacts with
the parser.
Limitations: This program can identify all real numbers but works on
only C like statements.
Page 14 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 05
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
Page 15 of 32
Objective: -
1. To recognize real numbers.
2. Input a Character.
3. Output a Character.
Source Code: -
%%
-?(([0-9]+)|([0-9]+\.[0-9]+)([eE][-+]?[0-9]+)?){printf(“%s : is a real
number\n”, yytext);}
.|\n {ECHO;}
%%
main()
yylex();
Page 16 of 32
Output: -
Discussion: -
Advantages: This program can identify all real numbers and we can
easily feed the output of this program to a parser program. It helps
us to understand how a scanner works and also how it interacts with
the parser.
Limitations: This program can identify all real numbers but works on
only C like statements.
Page 17 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 06
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
Page 18 of 32
Objective: -
1. To recognize integer.
2. Input a Character.
3. Output a Character.
Source Code: -
%%
.|\n {ECHO;}
%%
main()
yylex();
Page 19 of 32
Output: -
Discussion: -
Advantages: This program can identify all real numbers and we can
easily feed the output of this program to a parser program. It helps
us to understand how a scanner works and also how it interacts with
the parser.
Limitations: This program can identify all real numbers but works on
only C like statements.
Page 20 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 07
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
Page 21 of 32
Objective: -
1. To recognize for the positive and negative integer and float
number.
2. Input a Character.
3. Output a Character.
Source Code: -
%%
.|\n {ECHO;}
%%
main()
yylex();
Page 22 of 32
Output: -
Discussion: -
Advantages: This program can identify all real numbers and we can
easily feed the output of this program to a parser program. It helps
us to understand how a scanner works and also how it interact with
the parser.
Limitations: This program can identify all real numbers but works on
only C like statements.
Page 23 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 08
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
Page 24 of 32
Objective: -
1. To recognize different punctuation symbol.
2. Input a Character.
3. Output a Character.
Source Code: -
%{
#include<stdlib.h>
%}
DIGIT [0-9]
INTEGER (DIGIT})+
NINTEGER [-]({INTEGER})
FLOAT (INTEGER})?([-]{INTEGER}))
NFLOAT [-]({FLOAT})
BLANK ['', 'It', '\n']
AL (.) +
%%
{INTEGER} {printf("%s is a Positive Integer
Number\n",yytext);}
{NINTEGER} {printf("%s is a Negative Integer Number\n",
yytext);}
{FLOAT} {printf("%s is a Positive floating Number\n",
yytext);}
{NFLOAT} {printf("%s is a Negative floating Number\n",
yytext);}
{BLANK} {printf("");}
{AL} {printf("");}
%%
int main(void)
{
yyin = fopen("input.txt","r");
yylex();
return 0;
}
int yywrap (void) {
return 0;
}
Page 25 of 32
Output: -
Discussion: -
Advantages: This program can identify all real numbers and we can
easily feed the output of this program to a parser program. It helps
us to understand how a scanner works and also how it interact with
the parser.
Limitations: This program can identify all real numbers but works on
only C like statements.
Page 26 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 09
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
Page 27 of 32
Objective: -
1. To recognize digit.
2. Input a Character.
3. Output a Character.
Source code: -
%%
[\t]+ /* ignore whitespace */ ;
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 {printf(“%s : is a digit\n”, yytext);}
[a-zA-Z] + {printf(“%s : is not a digit”, yytext);}
.|\n {ECHO;}
%%
main()
{
yylex();
}
Page 28 of 32
Output: -
Discussion: -
Advantages: This program can identify all real numbers and we can
easily feed the output of this program to a parser program. It helps
us to understand how a scanner works and also how it interact with
the parser.
Limitations: This program can identify all real numbers but works on
only C like statements.
Page 29 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 10
Date of Experiment:
Date of Submission:
SUBMITTED TO:
ANANNA RASHID
Lect. Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207 Signature
SUBMITTED BY:
MD. ISMAIL HASAN TANJER
Roll-45, Reg. No: 16502000971
6th Semester
Department of CSE
Mohammadpur Kendriya COLLEGE-6458
Noorjahan Road, Mohammadpur, Dhaka-1207
Page 30 of 32
Objective: -
1. To recognize different operators.
2. Input a Character.
3. Output a Character.
Source Code: -
/* Lex program to check whether input is digit or not. */
%{
#include<stdio.h>
#include<stdlib.h>
%}
/* Rule Section */
%%
^[0-9]* printf("digit");
^[^0-9]|[0-9]*[a-zA-Z] printf("not a digit");
.;
%%
int main()
{
// The function that starts the analysis
yylex();
return 0;
}
Page 31 of 32
Output: -
Discussion: -
Advantages: In this program we can identify different operators and
we can easily feed the output of this program to a parser program. It
helps us to understand how a scanner works and also how it
interacts with the parser.
Page 32 of 32