-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
104 lines (87 loc) · 1.83 KB
/
parser.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
%define api.pure full
%lex-param {void *scanner}
%parse-param {void *scanner}{module *mod} // 传入参数
%define parse.trace
%define parse.error verbose
%{
#include "ast.h"
#include "parser.h"
#include "scanner.h"
void yyerror (yyscan_t *locp, module *mod, char const *msg);
%}
%code requires
{
#include "ast.h"
}
%union
{
int intval;
char *strval;
Cpro *cp;
CproList *cplist;
CNode *node;
CList *list;
} /* Generate YYSTYPE from these types: */
%token <strval> NAME
%token <intval> PIDNUM
%type <mode> sexps
%type <cp> CproClause
%type <node> expr
%type <list> Expression
%type <strval> CPU_INFO
%%
%start sexps;
sexps:CproClause {
if ($1 != NULL)
{
mod -> cpro = $1;
mod -> cproIsNull = false;
mod -> cnum = mod -> cpro -> cproList -> length;
}
else
{
mod -> cpro = NULL;
mod -> cproIsNull = true;
mod -> cnum = 0;
}
return 0;
};
CproClause: '[' Expression ']' {
if ($2 != NULL)
{
$$ = makeCNode(Cpro);
$$ -> cproList = $2;
}
else
$$ = NULL;
};
Expression: expr {
if ($1 != NULL)
$$ = clist_make1($1);
}
| Expression ',' expr {
if ($3 != NULL)
$$ = clappend($1, $3);
};
expr: { }
| '{' CPU_INFO ':' PIDNUM '}'
{
if ($4 != 0)
{
CproList *cpro;
cpro = makeCNode(CproList);
cpro -> cpunum = atoi((const char *)($2 + 4));
cpro -> pidnum = $4;
$$ = (CNode *)cpro;
}
else
{
$$ = NULL;
}
};
CPU_INFO: '"' NAME '"' { $$ = $2; };
%%
void yyerror (yyscan_t *locp, module *mod, char const *msg)
{
fprintf(stderr, "--> %s\n", msg);
}