Skip to content

Commit

Permalink
Improved how bison and flex are used
Browse files Browse the repository at this point in the history
Replaced bfok.sh with run_bison.sh and run_flex.sh.

Simplified these Makefile rules:

    make jparse.tab.c jparse.tab.h
    make jparse.c
    make parser

Removed bfok rule.

These Makefile variables:

    ${BISON}
    ${FLEX}
    ${BFOK_DIRS}

have been replaced with:

    ${BISON_BASENAME}
    ${BISON_DIRS}
    ${FLEX_BASENAME}
    ${FLEX_DIRS}

Here are the usage messages for run_bison.sh and run_flex.sh:

run_bison.sh:

    usage: ./run_bison.sh [-h] [-v level] [-V] [-b bison] [-l limit_ioccc.sh]
			    [-g verge] [-p prefix] [-s sorry.h] [-B dir] .. -- [bison_flags ..]

	-h              print help and exit 8
	-v level        set debug level (def: 0)
	-V              print version and exit 8
	-b bison        bison tool basename (def: bison)
	-l limit_ioccc.sh   version info file (def: ./limit_ioccc.sh)
	-g verge	    path to verge tool (def: ./verge)
	-p prefix	    The prefix of files to be used (def: jparse)
			    NOTE: If the final arg is bison:
			    NOTE:    The bison input file will be prefix.y
			    NOTE:	 If bison cannot be used, then these backup files are used:
			    NOTE:
			    NOTE:		prefix.tab.c prefix.tab.h
			    NOTE:
	-s sorry.h	    File to prepend to C output (def: sorry.tm.ca.h)
	-B dir          1st look for bison in dir (def: look just along $PATH)
			    NOTE: Multiple -B dir are allowed.
			    NOTE: Search is performed in dir order before the $PATH path.
			    NOTE: If dir is missing or not searchable, dir is ignored.
			    NOTE: This is ignored if the final arg is NOT bison.
	--		    End of ./run_bison.sh flags
	bison_flags ..  optional flags to give to bison for the prefix.y argument

    Exit codes:
	0    bison output files formed or backup files used instead

	2    good bison found and ran but failed to form proper output files
	3    bison input file missing or not readable:         backup file(s) had to be used

	4    backup file(s) are missing, or are not readable
	5    failed to use backup file(s) to form the bison C output file(s)

	6    limit_ioccc.sh sorry file missing or not readable, or verge missing or not executable
	7    BISON_VERSION missing or empty from limit_ioccc.sh

	8    -h and help string printed or -V and version string printed
	9    Command line usage error

	>=10  internal error

run_flex.sh:

    usage: ./run_flex.sh [-h] [-v level] [-V] [-f flex] [-l limit_ioccc.sh]
			    [-g verge] [-p prefix] [-s sorry.h] [-F dir] .. -- [flex_flags ..]

	-h              print help and exit 8
	-v level        set debug level (def: 0)
	-V              print version and exit 8
	-f flex        flex tool basename (def: flex)
	-l limit_ioccc.sh   version info file (def: ./limit_ioccc.sh)
	-g verge	    path to verge tool (def: ./verge)
	-p prefix	    The prefix of files to be used (def: jparse)
			    NOTE: If the final arg is flex:
			    NOTE:    The flex input file will be prefix.y
			    NOTE:	 If flex cannot be used, then these backup files are used:
			    NOTE:
			    NOTE:		prefix.c
			    NOTE:
	-s sorry.h	    File to prepend to C output (def: sorry.tm.ca.h)
	-F dir          1st look for flex in dir (def: look just along $PATH)
			    NOTE: Multiple -B dir are allowed.
			    NOTE: Search is performed in dir order before the $PATH path.
			    NOTE: If dir is missing or not searchable, dir is ignored.
			    NOTE: This is ignored if the final arg is NOT flex.
	--		    End of ./run_flex.sh flags
	flex_flags ..  optional flags to give to flex for the prefix.y argument

    Exit codes:
	0    flex output files formed or backup files used instead

	2    good flex found and ran but failed to form proper output files
	3    flex input file missing or not readable:         backup file(s) had to be used

	4    backup file(s) are missing, or are not readable
	5    failed to use backup file(s) to form the flex C output file(s)

	6    limit_ioccc.sh sorry file missing or not readable, or verge missing or not executable
	7    FLEX_VERSION missing or empty from limit_ioccc.sh

	8    -h and help string printed or -V and version string printed
	9    Command line usage error

	>=10  internal error
  • Loading branch information
