1 %{ // -*-Fundamental-*-
4 parser.yy -- YACC -> C++ parser for mudela
6 source file of the GNU LilyPond music typesetter
8 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
9 Jan Nieuwenhuizen <jan@digicash.com>
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"
26 #include "parseconstruct.hh"
28 #include "identifier.hh"
29 #include "command-request.hh"
30 #include "musical-request.hh"
31 #include "my-lily-parser.hh"
32 #include "text-def.hh"
33 #include "translator-group.hh"
35 #include "music-list.hh"
37 #include "duration-convert.hh"
38 #include "change-translator.hh"
39 #include "file-results.hh"
40 #include "mudela-version.hh"
44 Mudela_version oldest_version ("0.1.14");
45 Mudela_version version ("0.1.14");
48 // needed for bison.simple's malloc() and free()
51 int const GUESS_PLET = 5;
52 int guess_plet_a[GUESS_PLET] =
61 Paper_def* current_paper = 0;
67 #define YYERROR_VERBOSE 1
69 #define YYPARSE_PARAM my_lily_parser_l
70 #define YYLEX_PARAM my_lily_parser_l
71 #define THIS ((My_lily_parser *) my_lily_parser_l)
73 #define yyerror THIS->parser_error
79 Array<Interval>* intarr;
80 Array<Melodic_req*> *melreqvec;/* should clean up naming */
81 Array<String> * strvec;
89 Music_list *musiclist;
96 Music_output_def * outputdef;
103 General_script_def * script;
108 Symtables * symtables;
120 yylex (YYSTYPE *s, void * v_l)
122 My_lily_parser *pars_l = (My_lily_parser*) v_l;
123 My_lily_lexer * lex_l = pars_l->lexer_p_;
125 lex_l->lexval_l = (void*) s;
126 return lex_l->yylex ();
134 /* tokens which are not keywords */
159 %token MELODIC_REQUEST
191 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
195 %token <melreq> NOTENAME_IDENTIFIER
196 %token <id> DURATION_IDENTIFIER
197 %token <id> IDENTIFIER
198 %token <id> MELODIC_REQUEST_IDENTIFIER
199 %token <id> MUSIC_IDENTIFIER
200 %token <id> VOICE_IDENTIFIER
201 %token <id> POST_REQUEST_IDENTIFIER
202 %token <id> SCRIPT_IDENTIFIER
203 %token <id> COMMAND_IDENTIFIER
204 %token <id> REAL_IDENTIFIER
205 %token <id> STRING_IDENTIFIER
206 %token <id> TRANS_IDENTIFIER
207 %token <id> INT_IDENTIFIER
208 %token <id> SCORE_IDENTIFIER
209 %token <id> MIDI_IDENTIFIER
210 %token <id> PAPER_IDENTIFIER
211 %token <id> REQUEST_IDENTIFIER
213 %token <string> DURATION RESTNAME
214 %token <string> STRING
218 %type <outputdef> output_def
219 %type <header> mudela_header mudela_header_body
221 %type <i> open_request_parens close_request_parens
222 %type <i> open_abbrev_parens
223 %type <i> open_plet_parens close_plet_parens
224 %type <i> sub_quotes sup_quotes
225 %type <music> simple_element music_elt full_element lyrics_elt command_elt
226 %type <i> abbrev_type
227 %type <i> int unsigned
229 %type <id> identifier_init simple_identifier_init
230 %type <duration> explicit_steno_duration notemode_duration
231 %type <duration> entered_notemode_duration explicit_duration
232 %type <interval> dinterval
233 %type <intvec> intastint_list
234 %type <lookup> symtables symtables_body
235 %type <melreq> melodic_request steno_melodic_req
236 %type <notereq> steno_note_req
237 %type <melreqvec> pitch_list
238 %type <midi> midi_block midi_body
239 %type <moment> duration_length
241 %type <scalar> scalar
242 %type <music> Music transposed_music
243 %type <music> property_def translator_change
244 %type <musiclist> Voice Voice_body
245 %type <chord> Chord Chord_body
246 %type <paper> paper_block paper_body
247 %type <real> dim real
248 %type <real> real_mult_expression real_primary
250 %type <request> abbrev_command_req
251 %type <request> post_request structured_post_request
252 %type <pair> plet_fraction
253 %type <request> command_req verbose_command_req
254 %type <request> script_req dynamic_req
255 %type <string> string
256 %type <string> string_primary
257 %type <score> score_block score_body
258 %type <intarr> shape_array
259 %type <script> script_definition script_body mudela_script gen_script_def
260 %type <textdef> text_def finger
261 %type <string> script_abbreviation
262 %type <symbol> symboldef
263 %type <symtable> symtable symtable_body
264 %type <trans> translator_spec translator_spec_body
265 %type <tempo> tempo_request
266 %type <string> concat_strings
274 | mudela mudela_header {
275 delete THIS->default_header_p_ ;
276 THIS->default_header_p_ = $2;
278 | mudela score_block {
279 score_global_array.push ($2);
281 | mudela add_declaration { }
283 | mudela check_version { }
284 | mudela add_notenames { }
289 Mudela_version ver (*$2);
290 if (!((ver >= oldest_version) && (ver <= version))) {
291 if (THIS->ignore_version_b_) {
292 THIS->here_input ().error ("Incorrect mudela version");
294 THIS->fatal_error_i_ = 1;
295 THIS->parser_error ("Incorrect mudela version");
302 NOTENAMES '{' notenames_body '}'
307 | notenames_body CLEAR {
308 THIS->clear_notenames ();
310 | notenames_body STRING '=' melodic_request {
311 THIS->add_notename (*$2, $4);
320 | mudela_header_body STRING '=' concat_strings ';' {
328 HEADER '{' mudela_header_body '}' {
338 | concat_strings STRING {
349 THIS->remember_spot ();
351 /* cont */ '=' identifier_init optional_semicolon {
352 THIS->lexer_p_->set_identifier (*$1, $4);
353 $4->init_b_ = THIS->init_parse_b_;
354 $4->set_spot (THIS->pop_spot ());
360 $$ = new Score_identifier ($1, SCORE_IDENTIFIER);
364 $$ = new Paper_def_identifier ($1, PAPER_IDENTIFIER);
367 $$ = new Midi_def_identifier ($1, MIDI_IDENTIFIER);
370 | script_definition {
371 $$ = new General_script_def_identifier ($1, SCRIPT_IDENTIFIER);
375 $$ = new Music_identifier ($1, MUSIC_IDENTIFIER);
379 $$ = new Lookup_identifier ($1, IDENTIFIER);
383 $$ = new Request_identifier ($1, POST_REQUEST_IDENTIFIER);
386 $$ = new Request_identifier ($1, MELODIC_REQUEST_IDENTIFIER);
389 | explicit_duration {
390 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
392 | simple_identifier_init
395 simple_identifier_init:
397 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
400 $$ = new String_identifier ($1, STRING_IDENTIFIER);
403 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
406 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
411 TRANSLATOR '{' translator_spec_body '}'
415 translator_spec_body:
417 $$ = $1->translator ();
418 $$-> set_spot (THIS->here_input ());
421 $$ = get_translator_l (*$2)->clone ();
422 $$->set_spot (THIS->here_input ());
425 | translator_spec_body STRING '=' scalar ';' {
426 $$-> set_property (*$2, *$4);
430 | translator_spec_body CONSISTS STRING ';' {
431 $$->group_l ()->consists_str_arr_.push (*$3);
434 | translator_spec_body ACCEPTS STRING ';' {
435 $$->group_l ()->accepts_str_arr_.push (*$3);
444 SCORE { THIS->remember_spot ();
445 THIS->error_level_i_ =0;
447 /*cont*/ '{' score_body '}' {
449 $$->set_spot (THIS->pop_spot ());
450 if (!$$->def_p_arr_.size ())
451 $$->add (THIS->default_paper_p ());
453 /* handle error levels. */
454 $$->errorlevel_i_ = THIS->error_level_i_;
455 THIS->error_level_i_ = 0;
456 if (!$$->header_p_ && THIS->default_header_p_)
457 $$->header_p_ = new Header (*THIS->default_header_p_);
467 | score_body mudela_header {
472 $2->warning ("More than one music block");
475 | score_body output_def {
493 /* */ { $$ =new Array<int>; }
494 | intastint_list int '*' int {
495 $$->push ($2); $$->push ($4);
504 PAPER '{' paper_body '}' {
506 THIS-> lexer_p_->scope_l_arr_.pop ();
517 Paper_def *p = THIS->default_paper_p ();
518 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
521 | PAPER_IDENTIFIER optional_semicolon {
522 Paper_def *p = $1->paperdef ();
523 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
526 | paper_body OUTPUT STRING ';' {
527 $$->outfile_str_ = *$3;
530 | paper_body symtables { $$->set ($2); }
531 | paper_body STRING '=' simple_identifier_init optional_semicolon {
533 id->init_b_ = THIS->init_parse_b_;
534 if (id->is_type_b (Translator_identifier::static_name ()))
536 $$->assign_translator (*$2, id->translator ());
540 THIS->lexer_p_->set_identifier (*$2, id);
543 | paper_body STRING '=' translator_spec {
544 $$-> assign_translator (*$2, $4);
548 | paper_body SHAPE '=' shape_array ';' {
549 $$->shape_int_a_ = *$4;
562 Real *r_p = $1->real ();
564 DOUT << "Lookup real: " << *r_p << "\n";
575 | real '+' real_mult_expression {
578 | real '-' real_mult_expression {
583 real_mult_expression:
585 | real_mult_expression '*' real_primary {
588 | real_mult_expression '/' real_primary {
595 $$ = new Array<Interval>;
597 | shape_array dim dim {
598 $$->push(Interval($2, $2 + $3));
607 '{' midi_body '}' { $$ = $3; }
610 midi_body: /* empty */ {
611 $$ = THIS->default_midi_p ();
613 | midi_body STRING '=' translator_spec {
614 $$-> assign_translator (*$2, $4);
617 | midi_body OUTPUT STRING ';' {
618 $$->outfile_str_ = *$3;
621 | midi_body tempo_request ';' {
622 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
631 TEMPO entered_notemode_duration '=' unsigned {
635 $$-> metronome_i_ = $4;
652 $$->set_spot (THIS->here_input ());
660 full_element { $$ = $1; }
661 | TYPE STRING Music {
663 $$->translator_type_str_ = *$2;
666 | TYPE STRING '=' STRING Music {
668 $$->translator_type_str_ = *$2;
669 $$->translator_id_str_ = *$4;
675 | transposed_music { $$ = $1; }
676 | MUSIC_IDENTIFIER { $$ = $1->music (); }
678 { THIS->lexer_p_->push_note_state (); }
680 { $$=$3; THIS->lexer_p_->pop_state (); }
683 { THIS->lexer_p_->push_lyric_state (); }
685 { $$ = $3; THIS->lexer_p_->pop_state (); }
691 TRANSLATOR STRING '=' STRING {
692 Change_translator * t = new Change_translator;
693 t-> change_to_type_str_ = *$2;
694 t-> change_to_id_str_ = *$4;
697 $$->set_spot (THIS->here_input ());
704 PROPERTY STRING '.' STRING '=' scalar {
705 Translation_property *t = new Translation_property;
706 t-> translator_type_str_ = *$2;
710 $$->set_spot (THIS->here_input ());
718 STRING { $$ = new Scalar (*$1); delete $1; }
719 | int { $$ = new Scalar ($1); }
724 '<' Chord_body '>' { $$ = $2; }
725 | MULTI unsigned Chord {
727 $$->multi_level_i_=$2;
734 $$-> multi_level_i_ = 1;
735 $$->set_spot (THIS->here_input ());
743 TRANSPOSE steno_melodic_req Music {
745 $$ -> transpose ($2);
756 pre_requests simple_element post_requests {
757 THIS->add_requests ((Chord*)$2);//ugh
761 | voice_command ';' { $$ = 0; }
771 $$ = new Request_chord;
772 $$-> set_spot (THIS->here_input ());
773 $1-> set_spot (THIS->here_input ());
774 ((Chord*)$$) ->add ($1);//ugh
781 | verbose_command_req ';' { $$ = $1; }
786 $$ = new Barcheck_req;
788 | COMMAND_IDENTIFIER {
795 $$ = new Bar_req (*$2);
799 Break_req * b = new Break_req;
801 b-> set_spot (THIS->here_input ());
804 | METER unsigned '/' unsigned {
805 Meter_change_req *m = new Meter_change_req;
809 | SKIP duration_length {
810 Skip_req * skip_p = new Skip_req;
811 skip_p->duration_.set_plet ($2->num (),
821 $$ = new Cadenza_req ($2);
823 | PARTIAL duration_length {
824 $$ = new Partial_measure_req (*$2);
828 $$ = new Clef_change_req (*$2);
832 Key_change_req *key_p= new Key_change_req;
833 key_p->melodic_p_arr_ = *$2;
837 | GROUPING intastint_list {
838 $$ = get_grouping_req (*$2); delete $2;
844 /* something silly happened. Junk this stuff*/
845 if (!THIS->post_reqs.empty ())
847 warning ("Junking post-requests");
848 THIS->post_reqs.clear ();
851 | post_requests structured_post_request {
852 $2->set_spot (THIS->here_input ());
853 THIS->post_reqs.push ($2);
855 | post_requests close_request_parens {
856 Array<Request*>& r = *THIS->get_parens_request ($2);
857 for (int i = 0; i < r.size (); i++ )
858 r[i]->set_spot (THIS->here_input ());
859 THIS->post_reqs.concat (r);
864 structured_post_request:
870 POST_REQUEST_IDENTIFIER {
871 $$ = (Request*)$1->request ();
877 Abbreviation_req* a = new Abbreviation_req;
903 Whitespace mustn't be stripped. So what?
904 Python is cool, and there even the amount *and* type of whitespace
905 is significant. So this is not uncool per se, maybe context-free
906 just sucks for humans.
910 NOTENAME_IDENTIFIER {
911 Melodic_req* m = $1->clone ()->musical ()->melodic ();
912 $$ = THIS->get_melodic_req (m, 0 + THIS->default_octave_i_);
914 | NOTENAME_IDENTIFIER sup_quotes {
915 Melodic_req* m = $1->clone ()->musical ()->melodic ();
916 $$ = THIS->get_melodic_req (m, $2 + THIS->default_octave_i_);
918 | NOTENAME_IDENTIFIER sub_quotes {
919 Melodic_req* m = $1->clone ()->musical ()->melodic ();
920 $$ = THIS->get_melodic_req (m, -$2 + THIS->default_octave_i_);
927 *(Melodic_req *) $$ = *$1;
930 | steno_note_req '!' {
931 $$->forceacc_b_ = ! $$->forceacc_b_;
933 /* have to duration here. */
937 MELODIC_REQUEST '{' int int int '}' {/* ugh */
938 $$ = new Melodic_req;
940 $$->notename_i_ = $4;
941 $$->accidental_i_ = $5;
946 DURATION '{' int unsigned '}' {
954 ABSDYNAMIC '{' unsigned '}' {
955 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
956 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
959 | SPANDYNAMIC '{' int int '}' {
960 Span_dynamic_req * sp_p = new Span_dynamic_req;
961 sp_p->spantype = (Span_req::Spantype)$4;
962 sp_p-> dynamic_dir_ = (Direction)$3;
968 unsigned '/' unsigned {
975 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
983 THIS->plet_.type_i_ = $2[1];
984 THIS->plet_.iso_i_ = $2[0];
985 THIS->default_duration_.plet_.type_i_ = $2[1];
986 THIS->default_duration_.plet_.iso_i_ = $2[0];
990 THIS->plet_.type_i_ = 1;
991 THIS->plet_.iso_i_ = 1;
992 THIS->default_duration_.plet_.iso_i_ = 1;
993 THIS->default_duration_.plet_.type_i_ = 1;
995 | TELP plet_fraction {
997 THIS->plet_.type_i_ = $2[1];
998 THIS->plet_.iso_i_ = $2[0];
999 THIS->default_duration_.plet_.type_i_ = $2[1];
1000 THIS->default_duration_.plet_.iso_i_ = $2[0];
1004 close_request_parens:
1026 if (!Duration::duration_type_b ($3))
1027 THIS->parser_error (String ("Not a duration: ") + $3);
1029 THIS->parser_error ("Can't abbreviate");
1031 THIS->set_abbrev_beam ($3);
1038 THIS->plet_.type_i_ = $2[1];
1039 THIS->plet_.iso_i_ = $2[0];
1040 THIS->default_duration_.plet_.type_i_ = $2[1];
1041 THIS->default_duration_.plet_.iso_i_ = $2[0];
1043 | PLET plet_fraction {
1045 THIS->plet_.type_i_ = $2[1];
1046 THIS->plet_.iso_i_ = $2[0];
1047 THIS->default_duration_.plet_.type_i_ = $2[1];
1048 THIS->default_duration_.plet_.iso_i_ = $2[0];
1052 open_request_parens:
1062 | open_abbrev_parens
1069 SCRIPT '{' script_body '}' { $$ = $3; }
1073 STRING int int int int int {
1074 Script_def *s = new Script_def;
1075 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1082 script_dir gen_script_def {
1083 Musical_script_req *m = new Musical_script_req;
1085 m->scriptdef_p_ = $2;
1086 m->set_spot (THIS->here_input ());
1088 m->dir_ = (Direction)$1;
1095 ((Text_def*) $$)->align_i_ = CENTER; /* UGH */
1099 $$-> set_spot (THIS->here_input ());
1103 ((Text_def*)$$)->align_i_ = CENTER;
1109 Text_def *t = new Text_def;
1113 t->style_str_ = THIS->textstyle_str_;
1114 $$->set_spot (THIS->here_input ());
1120 Text_def* t = new Text_def;
1122 t->text_str_ = String ($1);
1123 t->style_str_ = "finger";
1124 $$->set_spot (THIS->here_input ());
1128 script_abbreviation:
1129 '^' { $$ = get_scriptdef ('^'); }
1130 | '+' { $$ = get_scriptdef ('+'); }
1131 | '-' { $$ = get_scriptdef ('-'); }
1132 | '|' { $$ = get_scriptdef ('|'); }
1133 | 'o' { $$ = get_scriptdef ('o'); }
1134 | '>' { $$ = get_scriptdef ('>'); }
1136 $$ = get_scriptdef ('.');
1141 SCRIPT_IDENTIFIER { $$ = $1->script (); }
1142 | script_definition { $$ = $1; }
1143 | script_abbreviation {
1144 $$ = THIS->lexer_p_->lookup_identifier (*$1)->script ();
1158 | pre_requests open_request_parens {
1159 Array<Request*>& r = *THIS->get_parens_request ($2);
1160 for (int i = 0; i < r.size (); i++ )
1161 r[i]->set_spot (THIS->here_input ());
1162 THIS->pre_reqs.concat (r);
1169 THIS->set_duration_mode (*$2);
1172 | DURATION entered_notemode_duration {
1173 THIS->set_default_duration ($2);
1178 This is weird, but default_octave_i_
1179 is used in steno_note_req too
1181 c' -> default_octave_i_ == 1
1184 /* why can't we have \oct 0 iso \oct{c'}*/
1185 // because that's silly.
1187 // for relative octaves, the octave setting is done
1188 // automatically by the parsing of steno_melodic_req!
1189 if (!THIS->relative_octave_mode_b_)
1190 THIS->default_octave_i_ = 1;
1194 if (!THIS->relative_octave_mode_b_)
1195 THIS->default_octave_i_ = $3->octave_i_;
1199 THIS->set_octave_mode (*$2);
1202 | TEXTSTYLE STRING {
1203 THIS->textstyle_str_ = *$2;
1210 $$ = new Moment (0,1);
1212 | duration_length explicit_steno_duration {
1213 *$$ += $2->length ();
1219 | dots '.' { $$ ++; }
1222 entered_notemode_duration:
1224 $$ = new Duration (THIS->default_duration_);
1225 // get sticky plet part too
1226 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1229 $$ = new Duration (THIS->default_duration_);
1230 // get sticky plet part too
1231 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1234 | explicit_steno_duration {
1235 THIS->set_last_duration ($1);
1241 entered_notemode_duration {
1246 explicit_steno_duration:
1249 if (!Duration::duration_type_b ($1))
1250 THIS->parser_error ("Not a duration:" + String ($1));
1252 $$->durlog_i_ = Duration_convert::i2_type ($1);
1253 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1256 | DURATION_IDENTIFIER {
1257 $$ = $1->duration ();
1259 | explicit_steno_duration '.' {
1262 | explicit_steno_duration '*' unsigned {
1263 $$->plet_.iso_i_ = $3; /* ugh. should do *= */
1265 | explicit_steno_duration '/' unsigned {
1266 $$->plet_.type_i_ = $3; /* ugh. should do *= iso = */
1276 if (!Duration::duration_type_b ($2))
1277 THIS->parser_error ("Not a duration:" + String ($2));
1279 THIS->parser_error ("Can't abbreviate");
1286 steno_note_req notemode_duration {
1287 if (!THIS->lexer_p_->note_state_b ())
1288 THIS->parser_error ("have to be in Note mode for notes");
1289 $1->set_duration (*$2);
1290 int durlog_i = $2->durlog_i_;
1291 $$ = THIS->get_note_element ($1, $2);
1293 | RESTNAME notemode_duration {
1294 $$ = THIS->get_rest_element (*$1, $2);
1295 delete $1; // delete notename
1297 | MEASURES notemode_duration {
1298 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1302 Chord*velt_p = new Request_chord;
1303 velt_p->set_spot (THIS->here_input ());
1311 text_def notemode_duration {
1312 /* this sux! text-def should be feature of lyric-engraver. */
1313 if (!THIS->lexer_p_->lyric_state_b ())
1314 THIS->parser_error ("Have to be in Lyric mode for lyrics");
1315 $$ = THIS->get_word_element ($1, $2);
1323 $$ = new Array<Melodic_req*>;
1325 | pitch_list NOTENAME_IDENTIFIER {
1326 $$->push ($2->clone ()->musical ()->melodic ());
1346 int *i_p = $1->intid ();
1356 | STRING_IDENTIFIER {
1357 String *s_p = $1->string ();
1360 DOUT << "Lookup string: " << *s_p << "\n";
1368 | string '+' string_primary {
1375 real_primary unit { $$ = $1*$2; }
1379 unit: CM_T { $$ = 1 CM; }
1380 |IN_T { $$ = 1 INCH; }
1381 |MM_T { $$ = 1 MM; }
1382 |PT_T { $$ = 1 PT; }
1389 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1399 | symtables_body TEXID STRING {
1400 $$->texsetting = *$3;
1403 | symtables_body STRING '=' symtable {
1410 TABLE '{' symtable_body '}' { $$ = $3; }
1414 { $$ = new Symtable; }
1415 | symtable_body STRING symboldef {
1424 $$ = new Atom (*$1, *$2);
1429 Box b (Interval (0,0), Interval (0,0));
1430 $$ = new Atom (*$1, b);
1436 dinterval dinterval {
1437 $$ = new Box (*$1, *$2);
1443 dinterval: dim dim {
1444 $$ = new Interval ($1, $2);
1451 My_lily_parser::set_yydebug (bool b)
1458 My_lily_parser::do_yyparse ()
1460 yyparse ((void*)this);