-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcl.y
96 lines (77 loc) · 2.09 KB
/
hcl.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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"
#define YYSTYPE node_ptr
/* Current line number. Maintained by lex */
int lineno = 1;
#define ERRLIM 5
int errcnt = 0;
extern FILE *outfile;
int yyparse(void);
int yylex(void);
void yyerror(const char *str)
{
fprintf(stderr, "Error, near line %d: %s\n", lineno, str);
if (++errcnt > ERRLIM) {
fprintf(stderr, "Too many errors, aborting\n");
exit(1);
}
}
static char errmsg[1024];
void yyserror(const char *str, char *other)
{
sprintf(errmsg, str, other);
yyerror(errmsg);
}
int yywrap()
{
return 1;
}
int main(int argc, char **argv)
{
init_node(argc, argv);
outfile = stdout;
yyparse();
finish_node(0);
return errcnt != 0;
}
%}
%token QUOTE BOOLARG BOOL WORDARG WORD QSTRING
VAR NUM ASSIGN SEMI COLON COMMA LPAREN RPAREN LBRACE
RBRACE LBRACK RBRACK AND OR NOT COMP IN
/* All operators are left associative. Listed from lowest to highest */
%left OR
%left AND
%left NOT
%left COMP
%left IN
%%
statements: /* empty */
| statements statement
;
statement:
QUOTE QSTRING { insert_code($2); }
| BOOLARG VAR QSTRING { add_arg($2, $3, 1); }
| WORDARG VAR QSTRING { add_arg($2, $3, 0); }
| BOOL VAR ASSIGN expr SEMI { gen_funct($2, $4, 1); }
| WORD VAR ASSIGN expr SEMI { gen_funct($2, $4, 0); }
;
expr:
VAR { $$=$1; }
| NUM { $$=$1; }
| LPAREN expr RPAREN { $$=$2; }
| NOT expr { $$=make_not($2); }
| expr AND expr { $$=make_and($1, $3); }
| expr OR expr { $$=make_or($1, $3); }
| expr COMP expr { $$=make_comp($2,$1,$3); }
| expr IN LBRACE exprlist RBRACE { $$=make_ele($1, $4);}
| LBRACK caselist RBRACK { $$=$2; }
;
exprlist:
expr { $$=$1; }
| exprlist COMMA expr { $$=concat($1, $3); }
caselist:
/* Empty */ { $$=NULL; }
| caselist expr COLON expr SEMI { $$=concat($1, make_case($2, $4));}