2021-06-21 18:07:02 +02:00
|
|
|
#!/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
|
2021-06-22 15:23:17 +02:00
|
|
|
#
|
|
|
|
# This script takes 1 or 2 parameters:
|
|
|
|
#
|
|
|
|
# Basic usage:
|
|
|
|
# ./extract_bnf.sh <bison-file-base-name> - will generate BNF notation in plain text
|
|
|
|
#
|
|
|
|
# Markdown:
|
|
|
|
# ./extract_bnf.sh <bison-file-base-name> --markdown
|
2021-06-21 18:07:02 +02:00
|
|
|
|
2021-06-22 15:23:17 +02:00
|
|
|
# Check if there are one or two arguments
|
|
|
|
if [ $# -ne 1 ] && [ $# -ne 2 ]; then
|
|
|
|
echo "extract_bnf.sh <bison-file-base-name> [--markdown]"
|
2021-06-21 18:07:02 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-06-22 15:23:17 +02:00
|
|
|
markdown=0
|
|
|
|
if [ $# -eq 2 ] && [ "$2" = "--markdown" ]; then
|
|
|
|
markdown=1
|
|
|
|
fi
|
|
|
|
|
2021-06-21 18:07:02 +02:00
|
|
|
# Get the output file
|
|
|
|
base=$1
|
|
|
|
output=
|
|
|
|
|
|
|
|
if [ -f "${base}.output" ]; then
|
|
|
|
output="${base}.output"
|
|
|
|
elif [ -f "${base}.yy" ]; then
|
2021-06-22 15:23:17 +02:00
|
|
|
/usr/bin/bison -v "${base}.yy" -o output
|
2021-06-21 18:07:02 +02:00
|
|
|
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
|
2019-07-23 00:26:42 +02:00
|
|
|
# - replace standard tokens
|
|
|
|
# - replace : by BNF ::=
|
2021-06-21 18:07:02 +02:00
|
|
|
# - 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 }' |\
|
2019-07-23 00:26:42 +02:00
|
|
|
@AWK@ '// { gsub("\"constant string\"", "STRING"); print }' |\
|
|
|
|
@AWK@ '// { gsub("\"integer\"", "INTEGER"); print }' |\
|
|
|
|
@AWK@ '// { gsub("\"floating point\"", "FLOAT"); print }' |\
|
|
|
|
@AWK@ '// { gsub("\"boolean\"", "BOOLEAN"); print }' |\
|
2019-07-23 12:47:58 +02:00
|
|
|
@AWK@ '// { gsub("\"null\"", "NULL"); print }' |\
|
2019-07-23 00:26:42 +02:00
|
|
|
@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 }' |\
|
2021-06-22 15:23:17 +02:00
|
|
|
cat -s > $output.2
|
|
|
|
|
|
|
|
if [ "$markdown" -eq 1 ]; then
|
|
|
|
echo ".. code-block:: BNF" > $output.3
|
|
|
|
echo " :linenos:" >> $output.3
|
|
|
|
cat $output.2 | @AWK@ '/^.+$/ { print " ",$0 }; /^$/ { print } ' >> $output.3
|
|
|
|
cat $output.3
|
|
|
|
else
|
|
|
|
cat $output.2
|
|
|
|
fi
|