bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/8] yacc.c, glr.c: a better YY_LOCATION_PRINT


From: Akim Demaille
Subject: [PATCH 2/8] yacc.c, glr.c: a better YY_LOCATION_PRINT
Date: Tue, 6 Nov 2012 17:47:11 +0100

* data/c.m4 (b4_yy_location_print_define): New.
Now issues "short" locations, e.g., "1.1" instead of "1.1-1.1".
Was initially a function, but then we face "static but unused"
warnings.
Simpler as a macro.
* tests/local.at, data/glr.c, data/yacc.c: Use it instead of duplicating.
* tests/actions.at: Adjust expectations.
---
 data/c.m4        | 31 ++++++++++++++++++++++++++++++-
 data/glr.c       | 22 ++--------------------
 data/yacc.c      | 24 +++---------------------
 tests/actions.at | 34 +++++++++++++++++-----------------
 tests/local.at   |  9 +--------
 5 files changed, 53 insertions(+), 67 deletions(-)

diff --git a/data/c.m4 b/data/c.m4
index 136b46e..a1013cc 100644
--- a/data/c.m4
+++ b/data/c.m4
@@ -644,8 +644,37 @@ m4_define([b4_yylloc_default_define],
 #endif
 ]])
 
+# b4_yy_location_print_define
+# ---------------------------
+# Define YY_LOCATION_PRINT.
+m4_define([b4_yy_location_print_define],
+[b4_locations_if([[
+/* YY_LOCATION_PRINT -- Print the location on the stream.
+   This macro was not mandated originally: define only if we know
+   we won't break user code: when these are the locations we know.  */
+
+#ifndef YY_LOCATION_PRINT
+# if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
+#  define YY_LOCATION_PRINT(File, Loc)                                   \
+  do {                                                                   \
+    fprintf (File, "%d.%d", (Loc).first_line, (Loc).first_column);       \
+    if ((Loc).first_line < (Loc).last_line)                              \
+      fprintf (File, "-%d.%d", (Loc).last_line,  (Loc).last_column - 1); \
+    else if ((Loc).first_column < (Loc).last_column - 1)                 \
+      fprintf (File, "-%d", (Loc).last_column - 1);                      \
+  } while (0)
+# else
+#  define YY_LOCATION_PRINT(File, Loc) ((void) 0)
+# endif
+#endif]],
+[[/* This macro is provided for backward compatibility. */
+#ifndef YY_LOCATION_PRINT
+# define YY_LOCATION_PRINT(File, Loc) ((void) 0)
+#endif]])
+])
+
 # b4_yyloc_default_define
-# ------------------------
+# -----------------------
 # Define yyloc_default, which can be used to initialize location
 # variables.
 m4_define([b4_yyloc_default_define],
diff --git a/data/glr.c b/data/glr.c
index 89a76e3..a2992e0 100644
--- a/data/glr.c
+++ b/data/glr.c
@@ -475,28 +475,10 @@ static const ]b4_int_type_for([b4_stos])[ yystos[] =
 #define YYTERROR 1
 
 ]b4_locations_if([[
-#ifndef YYLLOC_DEFAULT
 ]b4_yylloc_default_define[
 # define YYRHSLOC(Rhs, K) ((Rhs)[K].yystate.yyloc)
-
-/* YY_LOCATION_PRINT -- Print the location on the stream.
-   This macro was not mandated originally: define only if we know
-   we won't break user code: when these are the locations we know.  */
-
-# define YY_LOCATION_PRINT(File, Loc)                   \
-    fprintf (File, "%d.%d-%d.%d",                       \
-             (Loc).first_line, (Loc).first_column,      \
-             (Loc).last_line,  (Loc).last_column)
-#endif
-]],[
-#ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(Current, Rhs, N) ((void) 0)
-#endif
-])[
-
-#ifndef YY_LOCATION_PRINT
-# define YY_LOCATION_PRINT(File, Loc) ((void) 0)
-#endif
+]])[
+]b4_yy_location_print_define[
 
 /* YYLEX -- calling `yylex' with the right arguments.  */
 #define YYLEX ]b4_c_function_call([yylex], [int], b4_lex_param)[
diff --git a/data/yacc.c b/data/yacc.c
index 77f86ec..e82264f 100644
--- a/data/yacc.c
+++ b/data/yacc.c
@@ -766,33 +766,15 @@ do                                                        
      \
     }                                                          \
 while (YYID (0))
 
-
+/* Error token number */
 #define YYTERROR       1
 #define YYERRCODE      256
 
 ]b4_locations_if([[
 ]b4_yylloc_default_define[
 #define YYRHSLOC(Rhs, K) ((Rhs)[K])
-
-/* YY_LOCATION_PRINT -- Print the location on the stream.
-   This macro was not mandated originally: define only if we know
-   we won't break user code: when these are the locations we know.  */
-
-#ifndef YY_LOCATION_PRINT
-# if defined ]b4_api_PREFIX[LTYPE_IS_TRIVIAL && ]b4_api_PREFIX[LTYPE_IS_TRIVIAL
-#  define YY_LOCATION_PRINT(File, Loc)                 \
-     fprintf (File, "%d.%d-%d.%d",                     \
-             (Loc).first_line, (Loc).first_column,     \
-             (Loc).last_line,  (Loc).last_column)
-# else
-#  define YY_LOCATION_PRINT(File, Loc) ((void) 0)
-# endif
-#endif]],
-[[/* This macro is provided for backward compatibility. */
-#ifndef YY_LOCATION_PRINT
-# define YY_LOCATION_PRINT(File, Loc) ((void) 0)
-#endif]])[
-
+]])[
+]b4_yy_location_print_define[
 
 /* YYLEX -- calling `yylex' with the right arguments.  */
 #ifdef YYLEX_PARAM
diff --git a/tests/actions.at b/tests/actions.at
index 2314c1c..fc70f48 100644
--- a/tests/actions.at
+++ b/tests/actions.at
@@ -651,29 +651,29 @@ AT_PARSER_CHECK([./input], 1,
 ]],
 [[Starting parse
 Entering state 0
-Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
-Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
+Reading a token: Next token is token 'a' (1.1: <> printer for 'a' @ 1)
+Shifting token 'a' (1.1: <> printer for 'a' @ 1)
 Entering state 1
-Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
-Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
+Reading a token: Next token is token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
+Shifting token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
 Entering state 3
-Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
-Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
+Reading a token: Next token is token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
+Shifting token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
 Entering state 5
-Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
-Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
+Reading a token: Next token is token 'd' (1.4: <> printer for 'd' @ 4)
+Shifting token 'd' (1.4: <> printer for 'd' @ 4)
 Entering state 6
 Reading a token: Now at end of input.
 1.5: syntax error, unexpected $end, expecting 'e'
-Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
+Error: popping token 'd' (1.4: <> printer for 'd' @ 4)
 Stack now 0 1 3 5
-Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
+Error: popping token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
 Stack now 0 1 3
-Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
+Error: popping token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
 Stack now 0 1
-Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
+Error: popping token 'a' (1.1: <> printer for 'a' @ 1)
 Stack now 0
-Cleanup: discarding lookahead token $end (1.5-1.5: )
+Cleanup: discarding lookahead token $end (1.5: )
 Stack now 0
 ]])
 
@@ -895,15 +895,15 @@ AT_PARSER_CHECK([./input$1], 0,
 [[Starting parse
 Entering state 0
 Reducing stack by rule 1 (line 46):
--> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
+-> $$ = nterm start (1.1: <]]kind[[> for 'S' @ 1)
 Stack now 0
 Entering state 1
 Reading a token: Now at end of input.
-Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
+Shifting token END (1.1: <]]kind[[> for 'E' @ 1)
 Entering state 2
 Stack now 0 1 2
-Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
-Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
+Cleanup: popping token END (1.1: <]]kind[[> for 'E' @ 1)
+Cleanup: popping nterm start (1.1: <]]kind[[> for 'S' @ 1)
 ]])
 
 m4_popdef([kind])
diff --git a/tests/local.at b/tests/local.at
index 2af7971..73680c7 100644
--- a/tests/local.at
+++ b/tests/local.at
@@ -386,14 +386,7 @@ static
               [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\), *], [
   YYUSE(\1);])dnl
 AT_YYERROR_SEES_LOC_IF([[
-  fprintf (stderr, "%d.%d",
-           ]AT_LOC_FIRST_LINE[, ]AT_LOC_FIRST_COLUMN[);
-  if (]AT_LOC_FIRST_LINE[ < ]AT_LOC_LAST_LINE[)
-    fprintf (stderr, "-%d.%d",
-             ]AT_LOC_LAST_LINE[,  ]AT_LOC_LAST_COLUMN[ - 1);
-  else if (]AT_LOC_FIRST_COLUMN[ < ]AT_LOC_LAST_COLUMN[ - 1)
-    fprintf (stderr, "-%d",
-             ]AT_LOC_LAST_COLUMN[ - 1);
+  YY_LOCATION_PRINT (stderr, ]AT_LOC[);
   fprintf (stderr, ": ");]])[
   fprintf (stderr, "%s\n", msg);
 }]],
-- 
1.8.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]