examples: sort them per language and complete them

Convert some of the READMEs to Markdown, which is now more common, and
nicely displayed in some git hosting services.

Add missing READMEs and Makefiles.  Generate XML, HTML and Dot files.  Be
sure to ship the test files.  Complete CLEANFILES to remove all generated
files.

* examples/calc++: Move into...
* examples/c++: here.
* examples/mfcalc, examples/rpcalc: Move into...
* examples/c: here.

* examples/README.md, examples/c++/calc++/Makefile, examples/c/local.mk,
* examples/c/mfcalc/Makefile, examples/c/rpcalc/Makefile,
* examples/d/README.md, examples/java/README.md:
New files.

* examples/test (medir): Be robust to deeper directory nesting.
This commit is contained in:
Akim Demaille
2018-12-09 11:49:23 +01:00
parent 1e6a68858a
commit 85d303b713
28 changed files with 252 additions and 91 deletions

View File

@@ -1,11 +1,11 @@
This directory contains examples of Bison grammar files in C++.
# Examples in C++
* Examples in C++
This directory contains examples of Bison grammar files in C++.
You can run `make` to compile these examples. And `make clean` to tidy
afterwards.
** simple.yy - Simple example in C++14
## simple.yy - Simple example in C++14
A very simple example in C++, based on variants and symbol constructors.
Variants allow to use any C++ type as semantic value type, and symbol
constructors ensure consistency between declared token type and effective
@@ -16,21 +16,32 @@ Run as `./simple`.
Extracted from the documentation: "A Simple C++ Example".
https://www.gnu.org/software/bison/manual/html_node/A-Simple-C_002b_002b-Example.html
** variant.yy - Self-contained example in C++98
## variant.yy - Self-contained example in C++98
A variation of simple.yy, in C++98.
Run as `./variant`.
** variant-11.yy - Self-contained example in modern C++
## variant-11.yy - Self-contained example in modern C++
Another variation of simple.yy, closely related to the previous one, but
exhibiting support for C++11's move semantics.
Run as `./variant` or `./variant NUMBER`.
-----
## calc++ - A Complete C++ Example
A fully featured C++ version of the canonical example for parsers: a
calculator. Also uses Flex for the scanner.
Don't look at this example first: it is fully featured and can serve as a
starting point for a clean parser in C++. The previous examples are better
introductory examples, and the C examples are also useful introductory
examples.
Extracted from the documentation: "A Complete C++ Example".
https://www.gnu.org/software/bison/manual/html_node/A-Complete-C_002b_002b-Example.html
<!---
Local Variables:
mode: outline
fill-column: 76
ispell-dictionary: "american"
End:
@@ -45,3 +56,4 @@ Texts. A copy of the license is included in the "GNU Free
Documentation License" file as part of this distribution.
# LocalWords: mfcalc calc parsers yy ispell american
--->

19
examples/c++/calc++/.gitignore vendored Normal file
View File

