/* calc_eqn.y for interfacing with qm Quine_McClusky.c */ %{ #include #include extern int regs[26]; /* values of variables a, b, c, ... */ extern int eqn_result; /* returned equation value */ extern char line[]; /* equation line to parse */ extern int line_index; /* index, initialize to zero, each equation */ static void yyerror(char *message); static int yylex(); %} %start list %token DIGIT LETTER /* do without precedence */ %% /* beginning of rules section */ list : /* empty */ | list stat ';' | list stat '\n' | list '\n' | list error '\n' { yyerrok; } ; stat : expr {eqn_result=$1; } | LETTER '=' expr { regs[$1] = $3; } ; expr : expr '|' fctr { $$ = $1 || $3; } | fctr ; fctr : fctr '&' uexp { $$ = $1 && $3; } | uexp ; uexp : '~' term { $$ = ! $2; } | term ; term : '(' expr ')' { $$ = $2; } | LETTER { $$ = regs[$1]; } | number ; number : DIGIT { $$ = $1; } | number DIGIT { $$ = 2 * $1 + $2; } /* bad */ ; %% /* start of program */ static int yylex() { int c; /* int i; if(line_index==0){ printf("in yylex, line=%s\n", line); printf("regs a b c d e f g h i j k l m n o p q r s t u v w x\n"); printf(" "); for(i=0; i<24; i++) printf("%d ",regs[i]); printf("\n"); } */ c=line[line_index++]; /* printf("yylex read c= %c \n",c); */ if(islower(c)){ yylval=c-'a'; return (LETTER) ; } if(isdigit(c)){ yylval=c-'0'; return (DIGIT) ; } return (c); } /* end yylex */ static void yyerror(char *message) { printf("\n yyerror reports %s \n", message); /* can be commented out */ }