| 1 |
############################################################################ |
| 2 |
# |
| 3 |
# Name: ibwriter.icn |
| 4 |
# |
| 5 |
# Title: Ibpag2 parser/library writer |
| 6 |
# |
| 7 |
# Author: Richard L. Goerwitz |
| 8 |
# |
| 9 |
# Version: 1.7 |
| 10 |
# |
| 11 |
############################################################################ |
| 12 |
# |
| 13 |
# Given a grammar, an action table, a goto table, an open output |
| 14 |
# file, an open iiparser file, and a module name, sends to the output |
| 15 |
# file a fully loaded LR parser with run-time constructible action |
| 16 |
# and goto tables. The iiparser file contains the base LR parser |
| 17 |
# that the output file uses. |
| 18 |
# |
| 19 |
############################################################################ |
| 20 |
# |
| 21 |
# Links: itokens, ximage |
| 22 |
# |
| 23 |
# See also: iiparse.icn |
| 24 |
# |
| 25 |
############################################################################ |
| 26 |
|
| 27 |
#link itokens, ximage |
| 28 |
link ximage |
| 29 |
|
| 30 |
# defined in itokens.icn |
| 31 |
# record ib_TOK(sym, str) |
| 32 |
|
| 33 |
procedure ibwriter(iiparse_file, outfile, grammar, atbl, gtbl, module) |
| 34 |
|
| 35 |
local token, next_token, start_symbol, rule_list, ttbl |
| 36 |
|
| 37 |
/module := "" |
| 38 |
start_symbol := grammar.start |
| 39 |
rule_list := grammar.rules |
| 40 |
ttbl := grammar.tbl |
| 41 |
next_token := create itokens(iiparse_file, 1) |
| 42 |
|
| 43 |
# |
| 44 |
# Copy tokens in iiparse_file to outfile. Whenever we find a $ |
| 45 |
# (RHSARG), process: If we find $$, output $; If we find $module, |
| 46 |
# output image(module); and other such stuff. Note that |
| 47 |
# copy_iiparse_tokens suspends tokens before writing them. It |
| 48 |
# also blocks writing of any token whose sym field matches the |
| 49 |
# string given as arg 3. |
| 50 |
# |
| 51 |
every token := copy_iiparse_tokens(next_token, outfile, "RHSARG") |
| 52 |
do { |
| 53 |
if token.sym == "RHSARG" then { |
| 54 |
if (token := @next_token).sym == "RHSARG" then { |
| 55 |
writes(outfile, token.str) |
| 56 |
next |
| 57 |
} |
| 58 |
token.sym == "IDENT" | iohno(60, "line "|| line_number) |
| 59 |
writes(outfile, " ") |
| 60 |
case token.str of { |
| 61 |
# copy $module name over as a literal |
| 62 |
"module" : writes(outfile, image(module)) |
| 63 |
# use ximage to copy over action, goto, and token tables, |
| 64 |
# as well as the production list (used only for debugging) |
| 65 |
"atbl_insertion_point": writes(outfile, ximage(atbl)) |
| 66 |
"gtbl_insertion_point": writes(outfile, ximage(gtbl)) |
| 67 |
"ttbl_insertion_point": writes(outfile, ximage(ttbl)) |
| 68 |
"rule_list_insertion_point" : |
| 69 |
writes(outfile, ximage(rule_list)) |
| 70 |
# use image to copy the start symbol into the output file |
| 71 |
"start_symbol_insertion_point" : |
| 72 |
writes(outfile, image(start_symbol)) |
| 73 |
# add the module name to anything else beginning with $ |
| 74 |
default : writes(outfile, token.str, module, " ") |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return |
| 80 |
|
| 81 |
end |
| 82 |
|
| 83 |
|
| 84 |
# |
| 85 |
# copy_iiparse_tokens: coexpression x file x string -> ib_TOK records |
| 86 |
# (next_token, out, except) -> token records |
| 87 |
# |
| 88 |
# Copy Icon code to output stream, also suspending as we go. |
| 89 |
# Insert separators between tokens where needed. Do not output |
| 90 |
# any token whose sym field matches except. The point in |
| 91 |
# suspending tokens as we go is to enable the calling procedure to |
| 92 |
# look for signal tokens that indicate insertion or termination |
| 93 |
# points. Fail on EOF. |
| 94 |
# |
| 95 |
procedure copy_iiparse_tokens(next_token, out, except) |
| 96 |
|
| 97 |
local separator, T |
| 98 |
|
| 99 |
separator := "" |
| 100 |
while T := @next_token do { |
| 101 |
if \T.sym then suspend T |
| 102 |
if \T.sym == \except then next |
| 103 |
if any(&digits ++ &letters ++ '_.', \T.str, 1, 2) & \T.sym ~== "DOT" |
| 104 |
then writes(out, separator) |
| 105 |
writes(out, T.str) |
| 106 |
if any(&digits ++ &letters ++ '_.', \T.str, -1, 0) & \T.sym ~== "DOT" |
| 107 |
then separator := " " else separator := "" |
| 108 |
} |
| 109 |
|
| 110 |
end |