File: mergelex.awk

package info (click to toggle)
recode 3.4.1-11
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,560 kB
  • ctags: 622
  • sloc: ansic: 10,572; perl: 339; makefile: 317; lisp: 243; sh: 173; lex: 165; awk: 127; sed: 10
file content (194 lines) | stat: -rw-r--r-- 4,731 bytes parent folder | download | duplicates (2)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Conversion of files between different charsets and usages.
# Copyright (C) 1990, 1993, 1994 Free Software Foundation, Inc.
# Francois Pinard <pinard@iro.umontreal.ca>, 1990.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# This awk script merges several lex sources intended for GNU recode.

# At beginning, one temporary file is initialized for each section.

BEGIN {
  SECTION1 = "merged1.tmp"
  SECTION2 = "merged2.tmp"
  SECTION3 = "merged3.tmp"

  print "/* This file is generated automatically by mergelex.awk.  */"

  print ""						>SECTION1
  print "%{"						>SECTION1
  print "#include \"recode.h\""				>SECTION1
  print "#ifdef USE_FPUTC"				>SECTION1
  print "#define output(ch) fputc (ch, yyout)"		>SECTION1
  print "#else"						>SECTION1
  print "#define output(ch) putc (ch, yyout)"		>SECTION1
  print "#endif"					>SECTION1
  print "%}"						>SECTION1

  print "%%"						>SECTION2
  print "<<EOF>>			{ return 1; }"	>SECTION2

  print "%%"						>SECTION3
  print ""						>SECTION3
  print "#ifndef yywrap"				>SECTION3
  print "int"						>SECTION3
  print "yywrap (void)"					>SECTION3
  print "{"						>SECTION3
  print "  return 1;"					>SECTION3
  print "}"						>SECTION3
  print "#endif /* not yywrap */"			>SECTION3
}

# A `/* Step name: NAME.  */' line indicates the start of a new file.
# Generate an interface routine.  The step name is saved for broketed
# prefixes in rules.

$1 == "/*" && $2 == "Step" && $3 == "name:" && $5 == "*/" {
  section = 1
  step_name = $4
  sub (".$", "", step_name)
  print ""						>SECTION3
  print "static int"					>SECTION3
  print "file_" step_name " (const STEP *step, " \
	"FILE *input_file, FILE *output_file)" 		>SECTION3
  print "{"						>SECTION3
  print "  yy_init = 1;"				>SECTION3
  print "  yyin = input_file;"				>SECTION3
  print "  yyout = output_file;"			>SECTION3
  print "  BEGIN " step_name ";"			>SECTION3
  print "  return yylex ();"				>SECTION3
  print "}"						>SECTION3
  next
}

# Remove block C comments in section 1, except the very first.  It is
# assumed that, when a /* comment starts in column 1, there is no code
# following the closing */ on its line.  Also, remove all white lines.

/^\/\*/ && section == 1 {
  while (!match ($0, /\*\//)) {
    if (!comment_done) {
      print
    }
    getline
  }
  if (!comment_done) {
    print
  }
  comment_done = 1
  next
}

/^[ 	]*$/ && section == 1 {
  next
}

# A %% in column 1 signals the beginning of lex rules.

/^%%/ && section == 1 {
  print "%x " step_name					>SECTION1
  section = 2
  print ""						>SECTION2
  next
}

# A %{ comment in column 1 signals the start of a C code section ended
# by a %} line.

/^%\{/ {
  c_code = 1
  print ""						>SECTION1
  print							>SECTION1
  next
}

/^%\}/ {
  print							>SECTION1
  print ""						>SECTION1
  c_code = 0
  next
}

c_code {
  print							>SECTION1
  next
}

# Section 1 declarations are studied and copied, once each.
# Conflicting declaractions are reported at beginning of output.

/^[^ 	]/ && section == 1 {
  if ($1 in rules) {
    if ($0 != rules[$1]) {
      print "** Conflicting definition: " $0
    }
  }
  else {
    rules[$1] = $0
    print						>SECTION1
  }
  next
}

# In section 2, every non-empty line which does not start with white
# space has to be a lex rule, which is then prefixed by a start name
# derived from the step name.  However, a %% in column 1 starts
# section 3, which contains pure C code, which is copied verbatim.

/^$/ && section == 2 {
  print							>SECTION2
  next
}

/^[ 	]/ && section == 2 {
  print							>SECTION2
  next
}

/^%%/ && section == 2 {
  section = 3
  next
}

section == 2 {
  print "<" step_name ">" $0				>SECTION2
  next
}

section == 3 {
  print							>SECTION3
  next
}

# At end, all three temporary files are reread and output, followed by
# the generated interfaces: one routine for each step name.

END {
  close (SECTION1)
  while (getline <SECTION1)
    print
  close (SECTION1)

  close (SECTION2)
  while (getline <SECTION2)
    print
  close (SECTION2)

  close (SECTION3)
  while (getline <SECTION3)
    print
  close (SECTION3)
}