ELSE_IF Accepter Bison and Flex

IF.Y

%{

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

%}

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

%%

Statement: E4 ELSE CBO E2 CBC {printf(“Accept \n”);}

E4:A
  |H
  |E4 A
 

;

A:ELSEIF BRO E1 BRC CBO E2 CBC
;
 
H:IF BRO E1 BRC CBO E2 CBC

E1:ID EQUAL ID
  |ID RELOP ID
  |ID RELOP NUM
  ;
E2:NUM SEMI
  |ID SEMI
  ;

%%
void yyerror(char *s)
{
        
        {printf(“\n Syntax is Wrong”);}
    
}

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

IF.Flex

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

%%

if {return IF;}

“else if” {return ELSEIF;}

else {return ELSE;}

“(” {return BRO; }

“)” {return BRC; }

“{” {return CBO; }

“}” {return CBC; }

“;” {return SEMI; }

“<” {return RELOP; }

“>” {return RELOP; }

“=<” {return RELOP; }

“=>” {return RELOP; }

“<=” {return RELOP; }

“>=” {return RELOP; }

“==” {return RELOP; }

“=” {return EQUAL;}

“!=” {return RELOP;}

[0-9]+  {yylval=*yytext;return NUM;}

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

%%

int yywrap(void)

{

  return 1;

}

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