Home | History | Annotate | Line # | Download | only in test
calc3.y revision 1.1.1.3
      1 /*	$NetBSD: calc3.y,v 1.1.1.3 2011/09/10 21:22:07 christos Exp $	*/
      2 
      3 %pure-parser
      4 
      5 %parse-param { int regs[26] }
      6 %parse-param { int *base }
      7 
      8 %lex-param { int *base }
      9 
     10 %{
     11 # include <stdio.h>
     12 # include <ctype.h>
     13 
     14 %}
     15 
     16 %start list
     17 
     18 %token DIGIT LETTER
     19 
     20 %left '|'
     21 %left '&'
     22 %left '+' '-'
     23 %left '*' '/' '%'
     24 %left UMINUS   /* supplies precedence for unary minus */
     25 
     26 %% /* beginning of rules section */
     27 
     28 list  :  /* empty */
     29       |  list stat '\n'
     30       |  list error '\n'
     31             {  yyerrok ; }
     32       ;
     33 
     34 stat  :  expr
     35             {  printf("%d\n",$1);}
     36       |  LETTER '=' expr
     37             {  regs[$1] = $3; }
     38       ;
     39 
     40 expr  :  '(' expr ')'
     41             {  $$ = $2; }
     42       |  expr '+' expr
     43             {  $$ = $1 + $3; }
     44       |  expr '-' expr
     45             {  $$ = $1 - $3; }
     46       |  expr '*' expr
     47             {  $$ = $1 * $3; }
     48       |  expr '/' expr
     49             {  $$ = $1 / $3; }
     50       |  expr '%' expr
     51             {  $$ = $1 % $3; }
     52       |  expr '&' expr
     53             {  $$ = $1 & $3; }
     54       |  expr '|' expr
     55             {  $$ = $1 | $3; }
     56       |  '-' expr %prec UMINUS
     57             {  $$ = - $2; }
     58       |  LETTER
     59             {  $$ = regs[$1]; }
     60       |  number
     61       ;
     62 
     63 number:  DIGIT
     64          {  $$ = $1; (*base) = ($1==0) ? 8 : 10; }
     65       |  number DIGIT
     66          {  $$ = (*base) * $1 + $2; }
     67       ;
     68 
     69 %% /* start of programs */
     70 
     71 #ifdef YYBYACC
     72 extern int YYLEX_DECL();
     73 static void YYERROR_DECL();
     74 #endif
     75 
     76 int
     77 main (void)
     78 {
     79     int regs[26];
     80     int base = 10;
     81 
     82     while(!feof(stdin)) {
     83 	yyparse(regs, &base);
     84     }
     85     return 0;
     86 }
     87 
     88 static void
     89 YYERROR_DECL()
     90 {
     91     fprintf(stderr, "%s\n", s);
     92 }
     93 
     94 int
     95 YYLEX_DECL()
     96 {
     97 	/* lexical analysis routine */
     98 	/* returns LETTER for a lower case letter, yylval = 0 through 25 */
     99 	/* return DIGIT for a digit, yylval = 0 through 9 */
    100 	/* all other characters are returned immediately */
    101 
    102     int c;
    103 
    104     while( (c=getchar()) == ' ' )   { /* skip blanks */ }
    105 
    106     /* c is now nonblank */
    107 
    108     if( islower( c )) {
    109 	*yylval = (c - 'a');
    110 	return ( LETTER );
    111     }
    112     if( isdigit( c )) {
    113 	*yylval = (c - '0') % (*base);
    114 	return ( DIGIT );
    115     }
    116     return( c );
    117 }
    118