# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Get BNF grammars from bison files
+#
+# 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
-# Check if there is one argument only
-if [ $# -ne 1 ]; then
- echo "extract_bnf.sh <bison-file-base-name>"
+# Check if there are one or two arguments
+if [ $# -ne 1 ] && [ $# -ne 2 ]; then
+ echo "extract_bnf.sh <bison-file-base-name> [--markdown]"
exit 1
fi
+markdown=0
+if [ $# -eq 2 ] && [ "$2" = "--markdown" ]; then
+ markdown=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
+ /usr/bin/bison -v "${base}.yy" -o output
rm -f output output.h *.hh
mv output.output /tmp/output
output=/tmp/output
@AWK@ '// { gsub("\"end of file\"", "EOF"); print }' |\
@AWK@ '// { gsub("%empty", ""); print }' |\
@AWK@ '// { gsub(": ", " ::= "); print }' |\
-cat -s
+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