Debian Codegraph @

maa/arggram2c

raw
1 #!/usr/bin/awk -f
2
3 #
4 # converts arggram.txt to arggram.c
5 # usage: arggram2c < arggram.txt > arggram.c
6 #
7
8 BEGIN {
9 current_state = 0
10 current_action = 0
11 current_chartype = 0
12 }
13
14 {
15 sub(/[#].*$/, "")
16 }
17
18 NF == 0 {
19 next
20 }
21
22 {
23 sub(/EXIT/, "-1", $4)
24 sub(/ERROR/, "-2", $4)
25
26 if ($1 in states){
27 $1 = states [$1]
28 }else if ($1 >= 0){
29 states [$1] = current_state++
30 $1 = states [$1]
31 }
32
33 if ($2 in chartypes){
34 $2 = chartypes [$2]
35 }else{
36 chartypes [$2] = current_chartype++
37 $2 = chartypes [$2]
38 }
39
40 if ($3 in actions){
41 $3 = actions [$3]
42 }else{
43 actions [$3] = current_action++
44 $3 = actions [$3]
45 }
46
47 if ($4 in states){
48 $4 = states [$4]
49 }else if ($4 >= 0){
50 states [$4] = current_state++
51 $4 = states [$4]
52 }
53
54 act [$1, $2] = $3
55 trans [$1, $2] = $4
56 }
57
58 END {
59 printf "#define STATE_EXIT (int) (-1)\n"
60 printf "#define STATE_ERROR (int) (-2)\n"
61 printf "\n"
62 }
63
64 END {
65 for (i in chartypes){
66 print "#define CHARTYPE_" i " " chartypes [i]
67 }
68
69 printf "\n#define CHARTYPES_COUNT %i\n\n", current_chartype
70 }
71
72 END {
73 for (i in actions){
74 print "#define ACTION_" i " " actions [i]
75 }
76
77 printf "\n#define ACTIONS_COUNT %i\n\n", current_action
78 }
79
80 END {
81 print "static const int action [" current_state "] [" current_chartype "] = {"
82 for (i=0; i < current_state; ++i){
83 if (i)
84 printf ",\n"
85
86 printf " "
87
88 for (j=0; j < current_chartype; ++j){
89 if (j)
90 printf ", "
91 else
92 printf "{ "
93 printf "%i", act [i, j]
94 }
95 printf " }"
96 }
97 print "};\n"
98 }
99
100 END {
101 print "static const int transition [" current_state "] [" current_chartype "] = {"
102 for (i=0; i < current_state; ++i){
103 if (i)
104 printf ",\n"
105
106 printf " "
107 for (j=0; j < current_chartype; ++j){
108 if (j)
109 printf ", "
110 else
111 printf "{ "
112 printf "%i", trans [i, j]
113 }
114 printf " }"
115 }
116 print "};\n"
117 }