Home | History | Annotate | Line # | Download | only in test
calc3.y revision 1.1
      1 /*	$NetBSD: calc3.y,v 1.1 2010/12/23 23:36:30 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 int
     72 main (void)
     73 {
     74     int regs[26];
     75     int base = 10;
     76 
     77     while(!feof(stdin)) {
     78 	yyparse(regs, &base);
     79     }
     80     return 0;
     81 }
     82 
     83 static void
     84 YYERROR_DECL()
     85 {
     86     fprintf(stderr, "%s\n", s);
     87 }
     88 
     89 int
     90 YYLEX_DECL()
     91 {
     92 	/* lexical analysis routine */
     93 	/* returns LETTER for a lower case letter, yylval = 0 through 25 */
     94 	/* return DIGIT for a digit, yylval = 0 through 9 */
     95 	/* all other characters are returned immediately */
     96 
     97     int c;
     98 
     99     while( (c=getchar()) == ' ' )   { /* skip blanks */ }
    100 
    101     /* c is now nonblank */
    102 
    103     if( islower( c )) {
    104 	*yylval = (c - 'a');
    105 	return ( LETTER );
    106     }
    107     if( isdigit( c )) {
    108 	*yylval = (c - '0') % (*base);
    109 	return ( DIGIT );
    110     }
    111     return( c );
    112 }
    113