1 %{ // -*-Fundamental-*-
4 parser.yy -- Bison/C++ parser for mudela
6 source file of the GNU LilyPond music typesetter
8 (c) 1997 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 Jan Nieuwenhuizen <janneke@gnu.org>
13 #include "notename-table.hh"
15 #include "translation-property.hh"
16 #include "script-def.hh"
17 #include "symtable.hh"
20 #include "my-lily-lexer.hh"
21 #include "paper-def.hh"
22 #include "midi-def.hh"
24 #include "file-path.hh"
27 #include "parseconstruct.hh"
28 #include "dimension.hh"
29 #include "identifier.hh"
30 #include "command-request.hh"
31 #include "musical-request.hh"
32 #include "my-lily-parser.hh"
33 #include "text-def.hh"
34 #include "translator-group.hh"
36 #include "music-list.hh"
38 #include "duration-convert.hh"
39 #include "change-translator.hh"
40 #include "file-results.hh"
41 #include "mudela-version.hh"
43 #include "relative-music.hh"
47 Mudela_version oldest_version ("1.0.3");
48 Mudela_version version ("1.0.4");
51 // needed for bison.simple's malloc() and free()
54 int const GUESS_PLET = 5;
55 int guess_plet_a[GUESS_PLET] =
64 Paper_def* current_paper = 0;
70 #define YYERROR_VERBOSE 1
72 #define YYPARSE_PARAM my_lily_parser_l
73 #define YYLEX_PARAM my_lily_parser_l
74 #define THIS ((My_lily_parser *) my_lily_parser_l)
76 #define yyerror THIS->parser_error
77 #define ARRAY_SIZE(a,s) if (a.size () != s) THIS->parser_error (_f("expecting %d arguments", s))
84 Array<Interval>* intarr;
85 Array<Musical_pitch> *pitch_arr;
86 Array<String> * strvec;
89 Simultaneous_music *chord;
94 Music_list *music_list;
101 Music_output_def * outputdef;
102 Musical_pitch * pitch;
106 Notename_table *notenametab;
110 General_script_def * script;
115 Symtables * symtables;
127 yylex (YYSTYPE *s, void * v_l)
129 My_lily_parser *pars_l = (My_lily_parser*) v_l;
130 My_lily_lexer * lex_l = pars_l->lexer_p_;
132 lex_l->lexval_l = (void*) s;
133 return lex_l->yylex ();
141 /* tokens which are not keywords */
194 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
198 %token <pitch> NOTENAME_PITCH
199 %token <id> DURATION_IDENTIFIER
200 %token <id> IDENTIFIER
201 %token <id> NOTENAME_TABLE_IDENTIFIER
202 %token <id> MUSIC_IDENTIFIER
203 %token <id> POST_REQUEST_IDENTIFIER
204 %token <id> SCRIPT_IDENTIFIER
205 %token <id> COMMAND_IDENTIFIER
206 %token <id> REAL_IDENTIFIER
207 %token <id> STRING_IDENTIFIER
208 %token <id> TRANS_IDENTIFIER
209 %token <id> INT_IDENTIFIER
210 %token <id> SCORE_IDENTIFIER
211 %token <id> MIDI_IDENTIFIER
212 %token <id> PAPER_IDENTIFIER
213 %token <id> REQUEST_IDENTIFIER
215 %token <string> DURATION RESTNAME
216 %token <string> STRING
220 %type <outputdef> output_def
221 %type <header> mudela_header mudela_header_body
223 %type <i> open_request_parens close_request_parens
224 %type <i> open_abbrev_parens
225 %type <i> open_plet_parens close_plet_parens
226 %type <i> sub_quotes sup_quotes
227 %type <music> simple_element request_chord command_element
228 %type <i> abbrev_type
229 %type <i> int unsigned
231 %type <id> identifier_init simple_identifier_init
232 %type <duration> steno_duration notemode_duration
233 %type <duration> entered_notemode_duration explicit_duration
234 %type <interval> dinterval
235 %type <intvec> intastint_list int_list
236 %type <lookup> symtables symtables_body
238 %type <pitch> explicit_musical_pitch steno_musical_pitch musical_pitch absolute_musical_pitch
239 %type <notereq> steno_notepitch
240 %type <pitch_arr> pitch_list
241 %type <midi> midi_block midi_body
242 %type <duration> duration_length
244 %type <scalar> scalar
245 %type <music> Music relative_music Sequential_music Simultaneous_music
246 %type <music> property_def translator_change
247 %type <music_list> Music_list
248 %type <paper> paper_block paper_body
249 %type <real> dim real
250 %type <real> real_mult_expression real_primary
252 %type <request> abbrev_command_req
253 %type <request> post_request structured_post_request
254 %type <pair> plet_fraction
255 %type <request> command_req verbose_command_req
256 %type <request> script_req dynamic_req
257 %type <string> string
258 %type <string> string_primary
259 %type <score> score_block score_body
260 %type <intarr> shape_array
261 %type <script> script_definition script_body mudela_script gen_script_def
262 %type <textdef> text_def finger
263 %type <string> script_abbreviation
264 %type <symbol> symboldef
265 %type <symtable> symtable symtable_body
266 %type <trans> translator_spec translator_spec_body
267 %type <tempo> tempo_request
268 %type <notenametab> notenames_body notenames_block
275 | mudela toplevel_expression {}
276 | mudela add_declaration { }
278 | mudela check_version { }
283 THIS->lexer_p_->set_notename_table ($1);
286 delete header_global_p;
287 header_global_p = $1;
290 score_global_array.push ($1);
293 Identifier * id = new
294 Paper_def_identifier ($1, PAPER_IDENTIFIER);
295 THIS->lexer_p_->set_identifier ("$defaultpaper", id)
298 Identifier * id = new
299 Midi_def_identifier ($1, MIDI_IDENTIFIER);
300 THIS->lexer_p_->set_identifier ("$defaultmidi", id)
306 Mudela_version ver (*$2);
307 if (!((ver >= oldest_version) && (ver <= version))) {
308 if (THIS->ignore_version_b_) {
309 THIS->here_input ().error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
311 THIS->fatal_error_i_ = 1;
312 THIS->parser_error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
320 NOTENAMES '{' notenames_body '}' { $$ = $3; }
327 $$ = new Notename_table;
329 | NOTENAME_TABLE_IDENTIFIER {
330 $$ = $1-> access_Notename_table();
332 | notenames_body STRING '=' explicit_musical_pitch {
344 | mudela_header_body STRING '=' string ';' {
352 HEADER '{' mudela_header_body '}' {
363 THIS->remember_spot ();
365 /* cont */ '=' identifier_init optional_semicolon {
366 THIS->lexer_p_->set_identifier (*$1, $4);
367 $4->init_b_ = THIS->init_parse_b_;
368 $4->set_spot (THIS->pop_spot ());
374 $$ = new Score_identifier ($1, SCORE_IDENTIFIER);
378 $$ = new Notename_table_identifier ($1, NOTENAME_TABLE_IDENTIFIER);
381 $$ = new Paper_def_identifier ($1, PAPER_IDENTIFIER);
384 $$ = new Midi_def_identifier ($1, MIDI_IDENTIFIER);
387 | script_definition {
388 $$ = new General_script_def_identifier ($1, SCRIPT_IDENTIFIER);
392 $$ = new Music_identifier ($1, MUSIC_IDENTIFIER);
396 $$ = new Lookup_identifier ($1, IDENTIFIER);
400 $$ = new Request_identifier ($1, POST_REQUEST_IDENTIFIER);
402 | explicit_duration {
403 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
405 | simple_identifier_init
408 simple_identifier_init:
410 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
413 $$ = new String_identifier ($1, STRING_IDENTIFIER);
416 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
419 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
424 TRANSLATOR '{' translator_spec_body '}'
428 translator_spec_body:
430 $$ = $1->access_Translator ();
431 $$-> set_spot (THIS->here_input ());
434 Translator* t = get_translator_l (*$2);
436 t->set_spot (THIS->here_input ());
440 | translator_spec_body STRING '=' simple_identifier_init ';' {
442 String_identifier *s = id->access_String_identifier ();
443 Real_identifier *r= id->access_Real_identifier ();
444 int_identifier *i = id->access_int_identifier ();
447 if (s) str = *s->access_String (false);
448 if (i) str = to_str (*i->access_int (false));
449 if (r) str = to_str (*r->access_Real (false));
451 THIS->parser_error (_("Wrong type for property value"));
454 $$->set_property (*$2, str);
456 | translator_spec_body CONSISTS STRING ';' {
457 $$->group_l ()->consists_str_arr_.push (*$3);
460 | translator_spec_body ACCEPTS STRING ';' {
461 $$->group_l ()->accepts_str_arr_.push (*$3);
470 SCORE { THIS->remember_spot ();
471 THIS->error_level_i_ =0;
473 /*cont*/ '{' score_body '}' {
475 $$->set_spot (THIS->pop_spot ());
476 if (!$$->def_p_arr_.size ())
477 $$->add_output (THIS->default_paper_p ());
479 /* handle error levels. */
480 $$->errorlevel_i_ = THIS->error_level_i_;
481 THIS->error_level_i_ = 0;
489 $$ = $1->access_Score ();
491 | score_body mudela_header {
496 $2->warning (_ ("More than one music block"));
499 | score_body output_def {
517 /* */ { $$ =new Array<int>; }
518 | intastint_list int '*' int {
519 $$->push ($2); $$->push ($4);
521 | intastint_list int {
522 $$->push ($2); $$->push (1);
531 PAPER '{' paper_body '}' {
533 THIS-> lexer_p_->scope_l_arr_.pop ();
544 Paper_def *p = THIS->default_paper_p ();
545 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
548 | PAPER_IDENTIFIER optional_semicolon {
549 Paper_def *p = $1->access_Paper_def ();
550 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
553 | paper_body OUTPUT STRING ';' {
554 $$->outfile_str_ = *$3;
557 | paper_body int '=' symtables { // ugh, what a syntax
558 $$->set_lookup ($2, $4);
560 | paper_body STRING '=' simple_identifier_init optional_semicolon {
562 id->init_b_ = THIS->init_parse_b_;
563 if (id->is_type_b (Translator_identifier::static_name ()))
565 $$->assign_translator (*$2, id->access_Translator ());
569 THIS->lexer_p_->set_identifier (*$2, id);
572 | paper_body SHAPE '=' shape_array ';' {
573 $$->shape_int_a_ = *$4;
586 Real *r_p = $1->access_Real ();
588 DOUT << "Lookup real: " << *r_p << "\n";
599 | real '+' real_mult_expression {
602 | real '-' real_mult_expression {
607 real_mult_expression:
609 | real_mult_expression '*' real_primary {
612 | real_mult_expression '/' real_primary {
619 $$ = new Array<Interval>;
621 | shape_array dim dim {
622 $$->push(Interval($2, $2 + $3));
631 '{' midi_body '}' { $$ = $3; }
634 midi_body: /* empty */ {
635 $$ = THIS->default_midi_p ();
638 $$ = $1-> access_Midi_def ();
640 | midi_body STRING '=' translator_spec {
641 $$-> assign_translator (*$2, $4);
644 | midi_body OUTPUT STRING ';' {
645 $$->outfile_str_ = *$3;
648 | midi_body tempo_request ';' {
649 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
658 TEMPO entered_notemode_duration '=' unsigned {
662 $$-> metronome_i_ = $4;
666 Music_list: /* empty */ {
677 Sequential_music: '{' Music_list '}' {
678 $$ = new Sequential_music ($2);
682 Simultaneous_music: '<' Music_list '>' {
683 $$ = new Simultaneous_music ($2);
687 request_chord { $$ = $1; }
688 | TYPE STRING Music {
690 $$->translator_type_str_ = *$2;
693 | TYPE STRING '=' STRING Music {
695 $$->translator_type_str_ = *$2;
696 $$->translator_id_str_ = *$4;
700 | Simultaneous_music { $$ = $1; }
701 | Sequential_music { $$ = $1; }
702 | TRANSPOSE musical_pitch Music {
704 $$ -> transpose (*$2);
707 | MUSIC_IDENTIFIER { $$ = $1->access_Music (); }
709 { THIS->lexer_p_->push_note_state (); }
712 THIS->lexer_p_->pop_state ();
716 { THIS->lexer_p_->push_lyric_state (); }
720 THIS->lexer_p_->pop_state ();
724 | relative_music { $$ = $1; }
728 RELATIVE absolute_musical_pitch Music {
729 $$ = new Relative_octave_music ($3, *$2);
735 TRANSLATOR STRING '=' STRING {
736 Change_translator * t = new Change_translator;
737 t-> change_to_type_str_ = *$2;
738 t-> change_to_id_str_ = *$4;
741 $$->set_spot (THIS->here_input ());
748 PROPERTY STRING '.' STRING '=' scalar {
749 Translation_property *t = new Translation_property;
750 t-> translator_type_str_ = *$2;
754 $$->set_spot (THIS->here_input ());
762 STRING { $$ = new Scalar (*$1); delete $1; }
763 | int { $$ = new Scalar ($1); }
769 pre_requests simple_element post_requests {
770 THIS->add_requests ((Simultaneous_music*)$2);//ugh
781 $$ = new Request_chord;
782 $$-> set_spot (THIS->here_input ());
783 $1-> set_spot (THIS->here_input ());
784 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
791 | verbose_command_req ';' { $$ = $1; }
796 $$ = new Barcheck_req;
798 | COMMAND_IDENTIFIER {
799 $$ = $1->access_Request ();
805 $$ = new Bar_req (*$2);
809 $$ = new Mark_req (*$2);
813 $$ = new Mark_req (to_str ($2));
815 | TIME_T unsigned '/' unsigned {
816 Time_signature_change_req *m = new Time_signature_change_req;
822 Break_req * b = new Break_req;
824 b-> set_spot (THIS->here_input ());
827 | SKIP duration_length {
828 Skip_req * skip_p = new Skip_req;
829 skip_p->duration_ = *$2;
837 $$ = new Cadenza_req ($2);
839 | PARTIAL duration_length {
840 $$ = new Partial_measure_req ($2->length ());
844 $$ = new Clef_change_req (*$2);
847 | KEY NOTENAME_PITCH {
848 Key_change_req *key_p= new Key_change_req;
849 key_p->pitch_arr_.push(*$2);
850 key_p->ordinary_key_b_ = true;
854 | KEYSIGNATURE pitch_list {
855 Key_change_req *key_p= new Key_change_req;
856 key_p->pitch_arr_ = *$2;
857 key_p->ordinary_key_b_ = false;
861 | GROUPING intastint_list {
862 $$ = get_grouping_req (*$2); delete $2;
868 /* something silly happened. Junk this stuff*/
869 if (!THIS->post_reqs.empty ())
871 warning ("Junking post-requests");
872 THIS->post_reqs.clear ();
875 | post_requests structured_post_request {
876 $2->set_spot (THIS->here_input ());
877 THIS->post_reqs.push ($2);
879 | post_requests close_request_parens {
880 Array<Request*>& r = *THIS->get_parens_request ($2);
881 for (int i = 0; i < r.size (); i++ )
882 r[i]->set_spot (THIS->here_input ());
883 THIS->post_reqs.concat (r);
888 structured_post_request:
894 POST_REQUEST_IDENTIFIER {
895 $$ = (Request*)$1->access_Request ();
901 Abbreviation_req* a = new Abbreviation_req;
928 | NOTENAME_PITCH sup_quotes {
932 | NOTENAME_PITCH sub_quotes {
934 $$->octave_i_ += - $2;
938 explicit_musical_pitch:
939 MUSICAL_PITCH '{' int_list '}' {/* ugh */
942 $$ = new Musical_pitch;
943 $$->octave_i_ = a[0];
944 $$->notename_i_ = a[1];
945 $$->accidental_i_ = a[2];
952 | explicit_musical_pitch
962 | steno_notepitch '!' {
963 $$->forceacc_b_ = ! $$->forceacc_b_;
969 DURATION '{' int_list '}' {
974 $$-> durlog_i_ = a[0];
982 ABSDYNAMIC '{' unsigned '}' {
983 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
984 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
987 | SPANDYNAMIC '{' int int '}' {
988 Span_dynamic_req * sp_p = new Span_dynamic_req;
989 sp_p->spantype = (Span_req::Spantype)$4;
990 sp_p-> dynamic_dir_ = (Direction)$3;
996 unsigned '/' unsigned {
1003 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
1011 THIS->plet_.type_i_ = $2[1];
1012 THIS->plet_.iso_i_ = $2[0];
1013 THIS->default_duration_.plet_ = THIS->plet_;
1017 THIS->plet_.type_i_ = 1;
1018 THIS->plet_.iso_i_ = 1;
1019 THIS->default_duration_.plet_ = THIS->plet_;
1021 | TELP plet_fraction {
1023 THIS->plet_.type_i_ = $2[1];
1024 THIS->plet_.iso_i_ = $2[0];
1025 THIS->default_duration_.plet_ = THIS->plet_;
1029 close_request_parens:
1051 if (!Duration::duration_type_b ($3))
1052 THIS->parser_error (_f ("not a duration: %d", $3));
1054 THIS->parser_error (_ ("can't abbreviate"));
1056 THIS->set_abbrev_beam ($3);
1063 THIS->plet_.type_i_ = $2[1];
1064 THIS->plet_.iso_i_ = $2[0];
1065 THIS->default_duration_.plet_ = THIS->plet_;
1067 | PLET plet_fraction {
1069 THIS->plet_.type_i_ = $2[1];
1070 THIS->plet_.iso_i_ = $2[0];
1071 THIS->default_duration_.plet_ = THIS->plet_;
1075 open_request_parens:
1085 | open_abbrev_parens
1092 SCRIPT '{' script_body '}' { $$ = $3; }
1096 STRING int int int int int {
1097 Script_def *s = new Script_def;
1098 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1105 script_dir gen_script_def {
1106 Musical_script_req *m = new Musical_script_req;
1108 m->scriptdef_p_ = $2;
1109 m->set_spot (THIS->here_input ());
1111 m->dir_ = (Direction)$1;
1118 ((Text_def*) $$)->align_dir_ = LEFT; /* UGH */
1122 $$-> set_spot (THIS->here_input ());
1126 ((Text_def*)$$)->align_dir_ = RIGHT; /* UGH */
1132 Text_def *t = new Text_def;
1136 $$->set_spot (THIS->here_input ());
1142 Text_def* t = new Text_def;
1144 t->text_str_ = to_str ($1);
1145 t->style_str_ = "finger";
1146 $$->set_spot (THIS->here_input ());
1150 script_abbreviation:
1151 '^' { $$ = get_scriptdef ('^'); }
1152 | '+' { $$ = get_scriptdef ('+'); }
1153 | '-' { $$ = get_scriptdef ('-'); }
1154 | '|' { $$ = get_scriptdef ('|'); }
1155 | 'o' { $$ = get_scriptdef ('o'); }
1156 | '>' { $$ = get_scriptdef ('>'); }
1158 $$ = get_scriptdef ('.');
1163 SCRIPT_IDENTIFIER { $$ = $1->access_General_script_def (true); }
1164 | script_definition { $$ = $1; }
1165 | script_abbreviation {
1166 $$ = THIS->lexer_p_->lookup_identifier (*$1)->access_General_script_def (true);
1180 | pre_requests open_request_parens {
1181 Array<Request*>& r = *THIS->get_parens_request ($2);
1182 for (int i = 0; i < r.size (); i++ )
1183 r[i]->set_spot (THIS->here_input ());
1184 THIS->pre_reqs.concat (r);
1189 absolute_musical_pitch:
1190 steno_musical_pitch {
1203 | dots '.' { $$ ++; }
1206 entered_notemode_duration:
1208 $$ = new Duration (THIS->default_duration_);
1211 $$ = new Duration (THIS->default_duration_);
1215 THIS->set_last_duration ($1);
1220 entered_notemode_duration {
1228 if (!Duration::duration_type_b ($1))
1229 THIS->parser_error (_f ("not a duration: %d", $1));
1231 $$->durlog_i_ = Duration_convert::i2_type ($1);
1232 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1235 | DURATION_IDENTIFIER {
1236 $$ = $1->access_Duration ();
1238 | steno_duration '.' {
1241 | steno_duration '*' unsigned {
1242 $$->plet_.iso_i_ *= $3;
1244 | steno_duration '/' unsigned {
1245 $$->plet_.type_i_ *= $3;
1255 if (!Duration::duration_type_b ($2))
1256 THIS->parser_error (_f ("not a duration: %d", $2));
1258 THIS->parser_error (_ ("can't abbreviate"));
1266 steno_notepitch notemode_duration {
1267 if (!THIS->lexer_p_->note_state_b ())
1268 THIS->parser_error (_ ("have to be in Note mode for notes"));
1269 $1->set_duration (*$2);
1270 int durlog_i = $2->durlog_i_;
1271 $$ = THIS->get_note_element ($1, $2);
1273 | RESTNAME notemode_duration {
1274 $$ = THIS->get_rest_element (*$1, $2);
1275 delete $1; // delete notename
1277 | MEASURES notemode_duration {
1278 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1282 Simultaneous_music*velt_p = new Request_chord;
1283 velt_p->set_spot (THIS->here_input ());
1284 velt_p->add_music (m);
1288 | STRING notemode_duration {
1289 if (!THIS->lexer_p_->lyric_state_b ())
1290 THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1291 $$ = THIS->get_word_element (*$1, $2);
1301 $$ = new Array<Musical_pitch>;
1303 | pitch_list musical_pitch {
1335 int *i_p = $1->access_int ();
1345 | STRING_IDENTIFIER {
1346 String *s_p = $1->access_String ();
1349 DOUT << "Lookup string: " << *s_p << "\n";
1357 | string '+' string_primary {
1364 real_primary unit { $$ = $1*$2; }
1368 unit: CM_T { $$ = 1 CM; }
1369 |IN_T { $$ = 1 INCH; }
1370 |MM_T { $$ = 1 MM; }
1371 |PT_T { $$ = 1 PT; }
1378 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1386 $$ = $1->access_Lookup ();
1388 | symtables_body FONT STRING {
1390 $$->font_path_ = global_path.find (*$3);
1391 if (!$$->font_path_.length_i ())
1392 THIS->here_input ().error (_f("can't open file: `%s'", $3->ch_C()));
1396 | symtables_body STRING '=' symtable {
1403 TABLE '{' symtable_body '}' { $$ = $3; }
1407 { $$ = new Symtable; }
1408 | symtable_body STRING symboldef {
1417 $$ = new Atom (*$1, *$2);
1422 Box b (Interval (0,0), Interval (0,0));
1423 $$ = new Atom (*$1, b);
1429 dinterval dinterval {
1430 $$ = new Box (*$1, *$2);
1436 dinterval: dim dim {
1437 $$ = new Interval ($1, $2);
1444 My_lily_parser::set_yydebug (bool b)
1451 My_lily_parser::do_yyparse ()
1453 yyparse ((void*)this);