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

COMPILER DES. LAB MANUAL

Uploaded by

rishav ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

COMPILER DES. LAB MANUAL

Uploaded by

rishav ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

count the no of vowels and consonants

%{
#include<stdio.h>
int vowels=0;
int consonants=0;
%}
%%
[aeiouAEIOU] {vowels++;}
[a-zA-Z] {consonants++;}
%%
int yywrap(){
return 1;
}
int main(){
printf("Enter the string at end press^d\n");
yylex();
printf("No of vowels: %d\nNo of consonants: %d\n",vowels,consonants);
}
-------------------------------------------------------------------------
-----------------------------------------------------------------------
count the no of lines,spaces
%{
#include<stdio.h>
int word=0,lines=0,spaces=0,cc=0;
%}
%%
[^\t\n,\.:] {word++; cc=cc+yyleng;}
[\n] {lines++; cc=cc+yyleng;}
[ \t] {spaces++; cc=cc++yyleng;}
%%
int yywrap(){
return 1;
}
int main(int argc,char* argv[]){
if(argc!=2){
printf("Usage: <./a.out> <sourcefile>\n");
exit(0);
}
yyin=fopen(argv[1],"r");
yylex();
printf("No of word=%d\nNo of character=%d\nNo of spaces=%d\nNo of
lines=%d \n",word,character,spaces,lines);
return 0;
}
-------------------------------------------------------------------------
-----------------------------------------------------------------------
count the +ve and-ve fractions
%{
#include<stdio.h>
int positiveNumber=0;
int negativeNumber=0;
int positiveFraction=0;
int negativeFraction=0;
%}
DIGIT [0-9]
%%
\+?{DIGIT}+ {positiveNumber++;}
-{DIGIT}+ {negativeNumber++;}
\+?{DIGIT}*.{DIGIT}+ {positiveFraction++;}
-{DIGIT}*.{DIGIT}+ {negativeFraction++;}
.;
%%

int yywrap(){
return 1;
}
int main(){
yylex();
printf("No of positive integer is: %d\n",positiveNumber);
printf("No of negative integer is: %d\n",negativeNumber);
printf("No of positive fraction is: %d\n",positiveFraction);
printf("No of positive fraction is: %d\n",negativeFraction);
return 0;
}
-------------------------------------------------------------------------
-----------------------------------------------------------------------
count the no of comments
%{
#include<stdio.h>
int com=0;
%}
%%
"/*"[a-zA-Z0-9\t\n ]*"*/" {com++;}
"//"[a-zA-Z0-9\t\n ]*"\n" {com++;}
%%
int yywrap(){
return 1;
}
int main(int argc,char* argv[2]){
if(argc!=2){
printf("Usage: <./a.out> <sourcefile>\n");
exit(0);
}
yyin=fopen(argv[1],"r");
yylex();
printf("No of comments: %d\n",com);
return 0;
}
-------------------------------------------------------------------------
-----------------------------------------------------------------------
replace printf and scanf with readf and writef
%{
#include<stdio.h>
int pf=0,sf=0;
%}
%%
printf {fprintf(yyout,"readf");pf++;}
scanf {fprintf(yyout,"writef");sf++;}
%%
int yywrap(){
return 1;
}
int main(int argc,char* argv[]){
if(argc!=3){
printf("Usage ./a.out input.txt output.txt\n");
exit(0);
}
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
printf("No of printf is: %d\nNo of scanf is: %d\n",pf,sf);
return 0;
}
-------------------------------------------------------------------------
-----------------------------------------------------------------------
arithemetic expression
/* Lex program to recognize valid arithmetic expression

and identify the identifiers and operators */


%{
#include <stdio.h>
#include <string.h>

int operators_count = 0, operands_count = 0, valid = 1, top = -1, l =


0, j = 0;

char operands[10][10], operators[10][10], stack[100];


%}
%%

"(" {

top++;

stack[top] = '(';
}

"{" {

top++;

stack[top] = '{';
}

"[" {

top++;

stack[top] = '[';
}

")" {
if (stack[top] != '(') {

valid = 0;

else if(operands_count>0 && (operands_count-operators_count)!=1){

valid=0;

else{

top--;

operands_count=1;

operators_count=0;

}
}

"}" {

if (stack[top] != '{') {

valid = 0;

else if(operands_count>0 && (operands_count-operators_count)!=1){

valid=0;

else{

top--;

operands_count=1;

operators_count=0;

}
}

"]" {

if (stack[top] != '[') {

valid = 0;
}

else if(operands_count>0 && (operands_count-operators_count)!=1){

valid=0;

else{

top--;

operands_count=1;

operators_count=0;

"+"|"-"|"*"|"/" {

operators_count++;

strcpy(operators[l], yytext);

l++;
}
[0-9]+|[a-zA-Z][a-zA-Z0-9_]* {

operands_count++;

strcpy(operands[j], yytext);

j++;
}
%%

int yywrap()
{

return 1;
}

int main()
{

int k;
printf("Enter the arithmetic expression: ");

yylex();

if (valid == 1 && top == -1) {

printf("\nValid Expression\n");

else

printf("\nInvalid Expression\n");

return 0;
}
-------------------------------------------------------------------------
-----------------------------------------------------------------------
simple sentence,compound sentence
%{
#include<stdio.h>
int flag=0;
%}
%%
[^\t\n][aA][nN][dD] {flag=1;}
\n {return 0;}
%%
int yywrap(){
return 1;
}
int main(){
yylex();
if(flag==0){
printf("Simple sentence\n");
}else{
printf("Compound sentence\n");
}
}
-------------------------------------------------------------------------
-----------------------------------------------------------------------
no.of identifiers
%{
#include<stdio.h>
int count=0;
%}
op [+-*/]
letter [a-zA-Z]
digit [0-9]
id {letter}*|({letter}{digit})+
notid ({digit}{letter})+
%%
[\t\n]+
("int")|("float")|("char")|("string")|("printf")|("scanf")|("case")|
("default")|("if")|("for") {printf("%s is a keyword\n",yytext);}
{id} {printf("%s is a indentifier\n",yytext); count++;}
{notid} {printf("%s is not an identifier\n",yytext);}
%%
int yywrap(){
return 1;
}
int main(int argc, char* argv[]){
if(argc!=2){
printf("Usage: ./a.out <sourcefile>\n");
exit(0);
}
yyin=fopen(argv[1],"r");
yylex();
printf("No of identifier is: %d\n",count);
return 0;
}
-------------------------------------------------------------------------
-----------------------------------------------------------------------

___________________
| filename.l |
| lex filename.l |
| gcc lex.yy.c |
| ./a.out |
|___________________|

You might also like