author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:50:39 +0200 | |
changeset 74 | 829976007e62 |
parent 39 | 46d7ec9d63bd |
permissions | -rw-r--r-- |
37 | 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 |
||
38
48f6f3918b82
Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents:
37
diff
changeset
|
10 |
int regs[26]; |
37 | 11 |
int base; |
38
48f6f3918b82
Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents:
37
diff
changeset
|
12 |
|
37 | 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 |
||
38
48f6f3918b82
Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents:
37
diff
changeset
|
23 |
list: |
39 | 24 |
/* empty */ |
25 |
| list stat '\n' |
|
26 |
| list error '\n' { yyerrok; }; |
|
37 | 27 |
|
38
48f6f3918b82
Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents:
37
diff
changeset
|
28 |
stat: |
39 | 29 |
expr { (void) printf( "RESULT: %d\n", $1 ); } |
30 |
| LETTER '=' expr { regs[$1] = $3; } |
|
31 |
; |
|
37 | 32 |
|
38
48f6f3918b82
Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents:
37
diff
changeset
|
33 |
expr: |
39 | 34 |
'(' expr ')' |
35 |
{ |
|
36 |
$$ = $2; |
|
37 |
} |
|
38 |
| expr PLUS expr |
|
39 |
{ |
|
40 |
$$ = $1 + $3; |
|
41 |
} |
|
42 |
| expr MINUS expr |
|
43 |
{ |
|
44 |
$$ = $1 - $3; |
|
45 |
} |
|
46 |
| expr MUL expr |
|
47 |
{ |
|
48 |
$$ = $1 * $3; |
|
49 |
} |
|
50 |
| expr DIV expr |
|
51 |
{ |
|
52 |
$$ = $1 / $3; |
|
53 |
} |
|
54 |
| MINUS expr %prec UMINUS |
|
55 |
{ |
|
56 |
$$ = -$2; |
|
57 |
} |
|
58 |
| LETTER |
|
59 |
{ |
|
60 |
$$ = regs[$1]; |
|
61 |
} |
|
62 |
| number |
|
63 |
; |
|
37 | 64 |
|
38
48f6f3918b82
Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents:
37
diff
changeset
|
65 |
number: |
39 | 66 |
DIGIT |
67 |
{ |
|
68 |
$$ = $1; base = ($1==0) ? 8 : 10; |
|
69 |
} |
|
70 |
| number DIGIT |
|
71 |
{ |
|
72 |
$$ = base * $1 + $2; |
|
73 |
} |
|
74 |
; |
|
37 | 75 |
%% |
76 |
||
38
48f6f3918b82
Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents:
37
diff
changeset
|
77 |
int yyerror() |
37 | 78 |
{ |
39 | 79 |
printf("ERROR\n"); |
80 |
return 0; |
|
37 | 81 |
} |
82 |
||
38
48f6f3918b82
Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents:
37
diff
changeset
|
83 |
int yywrap() |
37 | 84 |
{ |
39 | 85 |
return 1; |
37 | 86 |
} |