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 #define MUDELA_VERSION "0.1.9"
18 #include "translation-property.hh"
19 #include "script-def.hh"
20 #include "symtable.hh"
23 #include "my-lily-lexer.hh"
24 #include "paper-def.hh"
25 #include "midi-def.hh"
29 #include "parseconstruct.hh"
31 #include "identifier.hh"
32 #include "command-request.hh"
33 #include "musical-request.hh"
34 #include "my-lily-parser.hh"
35 #include "text-def.hh"
36 #include "translator-group.hh"
38 #include "music-list.hh"
40 #include "duration-convert.hh"
41 #include "change-translator.hh"
43 // needed for bison.simple's malloc() and free()
46 int const GUESS_PLET = 5;
47 int guess_plet_a[GUESS_PLET] =
60 #define YYERROR_VERBOSE 1
62 #define YYPARSE_PARAM my_lily_parser_l
63 #define YYLEX_PARAM my_lily_parser_l
64 #define THIS ((My_lily_parser *) my_lily_parser_l)
66 #define yyerror THIS->parser_error
72 Array<Interval>* intarr;
73 Array<Melodic_req*> *melreqvec;/* should clean up naming */
74 Array<String> * strvec;
82 Music_list *musiclist;
89 Music_output_def * outputdef;
96 General_script_def * script;
101 Symtables * symtables;
113 yylex (YYSTYPE *s, void * v_l)
115 My_lily_parser *pars_l = (My_lily_parser*) v_l;
116 My_lily_lexer * lex_l = pars_l->lexer_p_;
118 lex_l->lexval_l = (void*) s;
119 return lex_l->yylex ();
127 /* tokens which are not keywords */
152 %token MELODIC_REQUEST
183 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
187 %token <melreq> NOTENAME_ID
188 %token <id> DURATION_IDENTIFIER
189 %token <id> IDENTIFIER
190 %token <id> MELODIC_REQUEST_IDENTIFIER
191 %token <id> MUSIC_IDENTIFIER
192 %token <id> VOICE_IDENTIFIER
193 %token <id> POST_REQUEST_IDENTIFIER
194 %token <id> SCRIPT_IDENTIFIER
195 %token <id> COMMAND_IDENTIFIER
196 %token <id> REAL_IDENTIFIER
197 %token <id> TRANS_IDENTIFIER
198 %token <id> INT_IDENTIFIER
199 %token <id> SCORE_IDENTIFIER
200 %token <id> MIDI_IDENTIFIER
201 %token <id> PAPER_IDENTIFIER
202 %token <id> REQUEST_IDENTIFIER
204 %token <string> DURATION RESTNAME
205 %token <string> STRING
207 %token <i> POST_QUOTES
208 %token <i> PRE_QUOTES
210 %type <outputdef> output_def
211 %type <header> mudela_header mudela_header_body
213 %type <i> open_request_parens close_request_parens
214 %type <c> open_abbrev_parens
215 %type <i> open_plet_parens close_plet_parens
216 %type <music> simple_element music_elt full_element lyrics_elt command_elt
217 %type <i> abbrev_type
218 %type <i> int unsigned
220 %type <id> identifier_init
221 %type <duration> explicit_steno_duration notemode_duration
222 %type <duration> entered_notemode_duration explicit_duration
223 %type <interval> dinterval
224 %type <intvec> intastint_list
225 %type <lookup> symtables symtables_body
226 %type <melreq> melodic_request steno_melodic_req
227 %type <notereq> steno_note_req
228 %type <melreqvec> pitch_list
229 %type <midi> midi_block midi_body
230 %type <moment> duration_length
232 %type <scalar> scalar
233 %type <music> Music transposed_music
234 %type <music> property_def translator_change
235 %type <musiclist> Voice Voice_body
236 %type <chord> Chord Chord_body
237 %type <paper> paper_block paper_body
238 %type <real> dim real
240 %type <request> abbrev_command_req
241 %type <request> post_request structured_post_request
242 %type <pair> plet_fraction
243 %type <request> command_req verbose_command_req
244 %type <request> script_req dynamic_req
245 %type <score> score_block score_body
246 %type <intarr> shape_array
247 %type <script> script_definition script_body mudela_script gen_script_def
248 %type <textdef> text_def finger
249 %type <string> script_abbreviation
250 %type <symbol> symboldef
251 %type <symtable> symtable symtable_body
252 %type <trans> translator_spec translator_spec_body
253 %type <tempo> tempo_request
254 %type <string> concat_strings
262 | mudela mudela_header {
263 delete THIS->default_header_p_ ;
264 THIS->default_header_p_ = $2;
266 | mudela score_block {
269 | mudela add_declaration { }
271 | mudela check_version { }
272 | mudela add_notenames { }
277 if (String (*$2) != MUDELA_VERSION) {
278 if (THIS->ignore_version_b_) {
279 THIS->here_input ().error ("Incorrect mudela version");
281 THIS->fatal_error_i_ = 1;
282 THIS->parser_error ("Incorrect mudela version");
289 NOTENAMES '{' notenames_body '}'
294 | notenames_body CLEAR {
295 THIS->clear_notenames ();
297 | notenames_body STRING '=' melodic_request {
298 THIS->add_notename (*$2, $4);
307 | mudela_header_body STRING '=' concat_strings ';' {
315 HEADER '{' mudela_header_body '}' {
325 | concat_strings STRING {
336 THIS->remember_spot ();
338 /* cont */ '=' identifier_init {
339 THIS->lexer_p_->set_identifier (*$1, $4);
340 $4->init_b_ = THIS->init_parse_b_;
341 $4->set_spot (THIS->pop_spot ());
347 $$ = new Score_id ($1, SCORE_IDENTIFIER);
351 $$ = new Paper_def_id ($1, PAPER_IDENTIFIER);
355 $$ = new Midi_def_id ($1, MIDI_IDENTIFIER);
358 | script_definition {
359 $$ = new Script_id ($1, SCRIPT_IDENTIFIER);
363 $$ = new Music_id ($1, MUSIC_IDENTIFIER);
367 $$ = new Lookup_id ($1, IDENTIFIER);
371 $$ = new Real_id (new Real ($1), REAL_IDENTIFIER);
375 $$ = new Int_id (new int ($1), INT_IDENTIFIER);
378 $$ = new Request_id ($1, POST_REQUEST_IDENTIFIER);
381 $$ = new Request_id ($1, MELODIC_REQUEST_IDENTIFIER);
385 $$ = new Translator_id ($1, TRANS_IDENTIFIER);
387 | explicit_duration {
388 $$ = new Duration_id ($1, DURATION_IDENTIFIER);
395 TRANSLATOR '{' translator_spec_body '}'
399 translator_spec_body:
401 $$ = $1->translator ();
402 $$-> set_spot (THIS->here_input ());
405 $$ = get_translator_l (*$2)->clone ();
406 $$->set_spot (THIS->here_input ());
409 | translator_spec_body STRING '=' scalar ';' {
410 $$-> set_property (*$2, *$4);
414 | translator_spec_body CONSISTS STRING ';' {
415 $$->group_l ()->consists_str_arr_.push (*$3);
418 | translator_spec_body ACCEPTS STRING ';' {
419 $$->group_l ()->accepts_str_arr_.push (*$3);
428 SCORE { THIS->remember_spot ();
429 THIS->error_level_i_ =0;
431 /*cont*/ '{' score_body '}' {
433 $$->set_spot (THIS->pop_spot ());
434 if (!$$->def_p_arr_.size ())
435 $$->add (THIS->default_paper ());
437 /* handle error levels. */
438 $$->errorlevel_i_ = THIS->error_level_i_;
439 THIS->error_level_i_ = 0;
440 if (!$$->header_p_ && THIS->default_header_p_)
441 $$->header_p_ = new Header (*THIS->default_header_p_);
451 | score_body mudela_header {
456 $2->warning ("More than one music block");
459 | score_body output_def {
477 /* */ { $$ =new Array<int>; }
478 | intastint_list int '*' int {
479 $$->push ($2); $$->push ($4);
489 '{' paper_body '}' { $$ = $3; }
494 $$ = THIS->default_paper ();
497 $$ = $1->paperdef ();
499 | paper_body OUTPUT STRING ';' {
500 $$->outfile_str_ = *$3;
503 | paper_body symtables { $$->set ($2); }
504 | paper_body STRING '=' dim ';' {
505 $$->set_var (*$2, $4);
507 | paper_body STRING '=' real ';' {
508 $$->set_var (*$2, $4);
510 | paper_body STRING '=' translator_spec {
511 $$-> assign_translator (*$2, $4);
514 | paper_body SHAPE '=' shape_array ';' {
515 $$->shape_int_a_ = *$4;
525 $$ = new Array<Interval>;
527 | shape_array dim dim {
528 $$->push(Interval($2, $2 + $3));
537 '{' midi_body '}' { $$ = $3; }
540 midi_body: /* empty */ {
541 $$ = THIS->default_midi ();
543 | midi_body STRING '=' translator_spec {
544 $$-> assign_translator (*$2, $4);
547 | midi_body OUTPUT STRING ';' {
548 $$->outfile_str_ = *$3;
551 | midi_body tempo_request ';' {
552 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
561 TEMPO entered_notemode_duration '=' unsigned {
565 $$-> metronome_i_ = $4;
582 $$->set_spot (THIS->here_input ());
590 full_element { $$ = $1; }
591 | TYPE STRING Music {
593 $$->translator_type_str_ = *$2;
596 | TYPE STRING '=' STRING Music {
598 $$->translator_type_str_ = *$2;
599 $$->translator_id_str_ = *$4;
605 | transposed_music { $$ = $1; }
606 | MUSIC_IDENTIFIER { $$ = $1->music (); }
607 | MUSIC_IDENTIFIER ';' { $$ = $1->music (); }
609 { THIS->lexer_p_->push_note_state (); }
611 { $$=$3; THIS->lexer_p_->pop_state (); }
614 { THIS->lexer_p_->push_lyric_state (); }
616 { $$ = $3; THIS->lexer_p_->pop_state (); }
622 TRANSLATOR STRING '=' STRING {
623 Change_translator * t = new Change_translator;
624 t-> change_to_type_str_ = *$2;
625 t-> change_to_id_str_ = *$4;
628 $$->set_spot (THIS->here_input ());
635 PROPERTY STRING '.' STRING '=' scalar {
636 Translation_property *t = new Translation_property;
637 t-> translator_type_str_ = *$2;
641 $$->set_spot (THIS->here_input ());
649 STRING { $$ = new Scalar (*$1); delete $1; }
650 | int { $$ = new Scalar ($1); }
655 '<' Chord_body '>' { $$ = $2; }
656 | MULTI unsigned Chord {
658 $$->multi_level_i_=$2;
665 $$-> multi_level_i_ = 1;
666 $$->set_spot (THIS->here_input ());
674 TRANSPOSE steno_melodic_req Music {
676 $$ -> transpose ($2);
687 pre_requests simple_element post_requests {
688 THIS->add_requests ((Chord*)$2);//ugh
692 | voice_command ';' { $$ = 0; }
702 $$ = new Request_chord;
703 $$-> set_spot (THIS->here_input ());
704 $1-> set_spot (THIS->here_input ());
705 ((Chord*)$$) ->add ($1);//ugh
712 | verbose_command_req ';' { $$ = $1; }
717 $$ = new Barcheck_req;
719 | COMMAND_IDENTIFIER {
726 $$ = new Bar_req (*$2);
730 Break_force_req * f = new Break_force_req;
731 f-> set_spot (THIS->here_input ());
734 | METER unsigned '/' unsigned {
735 Meter_change_req *m = new Meter_change_req;
739 | SKIP duration_length {
740 Skip_req * skip_p = new Skip_req;
741 skip_p->duration_.set_plet ($2->num (),
751 $$ = new Cadenza_req ($2);
753 | PARTIAL duration_length {
754 $$ = new Partial_measure_req (*$2);
758 $$ = new Clef_change_req (*$2);
762 Key_change_req *key_p= new Key_change_req;
763 key_p->melodic_p_arr_ = *$2;
767 | GROUPING intastint_list {
768 $$ = get_grouping_req (*$2); delete $2;
774 /* something silly happened. Junk this stuff*/
775 if (!THIS->post_reqs.empty ())
777 warning ("Junking post-requests");
778 THIS->post_reqs.clear ();
781 | post_requests structured_post_request {
782 $2->set_spot (THIS->here_input ());
783 THIS->post_reqs.push ($2);
785 | post_requests close_request_parens {
786 Array<Request*>& r = *THIS->get_parens_request ($2);
787 for (int i = 0; i < r.size (); i++ )
788 r[i]->set_spot (THIS->here_input ());
789 THIS->post_reqs.concat (r);
794 structured_post_request:
800 POST_REQUEST_IDENTIFIER {
801 $$ = (Request*)$1->request ();
807 Abbreviation_req* a = new Abbreviation_req;
820 $$ = $1->clone ()->musical ()->melodic ();
821 $$->octave_i_ += THIS->default_octave_i_;
823 | steno_melodic_req POST_QUOTES {
824 $$-> octave_i_ += $2;
826 | PRE_QUOTES steno_melodic_req {
828 $2-> octave_i_ -= $1;
835 * (Melodic_req *) $$ = *$1;
838 | steno_note_req '!' {
839 $$->forceacc_b_ = ! $$->forceacc_b_;
841 /* have to duration here. */
845 MELODIC_REQUEST '{' int int int '}' {/* ugh */
846 $$ = new Melodic_req;
848 $$->notename_i_ = $4;
849 $$->accidental_i_ = $5;
854 DURATION '{' int unsigned '}' {
862 ABSDYNAMIC '{' unsigned '}' {
863 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
864 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
867 | SPANDYNAMIC '{' int int '}' {
868 Span_dynamic_req * sp_p = new Span_dynamic_req;
869 sp_p->spantype = (Span_req::Spantype)$4;
870 sp_p-> dynamic_dir_ = (Direction)$3;
876 unsigned '/' unsigned {
883 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
891 THIS->plet_.type_i_ = $2[1];
892 THIS->plet_.iso_i_ = $2[0];
893 THIS->default_duration_.plet_.type_i_ = $2[1];
894 THIS->default_duration_.plet_.iso_i_ = $2[0];
898 THIS->plet_.type_i_ = 1;
899 THIS->plet_.iso_i_ = 1;
900 THIS->default_duration_.plet_.iso_i_ = 1;
901 THIS->default_duration_.plet_.type_i_ = 1;
903 | TELP plet_fraction {
905 THIS->plet_.type_i_ = $2[1];
906 THIS->plet_.iso_i_ = $2[0];
907 THIS->default_duration_.plet_.type_i_ = $2[1];
908 THIS->default_duration_.plet_.iso_i_ = $2[0];
912 close_request_parens:
928 | close_plet_parens {
935 if (!Duration::duration_type_b ($3))
936 THIS->parser_error ("1:Not a duration");
938 THIS->parser_error ("Can't abbreviate");
940 THIS->set_abbrev_beam ($3);
947 THIS->plet_.type_i_ = $2[1];
948 THIS->plet_.iso_i_ = $2[0];
949 THIS->default_duration_.plet_.type_i_ = $2[1];
950 THIS->default_duration_.plet_.iso_i_ = $2[0];
952 | PLET plet_fraction {
954 THIS->plet_.type_i_ = $2[1];
955 THIS->plet_.iso_i_ = $2[0];
956 THIS->default_duration_.plet_.type_i_ = $2[1];
957 THIS->default_duration_.plet_.iso_i_ = $2[0];
971 | open_abbrev_parens {
980 SCRIPT '{' script_body '}' { $$ = $3; }
984 STRING int int int int int {
985 Script_def *s = new Script_def;
986 s->set_from_input (*$1,$2, $3,$4,$5, $6);
993 script_dir gen_script_def {
994 Musical_script_req *m = new Musical_script_req;
996 m->scriptdef_p_ = $2;
997 m->set_spot (THIS->here_input ());
999 m->dir_ = (Direction)$1;
1006 ((Text_def*) $$)->align_i_ = CENTER; /* UGH */
1010 $$-> set_spot (THIS->here_input ());
1014 ((Text_def*)$$)->align_i_ = CENTER;
1020 Text_def *t = new Text_def;
1024 t->style_str_ = THIS->textstyle_str_;
1025 $$->set_spot (THIS->here_input ());
1031 Text_def* t = new Text_def;
1033 t->text_str_ = String ($1);
1034 t->style_str_ = "finger";
1035 $$->set_spot (THIS->here_input ());
1039 script_abbreviation:
1040 '^' { $$ = get_scriptdef ('^'); }
1041 | '+' { $$ = get_scriptdef ('+'); }
1042 | '-' { $$ = get_scriptdef ('-'); }
1043 | '|' { $$ = get_scriptdef ('|'); }
1044 | 'o' { $$ = get_scriptdef ('o'); }
1045 | '>' { $$ = get_scriptdef ('>'); }
1047 $$ = get_scriptdef ('.');
1052 SCRIPT_IDENTIFIER { $$ = $1->script (); }
1053 | script_definition { $$ = $1; }
1054 | script_abbreviation {
1055 $$ = THIS->lexer_p_->lookup_identifier (*$1)->script ();
1069 | pre_requests open_request_parens {
1070 Array<Request*>& r = *THIS->get_parens_request ($2);
1071 for (int i = 0; i < r.size (); i++ )
1072 r[i]->set_spot (THIS->here_input ());
1073 THIS->pre_reqs.concat (r);
1080 THIS->set_duration_mode (*$2);
1083 | DURATION entered_notemode_duration {
1084 THIS->set_default_duration ($2);
1089 This is weird, but default_octave_i_
1090 is used in steno_note_req too
1092 c' -> default_octave_i_ == 1
1094 /* why can't we have \oct 0 iso \oct{c'}*/
1095 THIS->default_octave_i_ = 1; }
1098 THIS->default_octave_i_ = $3->octave_i_;
1101 | TEXTSTYLE STRING {
1102 THIS->textstyle_str_ = *$2;
1109 $$ = new Moment (0,1);
1111 | duration_length explicit_steno_duration {
1112 *$$ += $2->length ();
1118 | dots '.' { $$ ++; }
1121 entered_notemode_duration:
1123 $$ = new Duration (THIS->default_duration_);
1126 $$ = new Duration (THIS->default_duration_);
1129 | explicit_steno_duration {
1130 THIS->set_last_duration ($1);
1136 entered_notemode_duration {
1141 explicit_steno_duration:
1144 if (!Duration::duration_type_b ($1))
1145 THIS->parser_error ("2:Not a duration");
1147 $$->durlog_i_ = Duration_convert::i2_type ($1);
1148 $$->set_plet (THIS->default_duration_);
1151 | DURATION_IDENTIFIER {
1152 $$ = $1->duration ();
1154 | explicit_steno_duration '.' {
1157 | explicit_steno_duration '*' unsigned {
1158 $$->plet_.iso_i_ = $3;
1160 | explicit_steno_duration '/' unsigned {
1161 $$->plet_.type_i_ = $3;
1171 if (!Duration::duration_type_b ($2))
1172 THIS->parser_error ("3:Not a duration");
1174 THIS->parser_error ("Can't abbreviate");
1181 steno_note_req notemode_duration {
1182 if (!THIS->lexer_p_->note_state_b ())
1183 THIS->parser_error ("have to be in Note mode for notes");
1184 $1->set_duration (*$2);
1185 int durlog_i = $2->durlog_i_;
1186 $$ = THIS->get_note_element ($1, $2);
1188 | RESTNAME notemode_duration {
1189 $$ = THIS->get_rest_element (*$1, $2);
1195 text_def notemode_duration {
1196 /* this sux! text-def should be feature of lyric-engraver. */
1197 if (!THIS->lexer_p_->lyric_state_b ())
1198 THIS->parser_error ("Have to be in Lyric mode for lyrics");
1199 $$ = THIS->get_word_element ($1, $2);
1207 $$ = new Array<Melodic_req*>;
1209 | pitch_list NOTENAME_ID {
1210 $$->push ($2->clone ()->musical ()->melodic ());
1230 int *i_p = $1->intid ();
1241 Real *r_p = $1->real ();
1250 real unit { $$ = $1*$2; }
1254 unit: CM_T { $$ = 1 CM; }
1255 |IN_T { $$ = 1 INCH; }
1256 |MM_T { $$ = 1 MM; }
1257 |PT_T { $$ = 1 PT; }
1264 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1274 | symtables_body TEXID STRING {
1275 $$->texsetting = *$3;
1278 | symtables_body STRING '=' symtable {
1285 TABLE '{' symtable_body '}' { $$ = $3; }
1289 { $$ = new Symtable; }
1290 | symtable_body STRING symboldef {
1299 $$ = new Atom (*$1, *$2);
1304 Box b (Interval (0,0), Interval (0,0));
1305 $$ = new Atom (*$1, b);
1311 dinterval dinterval {
1312 $$ = new Box (*$1, *$2);
1318 dinterval: dim dim {
1319 $$ = new Interval ($1, $2);
1326 My_lily_parser::set_yydebug (bool b)
1333 My_lily_parser::do_yyparse ()
1335 yyparse ((void*)this);
1339 My_lily_parser::default_paper ()
1341 Identifier *id = lexer_p_->lookup_identifier ("default_paper");
1342 return id ? id->paperdef () : new Paper_def ;
1346 My_lily_parser::default_midi ()
1348 Identifier *id = lexer_p_->lookup_identifier ("default_midi");
1349 return id ? id->mididef () : new Midi_def ;