Wisdom Materials
Home
About Us
Our Clients
Careers
Services
Education
Jobs
News
Business
Health
Astrology
Entertainment
RealEstate
Devotion
Contact Us
Compiler Construction Lab Manual
/ Scanner Programs Using LEX
Program Name
Write a Compiler Construction Program for Scanner Programs Using LEX.
Theory
Scanner Programs Using LEX
Program Code
Copy Program Code
%{ void display(char*,int); %} digit [0-9] letter [A-Za-z] number {digit}+ specialsymbol [,|;|""] operator [+|\-|*|/] identifier {letter}({letter}|{digit})* signednumber [+|\-]?{number} %% auto|int|main|void display(yytext,1); {identifier} display(yytext,2); {operator} display(yytext,3); {specialsymbol} display(yytext,4); {signednumber} display(yytext,5); %% main(int argc,char* argv[]) { yyin=fopen(argv[1],"r"); yylex(); fclose(yyin); } void display(char *yytext,int c) { switch(c) { case 1:printf("%s is a Keyword\n",yytext); break; case 2:printf("%s is a Identifier\n",yytext); break; case 3:printf("%s is a Operator\n",yytext); break; case 4:printf("%s is a Specialsymbol\n",yytext);break; case 5:printf("%s is a Signednumber\n",yytext);break; } } int yywrap(){ return 1; }
Create a text file b.txt and enter data.
Vi b.txt
hello world
welcome
12 + 24
" "
Saving:
vi scanner.l
Compiling and Running
lex ab.l
cc lex.yy.c
./a.out
Input
Vi b.txt
hello world
welcome
12 + 24
" "
Output
hello is a Identifier
world is a Identifier
welcome is a Identifier
12 is a Signednumber
+ is a Operator
24 is a Signednumber
" is a Specialsymbol
" is a Specialsymbol
Home
Back