parser/calc/parser.y
changeset 38 48f6f3918b82
parent 37 0fbbe329c3a2
child 39 46d7ec9d63bd
equal deleted inserted replaced
37:0fbbe329c3a2 38:48f6f3918b82
     5 
     5 
     6 %{
     6 %{
     7    #include <stdio.h>
     7    #include <stdio.h>
     8    #include <ctype.h>
     8    #include <ctype.h>
     9 
     9 
    10    int regs[26]; 
    10    int regs[26];
    11    int base;
    11    int base;
    12 	
    12 
    13    int yyerror();
    13    int yyerror();
    14    int yylex();
    14    int yylex();
    15 %}
    15 %}
    16 
    16 
    17 %start list
    17 %start list
    18 %token DIGIT LETTER
    18 %token DIGIT LETTER
    19 %left MINUS PLUS MUL DIV
    19 %left MINUS PLUS MUL DIV
    20 %left UMINUS
    20 %left UMINUS
    21 %%
    21 %%
    22 
    22 
    23 list : /* empty */ 
    23 list:
    24 	| list stat '\n' 
    24 	/* empty */
       
    25 	| list stat '\n'
    25 	| list error '\n' { yyerrok; };
    26 	| list error '\n' { yyerrok; };
    26 
    27 
    27 stat : 
    28 stat:
    28 	expr { (void) printf( "RESULT: %d\n", $1 ); } 
    29 	expr { (void) printf( "RESULT: %d\n", $1 ); }
    29 	| LETTER '=' expr { regs[$1] = $3; }
    30 	| LETTER '=' expr { regs[$1] = $3; }
    30 	;
    31 	;
    31 
    32 
    32    expr      :  '(' expr ')'
    33 expr:
    33              {
    34 	'(' expr ')'
    34                    $$ = $2;
    35 	{
    35              }
    36 		$$ = $2;
    36              |  expr PLUS expr
    37 	}
    37              {
    38 	| expr PLUS expr
    38                    $$ = $1 + $3;
    39 	{
    39              }
    40 		$$ = $1 + $3;
    40              |  expr MINUS expr
    41 	}
    41              {
    42 	| expr MINUS expr
    42                    $$ = $1 - $3;
    43 	{
    43              }
    44 		$$ = $1 - $3;
    44              |  expr MUL expr
    45 	}
    45              { 
    46 	| expr MUL expr
    46                    $$ = $1 * $3;
    47 	{
    47              }
    48 		$$ = $1 * $3;
    48              |  expr DIV expr
    49 	}
    49              {
    50 	| expr DIV expr
    50                    $$ = $1 / $3;
    51 	{
    51              }
    52 			$$ = $1 / $3;
    52              |  MINUS expr  %prec UMINUS
    53 	}
    53              {
    54 	| MINUS expr  %prec UMINUS
    54                    $$ = -$2;
    55 	{
    55              }
    56 		$$ = -$2;
    56              |  LETTER
    57 	}
    57              {
    58 	| LETTER
    58                    $$ = regs[$1];
    59 	{
    59              }
    60 		$$ = regs[$1];
    60              |  number
    61 	}
    61              ;
    62 	|  number
       
    63 	;
    62 
    64 
    63    number    :   DIGIT
    65 number:
    64              {
    66 	DIGIT
    65                     $$ = $1; base = ($1==0) ? 8 : 10;
    67 	{
    66              }
    68 		$$ = $1; base = ($1==0) ? 8 : 10;
    67              |   number DIGIT
    69 	}
    68              {
    70 	| number DIGIT
    69                     $$ = base * $1 + $2;
    71 	{
    70              }
    72 		$$ = base * $1 + $2;
    71              ;
    73 	}
    72    
    74 	;
    73 %%
    75 %%
    74 
    76 
    75 int yyerror() 
    77 int yyerror()
    76 {
    78 {
    77 	printf("ERROR\n");
    79 	printf("ERROR\n");
    78 	return 0;
    80 	return 0;
    79 }
    81 }
    80 
    82 
    81 int yywrap() 
    83 int yywrap()
    82 {
    84 {
    83 	return 1;
    85 	return 1;
    84 }
    86 }