Click here to load reader
Upload
bhargavi-chowdary
View
659
Download
0
Embed Size (px)
Citation preview
Write a lex program to count no of identifiers, keywords ,digits.
%{#include<stdio.h>int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;%}%% \n { lines++; words++;}[\t ' '] words++;[A-Z] c_letters++;[a-z] s_letters++;[0-9] num++;. spl_char++;%%
main(void){yyin= fopen("myfile.txt","r");yylex();total=s_letters+c_letters+num+spl_char;printf(" This File contains ...");printf("\n\t%d lines", lines);printf("\n\t%d words",words);printf("\n\t%d small letters", s_letters);printf("\n\t%d capital letters",c_letters);printf("\n\t%d digits", num);printf("\n\t%d special characters",spl_char);printf("\n\tIn total %d characters..\n",total);} int yywrap(){return(1);}
LEX Program to identify Keywords and convert it into uppercase.
%{#include<stdio.h>int i;%}keyword main|int|scanf|printf|if|else%%
{keyword} { for(i=0;i<yyleng;i++) printf("%c",toupper(yytext[i])); }%%
main(){yyin=fopen("num.c","r");yylex();}
int yywrap(){return 1;}OutputLet num.c contains following program fragment.
main(){int num;scanf("%d",&num);if(num%2)printf("Odd");else printf("Even")}
The output will be,
MAIN(){INT num;SCANF("%d",&num);IF(num%2)PRINTF("Odd");ELSE PRINTF("Even")
}
Lex program to count number of vowels and consonanta
%{ int v=0,c=0; %} %% [aeiouAEIOU] v++; [a-zA-Z] c++; %% main() { printf("ENTER INTPUT : \n"); yylex(); printf("VOWELS=%d\nCONSONANTS=%d\n",v,c); }
Lex program to count the type of numbers
%{ int pi=0,ni=0,pf=0,nf=0; %} %% \+?[0-9]+ pi++; \+?[0-9]*\.[0-9]+ pf++; \-[0-9]+ ni++; \-[0-9]*\.[0-9]+ nf++; %% main() { printf("ENTER INPUT : "); yylex(); printf("\nPOSITIVE INTEGER : %d",pi); printf("\nNEGATIVE INTEGER : %d",ni); printf("\nPOSITIVE FRACTION : %d",pf); printf("\nNEGATIVE FRACTION : %d\n",nf); }
Lex program to count the number of printf and scanf statements%{ #include "stdio.h"
int pf=0,sf=0; %} %% printf { pf++; fprintf(yyout,"%s","writef"); } scanf { sf++; fprintf(yyout,"%s","readf"); } %% main() { yyin=fopen("file1.l","r+"); yyout=fopen("file2.l","w+"); yylex(); printf("NUMBER OF PRINTF IS %d\n",pf); printf("NUMBER OF SCANF IS %d\n",sf); }
Lex program to find simple and compound statements
%{ }% %% "and"| "or"| "but"| "because"| "nevertheless" {printf("COMPOUNT SENTANCE"); exit(0); } . ; \n return 0; %% main() { prntf("\nENTER THE SENTANCE : "); yylex(); printf("SIMPLE SENTANCE"); }
Lex program to count the number of identifiers
%{ #include<stdio.h>
int id=0,flag=0; %} %% "int"|"char"|"float"|"double" { flag=1; printf("%s",yytext); } ";" { flag=0;printf("%s",yytext); } [a-zA-Z][a-zA-z0-9]* { if(flag!=0) id++; printf("%s",yytext); } [a-zA-Z0-9]*"="[0-9]+ { id++; printf("%s",yytext); } [0] return(0); %% main() { printf("\n *** output\n"); yyin=fopen("f1.l","r"); yylex(); printf("\nNUMBER OF IDENTIFIERS = %d\n",id); fclose(yyin); }
int yywrap() {
return(1); }
Lex program to count the number of words,characters,blank spaces and lines
%{ int c=0,w=0,l=0,s=0; %} %% [\n] l++; [' '\n\t] s++; [^' '\t\n]+ w++; c+=yyleng; %% int main(int argc, char *argv[]) { if(argc==2) { yyin=fopen(argv[1],"r"); yylex(); printf("\nNUMBER OF SPACES = %d",s); printf("\nCHARACTER=%d",c); printf("\nLINES=%d",l); printf("\nWORD=%d\n",w);
} else printf("ERROR"); }
Lex program to count the number of comment lines
%{ #include<stdio.h> int cc=0; %} %% "/*"[a-zA-Z0-9' '\t\n]*"*/" cc++; "//"[a-zA-Z0-9' '\t]* cc++; %% main() { yyin=fopen("f1.l","r"); yyout=fopen("f2.l","w"); yylex(); fclose(yyin); fclose(yyout);
printf("\nTHE NUMBER OF COMMENT LINES = %d\n",cc); }
Lex program to check the validity of arithematic statement
%{ #include<stdio.h> int opr=0,opd=0; int n; %} %% [\+\-\*\/] { printf("OPERATORS ARE %s\n",yytext); opr++; } [a-zA-Z]+ { printf("OPERANDS ARE %s\n",yytext); opd++; } [0-9]+ { printf("OPERANDS ARE %s\n",yytext); opd++; } [a-zA-Z]+\+\-\*\/[a-zA-Z]+ { n=0; } [0-9]+\+\-\*\/[0-9]+ { n=0; }
%% main() { printf("\nENTER THE EXPRESSION : \n"); yylex(); printf("\nNUMBER OF OPERATORS ARE %d",opr); printf("\nNUMBER OF OPERANDS ARE %d",opd); if((n==0)&&(opd==opr+1)) printf("\nVALID EXPRESSION\n"); else printf("\nINVALID EXPRESSION\n"); }
Lex program to find the number of constants
%{ #include<stdio.h> int cons=0; %} %% [0-9]+ { printf("\n%s",yytext); cons++; } . ;
%% main(int argc,char *argv[]) { if(argc==2) {yyin=fopen(argv[1],"r"); yylex(); printf("\nNUMBER OF CONSTANTS : %d\n",cons); } else printf("\nERROR"); }
Write a lex program to count the number of Positive numbers, Negative numbers & Fractions
%{ int postiveno=0; int negtiveno=0; int positivefractions=0; int negativefractions=0;%}
DIGIT [0-9]%%
\+?{DIGIT}+ postiveno++;-{DIGIT}+ negtiveno++;
\+?{DIGIT}*\.{DIGIT}+ positivefractions++;-{DIGIT}*\.{DIGIT}+ negativefractions++;. ; %%
main(){ yylex(); printf("\nNo. of positive numbers: %d",postiveno); printf("\nNo. of Negative numbers: %d",negtiveno); printf("\nNo. of Positive fractions: %d",positivefractions); printf("\nNo. of Negative fractions: %d\n",negativefractions);}