To validate characters using Lex > Lex and Yacc Program
To validate characters using Lex > Lex and Yacc Program
System Programming and Compiler Construction
Program:
%{
/* To validate characters using lex*/
%}
%%
[\t]+ ;
/* To validate characters using lex*/
%}
%%
[\t]+ ;
[0-9]+ {printf("This is a valid number\n");}
int|char|float|if|else {printf("%s- is a keyword \n",yytext);}
[a-zA-Z]+ {printf("%s- is a character\n",yytext);}
"++"|"--"|"=="|[-+*/] {printf("%s- is an operator\n",yytext);}
[(){}.;"=] {printf("%s- is a special symbol\n",yytext);}
. ECHO;
%%
main()
{
yylex();
}
int yywrap()
{
return(1);
}
OUTPUT:
[root@localhost Desktop]# lex 38spcc2.l
[root@localhost Desktop]# gcc lex.yy.c
[root@localhost Desktop]# ./a.out
int a=10;
int- is a keyword
a- is a character
=- is a special symbol
This is a valid number
;- is a special symbol
char b;float a=90;
char- is a keyword
b- is a character
;- is a special symbol
float- is a keyword
a- is a character
=- is a special symbol
This is a valid number
;- is a special symbol
[root@localhost Desktop]# gcc lex.yy.c
[root@localhost Desktop]# ./a.out
int a=10;
int- is a keyword
a- is a character
=- is a special symbol
This is a valid number
;- is a special symbol
char b;float a=90;
char- is a keyword
b- is a character
;- is a special symbol
float- is a keyword
a- is a character
=- is a special symbol
This is a valid number
;- is a special symbol
Comments
Post a Comment