LEXICAL ANALYZER USING LEX TOOL
AIM:
To write a program for implementing a Lexical analyzer using LEX tool in Linux platform.
ALGORITHM:
Step 1: Lex program contains three sections: definitions, rules, and user subroutines.
Step 2:Each section must be separated from the others by a line containing only the
delimiter, %%.
The format is as follows:
definitions
%%
rules
%%
user_subroutines
In definition section, the variables make up the left column, and their definitions make up the right column. Any C statements should be enclosed in %{..}%. Identifier is defined such that the first letter of an identifier is alphabet and remaining letters are alphanumeric.
Step 3: In rules section, the left column contains the pattern to be recognized in an input file
to yylex(). The right column contains the C program fragment executed when that
pattern is recognized.
Step 4: Each pattern may have a corresponding action, that is, a fragment of C source
code to execute when the pattern is matched.
Step 5: When yylex() matches a string in the input stream, it copies the matched text to an
external character array, yytext, before it executes any actions in the rules section.
Step 6: In user subroutine section, main routine calls yylex(). yywrap() is used to get more
input.
Step 7: The lex command uses the rules and actions contained in file to generate a program,
lex.yy.c, which can be compiled with the cc command.
PROGRAM:
%{
#include<stdio.h>
#include<string.h>
int i;
%}
%%
[a-z A-Z]* {
for(i=0;i<=yyleng;i++)
{
if((yytext[i]=='a')&&(yytext[i+1]=='b')&&(yytext[i+2]=='c'))
{
yytext[i]='A';
yytext[i+1]='B';
yytext[i+2]='C';
}
}
printf("%s",yytext);
}
[\t]* return;
.* {
ECHO;
}\n {printf("%s",yytext);
}
%%
main()
{
yylex();
}
int yywrap()
{
return 1;
}
OUTPUT:
[CSE@localhost ~]$ lex lex 1.l
[CSE@localhost ~]$ cc lex.yy.c
[CSE@localhost ~]$. /a.out
abc
ABC
5 (b) Finding vowels and consonant in a string:
%{
int vowel_cnt=0,consonant_cnt=0;
%}
vowel [aeiou]+
consonant [^aeiou]
eol \n
%%
{eol} return 0;
[\t]+ ;
{vowel} {vowel_cnt++;}
{consonant} {consonant_cnt++;}
%%
int main()
{
printf("\n Enter some input string:\n");
yylex();
printf("Vowels=%d and consonant=%d\n",vowel_cnt,consonant_cnt);
return 0;
}
int yywrap()
{
return 1;
}
OUTPUT:
[CSE@localhost ~]$ lex lex 2.l
[CSE@localhost ~]$ cc lex.yy.c
[CSE@localhost ~]$. /a.out
Enter some input string:
parth
Vowels=1 and consonant=4
5.(c)
Finding the capital:
%{
%}
%%
[A-Z]+[ \t\n\.\,] {printf("%s",yytext);
} ;
%%
main()
{
printf("\n Enter some input with capital words in between :\n");
yylex();
}
int yywrap()
{
return 1;
}
OUTPUT:
[CSE@localhost ~]$ lex lex2.l
[CSE@localhost ~]$ cc lex.yy.c
[CSE@localhost ~]$. /a.out
Enter some input with capital words in between:
I am PROUD of you
–
INDIA
I PROUD INDIA
5.(d)
AIM:
It is used to display the Keywords and identifiers in the given program:
%{
int COMMENT=0;
%}
identifier[a-z|A-Z][a-z|A-Z|0-9]*
%%
#.* {printf("\n%s is a preprocesor directive",yytext);}
int {printf("\n\t%s is a keyword",yytext);}
float {printf("\n\t%s is a keyword",yytext);}
double {printf("\n\t%s is a keyword",yytext);}
char {printf("\n\t%s is a keyword",yytext);}
if {printf("\n\t%s is a keyword",yytext);}
else {printf("\n\t%s is a keyword",yytext);}
while {printf("\n\t%s is a keyword",yytext);}
do {printf("\n\t%s is a keyword",yytext);}
return {printf("\n\t%s is a keyword",yytext);}
break {printf("\n\t%s is a keyword",yytext);}
continue {printf("\n\t%s is a keyword",yytext);}
void {printf("\n\t%s is a keyword",yytext);}
switch {printf("\n\t%s is keyword",yytext);}
for {printf("\n\t%s is a keyword",yytext);}
typedef {printf("\n\t%s is a keyword",yytext);}
struct {printf("\n\t%s is a keyword",yytext);}
goto {printf("\n\t%s is a keyword",yytext);}
"/*" {COMMENT=1;}
"*/" {COMMENT=0;}
{identifier}\( {if(!COMMENT)
printf("\nFUNCTIONS\n\t%s",yytext);
}\{ {if(!COMMENT)
printf("\nBLOCK BEGINS");}\
} {if(!COMMENT)
printf("\nBLOCK ENDS");}
{identifier} {if(!COMMENT)
printf("\n%sIDENTIFIER",yytext);}
{identifier}(\[[0-9]*\])?\
( {if(!COMMENT)
printf("\n%sIDENTIFIER",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%s is an assignment operator",yytext);}\> if(!COMMENT)
printf("n\t%s is a relational operator",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(1);
}
yyin=file;
}
yylex();
printf("\n\n");
return 0;
}
int yywrap()
{
return 0;
}
OUTPUT:
[CSE@localhost ~]$ lex lex5.l
[CSE@localhost ~]$ gcc lex.yy.c-II
[CSE@localhost ~]$. /a.out
FUNCTIONS
main()
{
BLOCK BEGINS
int a,b,c;
int is a keyword
aIDENTIFIER,
bIDENTIFIER,
cIDENTIFIER;
c=sum(a+b);
RESULT:
Thus the program for implementing Lexical analyser using LEX tool is executed and verified.
0 Comments