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