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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
%{ // -*-Fundamental-*-
#include <iostream.h>
#include "lexer.hh"
#include "paper.hh"
#include "staff.hh"
#include "score.hh"
#include "main.hh"
#include "keyword.hh"
#include "scommands.hh"
#include "debug.hh"
#include "parseconstruct.hh"
#include "dimen.hh"
#include "identifier.hh"
#ifndef NDEBUG
#define YYDEBUG 1
#endif
svec<Request*> pre_reqs, post_reqs;
%}
%union {
Real real;
Command *command;
Identifier *id;
Score_commands *scommands;
Voice *voice;
Voice_element *el;
Staff *staff;
String *string;
Score *score;
const char *consstr;
Paperdef *paper;
Request* request;
int i;
char c;
}
%token VOICE STAFF SCORE TITLE RHYTHMSTAFF BAR NOTENAME OUTPUT
%token CM IN PT MM PAPER WIDTH METER UNITSPACE SKIP COMMANDS
%token MELODICSTAFF GEOMETRIC START_T DURATIONCOMMAND OCTAVECOMMAND
%token <id> IDENTIFIER
%token <string> NEWIDENTIFIER
%token <string> PITCH DURATION RESTNAME
%token <real> REAL
%token <string> STRING
%token <i> OPEN_REQUEST_PARENS CLOSE_REQUEST_PARENS
%type <consstr> unit
%type <id> declaration
%type <paper> paper_block paper_body
%type <real> dim
%type <voice> voice_block voice_body voice_elts voice_elts_dollar
%type <el> voice_elt
%type <command> score_command
%type <score> score_block score_body
%type <staff> staff_block rhythmstaff_block rhythmstaff_body
%type <staff> melodicstaff_block melodicstaff_body staffdecl
%type <i> int
%type <scommands> score_commands_block score_commands_body
%type <request> post_request pre_request
%%
mudela: /* empty */
| mudela score_block {
add_score($2);
}
| mudela add_declaration { }
;
add_declaration: declaration {
add_identifier($1);
}
;
declaration:
NEWIDENTIFIER '=' staff_block {
$$ = new Staff_id(*$1, $3);
delete $1; // this sux
}
| NEWIDENTIFIER '=' voice_block {
$$ = new Voice_id(*$1, $3);
delete $1;
}
;
score_block: SCORE '{' score_body '}' { $$ = $3; }
;
score_body: { $$ = new Score; }
| score_body staff_block { $$->add($2); }
| score_body score_commands_block { $$->set($2); }
| score_body paper_block { $$->set($2); }
;
score_commands_block:
COMMANDS '{' score_commands_body '}' { $$ =$3;}
;
score_commands_body: { $$ = new Score_commands; }
| score_commands_body score_command {
$$->parser_add($2);
}
;
paper_block:
PAPER '{' paper_body '}' { $$ = $3; }
;
paper_body:
/* empty */ { $$ = new Paperdef; }
| paper_body WIDTH dim { $$->linewidth = $3;}
| paper_body OUTPUT STRING { $$->outfile = *$3;
delete $3;
}
| paper_body UNITSPACE dim { $$->whole_width = $3; }
| paper_body GEOMETRIC REAL { $$->geometric_ = $3; }
;
dim:
REAL unit { $$ = convert_dimen($1,$2); }
;
unit: CM { $$ = "cm"; }
|IN { $$ = "in"; }
|MM { $$ = "mm"; }
|PT { $$ = "pt"; }
;
/*
staff
*/
staff_block:
staffdecl
| rhythmstaff_block
| melodicstaff_block
;
staffdecl: STAFF '{' IDENTIFIER '}' { $$ = $3->staff()->clone(); }
;
rhythmstaff_block:
RHYTHMSTAFF '{' rhythmstaff_body '}' { $$ = $3; }
;
rhythmstaff_body:
/* empty */ { $$ = get_new_rhythmstaff(); }
| rhythmstaff_body voice_block { $$->add_voice($2); }
;
melodicstaff_block:
MELODICSTAFF '{' melodicstaff_body '}' { $$ = $3; }
;
melodicstaff_body:
/* empty */ { $$ = get_new_melodicstaff(); }
| melodicstaff_body voice_block { $$->add_voice($2); }
;
/*
voice
*/
voice_block:
VOICE '{' voice_body '}' { $$ = $3; }
;
voice_body:
IDENTIFIER { $$ = new Voice(*$1->voice()); }
| voice_elts_dollar { $$ = $1; }
| voice_body START_T REAL { $$->start = $3; }
;
voice_elts_dollar:
'$' voice_elts '$' { $$ = $2; }
;
voice_elts:
/* empty */ {
$$ = new Voice;
}
| voice_elts pre_requests voice_elt post_requests {
add_requests($3, pre_reqs);
add_requests($3, post_reqs);
$$->add($3);
}
| voice_elts voice_command { }
;
post_requests:
{
assert(post_reqs.empty());
}
| post_requests post_request {
post_reqs.add($2);
}
;
post_request:
CLOSE_REQUEST_PARENS { $$ = get_request($1); }
;
pre_requests:
| pre_requests pre_request {
pre_reqs.add($2);
}
;
pre_request:
OPEN_REQUEST_PARENS { $$ = get_request($1); }
;
/*
*/
voice_command:
DURATIONCOMMAND DURATION {
set_default_duration(*$2);
delete $2;
}
| OCTAVECOMMAND PITCH {
set_default_pitch(*$2);
delete $2;
}
;
voice_elt:
PITCH DURATION {
$$ = get_note_element(*$1, *$2);
delete $1;
delete $2;
}
| RESTNAME DURATION {
$$ = get_rest_element(*$1, *$2);
delete $1;
delete $2;
}
| PITCH { $$ = get_note_element(*$1, "");
delete $1;
}
| RESTNAME { $$ = get_rest_element(*$1, "");
delete $1;
}
;
score_command:
SKIP int ':' REAL {
$$ = get_skip_command($2, $4);
}
| METER int int {
$$ = get_meterchange_command($2, $3);
}
/* | PARTIALMEASURE REAL {
$$ = get_partial_command($2);
}*/
;
int:
REAL {
$$ = int($1);
if (ABS($1-Real(int($$))) > 1e-8)
yyerror("expecting integer number");
}
;
%%
void
parse_file(String s)
{
*mlog << "Parsing ... ";
#ifdef YYDEBUG
yydebug = !monitor.silence("Parser");
#endif
new_input(s);
yyparse();
delete_identifiers();
kill_lexer();
*mlog << "\n";
}
|