| 1 |
|
| 2 |
%union { |
| 3 |
Scope *scope; |
| 4 |
Expr *expr; |
| 5 |
Expr_List *elist; |
| 6 |
Type *type; |
| 7 |
Decl *decl; |
| 8 |
Decl_List *dlist; |
| 9 |
Code *code; |
| 10 |
char *id; |
| 11 |
}; |
| 12 |
|
| 13 |
%left '+' '-' |
| 14 |
%left '*' '/' '%' |
| 15 |
%nonassoc PREFIX |
| 16 |
%nonassoc POSTFIX '(' '[' '.' |
| 17 |
|
| 18 |
%token <id> ID |
| 19 |
%token <expr> CONSTANT |
| 20 |
%token EXTERN REGISTER STATIC CONST VOLATILE IF THEN ELSE CLCL |
| 21 |
|
| 22 |
%type <expr> expr(<scope>) |
| 23 |
%type decl(<scope>) declarator_list(<scope>, <type>) |
| 24 |
decl_list(<scope>) |
| 25 |
%type <code> statement(<scope>) statement_list(<scope>) |
| 26 |
block_statement(<scope>) |
| 27 |
%type <decl> declarator(<scope>, <type>) formal_arg(<scope>) |
| 28 |
%type <type> decl_specs(<scope>) decl_spec(<scope>) typename(<scope>) |
| 29 |
cv_quals cv_qual |
| 30 |
%type <scope> opt_scope(<scope>) |
| 31 |
%type <dlist> formal_arg_list(<scope>) nonempty_formal_arg_list(<scope>) |
| 32 |
|
| 33 |
%start input |
| 34 |
|
| 35 |
%% |
| 36 |
|
| 37 |
opt_scope($e): [ $$ = $e; ] |
| 38 |
| CLCL [ $$ = global_scope; ] |
| 39 |
| opt_scope ID CLCL [ Decl *d = lookup($1, $2); |
| 40 |
if (!d || !d->scope) YERROR; |
| 41 |
$$ = d->scope; ] |
| 42 |
; |
| 43 |
|
| 44 |
typename($e): opt_scope ID |
| 45 |
[ Decl *d = lookup($1, $2); |
| 46 |
if (!d || !d->istype()) YYERROR; |
| 47 |
$$ = d->type; ] |
| 48 |
; |
| 49 |
|
| 50 |
input: decl_list(global_scope = new_scope(0)) ; |
| 51 |
decl_list($e): | decl_list decl($e) ; |
| 52 |
decl($e): |
| 53 |
decl_specs declarator_list($e,$1) ';' [YYVALID;] |
| 54 |
| decl_specs declarator($e,$1) block_statement(start_fn_def($e, $2)) |
| 55 |
{ finish_fn_def($2, $3); } |
| 56 |
; |
| 57 |
|
| 58 |
decl_specs($e): |
| 59 |
decl_spec [ $$ = $1; ] |
| 60 |
| decl_specs decl_spec($e) [ $$ = type_combine($1, $2); ] |
| 61 |
; |
| 62 |
|
| 63 |
cv_quals: [ $$ = 0; ] |
| 64 |
| cv_quals cv_qual [ $$ = type_combine($1, $2); ] |
| 65 |
; |
| 66 |
|
| 67 |
decl_spec($e): |
| 68 |
cv_qual [ $$ = $1; ] |
| 69 |
| typename [ $$ = $1; ] |
| 70 |
| EXTERN [ $$ = bare_extern(); ] |
| 71 |
| REGISTER [ $$ = bare_register(); ] |
| 72 |
| STATIC [ $$ = bare_static(); ] |
| 73 |
; |
| 74 |
|
| 75 |
cv_qual: |
| 76 |
CONST [ $$ = bare_const(); ] |
| 77 |
| VOLATILE [ $$ = bare_volatile(); ] |
| 78 |
; |
| 79 |
|
| 80 |
declarator_list($e, $t): |
| 81 |
declarator_list ',' declarator($e, $t) |
| 82 |
| declarator |
| 83 |
; |
| 84 |
|
| 85 |
declarator($e, $t): |
| 86 |
/* empty */ [ if (!$t) YYERROR; ] |
| 87 |
{ $$ = declare($e, 0, $t); } |
| 88 |
| ID { $$ = declare($e, $1, $t); } |
| 89 |
| '(' declarator($e, $t) ')' { $$ = $2; } |
| 90 |
| '*' cv_quals declarator($e, $t) %prec PREFIX |
| 91 |
{ $$ = make_pointer($3, $2); } |
| 92 |
| declarator '[' expr($e) ']' |
| 93 |
{ $$ = make_array($1->type, $3); } |
| 94 |
| declarator '(' formal_arg_list($e) ')' cv_quals |
| 95 |
{ $$ = build_function($1, $3, $5); } |
| 96 |
; |
| 97 |
|
| 98 |
formal_arg_list($e): { $$ = 0; } |
| 99 |
| nonempty_formal_arg_list { $$ = $1; } |
| 100 |
; |
| 101 |
nonempty_formal_arg_list($e): |
| 102 |
nonempty_formal_arg_list ',' formal_arg($e) { $$ = append_dlist($1, $3); } |
| 103 |
| formal_arg { $$ = build_dlist($1); } |
| 104 |
; |
| 105 |
formal_arg($e): |
| 106 |
decl_specs declarator($e,$1) { $$ = $2; } |
| 107 |
; |
| 108 |
|
| 109 |
expr($e): |
| 110 |
expr '+' expr($e) { $$ = build_expr($1, ADD, $3); } |
| 111 |
| expr '-' expr($e) { $$ = build_expr($1, SUB, $3); } |
| 112 |
| expr '*' expr($e) { $$ = build_expr($1, MUL, $3); } |
| 113 |
| expr '%' expr($e) { $$ = build_expr($1, MOD, $3); } |
| 114 |
| expr '/' expr($e) { $$ = build_expr($1, DIV, $3); } |
| 115 |
| '*' expr($e) %prec PREFIX { $$ = build_expr(0, REF, $2); } |
| 116 |
| ID { $$ = var_expr($e, $1); } |
| 117 |
| CONSTANT { $$ = $1; } |
| 118 |
; |
| 119 |
|
| 120 |
statement($e): |
| 121 |
decl { $$ = 0; } |
| 122 |
| expr($e) ';' [YYVALID;] { $$ = build_expr_code($1); } |
| 123 |
| IF '(' expr($e) ')' THEN statement($e) ELSE statement($e) [YYVALID;] |
| 124 |
{ $$ = build_if($3, $6, $8); } |
| 125 |
| IF '(' expr($e) ')' THEN statement($e) [YYVALID;] |
| 126 |
{ $$ = build_if($3, $6, 0); } |
| 127 |
| block_statement(new_scope($e)) [YYVALID;]{ $$ = $1; } |
| 128 |
; |
| 129 |
|
| 130 |
statement_list($e): { $$ = 0; } |
| 131 |
| statement_list statement($e) { $$ = code_append($1, $2); } |
| 132 |
; |
| 133 |
|
| 134 |
block_statement($e): |
| 135 |
'{' statement_list($e) '}' { $$ = $2; } |
| 136 |
; |