Saturday, October 8

LEX program to check the syntax of PRINTF statement



%{
#include<stdio.h>
#include<ctype.h>
int i=0,j=0;
%}
a "%d"|"%f"|"%c"|"%s"
id [a-zA-Z][a-zA-Z0-9]*
%%

printf\((\"({a}*|.*)*\"(,{id})*\))\; {for(i=0;i<yyleng;i++) {
if(yytext[i]=='%')
j++;
if(yytext[i]==',')
j--;
}

if(j==0)
printf("Correct..!!");
else
printf("Incorrect..!!");
}
%%

main()
{

yyin=fopen("sample.c","r");
yylex();
}


int yywrap()
{

return 1;
}      




Explanation:

Well, checking syntax of printf statement is very easy.
The syntax should be "printf" followed by "(" and  . It can include a usual string or %d,%f type expression delimited by another ".

The entities specified by %f typed expression should be specified before ")" separated by commas.

The number of arguments is then checked within rule part ( i.e, no. of % is equal to no. of commas).


For more interesting LEX programs , click here...!!


                            

1 comments :

abhi CEA said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Unrecognized rule ennanu kaanikkunnath. Enthu cheyyum??

Post a Comment