0% found this document useful (0 votes)
89 views

Compiler Design Practical

The document provides an index of 10 experiments on writing simple LEX specifications to recognize various lexical elements such as verbs, parts of speech, keywords, identifiers, real numbers, integers, floats, punctuation symbols, and digits. Each experiment is assigned 2-4 pages and includes the objective, source code, output, and discussion. The index provides the experiment name and number, assigned page range, and submission details for each experiment.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Compiler Design Practical

The document provides an index of 10 experiments on writing simple LEX specifications to recognize various lexical elements such as verbs, parts of speech, keywords, identifiers, real numbers, integers, floats, punctuation symbols, and digits. Each experiment is assigned 2-4 pages and includes the objective, source code, output, and discussion. The index provides the experiment name and number, assigned page range, and submission details for each experiment.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

INDEX

No. Experiment Name Date of Date of Page


Experiment Submission
Write a simple lex
specification to recognize the
following verb: is, am, are
01 were, was, be, being, 2-4
been,do,does,did, will, would
should,can,could, has, have,
had,go. 
Write a simple lex
specification to recognize the
following words as different
02 parts of speech: is, am, are 5-7
were.go very
simply.quickly.gently,to, from
behind,between,if,then. 
Write a simple lex
03 specification to recognize 8-10
different keyword. 
Write a simple lex
04 specification to recognize the 11-13
identifier. 
Write a simple lex
05 specification to recognize real 14-16
numbers. 
Write a simple lex
06 specification to recognize 17-19
integer. 
Write a simple lex
07 specification to recognize 20-22
float. 
Write a simple lex
specification to recognize for
08 23-25
the positive and negative
integer and  float number. 
09 Write a simple lex 26-28
specification to recognize
different punctuation symbol. 
Write a simple lex
10 specification to recognize 29-31
digit. 

Course Teacher Chairman


Department of CSE Department of CSE

Page 2 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 01

Experiment Title: Write a simple lex specification to recognize the


following verb: is, am, are were, was, be, being, been, do, does, did, will,
would should, can, could, has, have, had, go.

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

Experiment Title: Write a simple lex specification to recognize the


following words as different parts of speech: is, am, are were.go very
simply.quickly.gently, to, from behind, between, if, then.

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:

Advantages: In this program we can identify some predefined words

as different parts of speech 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 used only some of the reserved words and
works on only C.

Efficiency: Under a little limitation the efficiency of this program is


100%.

Page 8 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 03

Experiment Title: Write a simple lex specification to recognize different


keyword.

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.

Efficiency: Under a little limitation the efficiency of this program is


100%.

Page 11 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 04

Experiment Title: Write a simple lex specification to recognize the


identifier.

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.

Efficiency: Under a little limitation the efficiency of this program is


100%.

Page 14 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 05

Experiment Title: Write a simple lex specification to recognize real


numbers.

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: -

%%

[\t]+ /* ignore whitespace */;

-?(([0-9]+)|([0-9]+\.[0-9]+)([eE][-+]?[0-9]+)?){printf(“%s : is a real
number\n”, yytext);}

[a-zA-Z]+ {printf(“%s : is not a real number”, 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.

Efficiency: Under a little limitation the efficiency of this program is


100%.

Page 17 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 06

Experiment Title: Write a simple lex specification to recognize integer.

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: -
%%

[\t]+ /* ignore whitespace */ ;

-? [0-9]+ {printf(“%s : is an integer number\n”, yytext);}

[a-zA-Z] + {printf(“%s : is not an integer number”, yytext);}

.|\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.

Efficiency: Under a little limitation the efficiency of this program is


100%.

Page 20 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 07

Experiment Title: Write a simple lex specification to recognize float.

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: -

%%

[\t]+ /* ignore whitespace */ ;

[0-9] {printf(“%s : is an integer \n”, yytext);}

([0-9]*\.[0-9]+) {printf(“%s : is a positive float number \n”, yytext);}

-?([0-9]*\.[0-9]+) {printf(“%s : is a negative float number \n”, yytext);}

[a-zA-Z] + {printf(“%s : is not integer or float number”, yytext);}

.|\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.

Efficiency: Under a little limitation the efficiency of this program is


100%.

Page 23 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 08

Experiment Title: Write a simple lex specification to recognize for the


positive and negative integer and float number.

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.

Efficiency: Under a little limitation the efficiency of this program is


100%.

Page 26 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 09

Experiment Title: Write a simple lex specification to recognize different


punctuation symbol.

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.

Efficiency: Under a little limitation the efficiency of this program is


100%.

Page 29 of 32
MOHAMMADPUR KENDRIYA COLLEGE, DHAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
CSE-327
Experiment No: 10

Experiment Title: Write a simple lex specification to recognize digit.

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.

Limitations: This program used only some of the reserved operators


and works on only C like statements.

Efficiency: Under a little limitation the efficiency of this program is


100%.

Page 32 of 32

You might also like