2021-06-21 18:07:02 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
2025-02-20 15:34:10 +02:00
|
|
|
# Copyright (C) 2019-2025 Internet Systems Consortium, Inc. ("ISC")
|
2021-06-21 18:07:02 +02:00
|
|
|
#
|
|
|
|
# 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
|
2021-06-23 12:40:48 +02:00
|
|
|
if [ $# -lt 1 ]; then
|
2021-06-22 19:40:13 +02:00
|
|
|
echo "extract_bnf.sh <bison-file-base-name> [--markdown <name>]"
|
2021-06-21 18:07:02 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-06-22 15:23:17 +02:00
|
|
|
markdown=0
|
2021-06-22 19:40:13 +02:00
|
|
|
if [ $# -eq 3 ] && [ "$2" = "--markdown" ]; then
|
2021-06-22 15:23:17 +02:00
|
|
|
markdown=1
|
2021-06-22 19:40:13 +02:00
|
|
|
md_name=$3
|
2021-06-22 15:23:17 +02:00
|
|
|
fi
|
|
|
|
|
2021-06-21 18:07:02 +02:00
|
|
|
# Get the output file
|
|
|
|
base=$1
|
|
|
|
output=
|
2021-06-22 19:40:13 +02:00
|
|
|
|
2022-01-18 15:45:08 +02:00
|
|
|
header="This grammar is generated from \`\`$(basename "${base}").yy\`\`. See ${md_name} for more details."
|
2021-06-21 18:07:02 +02:00
|
|
|
|
2021-06-23 12:40:48 +02:00
|
|
|
if [ -f "${base}.yy" ]; then
|
|
|
|
# We want to explicitly set the language to English. Otherwise
|
|
|
|
# bison may produce language specific wording (like "symbole terminalne"
|
|
|
|
# if you system is set to Polish, rather than "Terminals") and this will
|
|
|
|
# confuse our script.
|
|
|
|
LANG=en_US LANGUAGE=en_US @YACC@ -v "${base}.yy" -o output
|
2024-06-17 17:07:22 +03:00
|
|
|
rm -f output output.h ./*.hh
|
2021-06-21 18:07:02 +02:00
|
|
|
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
|
|
|
|
|
2024-06-17 17:07:22 +03:00
|
|
|
@AWK@ '{ print }' $output |\
|
2021-06-21 18:07:02 +02:00
|
|
|
@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
|
2021-06-22 19:40:13 +02:00
|
|
|
cat > $output.3 << EOF
|
|
|
|
$header
|
|
|
|
|
|
|
|
.. code-block:: BNF
|
|
|
|
:linenos:
|
|
|
|
|
|
|
|
EOF
|
2024-06-17 17:07:22 +03:00
|
|
|
# shellcheck disable=SC2016
|
|
|
|
# SC2016: Expressions don't expand in single quotes, use double quotes for that.
|
2024-06-17 20:46:54 +03:00
|
|
|
# Reason: we specifically do not want $0 to expand.
|
2024-06-17 17:07:22 +03:00
|
|
|
@AWK@ '/^.+$/ { print " ",$0 }; /^$/ { print } ' $output.2 >> $output.3
|
2021-06-22 15:23:17 +02:00
|
|
|
cat $output.3
|
|
|
|
else
|
|
|
|
cat $output.2
|
|
|
|
fi
|