-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser.y
164 lines (124 loc) · 4.78 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Bison parser definition.
For those who are new to flex/bison:
This file is transpiled (using 'bison parser.y') to a .cpp file that
implements the class yy::parser. This class has the job to analyze the
token stream resulting from repeated invocation of the yylex() function
and create an AST (abstract syntax tree) of the given string expressions.
Although the grammar is quite simple and does not require this level of
parsing power, using bison was a nice getting-to-know exercise and makes
language easier to extend and the code easier to maintain.
*/
/* C++ parser interface */
%skeleton "lalr1.cc"
%require "3.0"
/* add parser members (scanner, cb) and yylex parameters (loc, scanner) */
%parse-param {yy::scanner* scanner} {ParserCallback* cb}
%locations
/* increase usefulness of error messages */
%define parse.error verbose
/* assert correct cleanup of semantic value objects */
%define parse.assert
%define api.value.type variant
%define api.token.prefix {T_}
%token END 0 "end of file"
%token <std::string> NAME
%token <double> NUM
%token <int> SIGN
REL
%type <ast::Relation> inform_inequ
%type <ast::VarCore> mutual_indep
%type <ast::VarCore> markov_chain
%type <ast::FunctionOf> determ_depen
%type <ast::Expression> inform_expr
%type <ast::Term> inform_term
%type <ast::Quantity> inform_quant
%type <ast::Quantity> entropy
%type <ast::Quantity> mutual_inf
%type <ast::VarList> var_list
%type <ast::VarCore> mut_inf_core;
%start statement
%code requires {
#include <stdexcept>
#include <string>
#include "ast.hpp"
#include "location.hh"
namespace yy {
class scanner;
};
// results
struct ParserCallback {
virtual void relation(ast::Relation) = 0;
virtual void markov_chain(ast::MarkovChain) = 0;
virtual void mutual_independence(ast::MutualIndependence) = 0;
virtual void function_of(ast::FunctionOf) = 0;
};
}
%code {
#include <iostream> // cerr, endl
#include <utility> // move
#include <string>
#include "scanner.hpp"
using std::move;
#ifdef yylex
# undef yylex
#endif
#define yylex scanner->lex
template <class T, class V>
T&& enlist(T& t, V& v)
{
t.push_back(move(v));
return move(t);
}
}
%%
/* deliver output */
statement : %empty { /* allow empty (or pure comment) lines */ }
| inform_inequ { cb->relation(move($1)); }
| mutual_indep { cb->mutual_independence(move($1)); }
| markov_chain { cb->markov_chain(move($1)); }
| determ_depen { cb->function_of(move($1)); }
;
/* statements */
inform_inequ : inform_expr REL inform_expr { $$ = {$1, $2, $3}; }
;
markov_chain : markov_chain '/' var_list { $$ = enlist($1, $3); }
| var_list '/' var_list '/' var_list { $$ = {$1, $3, $5}; }
;
mutual_indep : mutual_indep '.' var_list { $$ = enlist($1, $3); }
| var_list '.' var_list { $$ = {$1, $3}; }
;
determ_depen : var_list ':' var_list { $$ = {$1, $3}; }
;
/* building blocks */
inform_expr : inform_expr SIGN inform_term { $$ = enlist($1, $3.flip_sign($2)); }
| SIGN inform_term { $$ = {$2.flip_sign($1)}; }
| inform_term { $$ = {$1}; }
;
inform_term : NUM inform_quant { $$ = {$1, $2}; }
| inform_quant { $$ = { 1, $1}; }
| NUM { $$ = {$1}; }
;
inform_quant : entropy { $$ = $1; }
| mutual_inf { $$ = $1; }
;
entropy : 'H' '(' var_list ')' { $$ = {{$3}}; }
| 'H' '(' var_list '|' var_list ')' { $$ = {{$3}, $5}; }
;
mutual_inf : 'I' '(' mut_inf_core ')' { $$ = {{$3}}; }
| 'I' '(' mut_inf_core '|' var_list ')' { $$ = {{$3}, $5}; }
;
mut_inf_core : mut_inf_core colon var_list { $$ = enlist($1, $3); }
| var_list colon var_list { $$ = {$1, $3}; }
;
colon : ':'
| ';'
;
var_list : var_list ',' NAME { $$ = enlist($1, $3); }
| NAME { $$ = {$1}; }
;
%%
void yy::parser::error(const parser::location_type& l, const std::string& m)
{
throw yy::parser::syntax_error(l, m);
}