Compiler Design
Compiler Design
LEX:
Lex is a program that generates lexical analyzer. It is used with YACC parser generator.
The lexical analyzer is a program that transforms an input stream into a sequence of tokens.
It reads the input stream and produces the source code as output through implementing the lexical
analyzer in the C program.
The function of Lex is as follows:
Firstly lexical analyzer creates a program lex.1 in the Lex language. Then Lex compiler runs the
lex.1 program and produces a C program lex.yy.c.
Finally C compiler runs the lex.yy.c program and produces an object program a.out.
a.out is lexical analyzer that transforms an input stream into a sequence of tokens.
Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
2. Design a LEX Code to identify and print valid Identifier of C/C++ in given Input
pattern.
SOURCE CODE:
%{
#include<stdio.h>
%}
%%
^[a-zA-Z_][a-zA-Z_0-9]* {printf("Valid Identifier");}
^[^a-zA-Z_]{printf("Invalid Identifier");}
%%
int main()
{
printf("Enter the identifier :");
yylex();
}
int yywrap()
{
return 1;
}
OUTPUT:
Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
3. Design a LEX Code to identify and print integer and float value in given Input
pattern.
SOURCE CODE;
%{
#include<stdio.h>
%}
%%
[0-9]+ {printf("Integer Number");}
[0-9]*.[0-9]+ {printf("Float Number");}
.* {printf("Invalid Number");}
%%
int main(void)
{
printf("Enter the number");
yylex();
}
int
yywrap()
{return 1;
}
OUTPUT:
Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
%%
while|for|if|else|for|int {printf("Keyword\n");}[(){},;]
{printf("Seperator\n");}
([a-zA-Z_])+|[a-zA-Z_0-9]* {printf("Identifier\n");}
[+-=*%] {printf("Operator\n");}
%%
int
main(){
yylex();
}
int
yywrap(){r
eturn 1;
}
OUTPUT:
Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
%%
[\n] {ln++; ch+=yyleng;}
[\t" "] {sp++; ch+=yyleng;}
[^\n\t" "]+ {wd++; ch+=yyleng;}
%%
int
main(){ yyin=fopen("input.txt",
"r");yylex();
printf("\nTotal number of line:: %d",ln);
printf("\nTotal number of space:: %d",sp);
printf("\nTotal number of character:: %d",ch);
printf("\nTotal number of word:: %d\n",wd);
return 0;
}
int yywrap()
{
return 1;
}
Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
OUTPUT:
Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
6. Design a LEX Code to replace white spaces of ‘Input.txt’ file by a single blank
character into ‘Output.txt’ file.
SOURCE CODE:
%{
#include<stdio.h>
%}
%%
[\t" "]+ fprintf(yyout," ");
.|\n fprintf(yyout,"%s",yytext);
%%
int
yywrap()
{return 1;
}
int main(void)
{
yyin=fopen("input.txt","r");
yyout=fopen("output.txt","w");
yylex();
return 0;
}
INPUT:
OUTPUT:
Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
7. Design a LEX Code to remove the comments from any C-Program given at run-time
and store into ‘out.c’ file.
SOURCE CODE:
%{
#include<stdio.h>
%}
%%
"//"(.*) {};
"/*"(.*\n)*.*"*/" {};
%%
int
yywrap()
{return 1;
}
int
main(){ yyin=fopen("i
p.c","r");
yyout=fopen("ot.c","w");
yylex();
return 0;
}
INPUT:
OUTPUT:
Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
Ǫ8: Design a LEX Code to extract all html tags in the given HTML file at run time and
store into Text file given at run time.
%{
#include<stdio.h>
%}
%%
\<[^>]*\> fprintf(yyout,"%s\n",yytext);
.|\n;
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("input.html","r");
yyout=fopen("out.txt","w");
yylex();
return 0;
}
INPUT:
Vedant pant/210111792/Section-S/64
COMPILER DESIGN LAB
OUTPUT:
Vedant pant/210111792/Section-S/64