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
105
106
107
108
109
110
111
112
113
114
115
116
117
|
%{
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include "lexer.h"
#include "parser.h"
static ast_t mkunop(int, ast_t);
static ast_t mkbinop(int, ast_t, ast_t);
static void *xmalloc(size_t);
static void yyerror(const char *);
extern const char *current_file;
%}
%code requires {
#include "pinocchio.h"
}
%union {
char ch;
ast_t ast;
}
%define parse.error verbose
%locations
/* Very important that NOT is the first token declared! Code depends on it. */
%start input
%type <ast> line exp
%token <ast> NOT AND OR XOR IMPL EQUIV OPAR CPAR
%token <ch> IDENT
%token EOL
%left OR
%left AND
%left XOR
%left IMPL EQUIV
%nonassoc NOT
%%
input:
%empty
| input line {
if ($2.eqn != NULL)
astprocess($2);
}
;
line:
EOL { $$.eqn = NULL; }
| exp eol { $$ = $1; }
;
eol: EOL | YYEOF;
exp:
IDENT {
$$.eqn = xmalloc(sizeof(eqn_t));
$$.eqn->type = IDENT;
$$.eqn->ch = $1;
$$.vars = 1 << (islower($1) ? $1-'a'+26 : $1-'A');
}
| NOT exp { $$ = mkunop(NOT, $2); }
| OPAR exp CPAR { $$ = mkunop(OPAR, $2); }
| exp AND exp { $$ = mkbinop(AND, $1, $3); }
| exp OR exp { $$ = mkbinop(OR, $1, $3); }
| exp XOR exp { $$ = mkbinop(XOR, $1, $3); }
| exp IMPL exp { $$ = mkbinop(IMPL, $1, $3); }
| exp EQUIV exp { $$ = mkbinop(EQUIV, $1, $3); }
;
%%
ast_t
mkunop(int op, ast_t rhs)
{
ast_t a = {
.eqn = xmalloc(sizeof(eqn_t)),
.vars = rhs.vars,
};
a.eqn->type = op;
a.eqn->rhs = rhs.eqn;
return a;
}
ast_t
mkbinop(int op, ast_t lhs, ast_t rhs)
{
ast_t a = {
.eqn = xmalloc(sizeof(eqn_t)),
.vars = lhs.vars | rhs.vars,
};
a.eqn->type = op;
a.eqn->lhs = lhs.eqn;
a.eqn->rhs = rhs.eqn;
return a;
}
void *
xmalloc(size_t n)
{
void *p = malloc(n);
if (p == NULL)
err(1, "malloc");
return p;
}
void
yyerror(const char *s)
{
user_error("%s:%d: %s", current_file, yylloc.first_line, s);
}
|