-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.awk
executable file
·121 lines (101 loc) · 2.24 KB
/
parse.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/awk -f
function cify(fn, args) {
out = fn "("
for (dat in args) {
if (dat == 1) {
out = out args[dat]
} else {
out = out ", " args[dat]
}
}
out = out ")"
return out
}
function cify_var(fn, defs) {
split(defs, args, " ")
args[1] = (length(args)-1)
return cify(fn, args)
}
function getfunc(id) {
functab["Tape"] = "MkTp"
functab["Accept"] = "Mkmask"
functab["Reject"] = "Mkmask"
functab["Edge"] = "MkEd"
return functab[id]
}
function edgify(data) {
match(data, /([0-9]*)( |\t|\n)*(L|R|X)+.*$/, matches)
mktmdat = "MkTmdat(" matches[1] "," matches[3] ")"
gsub(/[0-9]*(\t| |\n)*(L|R|X)+(\t| |\n)*$/, mktmdat, data)
return cify_var(functab["Edge"], data)
}
function tmgenify() {
tmgenfmt = "(struct tmgen){\n" \
"\t\t.tape = %s,\n" \
"\t\t.edges = MkList(Ed)(%d%s" \
"%s),\n" \
"\t\t.vertices = %d,\n" \
"\t\t.start = %d,\n" \
"\t\t.acceptmask = %s,\n" \
"\t\t.rejectmask = %s,\n" \
"\t\t.name = %s,\n" \
"\t}"
if (length(edges) > 0) {
prefix = ",\n"
for (i in edges) {
if (i < length(edges) - 1) {
elist = elist "\t\t" edges[i] ",\n"
} else {
elist = elist "\t\t" edges[i]
}
}
} else {
prefix = ""
elist = ""
}
return sprintf(tmgenfmt,
inputs["Tape"],
length(edges),
prefix,
elist,
inputs["Vertices"],
inputs["Start"],
inputs["Accept"],
inputs["Reject"],
inputs["Name"])
}
function mainify(tmgen) {
main="#include \"tm.h\"\n\n" \
"int main(void) {\n" \
"\tint ret;\n" \
"\tstruct tmgen tg = %s;\n" \
"\tTm * t = Tm_gen(&tg);\n" \
"\tTm_setverbose(t, %s);\n" \
"\tret = Tm_run(t);\n\n" \
"\treturn ret;\n}\n"
return sprintf(main, tmgen, inputs["Verbose"])
}
BEGIN {
RS=";"
inputs["Tape"] = ""
inputs["Accept"] = ""
inputs["Reject"] = ""
inputs["Start"] = ""
inputs["Vertices"] = ""
inputs["Verbose"] = "0"
# declare edges as empty array
delete edges[0]
}
/^.*#.*/ {}
$1 ~ /Tape|Accept|Reject/ { inputs[$1] = cify_var(getfunc($1), $0) }
$1 ~ /Start|Vertices/ { inputs[$1] = $2 }
$1 ~ /Verbose/ { inputs[$1] = "1" }
$1 ~ /Name/ {
gsub(/^( |\n|\t)*Name( |\t|\n)*/, "", $0);
gsub(/\n/, "\\n", $0)
inputs["Name"] = "\"" $0 "\"";
}
$1 ~ /Edge/ { edges[length(edges)] = edgify($0) }
END {
printf "%s\n", mainify(tmgenify())
}