lcn2 committed Apr 23, 2022
1 parent f0dc30f commit d057026
Show file tree
Hide file tree
Showing 4 changed files with 1,272 additions and 357 deletions.
173 changes: 29 additions & 144 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@
#############

AWK= awk
BISON = bison
CAT= cat
CC= cc
CP= cp
CTAGS= ctags
FLEX = flex
GREP= grep
INSTALL= install
MAKE= make
Expand Down Expand Up @@ -240,28 +238,44 @@ H_FILES= dbg.h jauthchk.h jinfochk.h json.h jstrdecode.h jstrencode.h limit_iocc
#
DSYMDIRS= $(TARGETS:=.dSYM)
SH_FILES= iocccsize-test.sh jstr-test.sh limit_ioccc.sh mkiocccentry-test.sh json-test.sh \
jcodechk.sh vermod.sh bfok.sh prep.sh
jcodechk.sh vermod.sh prep.sh run_bison.sh run_flex.sh
BUILD_LOG= build.log

# Where bfok.sh looks for bison and flex with a version
# the basename of bison (or yacc) to look for
#
BISON_BASENAME = bison
#BISON_BASENAME = yacc

# Where run_bison.sh will search for bison with a recent enough version
#
# The -B arguments specify where to look for bison with a version,
# that is >= the minimum version (see BISON_VERSION in limit_ioccc.sh),
# before searching for bison on $PATH.
#
# The -F arguments specify where to look for flex with a version,
# that is >= the minimum version (see FLEX_VERSION in limit_ioccc.sh),
# before searching for flex on $PATH.
#
# NOTE: If is OK if these directories do not exist.
#
BFOK_DIRS= \
BISON_DIRS= \
-B /opt/homebrew/opt/bison/bin \
-B /opt/homebrew/bin \
-B /opt/local/bin \
-B /usr/local/opt \
-B /usr/local/bin \
-B . \
-B .

# the basename of flex (or lex) to look for
#
FLEX_BASENAME= flex
#FLEX_BASENAME= lex

# Where run_flex.sh will search for flex with a recent enough version
#
# The -F arguments specify where to look for flex with a version,
# that is >= the minimum version (see FLEX_VERSION in limit_ioccc.sh),
# before searching for bison on $PATH.
#
# NOTE: If is OK if these directories do not exist.
#
FLEX_DIRS= \
-F /opt/homebrew/opt/flex/bin \
-F /opt/homebrew/bin \
-F /opt/local/bin \
Expand Down Expand Up @@ -296,7 +310,7 @@ all: ${TARGETS} ${TEST_TARGETS}
# rules, not file targets
#
.PHONY: all configure clean clobber install test reset_min_timestamp rebuild_jint_test \
rebuild_jfloat_test picky parser build bfok clean_generated_obj prep_clobber
rebuild_jfloat_test picky parser build clean_generated_obj prep_clobber


