File: arggram2c

package info (click to toggle)
libmaa 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,008 kB
  • sloc: ansic: 6,384; perl: 235; makefile: 176; awk: 92; sh: 23
file content (117 lines) | stat: -rw-r--r-- 1,968 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/awk -f

#
# converts arggram.txt to arggram.c
# usage: arggram2c < arggram.txt > arggram.c
#

BEGIN {
   current_state = 0
   current_action = 0
   current_chartype = 0
}

{
   sub(/[#].*$/, "")
}

NF == 0 {
   next
}

{
   sub(/EXIT/,  "-1", $4)
   sub(/ERROR/, "-2", $4)

   if ($1 in states){
      $1 = states [$1]
   }else if ($1 >= 0){
      states [$1] = current_state++
      $1 = states [$1]
   }

   if ($2 in chartypes){
      $2 = chartypes [$2]
   }else{
      chartypes [$2] = current_chartype++
      $2 = chartypes [$2]
   }

   if ($3 in actions){
      $3 = actions [$3]
   }else{
      actions [$3] = current_action++
      $3 = actions [$3]
   }

   if ($4 in states){
      $4 = states [$4]
   }else if ($4 >= 0){
      states [$4] = current_state++
      $4 = states [$4]
   }

   act   [$1, $2] = $3
   trans [$1, $2] = $4
}

END {
   printf "#define STATE_EXIT  (int) (-1)\n"
   printf "#define STATE_ERROR (int) (-2)\n"
   printf "\n"
}

END {
   for (i in chartypes){
      print "#define CHARTYPE_" i " " chartypes [i]
   }

   printf "\n#define CHARTYPES_COUNT %i\n\n", current_chartype
}

END {
   for (i in actions){
      print "#define ACTION_" i " " actions [i]
   }

   printf "\n#define ACTIONS_COUNT %i\n\n", current_action
}

END {
   print "static const int action [" current_state "] [" current_chartype "] = {"
   for (i=0; i < current_state; ++i){
      if (i)
	 printf ",\n"

      printf "   "

      for (j=0; j < current_chartype; ++j){
	 if (j)
	    printf ", "
	 else
	    printf "{ "
	 printf "%i", act [i, j]
      }
      printf " }"
   }
   print "};\n"
}

END {
   print "static const int transition [" current_state "] [" current_chartype "] = {"
   for (i=0; i < current_state; ++i){
      if (i)
	 printf ",\n"

      printf "   "
      for (j=0; j < current_chartype; ++j){
	 if (j)
	    printf ", "
	 else
	    printf "{ "
	 printf "%i", trans [i, j]
      }
      printf " }"
   }
   print "};\n"
}