@@ -0,0 +1,19 @@
/*.o
/*.tmp
/.deps
/calc++
/calc++.cc
/calc++.exe
/calc.stamp
/driver.cc
/driver.hh
/location.hh
/parser.cc
/parser.hh
/parser.output
/parser.stamp
/parser.yy
/position.hh
/scanner.cc
/scanner.ll
/stack.hh

View File

@@ -0,0 +1,41 @@
# This Makefile is designed to be simple and readable. It does not
# aim at portability. It requires GNU Make.
BASE = calc++
BISON = bison
CXX = g++
FLEX = flex
XSLTPROC = xsltproc
all: $(BASE)
%.cc %.hh %.xml %.gv: %.yy
$(BISON) $(BISONFLAGS) --xml --graph=$*.gv -o $*.cc $<
%.cc: %.ll
$(FLEX) $(FLEXFLAGS) -o$@ $<
%.o: %.cc
$(CXX) $(CXXFLAGS) -c -o$@ $<
$(BASE): $(BASE).o driver.o parser.o scanner.o
$(CXX) -o $@ $^
$(BASE).o: parser.hh
parser.o: parser.hh
scanner.o: parser.hh
run: $(BASE)
@echo "Type arithmetic expressions. Quit with ctrl-d."
./$< -
html: parser.html
%.html: %.xml
$(XSLTPROC) $(XSLTPROCFLAGS) -o $@ $$($(BISON) --print-datadir)/xslt/xml2xhtml.xsl $<
CLEANFILES = \
$(BASE) *.o \
parser.hh parser.cc parser.output parser.xml parser.html parser.gv location.hh \
scanner.cc
clean:
rm -f $(CLEANFILES)

View File

@@ -0,0 +1,54 @@
This directory contains calc++, a simple Bison grammar file in C++.
Please, read the corresponding chapter in the documentation: "A Complete C++
Example". It is also available on line (maybe with a different version of
Bison):
https://www.gnu.org/software/bison/manual/html_node/A-Complete-C_002b_002b-Example.html
To use it, copy this directory into some work directory, and run `make` to
compile the executable, and try it. It is a simple calculator which accepts
several variable definitions, one per line, and then a single expression to
evaluate.
The program calc++ expects the file to parse as argument; pass `-` to read
the standard input (and then hit <Ctrl-d>, control-d, to end your input).
```
$ ./calc++ -
one := 1
two := 2
three := 3
(one + two * three) * two * three
<Ctrl-d>
42
```
You may pass `-p` to activate the parser debug traces, and `-s` to activate
the scanner's.
<!---
Local Variables:
fill-column: 76
ispell-dictionary: "american"
End:
Copyright (C) 2018 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
# LocalWords: mfcalc calc parsers yy MERCHANTABILITY Ctrl ispell american
--->

58
examples/c++/calc++/calc++.test Executable file
View File

@@ -0,0 +1,58 @@
#! /bin/sh
# Copyright (C) 2005-2015, 2018 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
cat >input <<EOF
toto := 1
toto
EOF
run 0 1
run -noerr 0 1 -s
cat >input <<EOF
a := 1
b := 2
c := 3
d := a + b * c
d
EOF
run 0 7
run -noerr 0 7 -p
cat >input <<EOF
a := 1
b := 2
c := 3
d := (a + b) * c
d
EOF
run 0 9
cat >input <<EOF
a := 1
d := a + b * c
EOF
run 1 'err: -:3.1: syntax error, unexpected end of file, expecting ( or identifier or number'
cat >input <<EOF
a := 072101108108111044032119111114108100033
a
EOF
run 1 'err: -:1.6-44: integer is out of range: 072101108108111044032119111114108100033'

View File

@@ -0,0 +1,84 @@
## Copyright (C) 2005-2006, 2008-2015, 2018 Free Software Foundation,
## Inc.
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
## ------------------- ##
## Parser generation. ##
## ------------------- ##
# Don't depend on $(BISON) otherwise we would rebuild these files
# in srcdir, including during distcheck, which is forbidden.
%D%/parser.stamp: $(BISON_IN) $(dist_pkgdata_DATA)
SUFFIXES += .yy .stamp
.yy.stamp:
$(AM_V_YACC)rm -f $@
$(AM_V_at)touch $@.tmp
$(AM_V_at)$(YACCCOMPILE) -o $*.cc $<
$(AM_V_at)mv -f $@.tmp $@
$(calcxx_sources_generated): %D%/parser.stamp
@test -f $@ || rm -f %D%/parser.stamp
@test -f $@ || $(MAKE) $(AM_MAKEFLAGS) %D%/parser.stamp
CLEANFILES += \
$(calcxx_sources_generated) \
%D%/parser.output \
%D%/parser.stamp \
%D%/scanner.cc
CLEANDIRS += %D%/*.dSYM
## -------------------- ##
## Building & testing. ##
## -------------------- ##
# Avoid using BUILT_SOURCES which is too global.
$(%C%_calc___OBJECTS): $(calcxx_sources_generated)
calcxx_sources_extracted = \
%D%/driver.cc \
%D%/driver.hh \
%D%/scanner.ll \
%D%/calc++.cc
calcxx_extracted = \
$(calcxx_sources_extracted) \
%D%/parser.yy
extracted += $(calcxx_extracted)
calcxx_sources_generated = \
%D%/parser.cc \
%D%/parser.hh \
%D%/location.hh
calcxx_sources = \
$(calcxx_sources_extracted) \
$(calcxx_sources_generated)
if FLEX_CXX_WORKS
if ENABLE_CXX
check_PROGRAMS += %D%/calc++
nodist_%C%_calc___SOURCES = $(calcxx_sources)
# Don't use gnulib's system headers.
%C%_calc___CPPFLAGS = -I$(top_srcdir)/%D% -I$(top_builddir)/%D%
%C%_calc___CXXFLAGS = $(AM_CXXFLAGS) $(FLEX_SCANNER_CXXFLAGS)
TESTS += %D%/calc++.test
endif ENABLE_CXX
endif FLEX_CXX_WORKS
EXTRA_DIST += %D%/calc++.test
## ------------ ##
## Installing. ##
## ------------ ##
calcxxdir = $(docdir)/%D%
calcxx_DATA = $(calcxx_extracted)
dist_calcxx_DATA = %D%/README.md %D%/Makefile

View File

@@ -13,7 +13,8 @@
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
cxxdir = $(examplesdir)/c++
cxxdir = $(docdir)/%D%
include %D%/calc++/local.mk
## -------- ##
## Simple. ##
@@ -66,5 +67,5 @@ if ENABLE_CXX11
endif
EXTRA_DIST += %D%/variant-11.test
dist_cxx_DATA = %D%/README %D%/Makefile %D%/variant.yy %D%/variant-11.yy
dist_cxx_DATA = %D%/README.md %D%/Makefile %D%/variant.yy %D%/variant-11.yy
CLEANFILES += %D%/simple.output %D%/variant.output %D%/variant-11.output