parser/parser.yy
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:04 +0200
changeset 30 d037c433ec3e
parent 9 c3fecc82ade6
permissions -rw-r--r--
kleiner auffschrischungskurs compilerbau... committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 5
diff changeset
     1
/**
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 5
diff changeset
     2
 * test/demos/parser/parser.yy
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 5
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
5
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
%{
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
   #include <stdio.h>
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
   #include <ctype.h>
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
   int regs[26]; 
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
   int base;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
	
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
   int yyerror();
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
   int yylex();
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
%}
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
%start list
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
%token DIGIT LETTER
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
%left MINUS PLUS MUL DIV
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
%left UMINUS
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
%%
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
list : /* empty */ 
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
	| list stat '\n' 
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
	| list error '\n' { yyerrok; };
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
stat : 
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
	expr { (void) printf( "RESULT: %d\n", $1 ); } 
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
	| LETTER '=' expr { regs[$1] = $3; }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
	;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
   expr      :  '(' expr ')'
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
             {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
                   $$ = $2;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
             }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
             |  expr PLUS expr
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
             {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
                   $$ = $1 + $3;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
             }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
             |  expr MINUS expr
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
             {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
                   $$ = $1 - $3;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
             }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
             |  expr MUL expr
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
             { 
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
                   $$ = $1 * $3;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
             }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
             |  expr DIV expr
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
             {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
                   $$ = $1 / $3;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
             }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
             |  MINUS expr  %prec UMINUS
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
             {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
                   $$ = -$2;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
             }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
             |  LETTER
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
             {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
                   $$ = regs[$1];
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
             }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
             |  number
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
             ;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
   number    :   DIGIT
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
             {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
                    $$ = $1; base = ($1==0) ? 8 : 10;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
             }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
             |   number DIGIT
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
             {
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
                    $$ = base * $1 + $2;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
             }
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
             ;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
   
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
%%
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
int yyerror() 
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
{
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
	printf("ERROR\n");
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
	return 0;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
}
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
int yywrap() 
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
{
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
	return 1;
d752cbe8208e asm and parser demos added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
}