java: examples: split in two

* examples/java: Split in...
* examples/java/simple, examples/java/calc: these.
This commit is contained in:
Akim Demaille
2020-02-02 16:01:42 +01:00
parent d727e0ff23
commit 48be689a73
11 changed files with 261 additions and 25 deletions

9
NEWS
View File

@@ -75,6 +75,15 @@ GNU Bison NEWS
the translated symbol (i.e., it returns '_("variable")' rather that
'"variable"').
** Documentation
There are now two examples in examples/java: a very simple calculator, and
one that tracks locations to provide acurate error messages.
A new C example, bistromathic, is a fully featured calculator using many
Bison features: pure interface, location tracking, internationalized
custom error messages, lookahead-correction, rich debug traces, etc.
* Noteworthy changes in release 3.5.1 (2020-01-19) [stable]
** Bug fixes

View File

@@ -5,8 +5,11 @@ This directory contains examples of Bison grammar files in Java.
You can run `make` to compile these examples. And `make clean` to tidy
afterwards.
## java/Calc.y
The usual calculator.
## simple/Calc.y
The usual calculator, a very simple version.
## calc/Calc.y
The calculator, but with location tracking and debug traces.
<!---

View File

@@ -160,7 +160,7 @@ class Position {
line = p.line;
column = p.column;
}
public boolean equals (Position l)
{
return l.line == line && l.column == column;
@@ -168,7 +168,7 @@ class Position {
public String toString ()
{
return Integer.toString (line) + "." + Integer.toString(column);
return Integer.toString (line) + "." + Integer.toString (column);
}
public int line ()
@@ -187,7 +187,7 @@ class PositionReader extends BufferedReader {
private Position position = new Position ();
public PositionReader (Reader reader) {
super(reader);
super (reader);
}
public int read () throws IOException {

View File

@@ -0,0 +1,36 @@
## Copyright (C) 2018-2020 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/>.
java_calcdir = $(docdir)/%D%
## ------ ##
## Calc. ##
## ------ ##
if ENABLE_JAVA
check_SCRIPTS += %D%/Calc.class
TESTS += %D%/Calc.test
endif
EXTRA_DIST += %D%/Calc.test
%D%/Calc.java: %D%/Calc.y $(dependencies)
$(AM_V_GEN)$(MKDIR_P) %D%
$(AM_V_at)$(BISON) $(srcdir)/%D%/Calc.y -o $@
%D%/Calc.class: %D%/Calc.java
$(AM_V_GEN) $(SHELL) $(top_builddir)/javacomp.sh %D%/Calc.java
dist_java_calc_DATA = %D%/Calc.y %D%/Makefile
CLEANFILES += %D%/*.class %D%/Calc.java

View File

@@ -1,4 +1,4 @@
## Copyright (C) 2018-2020 Free Software Foundation, Inc.
## Copyright (C) 2020 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
@@ -14,23 +14,7 @@
## along with this program. If not, see <http://www.gnu.org/licenses/>.
javadir = $(docdir)/%D%
dist_java_DATA = %D%/README.md
## ------ ##
## Calc. ##
## ------ ##
if ENABLE_JAVA
check_SCRIPTS += %D%/Calc.class
TESTS += %D%/Calc.test
endif
EXTRA_DIST += %D%/Calc.test
%D%/Calc.java: %D%/Calc.y $(dependencies)
$(AM_V_GEN)$(MKDIR_P) %D%
$(AM_V_at)$(BISON) $(srcdir)/%D%/Calc.y -o $@
%D%/Calc.class: %D%/Calc.java
$(AM_V_GEN) $(SHELL) $(top_builddir)/javacomp.sh %D%/Calc.java
dist_java_DATA = %D%/Calc.y %D%/Makefile %D%/README.md
CLEANFILES += %D%/*.class %D%/Calc.java
include %D%/calc/local.mk
include %D%/simple/local.mk

View File

@@ -0,0 +1,33 @@
#! /bin/sh
# Copyright (C) 2018-2020 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
1 + 2 * 3
EOF
run 0 7
cat >input <<EOF
1 + 2 * 3 = 7
(1 + 2) * 3 = 9
EOF
run 0 '7
9'
cat >input <<EOF
1 + 2 * * 3
EOF
run 0 "err: syntax error, unexpected '*', expecting number or '-' or '(' or '!'"

109
examples/java/simple/Calc.y Normal file
View File

@@ -0,0 +1,109 @@
%language "Java"
%define api.parser.class {Calc}
%define api.parser.public
%define parse.error verbose
%code imports {
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StreamTokenizer;
}
%code {
public static void main (String args[]) throws IOException
{
CalcLexer l = new CalcLexer (System.in);
Calc p = new Calc (l);
if (!p.parse ())
System.exit (1);
}
}
/* Bison Declarations */
%token <Integer> NUM "number"
%type <Integer> exp
%nonassoc '=' /* comparison */
%left '-' '+'
%left '*' '/'
%precedence NEG /* negation--unary minus */
%right '^' /* exponentiation */
/* Grammar follows */
%%
input:
line
| input line
;
line:
'\n'
| exp '\n' { System.out.println ($exp); }
| error '\n'
;
exp:
NUM { $$ = $1; }
| exp '=' exp
{
if ($1.intValue () != $3.intValue ())
yyerror ("calc: error: " + $1 + " != " + $3);
}
| exp '+' exp { $$ = $1 + $3; }
| exp '-' exp { $$ = $1 - $3; }
| exp '*' exp { $$ = $1 * $3; }
| exp '/' exp { $$ = $1 / $3; }
| '-' exp %prec NEG { $$ = -$2; }
| exp '^' exp { $$ = (int) Math.pow ($1, $3); }
| '(' exp ')' { $$ = $2; }
| '(' error ')' { $$ = 1111; }
| '!' { $$ = 0; return YYERROR; }
| '-' error { $$ = 0; return YYERROR; }
;
%%
class CalcLexer implements Calc.Lexer {
StreamTokenizer st;
public CalcLexer (InputStream is)
{
st = new StreamTokenizer (new InputStreamReader (is));
st.resetSyntax ();
st.eolIsSignificant (true);
st.wordChars ('0', '9');
}
public void yyerror (String s)
{
System.err.println (s);
}
Integer yylval;
public Object getLVal () {
return yylval;
}
public int yylex () throws IOException {
int ttype = st.nextToken ();
if (ttype == st.TT_EOF)
return EOF;
else if (ttype == st.TT_EOL)
return (int) '\n';
else if (ttype == st.TT_WORD)
{
yylval = new Integer (st.sval);
return NUM;
}
else if (st.ttype == ' ' || st.ttype == '\t')
return yylex ();
else
return st.ttype;
}
}

View File

@@ -0,0 +1,26 @@
# This Makefile is designed to be simple and readable. It does not
# aim at portability. It requires GNU Make.
BISON = bison
JAVAC = javac
JAVA = java
XSLTPROC = xsltproc
all: Calc.class
%.java %.xml %.gv: %.y
$(BISON) $(BISONFLAGS) --xml --graph=$*.gv -o $*.java $<
%.class: %.java
$(JAVAC) $(JAVACFLAGS) $<
run: Calc.class
@echo "Type arithmetic expressions. Quit with ctrl-d."
$(JAVA) $(JAVAFLAGS) Calc
html: Calc.html
%.html: %.xml
$(XSLTPROC) $(XSLTPROCFLAGS) -o $@ $$($(BISON) --print-datadir)/xslt/xml2xhtml.xsl $<
clean:
rm -f *.class Calc.java Calc.html Calc.xml Calc.gv

View File

@@ -0,0 +1,36 @@
## Copyright (C) 2018-2020 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/>.
java_simpledir = $(docdir)/%D%
## ------ ##
## Calc. ##
## ------ ##
if ENABLE_JAVA
check_SCRIPTS += %D%/Calc.class
TESTS += %D%/Calc.test
endif
EXTRA_DIST += %D%/Calc.test
%D%/Calc.java: %D%/Calc.y $(dependencies)
$(AM_V_GEN)$(MKDIR_P) %D%
$(AM_V_at)$(BISON) $(srcdir)/%D%/Calc.y -o $@
%D%/Calc.class: %D%/Calc.java
$(AM_V_GEN) $(SHELL) $(top_builddir)/javacomp.sh %D%/Calc.java
dist_java_simple_DATA = %D%/Calc.y %D%/Makefile
CLEANFILES += %D%/*.class %D%/Calc.java