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