1*84d9c625SLionel Sambuc /* $NetBSD: code_calc.y,v 1.1.1.4 2013/04/06 14:45:27 christos Exp $ */ 24a17663cSThomas Veerman 34a17663cSThomas Veerman %{ 44a17663cSThomas Veerman # include <stdio.h> 54a17663cSThomas Veerman # include <ctype.h> 64a17663cSThomas Veerman 74a17663cSThomas Veerman int regs[26]; 84a17663cSThomas Veerman int base; 94a17663cSThomas Veerman 10*84d9c625SLionel Sambuc #ifdef YYBISON 11*84d9c625SLionel Sambuc int yylex(void); 12*84d9c625SLionel Sambuc static void yyerror(const char *s); 13*84d9c625SLionel Sambuc #endif 14*84d9c625SLionel Sambuc 154a17663cSThomas Veerman %} 164a17663cSThomas Veerman 174a17663cSThomas Veerman %start list 184a17663cSThomas Veerman 194a17663cSThomas Veerman %token DIGIT LETTER 204a17663cSThomas Veerman 214a17663cSThomas Veerman %left '|' 224a17663cSThomas Veerman %left '&' 234a17663cSThomas Veerman %left '+' '-' 244a17663cSThomas Veerman %left '*' '/' '%' 254a17663cSThomas Veerman %left UMINUS /* supplies precedence for unary minus */ 264a17663cSThomas Veerman 274a17663cSThomas Veerman %% /* beginning of rules section */ 284a17663cSThomas Veerman 294a17663cSThomas Veerman list : /* empty */ 304a17663cSThomas Veerman | list stat '\n' 314a17663cSThomas Veerman | list error '\n' 324a17663cSThomas Veerman { yyerrok ; } 334a17663cSThomas Veerman ; 344a17663cSThomas Veerman 354a17663cSThomas Veerman stat : expr 364a17663cSThomas Veerman { printf("%d\n",$1);} 374a17663cSThomas Veerman | LETTER '=' expr 384a17663cSThomas Veerman { regs[$1] = $3; } 394a17663cSThomas Veerman ; 404a17663cSThomas Veerman 414a17663cSThomas Veerman expr : '(' expr ')' 424a17663cSThomas Veerman { $$ = $2; } 434a17663cSThomas Veerman | expr '+' expr 444a17663cSThomas Veerman { $$ = $1 + $3; } 454a17663cSThomas Veerman | expr '-' expr 464a17663cSThomas Veerman { $$ = $1 - $3; } 474a17663cSThomas Veerman | expr '*' expr 484a17663cSThomas Veerman { $$ = $1 * $3; } 494a17663cSThomas Veerman | expr '/' expr 504a17663cSThomas Veerman { $$ = $1 / $3; } 514a17663cSThomas Veerman | expr '%' expr 524a17663cSThomas Veerman { $$ = $1 % $3; } 534a17663cSThomas Veerman | expr '&' expr 544a17663cSThomas Veerman { $$ = $1 & $3; } 554a17663cSThomas Veerman | expr '|' expr 564a17663cSThomas Veerman { $$ = $1 | $3; } 574a17663cSThomas Veerman | '-' expr %prec UMINUS 584a17663cSThomas Veerman { $$ = - $2; } 594a17663cSThomas Veerman | LETTER 604a17663cSThomas Veerman { $$ = regs[$1]; } 614a17663cSThomas Veerman | number 624a17663cSThomas Veerman ; 634a17663cSThomas Veerman 644a17663cSThomas Veerman number: DIGIT 654a17663cSThomas Veerman { $$ = $1; base = ($1==0) ? 8 : 10; } 664a17663cSThomas Veerman | number DIGIT 674a17663cSThomas Veerman { $$ = base * $1 + $2; } 684a17663cSThomas Veerman ; 694a17663cSThomas Veerman 704a17663cSThomas Veerman %% /* start of programs */ 714a17663cSThomas Veerman 724a17663cSThomas Veerman #ifdef YYBYACC 734a17663cSThomas Veerman extern int YYLEX_DECL(); 744a17663cSThomas Veerman #endif 754a17663cSThomas Veerman 764a17663cSThomas Veerman int main(void)774a17663cSThomas Veermanmain (void) 784a17663cSThomas Veerman { 794a17663cSThomas Veerman while(!feof(stdin)) { 804a17663cSThomas Veerman yyparse(); 814a17663cSThomas Veerman } 824a17663cSThomas Veerman return 0; 834a17663cSThomas Veerman } 844a17663cSThomas Veerman 854a17663cSThomas Veerman static void yyerror(const char * s)864a17663cSThomas Veermanyyerror(const char *s) 874a17663cSThomas Veerman { 884a17663cSThomas Veerman fprintf(stderr, "%s\n", s); 894a17663cSThomas Veerman } 904a17663cSThomas Veerman 914a17663cSThomas Veerman int yylex(void)92*84d9c625SLionel Sambucyylex(void) 93*84d9c625SLionel Sambuc { 944a17663cSThomas Veerman /* lexical analysis routine */ 954a17663cSThomas Veerman /* returns LETTER for a lower case letter, yylval = 0 through 25 */ 964a17663cSThomas Veerman /* return DIGIT for a digit, yylval = 0 through 9 */ 974a17663cSThomas Veerman /* all other characters are returned immediately */ 984a17663cSThomas Veerman 994a17663cSThomas Veerman int c; 1004a17663cSThomas Veerman 1014a17663cSThomas Veerman while( (c=getchar()) == ' ' ) { /* skip blanks */ } 1024a17663cSThomas Veerman 1034a17663cSThomas Veerman /* c is now nonblank */ 1044a17663cSThomas Veerman 1054a17663cSThomas Veerman if( islower( c )) { 1064a17663cSThomas Veerman yylval = c - 'a'; 1074a17663cSThomas Veerman return ( LETTER ); 1084a17663cSThomas Veerman } 1094a17663cSThomas Veerman if( isdigit( c )) { 1104a17663cSThomas Veerman yylval = c - '0'; 1114a17663cSThomas Veerman return ( DIGIT ); 1124a17663cSThomas Veerman } 1134a17663cSThomas Veerman return( c ); 1144a17663cSThomas Veerman } 115