#####################################
Expand Down Expand Up @@ -417,114 +431,17 @@ limit_ioccc.sh: limit_ioccc.h version.h Makefile
# if bison is found and has a recent enough version, otherwise
# use a pre-built reference copies stored in jparse.tab.ref.h and jparse.tab.ref.c.
#
jparse.tab.c jparse.tab.h: jparse.y bfok.sh limit_ioccc.sh verge jparse.tab.ref.c jparse.tab.ref.h Makefile
${RM} -f jparse.tab.c jparse.tab.h
@if `./bfok.sh ${BFOK_DIRS} 2>/dev/null`; then \
BISON_PATH="`./bfok.sh ${BFOK_DIRS} -p bison 2>/dev/null`"; \
TMP_JPARSE_TAB_C="`${MKTEMP} -t jparse.tab.c`"; \
TMP_JPARSE_TAB_H="`${MKTEMP} -t jparse.tab.h`"; \
if [[ -z $$BISON_PATH || -z $$TMP_JPARSE_TAB_H || -z $$TMP_JPARSE_TAB_C ]]; then \
echo "failed to discover the bison path" 1>&2; \
echo "will use backup files instead" 1>&2; \
echo "${CP} -f -v jparse.tab.ref.c jparse.tab.c"; \
${CP} -f -v jparse.tab.ref.c jparse.tab.c; \
echo "${CP} -f -v jparse.tab.ref.h jparse.tab.h"; \
${CP} -f -v jparse.tab.ref.h jparse.tab.h; \
else \
echo "$$BISON_PATH -d -Wyacc -Dparse.error=verbose -Dparse.lac=full jparse.y"; \
"$$BISON_PATH" -d -Wyacc -Dparse.error=verbose -Dparse.lac=full jparse.y; \
status="$$?"; \
if [[ $$status -eq 0 && -s jparse.tab.c && -s jparse.tab.h ]]; then \
echo '# prepending comment and line number reset to jparse.tab.c'; \
echo "${CP} -f -v sorry.tm.ca.h $$TMP_JPARSE_TAB_C"; \
${CP} -f -v sorry.tm.ca.h "$$TMP_JPARSE_TAB_C"; \
echo "echo '#line 1 \"jparse.tab.c\"' >> $$TMP_JPARSE_TAB_C"; \
echo '#line 1 "jparse.tab.c"' >> "$$TMP_JPARSE_TAB_C"; \
echo "${CAT} jparse.tab.c >> $$TMP_JPARSE_TAB_C"; \
${CAT} jparse.tab.c >> "$$TMP_JPARSE_TAB_C"; \
echo "${MV} -f -v $$TMP_JPARSE_TAB_C jparse.tab.c"; \
${MV} -f -v "$$TMP_JPARSE_TAB_C" jparse.tab.c; \
echo '# jparse.tab.c prepended and line number reset'; \
echo '# prepending comment and line number reset to jparse.tab.h'; \
echo "${CP} -f -v sorry.tm.ca.h $$TMP_JPARSE_TAB_H"; \
${CP} -f -v sorry.tm.ca.h "$$TMP_JPARSE_TAB_H"; \
echo "echo '#line 1 \"jparse.tab.h\"' >> $$TMP_JPARSE_TAB_H"; \
echo '#line 1 "jparse.tab.h"' >> "$$TMP_JPARSE_TAB_H"; \
echo "${CAT} jparse.tab.h >> $$TMP_JPARSE_TAB_H"; \
${CAT} jparse.tab.h >> "$$TMP_JPARSE_TAB_H"; \
echo "${MV} -f -v $$TMP_JPARSE_TAB_H jparse.tab.h"; \
${MV} -f -v "$$TMP_JPARSE_TAB_H" jparse.tab.h; \
echo '# jparse.tab.h prepended and line number reset'; \
else \
echo "unable to form jparse.tab.h and/or jparse.tab.c"; \
echo "if you've not modified jparse.y you can run:"; \
echo; \
echo " make use_ref all"; \
echo ; \
echo "to use the backup files. If you do this please"; \
echo "consider reporting this as a bug. If you've"; \
echo "modified jparse.y please fix it and rerun this rule."; \
exit 1; \
fi; \
fi; \
else \
echo "no bison, with version >= BISON_VERSION in limit_ioccc.sh, found" 1>&2; \
echo "will move both backup files in place instead" 1>&2; \
echo "${CP} -f -v jparse.tab.ref.c jparse.tab.c"; \
${CP} -f -v jparse.tab.ref.c jparse.tab.c; \
echo "${CP} -f -v jparse.tab.ref.h jparse.tab.h"; \
${CP} -f -v jparse.tab.ref.h jparse.tab.h; \
fi
jparse.tab.c jparse.tab.h: jparse.y run_bison.sh limit_ioccc.sh verge jparse.tab.ref.c jparse.tab.ref.h Makefile
./run_bison.sh ${BISON_DIRS} -p jparse -v 1 -- -d -Wyacc -Dparse.error=verbose -Dparse.lac=full

