You are on page 1of 7

Lab manual Compiler Construction

Submitted To: Mam Saba Zaib

Submitted By: Nasir Sadeeq

Roll no: 19

Date: 07_ September_ 2018
Class: BSCS (6th) Semester
Department: CS & IT
Task 1. Write a program to find the integer, alphabet and floating
number from the given input.
Code:
%option noyywrap

%{

#include<stdio.h>

%}

%%

[0-9]* {printf("found integer ", yytext);}

[a-z]* {printf("alphabet ", yytext);}

[0-9]\.[0-9]* {printf("found floating number ", yytext);}

%%

main()

{

printf("enter character or string or numerical value");

yylex();

}

Output:
Task 2. Write a program to find the integer, alphabet and floating number and
also print hello world message.
Code:
%option noyywrap

%{

#include<stdio.h>

%}

%%

[0-9]+ {printf("found integer ", yytext);}

[a-zA-Z0-9]+ {printf("found string ", yytext);}

[0-9]+\.[0-9]+ {printf("found floating number ", yytext);}

"hello world" {printf("found hello world message ", yytext);}

%%

main()

{

printf("enter character or string or numerical value");

yylex();

}

Output:
Task 3:Write a program to find the number of vowels and consonants from
given input.
Code:
%option noyywrap

%{

#include<stdio.h>

#include<conio.h>

int c=0;

int v=0;

%}

%%

[a|e|i|o|u|A|E|I|O|U] {v++;}

[a-zA-Z] {c++;}

. return 0;

%%

main()

{

printf("Enter a string\n");

yylex();

printf("Number of consonents=%d\nNnumber of vowels=%d\n",c,v);

getch();

}

Output:
Task 4. Write a program to find the number of positive integer, negative
integer, positive fraction and negative fraction.
Code:

%option noyywrap

%{

#include<stdio.h>

int pi=0;

int ni=0;

int pf=0;

int nf=0;

%}

%%

"+"?[0-9]+ {pi++;}

"-"[0-9]+ {ni++;}

"+"?[0-9]*"."[0-9]+ {pf++;}

"-"?[0-9]*"."[0-9]+ {nf++;}

. return 0;

%%

main()

{

printf("Enter a set of numbers\n");

yylex();

printf("Number of positive integer=%d\nNnumber of negative integer=%d\n",pi,ni);

printf("Number of positive fraction=%d\nNnumber of negative fracation=%d\n",pf,nf);

getch();

}
Output:

Task 5. Write a program to find the positive integer, negative integer, positive
fraction and negative fraction from the set of integer.
Code:
%option noyywrap

%{

#include<stdio.h>

int pi=0;

int ni=0;

int pf=0;

int nf=0;

%}

%%

"+"?[0-9]+ {printf("found positive integer",yytext);}

"-"[0-9]+ {printf("found negative integer",yytext);}

"+"?[0-9]*"."[0-9]+ {printf("found positive fraction",yytext);}

"-"?[0-9]*"."[0-9]+ {printf("found negative fraction",yytext);}

. return 0;

%%

main()

{

printf("Enter a set of numbers\n");

yylex();
}

Output: