Commit 6c3360f9f9be907ab5b082ceafbb0aefb1fb8a73
1 parent
c54c5cd7
*** empty log message ***
Showing
2 changed files
with
169 additions
and
71 deletions
Show diff stats
anubis_dev/library/syntactic_analysis/common.anubis
| ... | ... | @@ -43,6 +43,23 @@ public type APG_Error: |
| 43 | 43 | bad_end_of_precedence_declaration (Int32 line), |
| 44 | 44 | bad_end_of_type_declaration (Int32 line). |
| 45 | 45 | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + *Name* en_print | |
| 51 | + | |
| 52 | + *Description* The function `en_print` prints in English a message of type `APG_Error` | |
| 53 | + on the standard output. | |
| 54 | + | |
| 55 | +public define One | |
| 56 | + en_print | |
| 57 | + ( | |
| 58 | + APG_Error e | |
| 59 | + ). | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 46 | 63 | |
| 47 | 64 | |
| 48 | 65 | |
| ... | ... | @@ -51,25 +68,47 @@ public type APG_Error: |
| 51 | 68 | *Description* |
| 52 | 69 | |
| 53 | 70 | This is the type of (abstract) grammars. An object of this type is produced by the |
| 54 | - function `read_APG_grammar`, and given as an argument to the function | |
| 55 | - `make_APG_automaton` (see `make_automaton.anubis`). This type is opaque. | |
| 71 | + function `read_APG_grammar` (see `read_grammar.anubis`), and given as an argument to | |
| 72 | + the function `make_APG_automaton` (see `make_automaton.anubis`). This type is opaque. | |
| 56 | 73 | |
| 57 | 74 | public type APG_Grammar:... |
| 58 | 75 | |
| 59 | 76 | |
| 60 | 77 | |
| 78 | + | |
| 79 | + *Name* print_grammar | |
| 80 | + | |
| 81 | + *Description* | |
| 82 | + | |
| 83 | + This function prints a grammar (of type `APG_Grammar`) on the standard output. The | |
| 84 | + preambule and postambule are not printed. | |
| 85 | + | |
| 86 | +public define One | |
| 87 | + print_grammar | |
| 88 | + ( | |
| 89 | + APG_Grammar g | |
| 90 | + ). | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 61 | 96 | *Name* APG_Automaton: |
| 62 | 97 | |
| 63 | 98 | This type represents abstract automatons as they are constructed by the function |
| 64 | - `make_APG_automaton`. This is an opaque type. | |
| 99 | + `make_APG_automaton` (see `make_automaton.anubis`). This is an opaque type. | |
| 65 | 100 | |
| 66 | 101 | public type APG_Automaton:... |
| 67 | 102 | |
| 68 | 103 | |
| 69 | 104 | |
| 70 | 105 | |
| 106 | + | |
| 107 | + | |
| 71 | 108 | *Private* |
| 72 | 109 | |
| 110 | + | |
| 111 | +read tools/basis.anubis | |
| 73 | 112 | |
| 74 | 113 | public type APG_Precedence_Dec: |
| 75 | 114 | left (List(String) symbol_names), |
| ... | ... | @@ -103,4 +142,116 @@ public type APG_Automaton: |
| 103 | 142 | . |
| 104 | 143 | |
| 105 | 144 | |
| 106 | - | |
| 107 | 145 | \ No newline at end of file |
| 146 | +public define One | |
| 147 | + en_print | |
| 148 | + ( | |
| 149 | + APG_Error e | |
| 150 | + ) = | |
| 151 | + if e is | |
| 152 | + { | |
| 153 | + unexpected_end_of_input(Int32 line) then | |
| 154 | + print(integer_to_string(line)+": Unexpected end of input.\n"), | |
| 155 | + | |
| 156 | + no_parser_name(Int32 line) then | |
| 157 | + print(integer_to_string(line)+": No parser name.\n"), | |
| 158 | + | |
| 159 | + keyword_was_expected(Int32 line) then | |
| 160 | + print(integer_to_string(line)+": Keyword was expected.\n"), | |
| 161 | + | |
| 162 | + unknown_declaration_keyword(Int32 line,String keyword) then | |
| 163 | + print(integer_to_string(line)+": Unknown declaration keyword '"+keyword+"'.\n"), | |
| 164 | + | |
| 165 | + type_description_expected(Int32 line) then | |
| 166 | + print(integer_to_string(line)+": Type description expected.\n"), | |
| 167 | + | |
| 168 | + incorrect_symbol_value(Int32 line) then | |
| 169 | + print(integer_to_string(line)+": Incorrect symbol value.\n"), | |
| 170 | + | |
| 171 | + symbol_expected(Int32 line) then | |
| 172 | + print(integer_to_string(line)+": Symbol expected.\n"), | |
| 173 | + | |
| 174 | + colon_expected(Int32 line) then | |
| 175 | + print(integer_to_string(line)+": Colon expected.\n"), | |
| 176 | + | |
| 177 | + incorrect_rule_precedence(Int32 line) then | |
| 178 | + print(integer_to_string(line)+": Incorrect rule precedence.\n"), | |
| 179 | + | |
| 180 | + misclosed_rule_precedence(Int32 line) then | |
| 181 | + print(integer_to_string(line)+": Misclosed rule precedence.\n"), | |
| 182 | + | |
| 183 | + incorrect_end_of_rule(Int32 line) then | |
| 184 | + print(integer_to_string(line)+": Incorrect end of rule.\n"), | |
| 185 | + | |
| 186 | + bad_end_of_precedence_declaration(line) then | |
| 187 | + print(integer_to_string(line)+": Incorrect end of precedence declaration.\n"), | |
| 188 | + | |
| 189 | + bad_end_of_type_declaration(line) then | |
| 190 | + print(integer_to_string(line)+": Incorrect end of type declaration.\n"), | |
| 191 | + }. | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | +define One | |
| 197 | ||
| 198 | + ( | |
| 199 | + APG_Precedence_Dec d | |
| 200 | + ) = | |
| 201 | + if d is | |
| 202 | + { | |
| 203 | + left(names) then map_forget((String s) |-> print("left "+s+"\n"),names), | |
| 204 | + right(names) then map_forget((String s) |-> print("right "+s+"\n"),names), | |
| 205 | + non_assoc(names) then map_forget((String s) |-> print("non_assoc "+s+"\n"),names) | |
| 206 | + }. | |
| 207 | + | |
| 208 | + | |
| 209 | +define One | |
| 210 | ||
| 211 | + ( | |
| 212 | + APG_Type_Dec d | |
| 213 | + ) = | |
| 214 | + if d is type_dec(type,names) then | |
| 215 | + map_forget((String s) |-> print(type+" "+s+"\n"),names). | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | +define One | |
| 220 | ||
| 221 | + ( | |
| 222 | + APG_Symbol_Value sv | |
| 223 | + ) = | |
| 224 | + if sv is symbol_value(name,mb_value) | |
| 225 | + then print(name+" "+ if mb_value is | |
| 226 | + { | |
| 227 | + failure then "", | |
| 228 | + success(value) then "{"+value+"} " | |
| 229 | + }). | |
| 230 | + | |
| 231 | + | |
| 232 | +define One | |
| 233 | ||
| 234 | + ( | |
| 235 | + APG_Grammar_Rule r | |
| 236 | + ) = | |
| 237 | + if r is grammar_rule(id,head,body,prec) then | |
| 238 | + print("["+id+"] "); | |
| 239 | + print(head); | |
| 240 | + print(": "); | |
| 241 | + map_forget(print,body); | |
| 242 | + print("\n"). | |
| 243 | + | |
| 244 | +public define One | |
| 245 | + print_grammar | |
| 246 | + ( | |
| 247 | + APG_Grammar g | |
| 248 | + ) = | |
| 249 | + if g is grammar(preambule, | |
| 250 | + parser_name, | |
| 251 | + precedence_declarations, | |
| 252 | + type_declarations, | |
| 253 | + grammar_rules, | |
| 254 | + postambule) then | |
| 255 | + print("Parser name: "+parser_name+"\n"); | |
| 256 | + map_forget(print,precedence_declarations); | |
| 257 | + map_forget(print,type_declarations); | |
| 258 | + map_forget(print,grammar_rules). | |
| 108 | 259 | \ No newline at end of file | ... | ... |
anubis_dev/library/syntactic_analysis/read_grammar.anubis
| ... | ... | @@ -76,11 +76,10 @@ define $U -> Result($E,$W) |
| 76 | 76 | |
| 77 | 77 | |
| 78 | 78 | |
| 79 | - This one (the 'Kleisli applicator') does'nt work because in Anubis 1 we cannot overload | |
| 80 | - the applicator. This will work only in Anubis 2. | |
| 79 | + A variant: the 'Kleisli applicator': | |
| 81 | 80 | |
| 82 | - define Result($E,$W) | |
| 83 | - ($U -> Result($E,$W) f)(Result($E,$U) x) // this syntax is not accepted by Anubis 1 | |
| 81 | +define Result($E,$W) | |
| 82 | + $U -> Result($E,$W) f * Result($E,$U) x | |
| 84 | 83 | = |
| 85 | 84 | if x is |
| 86 | 85 | { |
| ... | ... | @@ -210,8 +209,7 @@ define Result(APG_Error,String) |
| 210 | 209 | if read_symbol(s) is |
| 211 | 210 | { |
| 212 | 211 | failure then error(no_parser_name(current_line(s))), |
| 213 | - success(parser_name) then print("parser name: <"+parser_name+">\n"); | |
| 214 | - ok(parser_name) | |
| 212 | + success(parser_name) then ok(parser_name) | |
| 215 | 213 | }. |
| 216 | 214 | |
| 217 | 215 | |
| ... | ... | @@ -271,7 +269,7 @@ define Maybe(String) |
| 271 | 269 | define Maybe(String) |
| 272 | 270 | read_type_of_dec |
| 273 | 271 | ( |
| 274 | - Stream s | |
| 272 | + Stream s | |
| 275 | 273 | ) = |
| 276 | 274 | skip_blanks_and_comments(s); |
| 277 | 275 | if read_byte(s) is |
| ... | ... | @@ -316,18 +314,13 @@ define Result(APG_Error,Dec_Item) |
| 316 | 314 | { |
| 317 | 315 | failure then error(unexpected_end_of_input(current_line(s))), |
| 318 | 316 | success(byte) then |
| 319 | - print("col1: "+integer_to_string(current_column(s))+"\n"); | |
| 320 | - print("lin1: "+integer_to_string(current_line(s))+"\n"); | |
| 321 | 317 | if byte = '#' |
| 322 | 318 | then ok(separator) |
| 323 | 319 | else unput_byte(byte,s); |
| 324 | - print("col2: "+integer_to_string(current_column(s))+"\n"); | |
| 325 | - print("lin2: "+integer_to_string(current_line(s))+"\n"); | |
| 326 | 320 | if read_symbol(s) is |
| 327 | 321 | { |
| 328 | 322 | failure then error(keyword_was_expected(current_line(s))), |
| 329 | 323 | success(keyword) then |
| 330 | - print("Keyword: "+keyword+"\n"); | |
| 331 | 324 | if keyword = "type" |
| 332 | 325 | then (((APG_Type_Dec d) |-> ok(type_dec(d))) * read_type_dec)(s) |
| 333 | 326 | else with g = (APG_Precedence_Dec d) |-> (Result(APG_Error,Dec_Item))ok(prec_dec(d)), |
| ... | ... | @@ -472,11 +465,12 @@ define Result(APG_Error,APG_Grammar_Rule) |
| 472 | 465 | then ok(grammar_rule(rule_id,_A,reverse(so_far),failure)) |
| 473 | 466 | else if byte = '[' // precedence |
| 474 | 467 | then read_rule_precedence(s,_A,rule_id,reverse(so_far)) |
| 475 | - else if read_symbol_and_value(s) is | |
| 476 | - { | |
| 477 | - error(msg) then error(msg), | |
| 478 | - ok(sv) then read_grammar_rule_tail(s,_A,rule_id,[sv . so_far]) | |
| 479 | - } | |
| 468 | + else unput_byte(byte,s); | |
| 469 | + if read_symbol_and_value(s) is | |
| 470 | + { | |
| 471 | + error(msg) then error(msg), | |
| 472 | + ok(sv) then read_grammar_rule_tail(s,_A,rule_id,[sv . so_far]) | |
| 473 | + } | |
| 480 | 474 | }. |
| 481 | 475 | |
| 482 | 476 | |
| ... | ... | @@ -619,54 +613,6 @@ public define Result(APG_Error,APG_Grammar) |
| 619 | 613 | |
| 620 | 614 | *** [8] Testing the grammar reader. |
| 621 | 615 | |
| 622 | -define One | |
| 623 | ||
| 624 | - ( | |
| 625 | - APG_Error e | |
| 626 | - ) = | |
| 627 | - if e is | |
| 628 | - { | |
| 629 | - unexpected_end_of_input(Int32 line) then | |
| 630 | - print(integer_to_string(line)+": Unexpected end of input.\n"), | |
| 631 | - | |
| 632 | - no_parser_name(Int32 line) then | |
| 633 | - print(integer_to_string(line)+": No parser name.\n"), | |
| 634 | - | |
| 635 | - keyword_was_expected(Int32 line) then | |
| 636 | - print(integer_to_string(line)+": Keyword was expected.\n"), | |
| 637 | - | |
| 638 | - unknown_declaration_keyword(Int32 line,String keyword) then | |
| 639 | - print(integer_to_string(line)+": Unknown declaration keyword '"+keyword+"'.\n"), | |
| 640 | - | |
| 641 | - type_description_expected(Int32 line) then | |
| 642 | - print(integer_to_string(line)+": Type description expected.\n"), | |
| 643 | - | |
| 644 | - incorrect_symbol_value(Int32 line) then | |
| 645 | - print(integer_to_string(line)+": Incorrect symbol value.\n"), | |
| 646 | - | |
| 647 | - symbol_expected(Int32 line) then | |
| 648 | - print(integer_to_string(line)+": Symbol expected.\n"), | |
| 649 | - | |
| 650 | - colon_expected(Int32 line) then | |
| 651 | - print(integer_to_string(line)+": Colon expected.\n"), | |
| 652 | - | |
| 653 | - incorrect_rule_precedence(Int32 line) then | |
| 654 | - print(integer_to_string(line)+": Incorrect rule precedence.\n"), | |
| 655 | - | |
| 656 | - misclosed_rule_precedence(Int32 line) then | |
| 657 | - print(integer_to_string(line)+": Misclosed rule precedence.\n"), | |
| 658 | - | |
| 659 | - incorrect_end_of_rule(Int32 line) then | |
| 660 | - print(integer_to_string(line)+": Incorrect end of rule.\n"), | |
| 661 | - | |
| 662 | - bad_end_of_precedence_declaration(line) then | |
| 663 | - print(integer_to_string(line)+": Incorrect end of precedence declaration.\n"), | |
| 664 | - | |
| 665 | - bad_end_of_type_declaration(line) then | |
| 666 | - print(integer_to_string(line)+": Incorrect end of type declaration.\n"), | |
| 667 | - }. | |
| 668 | - | |
| 669 | - | |
| 670 | 616 | global define One |
| 671 | 617 | grammar_read_test |
| 672 | 618 | ( |
| ... | ... | @@ -682,8 +628,9 @@ global define One |
| 682 | 628 | success(f) then |
| 683 | 629 | if read_APG_grammar(make_stream(f)) is |
| 684 | 630 | { |
| 685 | - error(msg) then print(msg), | |
| 686 | - ok(g) then print("Got a grammar !\n") | |
| 631 | + error(msg) then en_print(msg), | |
| 632 | + ok(g) then print("Got a grammar !\n"); | |
| 633 | + print_grammar(g) | |
| 687 | 634 | } |
| 688 | 635 | } |
| 689 | 636 | }. | ... | ... |