# How to create jparse.c
#
# Convert jparse.l into jparse.c via flex,
# if flex found and has a recent enough version, otherwise
# use a pre-built reference copy stored in jparse.ref.c
#
jparse.c: jparse.l jparse.tab.h bfok.sh limit_ioccc.sh verge jparse.ref.c Makefile
${RM} -f jparse.c
@if `./bfok.sh ${BFOK_DIRS} 2>/dev/null`; then \
FLEX_PATH="`./bfok.sh ${BFOK_DIRS} -p flex 2>/dev/null`"; \
TMP_JPARSE_C="`${MKTEMP} -t jparse.c`"; \
if [[ -z $$FLEX_PATH || -z $$TMP_JPARSE_C ]]; then \
echo "failed to discover the flex path" 1>&2; \
echo "will use backup files" 1>&2; \
echo "${CP} -f -v jparse.ref.c jparse.c"; \
${CP} -f -v jparse.ref.c jparse.c; \
else \
echo "$$FLEX_PATH -8 -o jparse.c jparse.l"; \
"$$FLEX_PATH" -8 -o jparse.c jparse.l; \
status="$$?"; \
if [[ $$status -eq 0 && -s jparse.c ]]; then \
echo '# prepending comment and line number reset to jparse.c'; \
echo "${CP} -f -v sorry.tm.ca.h $$TMP_JPARSE_C"; \
${CP} -f -v sorry.tm.ca.h "$$TMP_JPARSE_C"; \
echo "echo '#line 1 \"jparse.c\"' >> $$TMP_JPARSE_C"; \
echo '#line 1 "jparse.c"' >> "$$TMP_JPARSE_C"; \
echo "${CAT} jparse.c >> $$TMP_JPARSE_C"; \
${CAT} jparse.c >> "$$TMP_JPARSE_C"; \
echo "${MV} -f -v $$TMP_JPARSE_C jparse.c"; \
${MV} -f -v "$$TMP_JPARSE_C" jparse.c; \
echo '# jparse.c prepended and line number reset'; \
else \
echo "unable to form jparse.c"; \
echo "if you've not modified jparse.l you can run:"; \
echo; \
echo " make use_ref all"; \
echo ; \
echo "to use the backup file. If you do this please"; \
echo "consider reporting this as a bug. If you've"; \
echo "modified jparse.l please fix it and rerun this rule."; \
exit 1; \
fi; \
fi; \
else \
echo "no flex, with version >= FLEX_VERSION in limit_ioccc.sh, found" 1>&2; \
echo "will move backup file in place instead" 1>&2; \
echo "${CP} -f -v jparse.ref.c jparse.c"; \
${CP} -f -v jparse.ref.c jparse.c; \
fi
jparse.c: jparse.l jparse.tab.h run_flex.sh limit_ioccc.sh verge jparse.ref.c Makefile
./run_flex.sh ${FLEX_DIRS} -p jparse -v 1 -- -d -8 -o jparse.c


###################################################################
Expand Down Expand Up @@ -557,35 +474,8 @@ build release pull: prep.sh
parser: jparse.y jparse.l Makefile
${RM} -f jparse.tab.c jparse.tab.h
${MAKE} jparse.tab.c jparse.tab.h
@if [[ -s jparse.tab.c ]]; then \
echo "${RM} -f jparse.tab.ref.c"; \
${RM} -f jparse.tab.ref.c; \
echo "${CP} -f -v jparse.tab.c jparse.tab.ref.c"; \
${CP} -f -v jparse.tab.c jparse.tab.ref.c; \
else \
echo "jparse.tab.c is missing or empty, cannot update jparse.tab.ref.c" 1>&2; \
exit 1; \
fi
@if [[ -s jparse.tab.h ]]; then \
echo "${RM} -f jparse.tab.ref.h"; \
${RM} -f jparse.tab.ref.h; \
echo "${CP} -f -v jparse.tab.h jparse.tab.ref.h"; \
${CP} -f -v jparse.tab.h jparse.tab.ref.h; \
else \
echo "jparse.tab.h is missing or empty, cannot update jparse.tab.ref.h" 1>&2; \
exit 1; \
fi
${RM} -f jparse.c
${MAKE} jparse.c
@if [[ -s jparse.c ]]; then \
echo "${RM} -f jparse.ref.c"; \
${RM} -f jparse.ref.c; \
echo "${CP} -f -v jparse.c jparse.ref.c"; \
${CP} -f -v jparse.c jparse.ref.c; \
else \
echo "jparse.c is missing or empty, cannot update jparse.ref.c" 1>&2; \
exit 1; \
fi

# restore reference code that was produced by previous successful make parser
#
Expand Down Expand Up @@ -824,11 +714,6 @@ test: all iocccsize-test.sh dbg mkiocccentry-test.sh jstr-test.sh jint jfloat Ma
test-jinfochk: all jinfochk Makefile
./json-test.sh -t jinfo_only -v 1 -D 1 -d test_JSON

# rule used by prep.sh
#
bfok: bfok.sh limit_ioccc.sh verge
./bfok.sh ${BFOK_DIRS}

# rule used by prep.sh and make clean
#
clean_generated_obj:
Expand Down
Loading

0 comments on commit d057026

Please sign in to comment.