-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhparse.y
104 lines (86 loc) · 1.84 KB
/
hparse.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
%{
#include <string.h>
#include <stdio.h>
#include "pexports.h"
static int inc_flag = 1;
static int arg_size = 0;
extern int yylex(void);
static void yyerror(char *s);
#define INC_ARGS(n) arg_size += (inc_flag ? n : 0)
%}
%union {
char *s;
};
%token <s> ID
%type <s> type_name
%%
start
: function_declaration
| start function_declaration
;
function_declaration
: type_name '(' parameter_list ')' ';'
{
ADD_FUNCTION($1, arg_size);
arg_size = 0;
}
| type_name '(' ')' ';'
{
ADD_FUNCTION($1, 0);
arg_size = 0;
}
| error { arg_size = 0; yyclearin; }
;
type_name
: ID
| ID pointer
{ $$ = ""; }
| type_name ID
{ $$ = $2; }
| type_name ID pointer
{ $$ = ""; }
| type_name function_pointer
{ $$ = ""; }
;
function_pointer
: '(' function_pointer_name ')' '(' ')'
{}
| '(' function_pointer_name ')' '('
{ inc_flag = 0; }
parameter_list ')'
{ inc_flag = 1; }
;
function_pointer_name
: pointer
| pointer ID
;
pointer
: '*'
| '*' pointer
;
parameter_declaration
: type_name
{
if (strcmp($1, "POINT") == 0)
INC_ARGS(8);
else if (strcmp($1, "RECT") == 0)
INC_ARGS(16);
else if (strcmp($1, "float") == 0)
INC_ARGS(sizeof(float));
else if (strcmp($1, "double") == 0)
INC_ARGS(sizeof(double));
else if (strcmp($1, "void") != 0)
INC_ARGS(4);
}
;
parameter_list
: parameter_declaration
| parameter_list ',' parameter_declaration
;
%%
static void
yyerror(char *s)
{
/* ignore error */
arg_size = 0;
}