[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
FYI: Token enums
From: |
Akim Demaille |
Subject: |
FYI: Token enums |
Date: |
03 May 2002 10:42:38 +0200 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Common Lisp) |
This is what Paul suggested. Robert, I think everybody agrees that
for C++, you ought to use a namespace with the parser class with const
ints instead of enums. Now that there is m4_map_sep, it should be
easy for you.
Here is a sample:
/tmp % cat foo.y nostromo 10:41
%token a b c d e f g
%%
exp: a;
%%
/tmp % ~/src/bison-exp/tests/bison foo.y -d nostromo 10:41
/tmp % cat foo.tab.h nostromo 10:42
#ifndef BISON_FOO_TAB_H
# define BISON_FOO_TAB_H
#ifndef YYTOKENTYPE
# if defined (__STDC__) || defined (__cplusplus)
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
enum a = 258,
enum b = 259,
enum c = 260,
enum d = 261,
enum e = 262,
enum f = 263,
enum g = 264
};
/* POSIX requires `int' for tokens in interfaces. */
# define YYTOKENTYPE int
# endif
#endif
#define a 258
#define b 259
#define c 260
#define d 261
#define e 262
#define f 263
#define g 264
#ifndef YYSTYPE
typedef int
yystype;
# define YYSTYPE yystype
#endif
extern YYSTYPE yylval;
#endif /* not BISON_FOO_TAB_H */
/tmp % nostromo 10:42
Index: ChangeLog
from Akim Demaille <address@hidden>
* data/bison.simple (b4_token_enum): New.
(b4_token_defines): Use it to output tokens both as #define and
enums.
Suggested by Paul Eggert.
* src/output.c (token_definitions_output): Don't output spurious
white spaces.
Index: TODO
===================================================================
RCS file: /cvsroot/bison/bison/TODO,v
retrieving revision 1.55
diff -u -u -r1.55 TODO
--- TODO 2 May 2002 16:12:35 -0000 1.55
+++ TODO 3 May 2002 08:40:20 -0000
@@ -34,26 +34,6 @@
PDP-10 ports :-) but they should probably be documented
somewhere.
-* Using enums instead of int for tokens.
-Paul suggests:
-
- #ifndef YYTOKENTYPE
- # if defined (__STDC__) || defined (__cplusplus)
- /* Put the tokens into the symbol table, so that GDB and other debuggers
- know about them. */
- enum yytokentype {
- FOO = 256,
- BAR,
- ...
- };
- /* POSIX requires `int' for tokens in interfaces. */
- # define YYTOKENTYPE int
- # endif
- #endif
- #define FOO 256
- #define BAR 257
- ...
-
* Output directory
Akim:
Index: data/bison.c++
===================================================================
RCS file: /cvsroot/bison/bison/data/bison.c++,v
retrieving revision 1.26
diff -u -u -r1.26 bison.c++
--- data/bison.c++ 2 May 2002 16:43:00 -0000 1.26
+++ data/bison.c++ 3 May 2002 08:40:20 -0000
@@ -50,8 +50,8 @@
[location_stack_@<:@m4_eval([$1 - $2])@:>@])
-# b4_token_defines(TOKEN-NAME, TOKEN-NUMBER)
-# ------------------------------------------
+# b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
+# -----------------------------------------
# Output the definition of this token as #define.
m4_define([b4_token_define],
[#define $1 $2
Index: data/bison.simple
===================================================================
RCS file: /cvsroot/bison/bison/data/bison.simple,v
retrieving revision 1.19
diff -u -u -r1.19 bison.simple
--- data/bison.simple 2 May 2002 16:40:54 -0000 1.19
+++ data/bison.simple 3 May 2002 08:40:20 -0000
@@ -70,19 +70,39 @@
[[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]], [_])])
-# b4_token_defines(TOKEN-NAME, TOKEN-NUMBER)
-# ------------------------------------------
+# b4_token_define(TOKEN-NAME, TOKEN-NUMBER)
+# -----------------------------------------
# Output the definition of this token as #define.
m4_define([b4_token_define],
[#define $1 $2
])
+# b4_token_enum(TOKEN-NAME, TOKEN-NUMBER)
+# ---------------------------------------
+# Output the definition of this token as an enum.
+m4_define([b4_token_enum],
+[enum $1 = $2])
+
+
# b4_token_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
# -------------------------------------------------------
# Output the definition of the tokens as #define.
m4_define([b4_token_defines],
-[m4_map([b4_token_define], address@hidden)])
+[#ifndef YYTOKENTYPE
+# if defined (__STDC__) || defined (__cplusplus)
+ /* Put the tokens into the symbol table, so that GDB and other debuggers
+ know about them. */
+ enum yytokentype {
+m4_map_sep([ b4_token_enum], [,
+],
+ address@hidden)
+ };
+ /* POSIX requires `int' for tokens in interfaces. */
+# define YYTOKENTYPE int
+# endif
+#endif
+m4_map([b4_token_define], address@hidden)])
m4_divert(0)dnl
Index: src/output.c
===================================================================
RCS file: /cvsroot/bison/bison/src/output.c,v
retrieving revision 1.149
diff -u -u -r1.149 output.c
--- src/output.c 2 May 2002 15:06:46 -0000 1.149
+++ src/output.c 3 May 2002 08:40:20 -0000
@@ -603,7 +603,7 @@
if (strchr (symbol->tag, '.') || strchr (symbol->tag, '$'))
continue;
- fprintf (out, "%s [[[%s]], [%d]]",
+ fprintf (out, "%s[[[%s]], [%d]]",
first ? "" : ",\n", symbol->tag, number);
first = 0;
}
- FYI: Token enums,
Akim Demaille <=