Professional Documents
Culture Documents
LEX
Patterns
r?
r{2,5}
r{2,} r{4} \0 \123
\x2a
Patterns
<s>r
<s1,s2,s3>r
<*>r
<<EOF>> <s1,s2><<EOF>>
Special Directives
REJECT directs the scanner to proceed on to the second best rule which matched the input (or a prefix of the input). Multiple REJECT's are allowed, each one finding the next best choice to the currently active rule.
Example
%{
%} %%
int s=0;
%%
main() { yylex(); printf(%d\n",s); return 0; }
Lex Variables
yyin
Of the type FILE*. This points to the current file being parsed by the lexer.
yyout
yytext
yyleng yylineno
Gives the length of the matched pattern. Provides current line number
Lex Functions
yylex() yywrap()
The function that starts the analysis. In the C program generated by the lex specification file, when yylex() finished with the first file, it calls yywrap(), which opens the next file, and yylex() continues. When yywrap() has exhausted all the command line arguments, it returns 1, and yylex() returns with value 0.
unput(c)
input()
Returns all but the first n characters of the current token back
For example, on the input "foobar" the following will write out
"foobarbar
%% foobar [a-z]+ ECHO; yyless(3); ECHO;
yymore() tells the scanner that the next time it matches a rule, the corresponding token should be appended onto the current value of yytext rather than replacing it.
For example, given the input "mega-kludge" the following will write "mega-mega- kludge" to the output
%% megakludge
ECHO; yymore();
ECHO;
First "mega-" is matched and echoed to the output. Then "kludge" is matched, but the previous "mega-" is still hanging around at the beginning of yytext so the ECHO for
Start Conditions
Flex provides a mechanism for conditionally activating rules. Any rule whose pattern is prefixed with "<sc>" will only be active when the scanner is in the start condition named "sc".
<STRING>[^"]*
will be active only when the scanner is in the "STRING" start condition
Start conditions declared using %s are inclusive, then rules with no start conditions at all will also be active.
Start conditions declared using %x are inclusive, then only rules qualified with the start condition will be active.
Begin
BEGIN(0)
%x SPECIAL
%% if ( enter_special ) BEGIN(SPECIAL); <SPECIAL>[a-z] {printf(letter);} %%
Example
<TABL>"<\/table>"
<TABL><tr> <REC><\/tr> <REC><td> <DATA><\/td>
BEGIN INITIAL;
BEGIN REC; BEGIN TABL; BEGIN DATA; BEGIN REC; %% main()
<DATA>.
<DATA,REC,TABL>[ \t\n]
ECHO;
;
{
yylex(); }
int lineno=1;
%} line .*\n %%
A Lex program that finds a word from the given file and replaces
%{ #include<stdio.h> #include<string.h> FILE *ff,*fr; char p[20],q[20],r[20],fname[20]; %} word [a-zA-Z]+ %% {word} { if(strcmp(p,yytext)==0) fprintf(fr,q); else fprintf(fr,yytext); }
{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); }
5 {fprintf(fr,"V ");} 6|7|8 { z=atoi(yytext); {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); }
%% int main(int argc,char *argv[]) { strcpy(fname,argv[1]); ff=fopen(fname,"r+"); fr=fopen("rep.txt","w+"); yyin=ff; yylex(); return(0); }
B0 = "SSN"
C0 = "HW1" D0 = "HW2 Name SSN Scott Smith 123445678 Sam Sneed 999887777 HW1 82 92 HW2 44.2 84
{printf("<table>\n<tr>\n");} {printf("</tr>\n<tr>\n");}
; {printf("<td> "); ECHO; printf("</td>\n");} {printf("<td> "); ECHO; printf("</td>\n");} {printf("<td> "); yytext[yyleng1]= ' '; printf("%s </td>\n",yytext+1);} ; ; ECHO;
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
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 <stdio.h> #include <errno.h> int file_num; int file_num_max; char **files; extern int errno; %} %% (ftp|http):\/\/[^ \n<>"]* printf("%s\n",yytext); .|\n ; %% int main(int argc, char *argv[]) { file_num=1; file_num_max = argc; files = argv; if ( argc > 1 ) { if ( (yyin = fopen(argv[file_num],"r")) == 0){ perror(argv[file_num]); exit(1);
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;
"ftp://ftp.funet.fi/pub/Linux/" { printf("ftp://ftp.monash.edu.au/pub/linux/"); BEGIN COPY; } "http://weather/" { printf("http://www.bom.gov.au/"); BEGIN COPY; } "http://bypass." { printf("http://"); BEGIN COPY; } "ftp://bypass." { printf("ftp://"); BEGIN COPY; } <COPY>[^ \n] ECHO; <COPY>" " BEGIN SKIP; . BEGIN SKIP; <SKIP>. ; <*>\n { putchar('\n'); fflush(stdout); BEGIN 0; } %%
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 letterd. 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 fractional numbers. Convert all words to uppercase words Delete all alphanumeric words Write resulting content to a separate file.
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. 2. If the first letter is a vowel, just add ay to the end of the word. All nonletters are copied intact to the output.
Thank yu!
31 August 2012
42