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

COMPILER LAB

ASHISH KUMAR VERMA SEC: A ROLL NO.:22 SEM: 6 UNIVERSITY ROLL NO.: 0901010022
Page | 1

INDEX
Sl.no. Name of program Date Page no. 3 4 5 6 7 Signature

1 2 3 4 5

6 7 8

LEX program to identify key words and convert it to uppercase LEX program to count number of Identifiers and Keywords LEX program to identify consonants and vowels LEX Program to count the number of lines, words and letters. LEX program to read an input file and copy the content with line number to another file. LEX program to count the number of comment line LEX program to count the number of printf and scanf statement 8. LEX program to count the type of number

09/04/12 09/04/12 09/04/12 09/04/12 09/04/12

09/04/12 09/04/12 09/04/12

8 9 10

Page | 2

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; }

Page | 3

2. LEX program to count number of Identifiers and Keywords. %{ int count1=0; int count2=0; %} %% int I float I char {count++;printf("%s is keyword",yytext);} {letter}I({letter}I{digit})*{count2++;} [a-zA-Z]+ {printf("%s is not keyword",yytext);} .I\n {ECHO;} %% main() { yylex(); printf("%d no of keyword %d no of id",count1,count2); }

Page | 4

3. LEX program to identify consonants and vowels

%{ %} %% [aeiouAEIOU+ ,printf(it is vowel);[a-zA-Z+ ,printf(it is consonant);%% main() { yylex(); } int yywrap() { return 1; }

Page | 5

4. LEX Program to count the number of lines, words and letters.

%{ #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); }

Page | 6

5. LEX program to read an input file and copy the content with line number to another file.

%{ #include<stdio.h> #include<string.h> char line[20]; int count=0,i=0; FILE *out; %} %% ['\n'] { fprintf(out,"%d %s\n",++count,line);} (.*) { strcpy(line,yytext); }%% main() { yyin=fopen("in.c","r"); out=fopen("out.c","w"); yylex(); getch(); } int yywrap() { return 1; } Input file main() { int i=9, j=900,k=115; printf("%d,i+j+k); } Output file 1 main() 2{ 3 int i=9, j=900,k=115; 4 printf("%d,i+j+k); 5}
Page | 7

6. LEX program to count the number of comment line. %{ #include<stdio.h> int cc=0; %} %% /**a-zA-Z0-9' '\t\n+**\ cc++; //*a-zA-Z0-9' '\t]* cc++; %% main() { yyin=fopen(f1.c,r); yyout=fopen(f2.c,w); yylex(); fclose(yyin); fclose(yyout); printf(\n the number of comment line=%d\n,cc); } int yywrap() { return 1; }

Page | 8

7. LEX program to count the number of printf and scanf statement. %{ #include <stdio.h> int pf=0,sf=0; %} %% printf{ pf++; printf(yyout,"%s","writef"); } scanf{ sf++; printf(yyout,""%s","readf"); } %% main() { yyin=fopen("file1.1","r+"); yyout=fopen("file2.1","w"); yylex(); printf("number of printf is %d\n",pf); printf("numnber of sacanf is %d\n",sf); }

Page | 9

8. LEX program to count the type of number. %{ 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 the input:"); yylex(); printf("\n positive integer :%d",pi); printf("\n negative integer :%d",ni); printf("\n positive fraction :%d",pf); printf("\n negative fraction :%d",nf); }

Page | 10

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