Saturday, December 4, 2010

IDENTIFICATION OF Switch Case Statements!

LEX PROGRAM

%{
%}
ID [a-zA-Z][a-zA-Z0-9]*
NUMBER ([0-9]+)|([0-9]+\.[0-9]+)
OP "+"|"-"|"*"|"/"|"--"|"&&"|"||"|">"|"<"|"=="|">="|"<="|"="
%%
SWITCH {/*printf("It is switch\n");*/ return SWITCH; }
CASE {/*printf("It is case\n");*/ return CASE; }
BREAK {/*printf("It is case\n");*/ return BREAK; }
DEFAULT {/*printf("It is case\n");*/ return DEFAULT; }
{ID} {/*printf("It is id\n");*/ return ID; }
{NUMBER} {/*printf("It is number\n");*/ return NUMBER; }
{OP} {/*printf("It is operator\n");*/ return OP; }
"(" {return *yytext;}
")" {return *yytext;}
"{" {return *yytext;}
"}" {return *yytext;}
";" {return *yytext;}
":" {return *yytext;}
"\n" {return *yytext;}
[ \t] {}
. {return *yytext;}
%%
/*
int main()
{
yyin = fopen("test","r");
yylex();
}
*/
int yywrap()
{
return 1;
}

YACC PROGRAM

%token NUMBER
%token SWITCH
%token CASE
%token BREAK
%token DEFAULT
%token OP
%token '\n'
%token ID
%{
#include
int lineno=1;
%}
%%
STMT_LIST : STMT_LIST SWITCH_STMT '\n' {printf("VALID STATEMENT...\n"); lineno++; }
| error '\n' {printf("INVALID...\n"); lineno++; }
|
;
SWITCH_STMT : SWITCH'('EXPR')''{'CASE EXPR':'STMTS'}'
| SWITCH'('EXPR')''{'CASE EXPR':'STMTS BREAK';''}'
| SWITCH'('EXPR')''{'CASE EXPR':'STMTS BREAK';'DEFAULT':'STMTS'}'
|
;
STMTS : EXPR';'
|
;
EXPR : EXPR OP EXPR
| ID
| NUMBER
|
;
%%
#include "lex.yy.c"
int main()
{
yyin = fopen("test","r");
yyparse();
}
int yyerror(char *s)
{
fprintf(stderr," %s at line no %d. ",s,lineno);
}

No comments:

Post a Comment