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