summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAkim Demaille <akim.demaille@gmail.com>2020-05-02 09:01:34 +0200
committerAkim Demaille <akim.demaille@gmail.com>2020-05-03 11:28:36 +0200
commit13a1537dbaf61c522136fdd8c2fff28b144824e1 (patch)
tree6ff5156b7f79163e913e523e59a348be9adbcfd9 /examples
parentac2ba46053e3ccecda23f654ccec4d93b1484256 (diff)
downloadbison-13a1537dbaf61c522136fdd8c2fff28b144824e1.tar.gz
java: demonstrate push parsers
* data/skeletons/lalr1.java (Location): Make it a static class. (Lexer.yylex, Lexer.getLVal, Lexer.getStartPos, Lexer.getEndPos): These are not needed in push parsers. * examples/java/calc/Calc.y: Demonstrate push parsers in the Java. * doc/bison.texi: Push parsers have been supported for a long time, remove incorrect statements stating the opposite.
Diffstat (limited to 'examples')
-rw-r--r--examples/java/README.md2
-rw-r--r--examples/java/calc/Calc.y40
2 files changed, 28 insertions, 14 deletions
diff --git a/examples/java/README.md b/examples/java/README.md
index 4be92c80..25b7f27c 100644
--- a/examples/java/README.md
+++ b/examples/java/README.md
@@ -9,7 +9,7 @@ afterwards.
The usual calculator, a very simple version.
## calc/Calc.y
-The calculator, but with location tracking and debug traces.
+The calculator, but with location tracking, debug traces, and a push parser.
<!---
diff --git a/examples/java/calc/Calc.y b/examples/java/calc/Calc.y
index 5a5d2048..e9f89572 100644
--- a/examples/java/calc/Calc.y
+++ b/examples/java/calc/Calc.y
@@ -2,6 +2,7 @@
%define api.parser.class {Calc}
%define api.parser.public
+%define api.push-pull push
%define parse.error custom
%define parse.trace
@@ -20,12 +21,19 @@
%code {
public static void main(String[] args) throws IOException {
- CalcLexer l = new CalcLexer(System.in);
- Calc p = new Calc(l);
+ CalcLexer scanner = new CalcLexer(System.in);
+ Calc parser = new Calc(scanner);
for (String arg : args)
if (arg.equals("-p"))
- p.setDebugLevel(1);
- if (!p.parse())
+ parser.setDebugLevel(1);
+ int status;
+ do {
+ int token = scanner.getToken();
+ Object lval = scanner.getValue();
+ Calc.Location yyloc = scanner.getLocation();
+ status = parser.push_parse(token, lval, yyloc);
+ } while (status == Calc.YYPUSH_MORE);
+ if (status != Calc.YYACCEPT)
System.exit(1);
}
@@ -105,12 +113,12 @@ class CalcLexer implements Calc.Lexer {
Position start = new Position(1, 0);
Position end = new Position(1, 0);
- public Position getStartPos() {
- return new Position(start);
- }
-
- public Position getEndPos() {
- return new Position(end);
+ /**
+ * The location of the last token read.
+ * Implemented with getStartPos and getEndPos in pull parsers.
+ */
+ public Calc.Location getLocation() {
+ return new Calc.Location(new Position(start), new Position(end));
}
/**
@@ -150,11 +158,17 @@ class CalcLexer implements Calc.Lexer {
Integer yylval;
- public Object getLVal() {
+ /**
+ * The value of the last token read. Called getLVal in pull parsers.
+ */
+ public Object getValue() {
return yylval;
}
- public int yylex() throws IOException {
+ /**
+ * Fetch the next token. Called yylex in pull parsers.
+ */
+ public int getToken() throws IOException {
start.set(reader.getPosition());
int ttype = st.nextToken();
end.set(reader.getPosition());
@@ -170,7 +184,7 @@ class CalcLexer implements Calc.Lexer {
end.set(reader.getPreviousPosition());
return NUM;
case ' ': case '\t':
- return yylex();
+ return getToken();
case '!':
return BANG;
case '+':