Switch-Case Aceepter Flex and Bison

Switch.Y
%{
    #include <stdio.h>
        void yyerror(char*msg);
    
%}

%token SWITCH BRO BRC CBO CBC CASE COLON BREAK DEFAULT ID DIGIT SEMI

%%

S: Statement {printf(“ACCEPTED”);}

Statement:SWITCH BRO ID BRC CBO E CBC

E: A
 | A C
 | A E

A: A B
 | CASE DIGIT D

B:  BREAK SEMI
 

C : DEFAULT D B
 

D : COLON ID SEMI

 ;

%%

void yyerror(char *msg)
{
        
        {printf(“\n SWITHC Syntax is Wrong”);}
    
}
int main(void)
{
    yyparse();
        return 0;
}
SWITCH.Flex

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

%%

[ \t\n]   
switch {return SWITCH;}

“(” {return BRO; }

“)” {return BRC; }

“{” {return CBO; }

“}” {return CBC; }

case {return CASE; }

“:” {return COLON; }

break {return BREAK;}

default {return DEFAULT;}

“;” {return SEMI;}

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

[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