BEGIN DAT DATA; BEGIN REC; main() . ECHO; { ; yylex(); %% } Line no in text file %{ /* */ int lineno=1; %} line .*\n %% {line} { printf("%5d %s",lineno++,yytext); } %% main(int argc, char*argv[]) { extern FILE *yyin; yyin=fopen(argv[1],"r"); yylex(); return 0; } A Lex program that finds a word from the given file and replaces it with other word. %{ #include {eol} {fprintf(fr,yytext);} . {fprintf(fr,yytext);} %% int main(int argc,char *argv[]) { strcpy(fname,argv[1]); strcpy(p,argv[2]); strcpy(q,argv[3]); ff=fopen(fname,"r+"); fr=fopen("rep.txt","w+"); yyin=ff; yylex(); return(0); } Replace Arabic Nos with Roman %{ #include 5 {fprintf(fr,"V ");} 6|7|8 { z=atoi(yytext); z=atoi(yy text); {fprintf(fr,"V ");} while(z!=5) { {fprintf(fr,"I "); z--; } } 9 {fprintf(fr,"IX ");} 10 {fprintf(fr,"X ");} . {fprintf(fr,yytext);} %% int main(int argc,char *argv[]) { strcpy(fname,argv[1]); ff=fopen(fname,"r+"); fr=fopen("rep.txt","w+"); yyin=ff; yylex(); return(0); } Date Format Change %{ #include {fprintf(fr {fprintf (fr,"19"); ,"19"); fprintf(fr,yytext);} {fprintf(fr {fprintf( fr,"20"); ,"20"); fprintf(fr,yytext);} fprintf(fr,yytext);} %% int main(int argc,char *argv[]) { strcpy(fname,argv[1]); ff=fopen(fname,"r+"); fr=fopen("rep.txt","w+"); yyin=ff; yylex(); return(0); } The following lex specification file is used to generate a C program which is used to generate a html file from a data file. A0 = "Name" B0 = "SSN" C0 = "HW1" D0 = "HW2” … Name SSN HW1 HW2 Scott Smith 123445678 82 44.2 Sam Sneed 999887777 92 84 %% ^A0 ^A[09]* ^[BZ][09]* [09]* [09]*"."[09]* \"[^"]*\" {printf("
This lex specification file is used to generate a C program which counts how many times a given alphabet is next to another alphabet in the given input %{ #include main(int argc, char*argv[]) { extern FILE *yyin; int i,j; for(i=0;i<26;i++) for(j=0;j<26;j++)F[i][j]=0; yyin=fopen(argv[1],"r"); yylex(); for(i=0;i<26;i++){ for(j=0;j<26;j++) printf("%d", F[i][j]); printf("\n"); } return 0; } %{ #include Yywrap() Example } } while( yylex() ) ; return 0; } int yywrap() { fclose(yyin); if ( ++file_num < file_num_max ) { if ( (yyin = fopen(files[file_num],"r")) == 0){ perror(files[file_num]); exit(1); } return 0; } else { return 1; A lex-based squid redirector %x SKIP %x COPY %option always-interactive %% "http://www.debian.org/" | "http://www.yahoo.com/" { yytext[yyleng-1] = '\0'; printf("%s.au/",yytext); BEGIN COPY; COPY; } "http://"(www.)?"altavista.digital.com/" { printf("http://www.altavista.yellowpages.com.au/"); BEGIN COPY; COPY; } "ftp://sunsite.anu.edu.au/pub/linux/" | "ftp://sunsite.unc.edu/pub/Linux/" | "ftp://ftp.funet.fi/pub/Linux/" { printf("ftp://ftp.monash.edu.au printf("ftp://ftp.monash.edu.au/pub/linux/"); /pub/linux/"); BEGIN COPY; } "http://weather/" { printf("http://www.bom.gov.au/"); BEGIN COPY; } "http://bypass." "http://bypass. " { printf("http://"); printf("http://" ); BEGIN COPY; COPY; } "ftp://bypass." "ftp:// bypass." { printf("ftp://"); printf("ft p://"); BEGIN COPY; COPY; } Write a LEX program to count the number of palindrome string in a given file and write them into a separate file. Write a LEX program to read 3 Roman numbers and output its equivalent numbers and sum in decimal. Ex: Input: VI IX L Output: 6 + 9 + 50 = 65 Write a LEX program to read an expression in postfix form and evaluate the value of the expression Ex: 342+* is 18 Write a lex program to read a text file and reverse all words that begin with letter ‘a’ and end with letter ‘d’. Display the longest of such words. Write a LEX program to count number of even and odd numbers. Output the odd numbers to odd.txt and even numbers to even.txt files. Also display the largest odd and even numbers Write a lex program to read a text file and remove all words that begin with letter ‘a’ and end with letter‘d’. Write the remaining words to a separate file. Write a lex program to read a text file and do the following Add 1 to all integers. Add 0.5 to all all fractional fractional numbers. numbers. Convert all words to uppercase words Delete all alphanumeric words Write resulting content to a separate file. Encryption and decryption Write a Lex program that converts a file to "Pig latin." Specifically, assume the file is a sequence of words (groups of letters) separated by whitespace. Every time you encounter a word: 1. If the first letter is a consonant, move it to the end of the word and then add ay. ay. 2. If the first letter is a vowel, just add ay to the end of the word. All nonletters are copied copied intact to the output. Thank yu…! Sign In
Our partners will collect data and use cookies for ad personalization and measurement. Learn how we and our ad partner Google, collect and use data. Agree & close
|