Nested If flex bison

Flex File

%{
#include “if.tab.h”
#include<stdlib.h>

%}

%%

if {return IF;}

“else if” {return ELSEIF;}

else {return ELSE;}

“=” {return EQUAL;}

“(” {return BRO; }

“)” {return BRC; }

“{” {return CBO; }

“}” {return CBC; }

“;” {return SEMI; }

“>=”|”<=”|”==”|”!=”|”<“|”>”|”++”|”–” {
yylval = *yytext;
return multiop;
}
[0-9]+ {yylval=*yytext;return NUM;}

[a-zA-Z]+ {yylval=*yytext;return ID;}

[ \t] ; /* skip whitespace */
%%

int yywrap(void)

{

return 1;

}

Bison File

%{

#include <stdio.h>
void yyerror(char *s);

%}

%token IF BRO ELSE BRC CBO CBC SEMI EQUAL NUM ID ELSEIF multiop

%%

program: program State ‘\n’
|
;
State: IF BRO ID EQUAL ID BRC CBO E1 CBC ELSE CBO ID SEMI CBC {printf(“Accept \n”);}
;

E1:A
| A E1
;

A:IF BRO ID EQUAL ID BRC CBO ID SEMI CBC
| A B
;

B: ELSE CBO ID SEMI CBC
;

%%
void yyerror(char *s)
{

{printf(“\n Syntax is Wrong”);}

}

int main(void)
{
yyparse();
return 0;
}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s