Re: [PATCH 3/4] java: add support for lookahead correction
Akim Demaille <[email protected]>
| Newsgroups | gmane.comp.parsers.bison.patches |
|---|---|
| Message-ID | <[email protected]> |
> Le 1 nov. 2020 à 19:33, Akim Demaille <[email protected]> a écrit : > > Shamelessly stolen from Adrian Vogelsgesang's implementation in > lalr1.cc. > > * data/skeletons/lalr1.java (yycdebugNnl, yyLacCheck, yyLacEstablish) > (yyLacDiscard, yylacStack, yylacEstablished): New. > (Context): Use it. > * examples/java/calc/Calc.y: Enable LAC. I'll append this patch to the previous one. commit d15691045342fb67796a1368698ad8ee4c5799b9 Author: Akim Demaille <[email protected]> Date: Tue Nov 3 08:21:35 2020 +0100 java: prefer ArrayList to Vector Vector is synchronized, which is completely useless in our case (and not even relevant when concurrency matters). No seasoned Java programmer would use it. Reported by Uxio Prego. * data/skeletons/lalr1.java: Replace Vector with ArrayList. Unfortunately its API is not as rich, and lacks lastElement and setSize. diff --git a/data/skeletons/lalr1.java b/data/skeletons/lalr1.java index be2900ef..26508a8c 100644 --- a/data/skeletons/lalr1.java +++ b/data/skeletons/lalr1.java @@ -102,7 +102,7 @@ b4_output_begin([b4_parser_file_name])[ ]b4_user_pre_prologue[ ]b4_user_post_prologue[ import java.text.MessageFormat; -import java.util.Vector; +import java.util.ArrayList; ]b4_percent_code_get([[imports]])[ /** * A Bison parser, automatically generated from <tt>]m4_bpatsubst(b4_file_name, [^"\(.*\)"$], [\1])[</tt>. @@ -268,7 +268,7 @@ import java.util.Vector; public ]b4_parser_class[(]b4_parse_param_decl([b4_lex_param_decl])[)]b4_maybe_throws([b4_init_throws])[ { ]b4_percent_code_get([[init]])[]b4_lac_if([[ - this.yylacStack = new Vector<Integer>(); + this.yylacStack = new ArrayList<Integer>(); this.yylacEstablished = false;]])[ this.yylexer = new YYLexer(]b4_lex_param_call[); ]b4_parse_param_cons[ @@ -282,7 +282,7 @@ import java.util.Vector; ]b4_lexer_if([[protected]], [[public]]) b4_parser_class[(]b4_parse_param_decl([[Lexer yylexer]])[)]b4_maybe_throws([b4_init_throws])[ { ]b4_percent_code_get([[init]])[]b4_lac_if([[ - this.yylacStack = new Vector<Integer>(); + this.yylacStack = new ArrayList<Integer>(); this.yylacEstablished = false;]])[ this.yylexer = yylexer; ]b4_parse_param_cons[ @@ -852,7 +852,7 @@ b4_dollar_popdef[]dnl this.yylen = 0; this.yystate = 0; this.yystack = new YYStack();]b4_lac_if([[ - this.yylacStack = new Vector<Integer>(); + this.yylacStack = new ArrayList<Integer>(); this.yylacEstablished = false;]])[ this.label = YYNEWSTATE; @@ -1021,7 +1021,7 @@ b4_dollar_popdef[]dnl { int topState = (yylacStack.isEmpty() ? yystack.stateAt(lacTop) - : yylacStack.lastElement()); + : yylacStack.get(yylacStack.size() - 1)); int yyrule = yypact_[topState]; if (yyPactValueIsDefault(yyrule) || (yyrule += yytoken.getCode()) < 0 || YYLAST_ < yyrule @@ -1056,7 +1056,10 @@ b4_dollar_popdef[]dnl // First pop from the LAC stack as many tokens as possible. int lacSize = yylacStack.size(); if (yylen < lacSize) { - yylacStack.setSize(lacSize - yylen); + // yylacStack.setSize(lacSize - yylen); + for (/* Nothing */; 0 < yylen; yylen -= 1) { + yylacStack.remove(yylacStack.size() - 1); + } yylen = 0; } else if (lacSize != 0) { yylacStack.clear(); @@ -1069,7 +1072,7 @@ b4_dollar_popdef[]dnl // Keep topState in sync with the updated stack. topState = (yylacStack.isEmpty() ? yystack.stateAt(lacTop) - : yylacStack.lastElement()); + : yylacStack.get(yylacStack.size() - 1)); // Push the resulting state of the reduction. int state = yyLRGotoState(topState, yyr1_[yyrule]); yycdebugNnl(" G" + state); @@ -1139,7 +1142,7 @@ b4_dollar_popdef[]dnl * yylacCheck. We just store it as a member of this class to hold * on to the memory and to avoid frequent reallocations. */ - Vector<Integer> yylacStack; + ArrayList<Integer> yylacStack; /** Whether an initial LAC context was established. */ boolean yylacEstablished; ]])[