summaryrefslogtreecommitdiff
path: root/perly.c
diff options
context:
space:
mode:
authorDavid Mitchell <davem@iabyn.com>2016-12-04 08:10:27 +0000
committerDavid Mitchell <davem@iabyn.com>2016-12-05 11:54:03 +0000
commit0f8490d1d7ad76cac844fc2ae882994e38aaf2ef (patch)
tree25fea4e29ac14105e7b8cc46b37d7c90142c05a3 /perly.c
parentb2c9b6ee5d402c923568f214f2e2606287c912d3 (diff)
downloadperl-0f8490d1d7ad76cac844fc2ae882994e38aaf2ef.tar.gz
yyparse: only calculate yytoken on yychar change
yytoken is a translated (via lookup table) version of parser->yychar. So we only need to recalculate it when yychar changes (usually by assigning the result of yylex() to it). This means when multiple reductions are done without shifting another token, we skip the extra overhead each time.
Diffstat (limited to 'perly.c')
-rw-r--r--perly.c50
1 files changed, 28 insertions, 22 deletions
diff --git a/perly.c b/perly.c
index af44956ff5..5d1519f262 100644
--- a/perly.c
+++ b/perly.c
@@ -289,6 +289,8 @@ Perl_yyparse (pTHX_ int gramtype)
/* initialise state for this parse */
parser->yychar = gramtype;
+ yytoken = YYTRANSLATE(NATIVE_TO_UNI(parser->yychar));
+
parser->yyerrstatus = 0;
parser->yylen = 0;
Newx(parser->stack, YYINITDEPTH, yy_stack_frame);
@@ -325,40 +327,44 @@ Perl_yyparse (pTHX_ int gramtype)
parser->yylen = 0;
- /* Do appropriate processing given the current state. */
- /* Read a lookahead token if we need one and don't already have one. */
+ /* Do appropriate processing given the current state. Read a
+ * lookahead token if we need one and don't already have one.
+ * */
- /* First try to decide what to do without reference to lookahead token. */
+ /* First try to decide what to do without reference to
+ * lookahead token. */
yyn = yypact[yystate];
if (yyn == YYPACT_NINF)
goto yydefault;
- /* Not known => get a lookahead token if don't already have one. */
+ /* Not known => get a lookahead token if don't already have
+ * one. YYCHAR is either YYEMPTY or YYEOF or a valid
+ * lookahead symbol. */
- /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */
if (parser->yychar == YYEMPTY) {
YYDPRINTF ((Perl_debug_log, "Reading a token:\n"));
parser->yychar = yylex();
+ assert(parser->yychar >= 0);
+ if (parser->yychar == YYEOF)
+ YYDPRINTF ((Perl_debug_log, "Now at end of input.\n"));
+ /* perly.tab is shipped based on an ASCII system, so need
+ * to index it with characters translated to ASCII.
+ * Although it's not designed for this purpose, we can use
+ * NATIVE_TO_UNI here. It returns its argument on ASCII
+ * platforms, and on EBCDIC translates native to ascii in
+ * the 0-255 range, leaving everything else unchanged.
+ * This jibes with yylex() returning some bare characters
+ * in that range, but all tokens it returns are either 0,
+ * or above 255. There could be a problem if NULs weren't
+ * 0, or were ever returned as raw chars by yylex() */
+ yytoken = YYTRANSLATE(NATIVE_TO_UNI(parser->yychar));
}
- if (parser->yychar <= YYEOF) {
- parser->yychar = yytoken = YYEOF;
- YYDPRINTF ((Perl_debug_log, "Now at end of input.\n"));
- }
- else {
- /* perly.tab is shipped based on an ASCII system, so need to index it
- * with characters translated to ASCII. Although it's not designed for
- * this purpose, we can use NATIVE_TO_UNI here. It returns its
- * argument on ASCII platforms, and on EBCDIC translates native to
- * ascii in the 0-255 range, leaving everything else unchanged. This
- * jibes with yylex() returning some bare characters in that range, but
- * all tokens it returns are either 0, or above 255. There could be a
- * problem if NULs weren't 0, or were ever returned as raw chars by
- * yylex() */
- yytoken = YYTRANSLATE (NATIVE_TO_UNI(parser->yychar));
- YYDSYMPRINTF ("Next token is", yytoken, &parser->yylval);
- }
+ /* make sure no-ones changed yychar since the last call to yylex */
+ assert(yytoken == YYTRANSLATE(NATIVE_TO_UNI(parser->yychar)));
+ YYDSYMPRINTF("lookahead token is", yytoken, &parser->yylval);
+
/* If the proper action on seeing token YYTOKEN is to reduce or to
* detect an error, take that action.