Вы находитесь на странице: 1из 6

/* 1a. Write a program in LEX to count the number of vowels and consonants in a given string. */ %{ /* No.

of Vowels, Consonants & Blank characters */ int nvow=0,ncons=0,nblank=0; %} %% [aeiouAEIOU] nvow++; /* Increment the no. of Vowels */ [b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] ncons++; /* Increment the no. of Consonants */ [ ] nblank++; /* Increment the no. of BlankSpace */ %% #include"stdio.h" int main() { printf("\nEnter the string : "); yylex(); printf("\n\nNo. of Vowels = %d",nvow); printf("\n\nNo. of Consonants = %d",ncons); printf("\n\nNo. of Blank Spaces = %d\n",nblank); return 0; }

2a. Write a LEX program to count number of lines, words, characters and blank spaces */ %{ int cha=0; int words=0; int lines=0; int blanks=0;

%} %% \n lines++ ; [ \t] cha++; blanks++; [a-zA-Z0-9] cha++; [a-zA-Z0-9]/[ \n] words++; cha++; . cha++; %% main() { yylex(); printf("characters=%d\n",cha); printf("words=%d\n",words); printf("lines=%d\n",lines); printf("blanks=%d\n",blanks); } 3a. Write a LEX program to count the number of positive & negative integers and fractions */ %{ #include"stdio.h" int posnum=0; int negnum=0; int posfra=0; int negfra=0; %} dig [0-9] %% \+?{dig}+ posnum++; -{dig}+ negnum++; \+?{dig}*\.{dig}+([eE][-+?]?{dig}+)? posfra++; -{dig}*\.{dig}+([eE][+-]?{dig}+)? negfra++; %% main()

yylex(); printf("%d",negnum); printf("\n%d",posnum); printf("\n%d",posfra); printf("\n%d",negfra);

5a. Write a LEX program to count number of printf's and scanf's in a given program */ %{ #include"stdio.h" int pcount=0; int scount=0; %} %% "printf" fprintf(yyout,"write");pcount++; "scanf" fprintf(yyout,"read");scount++; %% main() { yyin=fopen("tester.c","r"); yyout=fopen("otester","w"); yylex(); printf("\nprintf count=%d\nscanf count=%d\n",pcount,scount); fclose(yyin); fclose(yyout); }

8a. Write a LEX program to count the number of identifiers */

%{ int id_ct=0; %} SPC [ \t]* IDE [a-zA-Z_][a-zA-Z0-9_]* DEC "int "|"float "|"char "|"long "|"unsigned " %s DEFN %% {SPC}{DEC}{SPC} {BEGIN(DEFN);} <DEFN>{IDE}{SPC}\,{SPC} {id_ct++;} <DEFN>{IDE}{SPC}\;{SPC} {id_ct++;BEGIN(INITIAL);} <*>./\n+; .; %% main(int argc,char *argv[]) { yyin=fopen(argv[1],"r"); yylex(); printf("\nNumber of identifiers is %d\n",id_ct); }

* 7a. Write a LEX program to identify a compound statement */ %{ #include"stdio.h" int count=0; %} %% " [aA][nN][dD] " count ++; " [oO][rR] " count++; " [bB][uU][tT] " count++;

%% main() { yylex(); if( count != 0 ) printf("\nIt is a compound statement"); else printf("\nIts is a Simple statement"); }

* 6a. Write a LEX program to recognize a valid arithmetic expression. Count number of operators present and print them seperately. */ %{ /* No. of operators and operands */ int nop=0,noprnd=0,top=-1,stack[20]; %} %% [0-9]*|[a-zA-Z]* noprnd++; /* Increment no. of operand */ [+-/*%] nop++; /* Increment no. of operator */ "(" { stack[++top]='(';} ")" { if((stack[top]='(')&&(top!=-1)) top--; } %% #include"stdio.h" int main() { printf("\n*** Enter an expression ***\n"); yylex(); if ((nop != (noprnd - 1)) || ( top==-1)) printf("\nIt's an INVALID statement"); else { printf("\nIt's a VALID statement"); printf("\nNumber of operators = %d",nop);

} return 0;

Вам также может понравиться