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
|
#!/bin/sh
# Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Get BNF grammars from bison files
# Check if there is one argument only
if [ $# -ne 1 ]; then
echo "extract_bnf.sh <bison-file-base-name>"
exit 1
fi
# Get the output file
base=$1
output=
if [ -f "${base}.output" ]; then
output="${base}.output"
elif [ -f "${base}.yy" ]; then
@YACC@ -v "${base}.yy" -o output
rm -f output output.h *.hh
mv output.output /tmp/output
output=/tmp/output
else
echo "cannot find ${base}.yy"
exit 1
fi
# Process the output file
# - extract the grammar part
# - remove line numbers
# - remove intermediate productions
# - remove intermediate non-terminals
# - replace standard tokens
# - replace : by BNF ::=
# - squeeze multiple blank lines
cat $output |\
@AWK@ '/^Terminal/ { exit }; // { print }' |\
@AWK@ '// { gsub("^ +[0-9]+ ", ""); print }' |\
@AWK@ '/^\$@[0-9]+:/ { next }; // { print }' |\
@AWK@ '// { gsub(" \\$@[0-9]+ ", " ") ; print }' |\
@AWK@ '// { gsub("\"constant string\"", "STRING"); print }' |\
@AWK@ '// { gsub("\"integer\"", "INTEGER"); print }' |\
@AWK@ '// { gsub("\"floating point\"", "FLOAT"); print }' |\
@AWK@ '// { gsub("\"boolean\"", "BOOLEAN"); print }' |\
@AWK@ '// { gsub("\"constant hexstring\"", "HEXSTRING"); print }' |\
@AWK@ '// { gsub("\"option name\"", "OPTION_NAME"); print }' |\
@AWK@ '// { gsub("\"ip address\"", "IP_ADDRESS"); print }' |\
@AWK@ '// { gsub("\"end of file\"", "EOF"); print }' |\
@AWK@ '// { gsub("%empty", ""); print }' |\
@AWK@ '// { gsub(": ", " ::= "); print }' |\
cat -s
|