0% found this document useful (0 votes)
154 views

Sample Lex Programs

The document discusses several Lex programs to recognize different patterns in source code. It provides problem statements, descriptions and source code for Lex programs that recognize keywords, strings ending in 00, strings starting or ending with 'k', assigning line numbers, and recognizing numbers with 1 in the 5th position from the right.

Uploaded by

sundermantu06
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

Sample Lex Programs

The document discusses several Lex programs to recognize different patterns in source code. It provides problem statements, descriptions and source code for Lex programs that recognize keywords, strings ending in 00, strings starting or ending with 'k', assigning line numbers, and recognizing numbers with 1 in the 5th position from the right.

Uploaded by

sundermantu06
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.Problem Statement : Lex programs to recognize Keywords.

Description : token can look like anything that is useful for processing an input text stream or
text file.
Source Code:
%{
#include<stdio.h>
%}
%%
int|float|char { printf(" data type: %s",yytext); }
%%
int main()
{
yylex();
return(0);
}
int yywrap()
{
}
2.Problem Statement : Lex programs to recognize String ending with 00.
Description : token can look like anything that is useful for processing an input text stream or
text file.
Source Code:
%{
#include<stdio.h>
%}
%%
[a-z A-Z 0-9]++00 { printf("string is acepted",yytext);}
.* { printf("not acepted",yytext);}
%%
int main()
{
yylex();
return(0);
}
int yywrap()
{
}
3.Problem Statement : Program to recognize the strings which are starting or ending with ‘k’.
Description : token can look like anything that is useful for processing an input text stream or
text file.
Source Code:
%{
#include<stdio.h>
%}
begin-with-k k.*
end-with-k .*k
%%
{begin-with-k} { printf(" %s is a word that begin with k",yytext);}
{end-with-k} { printf(" %s is a word that end with k",yytext);}
%%
int main()
{
yylex();
return(0);
}
int yywrap()
{
}
4.Problem Statement : program to assign line numbers for source code.
Description : token can look like anything that is useful for processing an input text stream or
text file.
Source Code:
%{
#include<stdio.h>
int lineno=0;
%}
line .*\n
%%
{line} { printf("%d .%s",lineno++,yytext); }
%%
int main(int argc,char **argv)
{
if(argc>1)
{
FILE *file;
file=fopen(argv[1],"r");
if(!file)
{
printf("could not open %s\n",argv[1]);
exit(0);
}
yyin=file;
}
yylex();
printf("\n\n");
return 0;
}
int yywrap()
{
return 0;
}
5.Problem Statement : Program to recognize the numbers which has 1 in its 5th position from
right.
Description : token can look like anything that is useful for processing an input text stream or
text file.
Source Code:
%{
#include<stdio.h>
%}
%%
[0-9]*1[0-9]{4} { printf("acepted:");}
[0-9]*1[0-9] { printf("NOT acepted:");}
%%
int main()
{
yylex();
return(0);
}
int yywrap()
{
}

Exercise
1. Write Lex specification to replace space with $
2. Write Lex specification to identify the number divide by 9
3. Write Lex specification identify identifier and functional identifier
4. Write Lex specification identify comment
5. Write Lex specification identify real number
6.Problem Statement : Implement lexical analyzer in Lex.
Description : The lexical analysis programs written with Lex accept ambiguous specifications
and choose the longest match possible at each input point. If necessary, substantial look a head is
performed on the input, but the input stream will be backed up to the end of the current partition,
so that the user has general freedom to manipulate it.
Source Code:
%{
int COMMENT=0;
%}
identifier [a-z A-Z][a-z A-Z 0-9]*
%%
#.* {printf("\n %s is a PREPROCESSOR DIRECTIVE",yytext);}
int|float|char|double|while|for|do|if|break|continue|void|switch|case|long|struc
t|const|typedef|return|else|goto|main {printf("\n\t %s is a KEYWORD",yytext);}
"%*" {COMMENT=1;}
"*/" {COMMENT=0;}
{identifier}\( {if(!COMMENT) printf("\n\n FUNCTION \n\t%s",yytext); }
\{ {if(!COMMENT) printf("\n BLOCK BEGINS");}
\} {if(!COMMENT) printf("\n BLOCK ENDS");}
{identifier}(\ [[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
\".*\" { if(!COMMENT) printf("\n\t%s is a STRING",yytext);}
[0-9]+ { if(!COMMENT) printf("\n\t%s is a NUMBER",yytext);}
\)(\;)? {if(!COMMENT) printf("\n\t");
ECHO;
printf("\n");}
\(ECHO;
= {if(!COMMENT) printf("\n\t is an ASSIGNMENT OPERATOR",yytext);}
\<=|\>=|\<|==|\> {if(!COMMENT) printf("\n\t %s is a RELATIONAL OPERATOR",yytext)
;}
\b|\t {if(!COMMENT) printf("\n white space",yytext);}
\n { ; }
%%
int main(int argc,char **argv)
{
if(argc>1)
{
FILE *file;
file=fopen(argv[1],"r");
if(!file)
{
printf("could not open %s\n",argv[1]);
exit(0);
}
yyin=file;
}
yylex();
printf("\n\n");
return 0;
}
int yywrap()
{
return 0;
}

You might also like