I have a Lex and a YACC program to generate a switch parser, which should produce what follows:
switch(var) {
case 0: z=cost0;
break;
case N: z=costN;
break;
default: z=costD;
break;
}
and by passing 2 default values (lets say 1 and 2 respectively) which are stored in variables x and y, each one of them would be passed to the val in switch, and all the N conditions would be checked, so, z should be storing variable for the value of the found condition and print it. but i cant write the main function of my YACC to organize the input of x and y , and print z. here is my programs:
Lex:
%{
#include"ma1.tab.h"
%}
alpha [a-zA-Z]
digit [0-9]
%%
[ \t\n]
switch return SWITCH;
int return INT;
case return CASE;
break return BREAK;
default return DEFAULT;
{digit}+ return NUM;
{alpha}({alpha}|{digit})* return ID;
. return yytext[0];
%%
YACC:
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token ID NUM SWITCH CASE DEFAULT BREAK INT
%right '='
%%
program: varassign switchstm {printf("Input accepted.\n");exit(0);}
;
varassign: INT vardef ‘;’
;
vardef : ID '=' NUM | ID '=' NUM varassign {$1=$3;}
;
switchstm: SWITCH '(' ID ')' '{' block '}'
;
block: caselist
| caselist defaultstm
;
caselist: casestm | casestm caselist ;
casestm: CASE NUM ':' assign ';' BREAK ';'
;
defaultstm : DEFAULT ':' assign ';' BREAK ';'
;
assign : ID'='NUM {$1=$3}
%%
main()
{
printf("Enter the exp: ");
yyparse();
}
but when I run the resulting a.out file, it just does nothing! so my problem is getting around the code in order to get the 2 integers and get them through producing one z per each. Thanks in advance.