]> git.saurik.com Git - bison.git/blobdiff - data/glr.c
Clean up %define and %code implementation in M4 some. Most
[bison.git] / data / glr.c
index 16ced04fc2ea94520a5bf0c71cb17ed4b8b763f4..09c7ab53a90dafce640b653a73d34b28cafda5c5 100644 (file)
@@ -1,4 +1,4 @@
-m4_divert(-1)                                                       -*- C -*-
+                                                                    -*- C -*-
 
 # GLR skeleton for Bison
 # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
@@ -21,6 +21,9 @@ m4_divert(-1)                                                       -*- C -*-
 
 m4_include(b4_pkgdatadir/[c.m4])
 
+b4_push_if([
+b4_complain([[non-deterministic push parsers are not yet supported]])])
+
 ## ---------------- ##
 ## Default values.  ##
 ## ---------------- ##
@@ -141,8 +144,8 @@ m4_define([b4_rhs_location],
 
 # We do want M4 expansion after # for CPP macros.
 m4_changecom()
-m4_divert(0)dnl
-@output @output_parser_name@
+m4_divert_push(0)dnl
+@output(b4_parser_file_name@)
 b4_copyright([Skeleton implementation for Bison GLR parsers in C],
   [2002, 2003, 2004, 2005, 2006])
 [
@@ -150,6 +153,7 @@ b4_copyright([Skeleton implementation for Bison GLR parsers in C],
 
 ]b4_identification
 
+b4_percent_code_get([[top]])[]dnl
 m4_if(b4_prefix, [yy], [],
 [/* Substitute the variable and function names.  */
 #define yyparse b4_prefix[]parse
@@ -162,29 +166,27 @@ m4_if(b4_prefix, [yy], [],
 #define yylloc  b4_prefix[]lloc])[
 
 /* Copy the first part of user declarations.  */
-]b4_pre_prologue
+]b4_user_pre_prologue
 
 dnl # b4_shared_declarations
 dnl # ----------------------
 dnl # Declaration that might either go into the header (if --defines)
 dnl # or open coded in the parser body.
 m4_define([b4_shared_declarations],
-[m4_ifdef([b4_before_definitions],
-[[/* Copy the %before-definitions blocks.  */
-]b4_before_definitions])[]dnl
+[b4_percent_code_get([[requires]])[]dnl
 
 b4_token_enums(b4_tokens)
 
-[#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
+[#ifndef YYSTYPE
 ]m4_ifdef([b4_stype],
-[typedef union b4_union_name
-b4_stype
-/* Line __line__ of glr.c.  */
-b4_syncline([@oline@], [@ofile@])
-       YYSTYPE;],
-[typedef int YYSTYPE;])[
-# define YYSTYPE_IS_DECLARED 1
-# define YYSTYPE_IS_TRIVIAL 1
+[[typedef union ]b4_union_name[
+{
+]b4_user_stype[
+} YYSTYPE;
+# define YYSTYPE_IS_TRIVIAL 1]],
+[m4_if(b4_tag_seen_flag, 0,
+[[typedef int YYSTYPE;
+# define YYSTYPE_IS_TRIVIAL 1]])])[
 #endif
 
 #if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
@@ -203,12 +205,10 @@ typedef struct YYLTYPE
 # define YYLTYPE_IS_TRIVIAL 1
 #endif
 
-]m4_ifdef([b4_after_definitions],
-[[/* Copy the %after-definitions blocks.  */
-]b4_after_definitions])[]dnl
+]b4_percent_code_get([[provides]])[]dnl
 ])
 
-b4_defines_if([#include @output_header_name@],
+b4_defines_if([[#include "@basename(]b4_spec_defines_file[@)"]],
              [b4_shared_declarations])[
 
 /* Enabling traces.  */
@@ -237,15 +237,12 @@ b4_defines_if([#include @output_header_name@],
 static YYSTYPE yyval_default;
 
 /* Copy the second part of user declarations.  */
-]b4_post_prologue[
+]b4_user_post_prologue
+b4_percent_code_get[]dnl
 
-]/* Line __line__ of glr.c.  */
-b4_syncline([@oline@], [@ofile@])
-[
-#include <stdio.h>
+[#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdarg.h>
 
 #ifndef YY_
 # if YYENABLE_NLS
@@ -350,8 +347,7 @@ b4_syncline([@oline@], [@ofile@])
 #define YYMAXUTOK   ]b4_user_token_number_max[
 
 #define YYTRANSLATE(YYX)                                               \
-  ((YYX <= 0) ? YYEOF :                                                        \
-   (unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
+  ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
 
 /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX.  */
 static const ]b4_int_type_for([b4_translate])[ yytranslate[] =
@@ -934,9 +930,7 @@ yyuserAction (yyRuleNum yyn, int yyrhslen, yyGLRStackItem* yyvsp,
 ]])[
   switch (yyn)
     {
-      ]b4_actions
-/* Line __line__ of glr.c.  */
-b4_syncline([@oline@], [@ofile@])[
+      ]b4_user_actions[
       default: break;
     }
 
@@ -2046,11 +2040,19 @@ yyprocessOneStack (yyGLRStack* yystackp, size_t yyk,
            {
              YYDPRINTF ((stderr, "Reading a token: "));
              yychar = YYLEX;
+           }
+
+         if (yychar <= YYEOF)
+           {
+             yychar = yytoken = YYEOF;
+             YYDPRINTF ((stderr, "Now at end of input.\n"));
+           }
+         else
+           {
              yytoken = YYTRANSLATE (yychar);
              YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
            }
-         else
-           yytoken = YYTRANSLATE (yychar);
+
          yygetLRActions (yystate, yytoken, &yyaction, &yyconflicts);
 
          while (*yyconflicts != 0)
@@ -2216,8 +2218,16 @@ yyrecoverSyntaxError (yyGLRStack* yystackp]b4_user_formals[)
          }
        YYDPRINTF ((stderr, "Reading a token: "));
        yychar = YYLEX;
-       yytoken = YYTRANSLATE (yychar);
-       YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
+       if (yychar <= YYEOF)
+         {
+           yychar = yytoken = YYEOF;
+           YYDPRINTF ((stderr, "Now at end of input.\n"));
+         }
+       else
+         {
+           yytoken = YYTRANSLATE (yychar);
+           YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
+         }
        yyj = yypact[yystackp->yytops.yystates[0]->yylrState];
        if (yyis_pact_ninf (yyj))
          return;
@@ -2267,7 +2277,8 @@ yyrecoverSyntaxError (yyGLRStack* yystackp]b4_user_formals[)
            }
        }
 ]b4_locations_if([[      yystackp->yyerror_range[1].yystate.yyloc = yys->yyloc;]])[
-      yydestroyGLRState ("Error: popping", yys]b4_user_args[);
+      if (yys->yypred != NULL)
+       yydestroyGLRState ("Error: popping", yys]b4_user_args[);
       yystackp->yytops.yystates[0] = yys->yypred;
       yystackp->yynextFree -= 1;
       yystackp->yyspaceLeft += 1;
@@ -2310,19 +2321,17 @@ yyrecoverSyntaxError (yyGLRStack* yystackp]b4_user_formals[)
   yylval = yyval_default;
 ]b4_locations_if([
 #if YYLTYPE_IS_TRIVIAL
-  yylloc.first_line   = yylloc.last_line   = 1;
-  yylloc.first_column = yylloc.last_column = 0;
+  yylloc.first_line   = yylloc.last_line   = ]b4_location_initial_line[;
+  yylloc.first_column = yylloc.last_column = ]b4_location_initial_column[;
 #endif
 ])
 m4_ifdef([b4_initial_action], [
 m4_pushdef([b4_at_dollar],     [yylloc])dnl
 m4_pushdef([b4_dollar_dollar], [yylval])dnl
   /* User initialization code.  */
-  b4_initial_action
+  b4_user_initial_action
 m4_popdef([b4_dollar_dollar])dnl
-m4_popdef([b4_at_dollar])dnl
-/* Line __line__ of glr.c.  */
-b4_syncline([@oline@], [@ofile@])])dnl
+m4_popdef([b4_at_dollar])])dnl
 [
   if (! yyinitGLRStack (yystackp, YYINITDEPTH))
     goto yyexhaustedlab;
@@ -2370,19 +2379,26 @@ b4_syncline([@oline@], [@ofile@])])dnl
                {
                  YYDPRINTF ((stderr, "Reading a token: "));
                  yychar = YYLEX;
+               }
+
+             if (yychar <= YYEOF)
+               {
+                 yychar = yytoken = YYEOF;
+                 YYDPRINTF ((stderr, "Now at end of input.\n"));
+               }
+             else
+               {
                  yytoken = YYTRANSLATE (yychar);
                  YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
                }
-             else
-               yytoken = YYTRANSLATE (yychar);
+
              yygetLRActions (yystate, yytoken, &yyaction, &yyconflicts);
              if (*yyconflicts != 0)
                break;
              if (yyisShiftAction (yyaction))
                {
                  YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
-                 if (yychar != YYEOF)
-                   yychar = YYEMPTY;
+                 yychar = YYEMPTY;
                  yyposn += 1;
                  yyglrShift (&yystack, 0, yyaction, yyposn, &yylval, &yylloc);
                  if (0 < yystack.yyerrState)
@@ -2498,7 +2514,7 @@ b4_syncline([@oline@], [@ofile@])])dnl
   goto yyreturn;
 
  yyreturn:
-  if (yychar != YYEOF && yychar != YYEMPTY)
+  if (yychar != YYEMPTY)
     yydestruct ("Cleanup: discarding lookahead",
                YYTRANSLATE (yychar),
                &yylval]b4_locations_if([, &yylloc])[]b4_user_args[);
@@ -2520,7 +2536,8 @@ b4_syncline([@oline@], [@ofile@])])dnl
                  {
                    yyGLRState *yys = yystates[yyk];
 ]b4_locations_if([[                yystack.yyerror_range[1].yystate.yyloc = yys->yyloc;]]
-)[                 yydestroyGLRState ("Cleanup: popping", yys]b4_user_args[);
+)[                 if (yys->yypred != NULL)
+                     yydestroyGLRState ("Cleanup: popping", yys]b4_user_args[);
                    yystates[yyk] = yys->yypred;
                    yystack.yynextFree -= 1;
                    yystack.yyspaceLeft += 1;
@@ -2609,8 +2626,12 @@ yypdumpstack (yyGLRStack* yystackp)
 ]
 
 b4_epilogue
-b4_defines_if(
-[@output @output_header_name@
+dnl
+dnl glr.cc produces its own header.
+dnl
+m4_if(b4_skeleton, ["glr.c"],
+[b4_defines_if(
+[@output(b4_spec_defines_file@)
 b4_copyright([Skeleton interface for Bison GLR parsers in C],
   [2002, 2003, 2004, 2005, 2006])
 
@@ -2621,4 +2642,5 @@ extern YYSTYPE b4_prefix[]lval;
 b4_locations_if([b4_pure_if([],
 [extern YYLTYPE b4_prefix[]lloc;])
 ])
-])
+])])
+m4_divert_pop(0)