12d2dfa348b33c3cc9c0d112eb631d9b87aafd18
[platform/upstream/groff.git] / src / preproc / refer / label.cpp
1 /* A Bison parser, made by GNU Bison 3.0.2.  */
2
3 /* Bison implementation for Yacc-like parsers in C
4
5    Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* As a special exception, you may create a larger work that contains
21    part or all of the Bison parser skeleton and distribute that work
22    under terms of your choice, so long as that work isn't itself a
23    parser generator using the skeleton or a modified version thereof
24    as a parser skeleton.  Alternatively, if you modify or redistribute
25    the parser skeleton itself, you may (at your option) remove this
26    special exception, which will cause the skeleton and the resulting
27    Bison output files to be licensed under the GNU General Public
28    License without this special exception.
29
30    This special exception was added by the Free Software Foundation in
31    version 2.2 of Bison.  */
32
33 /* C LALR(1) parser skeleton written by Richard Stallman, by
34    simplifying the original so-called "semantic" parser.  */
35
36 /* All symbols defined below should begin with yy or YY, to avoid
37    infringing on user name space.  This should be done even for local
38    variables, as they might otherwise be expanded by user macros.
39    There are some unavoidable exceptions within include files to
40    define necessary library symbols; they are noted "INFRINGES ON
41    USER NAME SPACE" below.  */
42
43 /* Identify Bison output.  */
44 #define YYBISON 1
45
46 /* Bison version.  */
47 #define YYBISON_VERSION "3.0.2"
48
49 /* Skeleton name.  */
50 #define YYSKELETON_NAME "yacc.c"
51
52 /* Pure parsers.  */
53 #define YYPURE 0
54
55 /* Push parsers.  */
56 #define YYPUSH 0
57
58 /* Pull parsers.  */
59 #define YYPULL 1
60
61
62
63
64 /* Copy the first part of user declarations.  */
65 #line 20 "label.y" /* yacc.c:339  */
66
67
68 #include "refer.h"
69 #include "refid.h"
70 #include "ref.h"
71 #include "token.h"
72
73 int yylex();
74 void yyerror(const char *);
75 int yyparse();
76
77 static const char *format_serial(char c, int n);
78
79 struct label_info {
80   int start;
81   int length;
82   int count;
83   int total;
84   label_info(const string &);
85 };
86
87 label_info *lookup_label(const string &label);
88
89 struct expression {
90   enum {
91     // Does the tentative label depend on the reference?
92     CONTAINS_VARIABLE = 01, 
93     CONTAINS_STAR = 02,
94     CONTAINS_FORMAT = 04,
95     CONTAINS_AT = 010
96   };
97   virtual ~expression() { }
98   virtual void evaluate(int, const reference &, string &,
99                         substring_position &) = 0;
100   virtual unsigned analyze() { return 0; }
101 };
102
103 class at_expr : public expression {
104 public:
105   at_expr() { }
106   void evaluate(int, const reference &, string &, substring_position &);
107   unsigned analyze() { return CONTAINS_VARIABLE|CONTAINS_AT; }
108 };
109
110 class format_expr : public expression {
111   char type;
112   int width;
113   int first_number;
114 public:
115   format_expr(char c, int w = 0, int f = 1)
116     : type(c), width(w), first_number(f) { }
117   void evaluate(int, const reference &, string &, substring_position &);
118   unsigned analyze() { return CONTAINS_FORMAT; }
119 };
120
121 class field_expr : public expression {
122   int number;
123   char name;
124 public:
125   field_expr(char nm, int num) : number(num), name(nm) { }
126   void evaluate(int, const reference &, string &, substring_position &);
127   unsigned analyze() { return CONTAINS_VARIABLE; }
128 };
129
130 class literal_expr : public expression {
131   string s;
132 public:
133   literal_expr(const char *ptr, int len) : s(ptr, len) { }
134   void evaluate(int, const reference &, string &, substring_position &);
135 };
136
137 class unary_expr : public expression {
138 protected:
139   expression *expr;
140 public:
141   unary_expr(expression *e) : expr(e) { }
142   ~unary_expr() { delete expr; }
143   void evaluate(int, const reference &, string &, substring_position &) = 0;
144   unsigned analyze() { return expr ? expr->analyze() : 0; }
145 };
146
147 // This caches the analysis of an expression.
148
149 class analyzed_expr : public unary_expr {
150   unsigned flags;
151 public:
152   analyzed_expr(expression *);
153   void evaluate(int, const reference &, string &, substring_position &);
154   unsigned analyze() { return flags; }
155 };
156
157 class star_expr : public unary_expr {
158 public:
159   star_expr(expression *e) : unary_expr(e) { }
160   void evaluate(int, const reference &, string &, substring_position &);
161   unsigned analyze() {
162     return ((expr ? (expr->analyze() & ~CONTAINS_VARIABLE) : 0)
163             | CONTAINS_STAR);
164   }
165 };
166
167 typedef void map_func(const char *, const char *, string &);
168
169 class map_expr : public unary_expr {
170   map_func *func;
171 public:
172   map_expr(expression *e, map_func *f) : unary_expr(e), func(f) { }
173   void evaluate(int, const reference &, string &, substring_position &);
174 };
175   
176 typedef const char *extractor_func(const char *, const char *, const char **);
177
178 class extractor_expr : public unary_expr {
179   int part;
180   extractor_func *func;
181 public:
182   enum { BEFORE = +1, MATCH = 0, AFTER = -1 };
183   extractor_expr(expression *e, extractor_func *f, int pt)
184     : unary_expr(e), part(pt), func(f) { }
185   void evaluate(int, const reference &, string &, substring_position &);
186 };
187
188 class truncate_expr : public unary_expr {
189   int n;
190 public:
191   truncate_expr(expression *e, int i) : unary_expr(e), n(i) { } 
192   void evaluate(int, const reference &, string &, substring_position &);
193 };
194
195 class separator_expr : public unary_expr {
196 public:
197   separator_expr(expression *e) : unary_expr(e) { }
198   void evaluate(int, const reference &, string &, substring_position &);
199 };
200
201 class binary_expr : public expression {
202 protected:
203   expression *expr1;
204   expression *expr2;
205 public:
206   binary_expr(expression *e1, expression *e2) : expr1(e1), expr2(e2) { }
207   ~binary_expr() { delete expr1; delete expr2; }
208   void evaluate(int, const reference &, string &, substring_position &) = 0;
209   unsigned analyze() {
210     return (expr1 ? expr1->analyze() : 0) | (expr2 ? expr2->analyze() : 0);
211   }
212 };
213
214 class alternative_expr : public binary_expr {
215 public:
216   alternative_expr(expression *e1, expression *e2) : binary_expr(e1, e2) { }
217   void evaluate(int, const reference &, string &, substring_position &);
218 };
219
220 class list_expr : public binary_expr {
221 public:
222   list_expr(expression *e1, expression *e2) : binary_expr(e1, e2) { }
223   void evaluate(int, const reference &, string &, substring_position &);
224 };
225
226 class substitute_expr : public binary_expr {
227 public:
228   substitute_expr(expression *e1, expression *e2) : binary_expr(e1, e2) { }
229   void evaluate(int, const reference &, string &, substring_position &);
230 };
231
232 class ternary_expr : public expression {
233 protected:
234   expression *expr1;
235   expression *expr2;
236   expression *expr3;
237 public:
238   ternary_expr(expression *e1, expression *e2, expression *e3)
239     : expr1(e1), expr2(e2), expr3(e3) { }
240   ~ternary_expr() { delete expr1; delete expr2; delete expr3; }
241   void evaluate(int, const reference &, string &, substring_position &) = 0;
242   unsigned analyze() {
243     return ((expr1 ? expr1->analyze() : 0)
244             | (expr2 ? expr2->analyze() : 0)
245             | (expr3 ? expr3->analyze() : 0));
246   }
247 };
248
249 class conditional_expr : public ternary_expr {
250 public:
251   conditional_expr(expression *e1, expression *e2, expression *e3)
252     : ternary_expr(e1, e2, e3) { }
253   void evaluate(int, const reference &, string &, substring_position &);
254 };
255
256 static expression *parsed_label = 0;
257 static expression *parsed_date_label = 0;
258 static expression *parsed_short_label = 0;
259
260 static expression *parse_result;
261
262 string literals;
263
264
265 #line 266 "label.cpp" /* yacc.c:339  */
266
267 # ifndef YY_NULLPTR
268 #  if defined __cplusplus && 201103L <= __cplusplus
269 #   define YY_NULLPTR nullptr
270 #  else
271 #   define YY_NULLPTR 0
272 #  endif
273 # endif
274
275 /* Enabling verbose error messages.  */
276 #ifdef YYERROR_VERBOSE
277 # undef YYERROR_VERBOSE
278 # define YYERROR_VERBOSE 1
279 #else
280 # define YYERROR_VERBOSE 0
281 #endif
282
283
284 /* Debug traces.  */
285 #ifndef YYDEBUG
286 # define YYDEBUG 0
287 #endif
288 #if YYDEBUG
289 extern int yydebug;
290 #endif
291
292 /* Token type.  */
293 #ifndef YYTOKENTYPE
294 # define YYTOKENTYPE
295   enum yytokentype
296   {
297     TOKEN_LETTER = 258,
298     TOKEN_LITERAL = 259,
299     TOKEN_DIGIT = 260
300   };
301 #endif
302 /* Tokens.  */
303 #define TOKEN_LETTER 258
304 #define TOKEN_LITERAL 259
305 #define TOKEN_DIGIT 260
306
307 /* Value type.  */
308 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
309 typedef union YYSTYPE YYSTYPE;
310 union YYSTYPE
311 {
312 #line 220 "label.y" /* yacc.c:355  */
313
314   int num;
315   expression *expr;
316   struct { int ndigits; int val; } dig;
317   struct { int start; int len; } str;
318
319 #line 320 "label.cpp" /* yacc.c:355  */
320 };
321 # define YYSTYPE_IS_TRIVIAL 1
322 # define YYSTYPE_IS_DECLARED 1
323 #endif
324
325
326 extern YYSTYPE yylval;
327
328 int yyparse (void);
329
330
331
332 /* Copy the second part of user declarations.  */
333
334 #line 335 "label.cpp" /* yacc.c:358  */
335
336 #ifdef short
337 # undef short
338 #endif
339
340 #ifdef YYTYPE_UINT8
341 typedef YYTYPE_UINT8 yytype_uint8;
342 #else
343 typedef unsigned char yytype_uint8;
344 #endif
345
346 #ifdef YYTYPE_INT8
347 typedef YYTYPE_INT8 yytype_int8;
348 #else
349 typedef signed char yytype_int8;
350 #endif
351
352 #ifdef YYTYPE_UINT16
353 typedef YYTYPE_UINT16 yytype_uint16;
354 #else
355 typedef unsigned short int yytype_uint16;
356 #endif
357
358 #ifdef YYTYPE_INT16
359 typedef YYTYPE_INT16 yytype_int16;
360 #else
361 typedef short int yytype_int16;
362 #endif
363
364 #ifndef YYSIZE_T
365 # ifdef __SIZE_TYPE__
366 #  define YYSIZE_T __SIZE_TYPE__
367 # elif defined size_t
368 #  define YYSIZE_T size_t
369 # elif ! defined YYSIZE_T
370 #  include <stddef.h> /* INFRINGES ON USER NAME SPACE */
371 #  define YYSIZE_T size_t
372 # else
373 #  define YYSIZE_T unsigned int
374 # endif
375 #endif
376
377 #define YYSIZE_MAXIMUM ((YYSIZE_T) -1)
378
379 #ifndef YY_
380 # if defined YYENABLE_NLS && YYENABLE_NLS
381 #  if ENABLE_NLS
382 #   include <libintl.h> /* INFRINGES ON USER NAME SPACE */
383 #   define YY_(Msgid) dgettext ("bison-runtime", Msgid)
384 #  endif
385 # endif
386 # ifndef YY_
387 #  define YY_(Msgid) Msgid
388 # endif
389 #endif
390
391 #ifndef YY_ATTRIBUTE
392 # if (defined __GNUC__                                               \
393       && (2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)))  \
394      || defined __SUNPRO_C && 0x5110 <= __SUNPRO_C
395 #  define YY_ATTRIBUTE(Spec) __attribute__(Spec)
396 # else
397 #  define YY_ATTRIBUTE(Spec) /* empty */
398 # endif
399 #endif
400
401 #ifndef YY_ATTRIBUTE_PURE
402 # define YY_ATTRIBUTE_PURE   YY_ATTRIBUTE ((__pure__))
403 #endif
404
405 #ifndef YY_ATTRIBUTE_UNUSED
406 # define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE ((__unused__))
407 #endif
408
409 #if !defined _Noreturn \
410      && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112)
411 # if defined _MSC_VER && 1200 <= _MSC_VER
412 #  define _Noreturn __declspec (noreturn)
413 # else
414 #  define _Noreturn YY_ATTRIBUTE ((__noreturn__))
415 # endif
416 #endif
417
418 /* Suppress unused-variable warnings by "using" E.  */
419 #if ! defined lint || defined __GNUC__
420 # define YYUSE(E) ((void) (E))
421 #else
422 # define YYUSE(E) /* empty */
423 #endif
424
425 #if defined __GNUC__ && 407 <= __GNUC__ * 100 + __GNUC_MINOR__
426 /* Suppress an incorrect diagnostic about yylval being uninitialized.  */
427 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
428     _Pragma ("GCC diagnostic push") \
429     _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\
430     _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
431 # define YY_IGNORE_MAYBE_UNINITIALIZED_END \
432     _Pragma ("GCC diagnostic pop")
433 #else
434 # define YY_INITIAL_VALUE(Value) Value
435 #endif
436 #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
437 # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
438 # define YY_IGNORE_MAYBE_UNINITIALIZED_END
439 #endif
440 #ifndef YY_INITIAL_VALUE
441 # define YY_INITIAL_VALUE(Value) /* Nothing. */
442 #endif
443
444
445 #if ! defined yyoverflow || YYERROR_VERBOSE
446
447 /* The parser invokes alloca or malloc; define the necessary symbols.  */
448
449 # ifdef YYSTACK_USE_ALLOCA
450 #  if YYSTACK_USE_ALLOCA
451 #   ifdef __GNUC__
452 #    define YYSTACK_ALLOC __builtin_alloca
453 #   elif defined __BUILTIN_VA_ARG_INCR
454 #    include <alloca.h> /* INFRINGES ON USER NAME SPACE */
455 #   elif defined _AIX
456 #    define YYSTACK_ALLOC __alloca
457 #   elif defined _MSC_VER
458 #    include <malloc.h> /* INFRINGES ON USER NAME SPACE */
459 #    define alloca _alloca
460 #   else
461 #    define YYSTACK_ALLOC alloca
462 #    if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS
463 #     include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
464       /* Use EXIT_SUCCESS as a witness for stdlib.h.  */
465 #     ifndef EXIT_SUCCESS
466 #      define EXIT_SUCCESS 0
467 #     endif
468 #    endif
469 #   endif
470 #  endif
471 # endif
472
473 # ifdef YYSTACK_ALLOC
474    /* Pacify GCC's 'empty if-body' warning.  */
475 #  define YYSTACK_FREE(Ptr) do { /* empty */; } while (0)
476 #  ifndef YYSTACK_ALLOC_MAXIMUM
477     /* The OS might guarantee only one guard page at the bottom of the stack,
478        and a page size can be as small as 4096 bytes.  So we cannot safely
479        invoke alloca (N) if N exceeds 4096.  Use a slightly smaller number
480        to allow for a few compiler-allocated temporary stack slots.  */
481 #   define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
482 #  endif
483 # else
484 #  define YYSTACK_ALLOC YYMALLOC
485 #  define YYSTACK_FREE YYFREE
486 #  ifndef YYSTACK_ALLOC_MAXIMUM
487 #   define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
488 #  endif
489 #  if (defined __cplusplus && ! defined EXIT_SUCCESS \
490        && ! ((defined YYMALLOC || defined malloc) \
491              && (defined YYFREE || defined free)))
492 #   include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
493 #   ifndef EXIT_SUCCESS
494 #    define EXIT_SUCCESS 0
495 #   endif
496 #  endif
497 #  ifndef YYMALLOC
498 #   define YYMALLOC malloc
499 #   if ! defined malloc && ! defined EXIT_SUCCESS
500 void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
501 #   endif
502 #  endif
503 #  ifndef YYFREE
504 #   define YYFREE free
505 #   if ! defined free && ! defined EXIT_SUCCESS
506 void free (void *); /* INFRINGES ON USER NAME SPACE */
507 #   endif
508 #  endif
509 # endif
510 #endif /* ! defined yyoverflow || YYERROR_VERBOSE */
511
512
513 #if (! defined yyoverflow \
514      && (! defined __cplusplus \
515          || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
516
517 /* A type that is properly aligned for any stack member.  */
518 union yyalloc
519 {
520   yytype_int16 yyss_alloc;
521   YYSTYPE yyvs_alloc;
522 };
523
524 /* The size of the maximum gap between one aligned stack and the next.  */
525 # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
526
527 /* The size of an array large to enough to hold all stacks, each with
528    N elements.  */
529 # define YYSTACK_BYTES(N) \
530      ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
531       + YYSTACK_GAP_MAXIMUM)
532
533 # define YYCOPY_NEEDED 1
534
535 /* Relocate STACK from its old location to the new one.  The
536    local variables YYSIZE and YYSTACKSIZE give the old and new number of
537    elements in the stack, and YYPTR gives the new location of the
538    stack.  Advance YYPTR to a properly aligned location for the next
539    stack.  */
540 # define YYSTACK_RELOCATE(Stack_alloc, Stack)                           \
541     do                                                                  \
542       {                                                                 \
543         YYSIZE_T yynewbytes;                                            \
544         YYCOPY (&yyptr->Stack_alloc, Stack, yysize);                    \
545         Stack = &yyptr->Stack_alloc;                                    \
546         yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
547         yyptr += yynewbytes / sizeof (*yyptr);                          \
548       }                                                                 \
549     while (0)
550
551 #endif
552
553 #if defined YYCOPY_NEEDED && YYCOPY_NEEDED
554 /* Copy COUNT objects from SRC to DST.  The source and destination do
555    not overlap.  */
556 # ifndef YYCOPY
557 #  if defined __GNUC__ && 1 < __GNUC__
558 #   define YYCOPY(Dst, Src, Count) \
559       __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src)))
560 #  else
561 #   define YYCOPY(Dst, Src, Count)              \
562       do                                        \
563         {                                       \
564           YYSIZE_T yyi;                         \
565           for (yyi = 0; yyi < (Count); yyi++)   \
566             (Dst)[yyi] = (Src)[yyi];            \
567         }                                       \
568       while (0)
569 #  endif
570 # endif
571 #endif /* !YYCOPY_NEEDED */
572
573 /* YYFINAL -- State number of the termination state.  */
574 #define YYFINAL  21
575 /* YYLAST -- Last index in YYTABLE.  */
576 #define YYLAST   39
577
578 /* YYNTOKENS -- Number of terminals.  */
579 #define YYNTOKENS  21
580 /* YYNNTS -- Number of nonterminals.  */
581 #define YYNNTS  12
582 /* YYNRULES -- Number of rules.  */
583 #define YYNRULES  34
584 /* YYNSTATES -- Number of states.  */
585 #define YYNSTATES  49
586
587 /* YYTRANSLATE[YYX] -- Symbol number corresponding to YYX as returned
588    by yylex, with out-of-bounds checking.  */
589 #define YYUNDEFTOK  2
590 #define YYMAXUTOK   260
591
592 #define YYTRANSLATE(YYX)                                                \
593   ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
594
595 /* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM
596    as returned by yylex, without out-of-bounds checking.  */
597 static const yytype_uint8 yytranslate[] =
598 {
599        0,     2,     2,     2,     2,     2,     2,     2,     2,     2,
600        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
601        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
602        2,     2,     2,     2,     2,     2,     2,    12,     9,     2,
603       17,    18,    16,    14,     2,    15,    13,     2,     2,     2,
604        2,     2,     2,     2,     2,     2,     2,     2,     7,     2,
605       19,     2,    20,     6,    11,     2,     2,     2,     2,     2,
606        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
607        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
608        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
609        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
610        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
611        2,     2,     2,     2,     8,     2,    10,     2,     2,     2,
612        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
613        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
614        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
615        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
616        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
617        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
618        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
619        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
620        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
621        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
622        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
623        2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
624        2,     2,     2,     2,     2,     2,     1,     2,     3,     4,
625        5
626 };
627
628 #if YYDEBUG
629   /* YYRLINE[YYN] -- Source line where rule number YYN was defined.  */
630 static const yytype_uint16 yyrline[] =
631 {
632        0,   248,   248,   253,   255,   261,   262,   267,   269,   271,
633      276,   278,   283,   285,   290,   292,   297,   299,   301,   317,
634      321,   352,   354,   356,   358,   360,   366,   367,   372,   374,
635      379,   381,   388,   389,   391
636 };
637 #endif
638
639 #if YYDEBUG || YYERROR_VERBOSE || 0
640 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
641    First, the terminals, then, starting at YYNTOKENS, nonterminals.  */
642 static const char *const yytname[] =
643 {
644   "$end", "error", "$undefined", "TOKEN_LETTER", "TOKEN_LITERAL",
645   "TOKEN_DIGIT", "'?'", "':'", "'|'", "'&'", "'~'", "'@'", "'%'", "'.'",
646   "'+'", "'-'", "'*'", "'('", "')'", "'<'", "'>'", "$accept", "expr",
647   "conditional", "optional_conditional", "alternative", "list",
648   "substitute", "string", "optional_number", "number", "digits", "flag", YY_NULLPTR
649 };
650 #endif
651
652 # ifdef YYPRINT
653 /* YYTOKNUM[NUM] -- (External) token number corresponding to the
654    (internal) symbol number NUM (which must be that of a token).  */
655 static const yytype_uint16 yytoknum[] =
656 {
657        0,   256,   257,   258,   259,   260,    63,    58,   124,    38,
658      126,    64,    37,    46,    43,    45,    42,    40,    41,    60,
659       62
660 };
661 # endif
662
663 #define YYPACT_NINF -26
664
665 #define yypact_value_is_default(Yystate) \
666   (!!((Yystate) == (-26)))
667
668 #define YYTABLE_NINF -1
669
670 #define yytable_value_is_error(Yytable_value) \
671   0
672
673   /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
674      STATE-NUM.  */
675 static const yytype_int8 yypact[] =
676 {
677        2,    11,   -26,   -26,    12,     2,     2,    24,   -26,   -26,
678       21,     2,    18,    -6,   -26,    26,   -26,   -26,    27,    15,
679       14,   -26,     2,     2,     2,    18,     2,    -3,    11,    11,
680      -26,   -26,   -26,   -26,   -26,    28,     2,     2,    -6,   -26,
681      -26,    33,    26,    26,     2,    11,   -26,   -26,    26
682 };
683
684   /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
685      Performed when YYTABLE does not specify something else to do.  Zero
686      means the default is an error.  */
687 static const yytype_uint8 yydefact[] =
688 {
689        5,    16,    15,    14,     0,     5,     5,     0,     6,     2,
690        3,     7,    10,    12,    28,    17,    18,    30,    19,     0,
691        0,     1,     5,     0,     0,    11,     0,    32,     0,     0,
692       23,    29,    31,    24,    25,     0,     8,     9,    13,    33,
693       34,     0,    21,    22,     0,    26,     4,    20,    27
694 };
695
696   /* YYPGOTO[NTERM-NUM].  */
697 static const yytype_int8 yypgoto[] =
698 {
699      -26,   -26,    -7,    -4,   -26,    -1,   -11,    13,   -26,   -25,
700      -26,   -26
701 };
702
703   /* YYDEFGOTO[NTERM-NUM].  */
704 static const yytype_int8 yydefgoto[] =
705 {
706       -1,     7,     8,     9,    10,    11,    12,    13,    47,    15,
707       18,    41
708 };
709
710   /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM.  If
711      positive, shift that token.  If negative, reduce the rule whose
712      number is the opposite.  If YYTABLE_NINF, syntax error.  */
713 static const yytype_uint8 yytable[] =
714 {
715       25,    19,    20,    42,    43,     1,     2,    27,    28,    29,
716       30,    39,    40,     3,     4,    16,    14,    17,    35,     5,
717       48,     6,    36,    37,    21,    25,    25,    22,    26,    23,
718       24,    31,    32,    33,    34,    44,    45,    46,     0,    38
719 };
720
721 static const yytype_int8 yycheck[] =
722 {
723       11,     5,     6,    28,    29,     3,     4,    13,    14,    15,
724       16,    14,    15,    11,    12,     3,     5,     5,    22,    17,
725       45,    19,    23,    24,     0,    36,    37,     6,    10,     8,
726        9,     5,     5,    18,    20,     7,     3,    44,    -1,    26
727 };
728
729   /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
730      symbol of state STATE-NUM.  */
731 static const yytype_uint8 yystos[] =
732 {
733        0,     3,     4,    11,    12,    17,    19,    22,    23,    24,
734       25,    26,    27,    28,     5,    30,     3,     5,    31,    24,
735       24,     0,     6,     8,     9,    27,    10,    13,    14,    15,
736       16,     5,     5,    18,    20,    24,    26,    26,    28,    14,
737       15,    32,    30,    30,     7,     3,    23,    29,    30
738 };
739
740   /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
741 static const yytype_uint8 yyr1[] =
742 {
743        0,    21,    22,    23,    23,    24,    24,    25,    25,    25,
744       26,    26,    27,    27,    28,    28,    28,    28,    28,    28,
745       28,    28,    28,    28,    28,    28,    29,    29,    30,    30,
746       31,    31,    32,    32,    32
747 };
748
749   /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.  */
750 static const yytype_uint8 yyr2[] =
751 {
752        0,     2,     1,     1,     5,     0,     1,     1,     3,     3,
753        1,     2,     1,     3,     1,     1,     1,     2,     2,     2,
754        5,     3,     3,     2,     3,     3,     0,     1,     1,     2,
755        1,     2,     0,     1,     1
756 };
757
758
759 #define yyerrok         (yyerrstatus = 0)
760 #define yyclearin       (yychar = YYEMPTY)
761 #define YYEMPTY         (-2)
762 #define YYEOF           0
763
764 #define YYACCEPT        goto yyacceptlab
765 #define YYABORT         goto yyabortlab
766 #define YYERROR         goto yyerrorlab
767
768
769 #define YYRECOVERING()  (!!yyerrstatus)
770
771 #define YYBACKUP(Token, Value)                                  \
772 do                                                              \
773   if (yychar == YYEMPTY)                                        \
774     {                                                           \
775       yychar = (Token);                                         \
776       yylval = (Value);                                         \
777       YYPOPSTACK (yylen);                                       \
778       yystate = *yyssp;                                         \
779       goto yybackup;                                            \
780     }                                                           \
781   else                                                          \
782     {                                                           \
783       yyerror (YY_("syntax error: cannot back up")); \
784       YYERROR;                                                  \
785     }                                                           \
786 while (0)
787
788 /* Error token number */
789 #define YYTERROR        1
790 #define YYERRCODE       256
791
792
793
794 /* Enable debugging if requested.  */
795 #if YYDEBUG
796
797 # ifndef YYFPRINTF
798 #  include <stdio.h> /* INFRINGES ON USER NAME SPACE */
799 #  define YYFPRINTF fprintf
800 # endif
801
802 # define YYDPRINTF(Args)                        \
803 do {                                            \
804   if (yydebug)                                  \
805     YYFPRINTF Args;                             \
806 } while (0)
807
808 /* This macro is provided for backward compatibility. */
809 #ifndef YY_LOCATION_PRINT
810 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
811 #endif
812
813
814 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)                    \
815 do {                                                                      \
816   if (yydebug)                                                            \
817     {                                                                     \
818       YYFPRINTF (stderr, "%s ", Title);                                   \
819       yy_symbol_print (stderr,                                            \
820                   Type, Value); \
821       YYFPRINTF (stderr, "\n");                                           \
822     }                                                                     \
823 } while (0)
824
825
826 /*----------------------------------------.
827 | Print this symbol's value on YYOUTPUT.  |
828 `----------------------------------------*/
829
830 static void
831 yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
832 {
833   FILE *yyo = yyoutput;
834   YYUSE (yyo);
835   if (!yyvaluep)
836     return;
837 # ifdef YYPRINT
838   if (yytype < YYNTOKENS)
839     YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
840 # endif
841   YYUSE (yytype);
842 }
843
844
845 /*--------------------------------.
846 | Print this symbol on YYOUTPUT.  |
847 `--------------------------------*/
848
849 static void
850 yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
851 {
852   YYFPRINTF (yyoutput, "%s %s (",
853              yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]);
854
855   yy_symbol_value_print (yyoutput, yytype, yyvaluep);
856   YYFPRINTF (yyoutput, ")");
857 }
858
859 /*------------------------------------------------------------------.
860 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
861 | TOP (included).                                                   |
862 `------------------------------------------------------------------*/
863
864 static void
865 yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop)
866 {
867   YYFPRINTF (stderr, "Stack now");
868   for (; yybottom <= yytop; yybottom++)
869     {
870       int yybot = *yybottom;
871       YYFPRINTF (stderr, " %d", yybot);
872     }
873   YYFPRINTF (stderr, "\n");
874 }
875
876 # define YY_STACK_PRINT(Bottom, Top)                            \
877 do {                                                            \
878   if (yydebug)                                                  \
879     yy_stack_print ((Bottom), (Top));                           \
880 } while (0)
881
882
883 /*------------------------------------------------.
884 | Report that the YYRULE is going to be reduced.  |
885 `------------------------------------------------*/
886
887 static void
888 yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, int yyrule)
889 {
890   unsigned long int yylno = yyrline[yyrule];
891   int yynrhs = yyr2[yyrule];
892   int yyi;
893   YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n",
894              yyrule - 1, yylno);
895   /* The symbols being reduced.  */
896   for (yyi = 0; yyi < yynrhs; yyi++)
897     {
898       YYFPRINTF (stderr, "   $%d = ", yyi + 1);
899       yy_symbol_print (stderr,
900                        yystos[yyssp[yyi + 1 - yynrhs]],
901                        &(yyvsp[(yyi + 1) - (yynrhs)])
902                                               );
903       YYFPRINTF (stderr, "\n");
904     }
905 }
906
907 # define YY_REDUCE_PRINT(Rule)          \
908 do {                                    \
909   if (yydebug)                          \
910     yy_reduce_print (yyssp, yyvsp, Rule); \
911 } while (0)
912
913 /* Nonzero means print parse trace.  It is left uninitialized so that
914    multiple parsers can coexist.  */
915 int yydebug;
916 #else /* !YYDEBUG */
917 # define YYDPRINTF(Args)
918 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
919 # define YY_STACK_PRINT(Bottom, Top)
920 # define YY_REDUCE_PRINT(Rule)
921 #endif /* !YYDEBUG */
922
923
924 /* YYINITDEPTH -- initial size of the parser's stacks.  */
925 #ifndef YYINITDEPTH
926 # define YYINITDEPTH 200
927 #endif
928
929 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
930    if the built-in stack extension method is used).
931
932    Do not make this value too large; the results are undefined if
933    YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
934    evaluated with infinite-precision integer arithmetic.  */
935
936 #ifndef YYMAXDEPTH
937 # define YYMAXDEPTH 10000
938 #endif
939
940
941 #if YYERROR_VERBOSE
942
943 # ifndef yystrlen
944 #  if defined __GLIBC__ && defined _STRING_H
945 #   define yystrlen strlen
946 #  else
947 /* Return the length of YYSTR.  */
948 static YYSIZE_T
949 yystrlen (const char *yystr)
950 {
951   YYSIZE_T yylen;
952   for (yylen = 0; yystr[yylen]; yylen++)
953     continue;
954   return yylen;
955 }
956 #  endif
957 # endif
958
959 # ifndef yystpcpy
960 #  if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
961 #   define yystpcpy stpcpy
962 #  else
963 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
964    YYDEST.  */
965 static char *
966 yystpcpy (char *yydest, const char *yysrc)
967 {
968   char *yyd = yydest;
969   const char *yys = yysrc;
970
971   while ((*yyd++ = *yys++) != '\0')
972     continue;
973
974   return yyd - 1;
975 }
976 #  endif
977 # endif
978
979 # ifndef yytnamerr
980 /* Copy to YYRES the contents of YYSTR after stripping away unnecessary
981    quotes and backslashes, so that it's suitable for yyerror.  The
982    heuristic is that double-quoting is unnecessary unless the string
983    contains an apostrophe, a comma, or backslash (other than
984    backslash-backslash).  YYSTR is taken from yytname.  If YYRES is
985    null, do not copy; instead, return the length of what the result
986    would have been.  */
987 static YYSIZE_T
988 yytnamerr (char *yyres, const char *yystr)
989 {
990   if (*yystr == '"')
991     {
992       YYSIZE_T yyn = 0;
993       char const *yyp = yystr;
994
995       for (;;)
996         switch (*++yyp)
997           {
998           case '\'':
999           case ',':
1000             goto do_not_strip_quotes;
1001
1002           case '\\':
1003             if (*++yyp != '\\')
1004               goto do_not_strip_quotes;
1005             /* Fall through.  */
1006           default:
1007             if (yyres)
1008               yyres[yyn] = *yyp;
1009             yyn++;
1010             break;
1011
1012           case '"':
1013             if (yyres)
1014               yyres[yyn] = '\0';
1015             return yyn;
1016           }
1017     do_not_strip_quotes: ;
1018     }
1019
1020   if (! yyres)
1021     return yystrlen (yystr);
1022
1023   return yystpcpy (yyres, yystr) - yyres;
1024 }
1025 # endif
1026
1027 /* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message
1028    about the unexpected token YYTOKEN for the state stack whose top is
1029    YYSSP.
1030
1031    Return 0 if *YYMSG was successfully written.  Return 1 if *YYMSG is
1032    not large enough to hold the message.  In that case, also set
1033    *YYMSG_ALLOC to the required number of bytes.  Return 2 if the
1034    required number of bytes is too large to store.  */
1035 static int
1036 yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
1037                 yytype_int16 *yyssp, int yytoken)
1038 {
1039   YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]);
1040   YYSIZE_T yysize = yysize0;
1041   enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
1042   /* Internationalized format string. */
1043   const char *yyformat = YY_NULLPTR;
1044   /* Arguments of yyformat. */
1045   char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
1046   /* Number of reported tokens (one for the "unexpected", one per
1047      "expected"). */
1048   int yycount = 0;
1049
1050   /* There are many possibilities here to consider:
1051      - If this state is a consistent state with a default action, then
1052        the only way this function was invoked is if the default action
1053        is an error action.  In that case, don't check for expected
1054        tokens because there are none.
1055      - The only way there can be no lookahead present (in yychar) is if
1056        this state is a consistent state with a default action.  Thus,
1057        detecting the absence of a lookahead is sufficient to determine
1058        that there is no unexpected or expected token to report.  In that
1059        case, just report a simple "syntax error".
1060      - Don't assume there isn't a lookahead just because this state is a
1061        consistent state with a default action.  There might have been a
1062        previous inconsistent state, consistent state with a non-default
1063        action, or user semantic action that manipulated yychar.
1064      - Of course, the expected token list depends on states to have
1065        correct lookahead information, and it depends on the parser not
1066        to perform extra reductions after fetching a lookahead from the
1067        scanner and before detecting a syntax error.  Thus, state merging
1068        (from LALR or IELR) and default reductions corrupt the expected
1069        token list.  However, the list is correct for canonical LR with
1070        one exception: it will still contain any token that will not be
1071        accepted due to an error action in a later state.
1072   */
1073   if (yytoken != YYEMPTY)
1074     {
1075       int yyn = yypact[*yyssp];
1076       yyarg[yycount++] = yytname[yytoken];
1077       if (!yypact_value_is_default (yyn))
1078         {
1079           /* Start YYX at -YYN if negative to avoid negative indexes in
1080              YYCHECK.  In other words, skip the first -YYN actions for
1081              this state because they are default actions.  */
1082           int yyxbegin = yyn < 0 ? -yyn : 0;
1083           /* Stay within bounds of both yycheck and yytname.  */
1084           int yychecklim = YYLAST - yyn + 1;
1085           int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
1086           int yyx;
1087
1088           for (yyx = yyxbegin; yyx < yyxend; ++yyx)
1089             if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR
1090                 && !yytable_value_is_error (yytable[yyx + yyn]))
1091               {
1092                 if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
1093                   {
1094                     yycount = 1;
1095                     yysize = yysize0;
1096                     break;
1097                   }
1098                 yyarg[yycount++] = yytname[yyx];
1099                 {
1100                   YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, yytname[yyx]);
1101                   if (! (yysize <= yysize1
1102                          && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1103                     return 2;
1104                   yysize = yysize1;
1105                 }
1106               }
1107         }
1108     }
1109
1110   switch (yycount)
1111     {
1112 # define YYCASE_(N, S)                      \
1113       case N:                               \
1114         yyformat = S;                       \
1115       break
1116       YYCASE_(0, YY_("syntax error"));
1117       YYCASE_(1, YY_("syntax error, unexpected %s"));
1118       YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
1119       YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
1120       YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
1121       YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
1122 # undef YYCASE_
1123     }
1124
1125   {
1126     YYSIZE_T yysize1 = yysize + yystrlen (yyformat);
1127     if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
1128       return 2;
1129     yysize = yysize1;
1130   }
1131
1132   if (*yymsg_alloc < yysize)
1133     {
1134       *yymsg_alloc = 2 * yysize;
1135       if (! (yysize <= *yymsg_alloc
1136              && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM))
1137         *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM;
1138       return 1;
1139     }
1140
1141   /* Avoid sprintf, as that infringes on the user's name space.
1142      Don't have undefined behavior even if the translation
1143      produced a string with the wrong number of "%s"s.  */
1144   {
1145     char *yyp = *yymsg;
1146     int yyi = 0;
1147     while ((*yyp = *yyformat) != '\0')
1148       if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount)
1149         {
1150           yyp += yytnamerr (yyp, yyarg[yyi++]);
1151           yyformat += 2;
1152         }
1153       else
1154         {
1155           yyp++;
1156           yyformat++;
1157         }
1158   }
1159   return 0;
1160 }
1161 #endif /* YYERROR_VERBOSE */
1162
1163 /*-----------------------------------------------.
1164 | Release the memory associated to this symbol.  |
1165 `-----------------------------------------------*/
1166
1167 static void
1168 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep)
1169 {
1170   YYUSE (yyvaluep);
1171   if (!yymsg)
1172     yymsg = "Deleting";
1173   YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
1174
1175   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1176   YYUSE (yytype);
1177   YY_IGNORE_MAYBE_UNINITIALIZED_END
1178 }
1179
1180
1181
1182
1183 /* The lookahead symbol.  */
1184 int yychar;
1185
1186 /* The semantic value of the lookahead symbol.  */
1187 YYSTYPE yylval;
1188 /* Number of syntax errors so far.  */
1189 int yynerrs;
1190
1191
1192 /*----------.
1193 | yyparse.  |
1194 `----------*/
1195
1196 int
1197 yyparse (void)
1198 {
1199     int yystate;
1200     /* Number of tokens to shift before error messages enabled.  */
1201     int yyerrstatus;
1202
1203     /* The stacks and their tools:
1204        'yyss': related to states.
1205        'yyvs': related to semantic values.
1206
1207        Refer to the stacks through separate pointers, to allow yyoverflow
1208        to reallocate them elsewhere.  */
1209
1210     /* The state stack.  */
1211     yytype_int16 yyssa[YYINITDEPTH];
1212     yytype_int16 *yyss;
1213     yytype_int16 *yyssp;
1214
1215     /* The semantic value stack.  */
1216     YYSTYPE yyvsa[YYINITDEPTH];
1217     YYSTYPE *yyvs;
1218     YYSTYPE *yyvsp;
1219
1220     YYSIZE_T yystacksize;
1221
1222   int yyn;
1223   int yyresult;
1224   /* Lookahead token as an internal (translated) token number.  */
1225   int yytoken = 0;
1226   /* The variables used to return semantic value and location from the
1227      action routines.  */
1228   YYSTYPE yyval;
1229
1230 #if YYERROR_VERBOSE
1231   /* Buffer for error messages, and its allocated size.  */
1232   char yymsgbuf[128];
1233   char *yymsg = yymsgbuf;
1234   YYSIZE_T yymsg_alloc = sizeof yymsgbuf;
1235 #endif
1236
1237 #define YYPOPSTACK(N)   (yyvsp -= (N), yyssp -= (N))
1238
1239   /* The number of symbols on the RHS of the reduced rule.
1240      Keep to zero when no symbol should be popped.  */
1241   int yylen = 0;
1242
1243   yyssp = yyss = yyssa;
1244   yyvsp = yyvs = yyvsa;
1245   yystacksize = YYINITDEPTH;
1246
1247   YYDPRINTF ((stderr, "Starting parse\n"));
1248
1249   yystate = 0;
1250   yyerrstatus = 0;
1251   yynerrs = 0;
1252   yychar = YYEMPTY; /* Cause a token to be read.  */
1253   goto yysetstate;
1254
1255 /*------------------------------------------------------------.
1256 | yynewstate -- Push a new state, which is found in yystate.  |
1257 `------------------------------------------------------------*/
1258  yynewstate:
1259   /* In all cases, when you get here, the value and location stacks
1260      have just been pushed.  So pushing a state here evens the stacks.  */
1261   yyssp++;
1262
1263  yysetstate:
1264   *yyssp = yystate;
1265
1266   if (yyss + yystacksize - 1 <= yyssp)
1267     {
1268       /* Get the current used size of the three stacks, in elements.  */
1269       YYSIZE_T yysize = yyssp - yyss + 1;
1270
1271 #ifdef yyoverflow
1272       {
1273         /* Give user a chance to reallocate the stack.  Use copies of
1274            these so that the &'s don't force the real ones into
1275            memory.  */
1276         YYSTYPE *yyvs1 = yyvs;
1277         yytype_int16 *yyss1 = yyss;
1278
1279         /* Each stack pointer address is followed by the size of the
1280            data in use in that stack, in bytes.  This used to be a
1281            conditional around just the two extra args, but that might
1282            be undefined if yyoverflow is a macro.  */
1283         yyoverflow (YY_("memory exhausted"),
1284                     &yyss1, yysize * sizeof (*yyssp),
1285                     &yyvs1, yysize * sizeof (*yyvsp),
1286                     &yystacksize);
1287
1288         yyss = yyss1;
1289         yyvs = yyvs1;
1290       }
1291 #else /* no yyoverflow */
1292 # ifndef YYSTACK_RELOCATE
1293       goto yyexhaustedlab;
1294 # else
1295       /* Extend the stack our own way.  */
1296       if (YYMAXDEPTH <= yystacksize)
1297         goto yyexhaustedlab;
1298       yystacksize *= 2;
1299       if (YYMAXDEPTH < yystacksize)
1300         yystacksize = YYMAXDEPTH;
1301
1302       {
1303         yytype_int16 *yyss1 = yyss;
1304         union yyalloc *yyptr =
1305           (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
1306         if (! yyptr)
1307           goto yyexhaustedlab;
1308         YYSTACK_RELOCATE (yyss_alloc, yyss);
1309         YYSTACK_RELOCATE (yyvs_alloc, yyvs);
1310 #  undef YYSTACK_RELOCATE
1311         if (yyss1 != yyssa)
1312           YYSTACK_FREE (yyss1);
1313       }
1314 # endif
1315 #endif /* no yyoverflow */
1316
1317       yyssp = yyss + yysize - 1;
1318       yyvsp = yyvs + yysize - 1;
1319
1320       YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1321                   (unsigned long int) yystacksize));
1322
1323       if (yyss + yystacksize - 1 <= yyssp)
1324         YYABORT;
1325     }
1326
1327   YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1328
1329   if (yystate == YYFINAL)
1330     YYACCEPT;
1331
1332   goto yybackup;
1333
1334 /*-----------.
1335 | yybackup.  |
1336 `-----------*/
1337 yybackup:
1338
1339   /* Do appropriate processing given the current state.  Read a
1340      lookahead token if we need one and don't already have one.  */
1341
1342   /* First try to decide what to do without reference to lookahead token.  */
1343   yyn = yypact[yystate];
1344   if (yypact_value_is_default (yyn))
1345     goto yydefault;
1346
1347   /* Not known => get a lookahead token if don't already have one.  */
1348
1349   /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol.  */
1350   if (yychar == YYEMPTY)
1351     {
1352       YYDPRINTF ((stderr, "Reading a token: "));
1353       yychar = yylex ();
1354     }
1355
1356   if (yychar <= YYEOF)
1357     {
1358       yychar = yytoken = YYEOF;
1359       YYDPRINTF ((stderr, "Now at end of input.\n"));
1360     }
1361   else
1362     {
1363       yytoken = YYTRANSLATE (yychar);
1364       YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
1365     }
1366
1367   /* If the proper action on seeing token YYTOKEN is to reduce or to
1368      detect an error, take that action.  */
1369   yyn += yytoken;
1370   if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
1371     goto yydefault;
1372   yyn = yytable[yyn];
1373   if (yyn <= 0)
1374     {
1375       if (yytable_value_is_error (yyn))
1376         goto yyerrlab;
1377       yyn = -yyn;
1378       goto yyreduce;
1379     }
1380
1381   /* Count tokens shifted since error; after three, turn off error
1382      status.  */
1383   if (yyerrstatus)
1384     yyerrstatus--;
1385
1386   /* Shift the lookahead token.  */
1387   YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
1388
1389   /* Discard the shifted token.  */
1390   yychar = YYEMPTY;
1391
1392   yystate = yyn;
1393   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1394   *++yyvsp = yylval;
1395   YY_IGNORE_MAYBE_UNINITIALIZED_END
1396
1397   goto yynewstate;
1398
1399
1400 /*-----------------------------------------------------------.
1401 | yydefault -- do the default action for the current state.  |
1402 `-----------------------------------------------------------*/
1403 yydefault:
1404   yyn = yydefact[yystate];
1405   if (yyn == 0)
1406     goto yyerrlab;
1407   goto yyreduce;
1408
1409
1410 /*-----------------------------.
1411 | yyreduce -- Do a reduction.  |
1412 `-----------------------------*/
1413 yyreduce:
1414   /* yyn is the number of a rule to reduce with.  */
1415   yylen = yyr2[yyn];
1416
1417   /* If YYLEN is nonzero, implement the default value of the action:
1418      '$$ = $1'.
1419
1420      Otherwise, the following line sets YYVAL to garbage.
1421      This behavior is undocumented and Bison
1422      users should not rely upon it.  Assigning to YYVAL
1423      unconditionally makes the parser a bit smaller, and it avoids a
1424      GCC warning that YYVAL may be used uninitialized.  */
1425   yyval = yyvsp[1-yylen];
1426
1427
1428   YY_REDUCE_PRINT (yyn);
1429   switch (yyn)
1430     {
1431         case 2:
1432 #line 249 "label.y" /* yacc.c:1646  */
1433     { parse_result = ((yyvsp[0].expr) ? new analyzed_expr((yyvsp[0].expr)) : 0); }
1434 #line 1435 "label.cpp" /* yacc.c:1646  */
1435     break;
1436
1437   case 3:
1438 #line 254 "label.y" /* yacc.c:1646  */
1439     { (yyval.expr) = (yyvsp[0].expr); }
1440 #line 1441 "label.cpp" /* yacc.c:1646  */
1441     break;
1442
1443   case 4:
1444 #line 256 "label.y" /* yacc.c:1646  */
1445     { (yyval.expr) = new conditional_expr((yyvsp[-4].expr), (yyvsp[-2].expr), (yyvsp[0].expr)); }
1446 #line 1447 "label.cpp" /* yacc.c:1646  */
1447     break;
1448
1449   case 5:
1450 #line 261 "label.y" /* yacc.c:1646  */
1451     { (yyval.expr) = 0; }
1452 #line 1453 "label.cpp" /* yacc.c:1646  */
1453     break;
1454
1455   case 6:
1456 #line 263 "label.y" /* yacc.c:1646  */
1457     { (yyval.expr) = (yyvsp[0].expr); }
1458 #line 1459 "label.cpp" /* yacc.c:1646  */
1459     break;
1460
1461   case 7:
1462 #line 268 "label.y" /* yacc.c:1646  */
1463     { (yyval.expr) = (yyvsp[0].expr); }
1464 #line 1465 "label.cpp" /* yacc.c:1646  */
1465     break;
1466
1467   case 8:
1468 #line 270 "label.y" /* yacc.c:1646  */
1469     { (yyval.expr) = new alternative_expr((yyvsp[-2].expr), (yyvsp[0].expr)); }
1470 #line 1471 "label.cpp" /* yacc.c:1646  */
1471     break;
1472
1473   case 9:
1474 #line 272 "label.y" /* yacc.c:1646  */
1475     { (yyval.expr) = new conditional_expr((yyvsp[-2].expr), (yyvsp[0].expr), 0); }
1476 #line 1477 "label.cpp" /* yacc.c:1646  */
1477     break;
1478
1479   case 10:
1480 #line 277 "label.y" /* yacc.c:1646  */
1481     { (yyval.expr) = (yyvsp[0].expr); }
1482 #line 1483 "label.cpp" /* yacc.c:1646  */
1483     break;
1484
1485   case 11:
1486 #line 279 "label.y" /* yacc.c:1646  */
1487     { (yyval.expr) = new list_expr((yyvsp[-1].expr), (yyvsp[0].expr)); }
1488 #line 1489 "label.cpp" /* yacc.c:1646  */
1489     break;
1490
1491   case 12:
1492 #line 284 "label.y" /* yacc.c:1646  */
1493     { (yyval.expr) = (yyvsp[0].expr); }
1494 #line 1495 "label.cpp" /* yacc.c:1646  */
1495     break;
1496
1497   case 13:
1498 #line 286 "label.y" /* yacc.c:1646  */
1499     { (yyval.expr) = new substitute_expr((yyvsp[-2].expr), (yyvsp[0].expr)); }
1500 #line 1501 "label.cpp" /* yacc.c:1646  */
1501     break;
1502
1503   case 14:
1504 #line 291 "label.y" /* yacc.c:1646  */
1505     { (yyval.expr) = new at_expr; }
1506 #line 1507 "label.cpp" /* yacc.c:1646  */
1507     break;
1508
1509   case 15:
1510 #line 293 "label.y" /* yacc.c:1646  */
1511     {
1512                   (yyval.expr) = new literal_expr(literals.contents() + (yyvsp[0].str).start,
1513                                         (yyvsp[0].str).len);
1514                 }
1515 #line 1516 "label.cpp" /* yacc.c:1646  */
1516     break;
1517
1518   case 16:
1519 #line 298 "label.y" /* yacc.c:1646  */
1520     { (yyval.expr) = new field_expr((yyvsp[0].num), 0); }
1521 #line 1522 "label.cpp" /* yacc.c:1646  */
1522     break;
1523
1524   case 17:
1525 #line 300 "label.y" /* yacc.c:1646  */
1526     { (yyval.expr) = new field_expr((yyvsp[-1].num), (yyvsp[0].num) - 1); }
1527 #line 1528 "label.cpp" /* yacc.c:1646  */
1528     break;
1529
1530   case 18:
1531 #line 302 "label.y" /* yacc.c:1646  */
1532     {
1533                   switch ((yyvsp[0].num)) {
1534                   case 'I':
1535                   case 'i':
1536                   case 'A':
1537                   case 'a':
1538                     (yyval.expr) = new format_expr((yyvsp[0].num));
1539                     break;
1540                   default:
1541                     command_error("unrecognized format `%1'", char((yyvsp[0].num)));
1542                     (yyval.expr) = new format_expr('a');
1543                     break;
1544                   }
1545                 }
1546 #line 1547 "label.cpp" /* yacc.c:1646  */
1547     break;
1548
1549   case 19:
1550 #line 318 "label.y" /* yacc.c:1646  */
1551     {
1552                   (yyval.expr) = new format_expr('0', (yyvsp[0].dig).ndigits, (yyvsp[0].dig).val);
1553                 }
1554 #line 1555 "label.cpp" /* yacc.c:1646  */
1555     break;
1556
1557   case 20:
1558 #line 322 "label.y" /* yacc.c:1646  */
1559     {
1560                   switch ((yyvsp[-1].num)) {
1561                   case 'l':
1562                     (yyval.expr) = new map_expr((yyvsp[-4].expr), lowercase);
1563                     break;
1564                   case 'u':
1565                     (yyval.expr) = new map_expr((yyvsp[-4].expr), uppercase);
1566                     break;
1567                   case 'c':
1568                     (yyval.expr) = new map_expr((yyvsp[-4].expr), capitalize);
1569                     break;
1570                   case 'r':
1571                     (yyval.expr) = new map_expr((yyvsp[-4].expr), reverse_name);
1572                     break;
1573                   case 'a':
1574                     (yyval.expr) = new map_expr((yyvsp[-4].expr), abbreviate_name);
1575                     break;
1576                   case 'y':
1577                     (yyval.expr) = new extractor_expr((yyvsp[-4].expr), find_year, (yyvsp[-2].num));
1578                     break;
1579                   case 'n':
1580                     (yyval.expr) = new extractor_expr((yyvsp[-4].expr), find_last_name, (yyvsp[-2].num));
1581                     break;
1582                   default:
1583                     (yyval.expr) = (yyvsp[-4].expr);
1584                     command_error("unknown function `%1'", char((yyvsp[-1].num)));
1585                     break;
1586                   }
1587                 }
1588 #line 1589 "label.cpp" /* yacc.c:1646  */
1589     break;
1590
1591   case 21:
1592 #line 353 "label.y" /* yacc.c:1646  */
1593     { (yyval.expr) = new truncate_expr((yyvsp[-2].expr), (yyvsp[0].num)); }
1594 #line 1595 "label.cpp" /* yacc.c:1646  */
1595     break;
1596
1597   case 22:
1598 #line 355 "label.y" /* yacc.c:1646  */
1599     { (yyval.expr) = new truncate_expr((yyvsp[-2].expr), -(yyvsp[0].num)); }
1600 #line 1601 "label.cpp" /* yacc.c:1646  */
1601     break;
1602
1603   case 23:
1604 #line 357 "label.y" /* yacc.c:1646  */
1605     { (yyval.expr) = new star_expr((yyvsp[-1].expr)); }
1606 #line 1607 "label.cpp" /* yacc.c:1646  */
1607     break;
1608
1609   case 24:
1610 #line 359 "label.y" /* yacc.c:1646  */
1611     { (yyval.expr) = (yyvsp[-1].expr); }
1612 #line 1613 "label.cpp" /* yacc.c:1646  */
1613     break;
1614
1615   case 25:
1616 #line 361 "label.y" /* yacc.c:1646  */
1617     { (yyval.expr) = new separator_expr((yyvsp[-1].expr)); }
1618 #line 1619 "label.cpp" /* yacc.c:1646  */
1619     break;
1620
1621   case 26:
1622 #line 366 "label.y" /* yacc.c:1646  */
1623     { (yyval.num) = -1; }
1624 #line 1625 "label.cpp" /* yacc.c:1646  */
1625     break;
1626
1627   case 27:
1628 #line 368 "label.y" /* yacc.c:1646  */
1629     { (yyval.num) = (yyvsp[0].num); }
1630 #line 1631 "label.cpp" /* yacc.c:1646  */
1631     break;
1632
1633   case 28:
1634 #line 373 "label.y" /* yacc.c:1646  */
1635     { (yyval.num) = (yyvsp[0].num); }
1636 #line 1637 "label.cpp" /* yacc.c:1646  */
1637     break;
1638
1639   case 29:
1640 #line 375 "label.y" /* yacc.c:1646  */
1641     { (yyval.num) = (yyvsp[-1].num)*10 + (yyvsp[0].num); }
1642 #line 1643 "label.cpp" /* yacc.c:1646  */
1643     break;
1644
1645   case 30:
1646 #line 380 "label.y" /* yacc.c:1646  */
1647     { (yyval.dig).ndigits = 1; (yyval.dig).val = (yyvsp[0].num); }
1648 #line 1649 "label.cpp" /* yacc.c:1646  */
1649     break;
1650
1651   case 31:
1652 #line 382 "label.y" /* yacc.c:1646  */
1653     { (yyval.dig).ndigits = (yyvsp[-1].dig).ndigits + 1; (yyval.dig).val = (yyvsp[-1].dig).val*10 + (yyvsp[0].num); }
1654 #line 1655 "label.cpp" /* yacc.c:1646  */
1655     break;
1656
1657   case 32:
1658 #line 388 "label.y" /* yacc.c:1646  */
1659     { (yyval.num) = 0; }
1660 #line 1661 "label.cpp" /* yacc.c:1646  */
1661     break;
1662
1663   case 33:
1664 #line 390 "label.y" /* yacc.c:1646  */
1665     { (yyval.num) = 1; }
1666 #line 1667 "label.cpp" /* yacc.c:1646  */
1667     break;
1668
1669   case 34:
1670 #line 392 "label.y" /* yacc.c:1646  */
1671     { (yyval.num) = -1; }
1672 #line 1673 "label.cpp" /* yacc.c:1646  */
1673     break;
1674
1675
1676 #line 1677 "label.cpp" /* yacc.c:1646  */
1677       default: break;
1678     }
1679   /* User semantic actions sometimes alter yychar, and that requires
1680      that yytoken be updated with the new translation.  We take the
1681      approach of translating immediately before every use of yytoken.
1682      One alternative is translating here after every semantic action,
1683      but that translation would be missed if the semantic action invokes
1684      YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or
1685      if it invokes YYBACKUP.  In the case of YYABORT or YYACCEPT, an
1686      incorrect destructor might then be invoked immediately.  In the
1687      case of YYERROR or YYBACKUP, subsequent parser actions might lead
1688      to an incorrect destructor call or verbose syntax error message
1689      before the lookahead is translated.  */
1690   YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
1691
1692   YYPOPSTACK (yylen);
1693   yylen = 0;
1694   YY_STACK_PRINT (yyss, yyssp);
1695
1696   *++yyvsp = yyval;
1697
1698   /* Now 'shift' the result of the reduction.  Determine what state
1699      that goes to, based on the state we popped back to and the rule
1700      number reduced by.  */
1701
1702   yyn = yyr1[yyn];
1703
1704   yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
1705   if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
1706     yystate = yytable[yystate];
1707   else
1708     yystate = yydefgoto[yyn - YYNTOKENS];
1709
1710   goto yynewstate;
1711
1712
1713 /*--------------------------------------.
1714 | yyerrlab -- here on detecting error.  |
1715 `--------------------------------------*/
1716 yyerrlab:
1717   /* Make sure we have latest lookahead translation.  See comments at
1718      user semantic actions for why this is necessary.  */
1719   yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar);
1720
1721   /* If not already recovering from an error, report this error.  */
1722   if (!yyerrstatus)
1723     {
1724       ++yynerrs;
1725 #if ! YYERROR_VERBOSE
1726       yyerror (YY_("syntax error"));
1727 #else
1728 # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \
1729                                         yyssp, yytoken)
1730       {
1731         char const *yymsgp = YY_("syntax error");
1732         int yysyntax_error_status;
1733         yysyntax_error_status = YYSYNTAX_ERROR;
1734         if (yysyntax_error_status == 0)
1735           yymsgp = yymsg;
1736         else if (yysyntax_error_status == 1)
1737           {
1738             if (yymsg != yymsgbuf)
1739               YYSTACK_FREE (yymsg);
1740             yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc);
1741             if (!yymsg)
1742               {
1743                 yymsg = yymsgbuf;
1744                 yymsg_alloc = sizeof yymsgbuf;
1745                 yysyntax_error_status = 2;
1746               }
1747             else
1748               {
1749                 yysyntax_error_status = YYSYNTAX_ERROR;
1750                 yymsgp = yymsg;
1751               }
1752           }
1753         yyerror (yymsgp);
1754         if (yysyntax_error_status == 2)
1755           goto yyexhaustedlab;
1756       }
1757 # undef YYSYNTAX_ERROR
1758 #endif
1759     }
1760
1761
1762
1763   if (yyerrstatus == 3)
1764     {
1765       /* If just tried and failed to reuse lookahead token after an
1766          error, discard it.  */
1767
1768       if (yychar <= YYEOF)
1769         {
1770           /* Return failure if at end of input.  */
1771           if (yychar == YYEOF)
1772             YYABORT;
1773         }
1774       else
1775         {
1776           yydestruct ("Error: discarding",
1777                       yytoken, &yylval);
1778           yychar = YYEMPTY;
1779         }
1780     }
1781
1782   /* Else will try to reuse lookahead token after shifting the error
1783      token.  */
1784   goto yyerrlab1;
1785
1786
1787 /*---------------------------------------------------.
1788 | yyerrorlab -- error raised explicitly by YYERROR.  |
1789 `---------------------------------------------------*/
1790 yyerrorlab:
1791
1792   /* Pacify compilers like GCC when the user code never invokes
1793      YYERROR and the label yyerrorlab therefore never appears in user
1794      code.  */
1795   if (/*CONSTCOND*/ 0)
1796      goto yyerrorlab;
1797
1798   /* Do not reclaim the symbols of the rule whose action triggered
1799      this YYERROR.  */
1800   YYPOPSTACK (yylen);
1801   yylen = 0;
1802   YY_STACK_PRINT (yyss, yyssp);
1803   yystate = *yyssp;
1804   goto yyerrlab1;
1805
1806
1807 /*-------------------------------------------------------------.
1808 | yyerrlab1 -- common code for both syntax error and YYERROR.  |
1809 `-------------------------------------------------------------*/
1810 yyerrlab1:
1811   yyerrstatus = 3;      /* Each real token shifted decrements this.  */
1812
1813   for (;;)
1814     {
1815       yyn = yypact[yystate];
1816       if (!yypact_value_is_default (yyn))
1817         {
1818           yyn += YYTERROR;
1819           if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
1820             {
1821               yyn = yytable[yyn];
1822               if (0 < yyn)
1823                 break;
1824             }
1825         }
1826
1827       /* Pop the current state because it cannot handle the error token.  */
1828       if (yyssp == yyss)
1829         YYABORT;
1830
1831
1832       yydestruct ("Error: popping",
1833                   yystos[yystate], yyvsp);
1834       YYPOPSTACK (1);
1835       yystate = *yyssp;
1836       YY_STACK_PRINT (yyss, yyssp);
1837     }
1838
1839   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1840   *++yyvsp = yylval;
1841   YY_IGNORE_MAYBE_UNINITIALIZED_END
1842
1843
1844   /* Shift the error token.  */
1845   YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
1846
1847   yystate = yyn;
1848   goto yynewstate;
1849
1850
1851 /*-------------------------------------.
1852 | yyacceptlab -- YYACCEPT comes here.  |
1853 `-------------------------------------*/
1854 yyacceptlab:
1855   yyresult = 0;
1856   goto yyreturn;
1857
1858 /*-----------------------------------.
1859 | yyabortlab -- YYABORT comes here.  |
1860 `-----------------------------------*/
1861 yyabortlab:
1862   yyresult = 1;
1863   goto yyreturn;
1864
1865 #if !defined yyoverflow || YYERROR_VERBOSE
1866 /*-------------------------------------------------.
1867 | yyexhaustedlab -- memory exhaustion comes here.  |
1868 `-------------------------------------------------*/
1869 yyexhaustedlab:
1870   yyerror (YY_("memory exhausted"));
1871   yyresult = 2;
1872   /* Fall through.  */
1873 #endif
1874
1875 yyreturn:
1876   if (yychar != YYEMPTY)
1877     {
1878       /* Make sure we have latest lookahead translation.  See comments at
1879          user semantic actions for why this is necessary.  */
1880       yytoken = YYTRANSLATE (yychar);
1881       yydestruct ("Cleanup: discarding lookahead",
1882                   yytoken, &yylval);
1883     }
1884   /* Do not reclaim the symbols of the rule whose action triggered
1885      this YYABORT or YYACCEPT.  */
1886   YYPOPSTACK (yylen);
1887   YY_STACK_PRINT (yyss, yyssp);
1888   while (yyssp != yyss)
1889     {
1890       yydestruct ("Cleanup: popping",
1891                   yystos[*yyssp], yyvsp);
1892       YYPOPSTACK (1);
1893     }
1894 #ifndef yyoverflow
1895   if (yyss != yyssa)
1896     YYSTACK_FREE (yyss);
1897 #endif
1898 #if YYERROR_VERBOSE
1899   if (yymsg != yymsgbuf)
1900     YYSTACK_FREE (yymsg);
1901 #endif
1902   return yyresult;
1903 }
1904 #line 395 "label.y" /* yacc.c:1906  */
1905
1906
1907 /* bison defines const to be empty unless __STDC__ is defined, which it
1908 isn't under cfront */
1909
1910 #ifdef const
1911 #undef const
1912 #endif
1913
1914 const char *spec_ptr;
1915 const char *spec_end;
1916 const char *spec_cur;
1917
1918 static char uppercase_array[] = {
1919   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
1920   'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
1921   'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
1922   'Y', 'Z',
1923 };
1924   
1925 static char lowercase_array[] = {
1926   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
1927   'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
1928   'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
1929   'y', 'z',
1930 };
1931
1932 int yylex()
1933 {
1934   while (spec_ptr < spec_end && csspace(*spec_ptr))
1935     spec_ptr++;
1936   spec_cur = spec_ptr;
1937   if (spec_ptr >= spec_end)
1938     return 0;
1939   unsigned char c = *spec_ptr++;
1940   if (csalpha(c)) {
1941     yylval.num = c;
1942     return TOKEN_LETTER;
1943   }
1944   if (csdigit(c)) {
1945     yylval.num = c - '0';
1946     return TOKEN_DIGIT;
1947   }
1948   if (c == '\'') {
1949     yylval.str.start = literals.length();
1950     for (; spec_ptr < spec_end; spec_ptr++) {
1951       if (*spec_ptr == '\'') {
1952         if (++spec_ptr < spec_end && *spec_ptr == '\'')
1953           literals += '\'';
1954         else {
1955           yylval.str.len = literals.length() - yylval.str.start;
1956           return TOKEN_LITERAL;
1957         }
1958       }
1959       else
1960         literals += *spec_ptr;
1961     }
1962     yylval.str.len = literals.length() - yylval.str.start;
1963     return TOKEN_LITERAL;
1964   }
1965   return c;
1966 }
1967
1968 int set_label_spec(const char *label_spec)
1969 {
1970   spec_cur = spec_ptr = label_spec;
1971   spec_end = strchr(label_spec, '\0');
1972   literals.clear();
1973   if (yyparse())
1974     return 0;
1975   delete parsed_label;
1976   parsed_label = parse_result;
1977   return 1;
1978 }
1979
1980 int set_date_label_spec(const char *label_spec)
1981 {
1982   spec_cur = spec_ptr = label_spec;
1983   spec_end = strchr(label_spec, '\0');
1984   literals.clear();
1985   if (yyparse())
1986     return 0;
1987   delete parsed_date_label;
1988   parsed_date_label = parse_result;
1989   return 1;
1990 }
1991
1992 int set_short_label_spec(const char *label_spec)
1993 {
1994   spec_cur = spec_ptr = label_spec;
1995   spec_end = strchr(label_spec, '\0');
1996   literals.clear();
1997   if (yyparse())
1998     return 0;
1999   delete parsed_short_label;
2000   parsed_short_label = parse_result;
2001   return 1;
2002 }
2003
2004 void yyerror(const char *message)
2005 {
2006   if (spec_cur < spec_end)
2007     command_error("label specification %1 before `%2'", message, spec_cur);
2008   else
2009     command_error("label specification %1 at end of string",
2010                   message, spec_cur);
2011 }
2012
2013 void at_expr::evaluate(int tentative, const reference &ref,
2014                        string &result, substring_position &)
2015 {
2016   if (tentative)
2017     ref.canonicalize_authors(result);
2018   else {
2019     const char *end, *start = ref.get_authors(&end);
2020     if (start)
2021       result.append(start, end - start);
2022   }
2023 }
2024
2025 void format_expr::evaluate(int tentative, const reference &ref,
2026                            string &result, substring_position &)
2027 {
2028   if (tentative)
2029     return;
2030   const label_info *lp = ref.get_label_ptr();
2031   int num = lp == 0 ? ref.get_number() : lp->count;
2032   if (type != '0')
2033     result += format_serial(type, num + 1);
2034   else {
2035     const char *ptr = i_to_a(num + first_number);
2036     int pad = width - strlen(ptr);
2037     while (--pad >= 0)
2038       result += '0';
2039     result += ptr;
2040   }
2041 }
2042
2043 static const char *format_serial(char c, int n)
2044 {
2045   assert(n > 0);
2046   static char buf[128]; // more than enough.
2047   switch (c) {
2048   case 'i':
2049   case 'I':
2050     {
2051       char *p = buf;
2052       // troff uses z and w to represent 10000 and 5000 in Roman
2053       // numerals; I can find no historical basis for this usage
2054       const char *s = c == 'i' ? "zwmdclxvi" : "ZWMDCLXVI";
2055       if (n >= 40000)
2056         return i_to_a(n);
2057       while (n >= 10000) {
2058         *p++ = s[0];
2059         n -= 10000;
2060       }
2061       for (int i = 1000; i > 0; i /= 10, s += 2) {
2062         int m = n/i;
2063         n -= m*i;
2064         switch (m) {
2065         case 3:
2066           *p++ = s[2];
2067           /* falls through */
2068         case 2:
2069           *p++ = s[2];
2070           /* falls through */
2071         case 1:
2072           *p++ = s[2];
2073           break;
2074         case 4:
2075           *p++ = s[2];
2076           *p++ = s[1];
2077           break;
2078         case 8:
2079           *p++ = s[1];
2080           *p++ = s[2];
2081           *p++ = s[2];
2082           *p++ = s[2];
2083           break;
2084         case 7:
2085           *p++ = s[1];
2086           *p++ = s[2];
2087           *p++ = s[2];
2088           break;
2089         case 6:
2090           *p++ = s[1];
2091           *p++ = s[2];
2092           break;
2093         case 5:
2094           *p++ = s[1];
2095           break;
2096         case 9:
2097           *p++ = s[2];
2098           *p++ = s[0];
2099         }
2100       }
2101       *p = 0;
2102       break;
2103     }
2104   case 'a':
2105   case 'A':
2106     {
2107       char *p = buf;
2108       // this is derived from troff/reg.c
2109       while (n > 0) {
2110         int d = n % 26;
2111         if (d == 0)
2112           d = 26;
2113         n -= d;
2114         n /= 26;
2115         *p++ = c == 'a' ? lowercase_array[d - 1] :
2116                                uppercase_array[d - 1];
2117       }
2118       *p-- = 0;
2119       // Reverse it.
2120       char *q = buf;
2121       while (q < p) {
2122         char temp = *q;
2123         *q = *p;
2124         *p = temp;
2125         --p;
2126         ++q;
2127       }
2128       break;
2129     }
2130   default:
2131     assert(0);
2132   }
2133   return buf;
2134 }
2135
2136 void field_expr::evaluate(int, const reference &ref,
2137                           string &result, substring_position &)
2138 {
2139   const char *end;
2140   const char *start = ref.get_field(name, &end);
2141   if (start) {
2142     start = nth_field(number, start, &end);
2143     if (start)
2144       result.append(start, end - start);
2145   }
2146 }
2147
2148 void literal_expr::evaluate(int, const reference &,
2149                             string &result, substring_position &)
2150 {
2151   result += s;
2152 }
2153
2154 analyzed_expr::analyzed_expr(expression *e)
2155 : unary_expr(e), flags(e ? e->analyze() : 0)
2156 {
2157 }
2158
2159 void analyzed_expr::evaluate(int tentative, const reference &ref,
2160                              string &result, substring_position &pos)
2161 {
2162   if (expr)
2163     expr->evaluate(tentative, ref, result, pos);
2164 }
2165
2166 void star_expr::evaluate(int tentative, const reference &ref,
2167                          string &result, substring_position &pos)
2168 {
2169   const label_info *lp = ref.get_label_ptr();
2170   if (!tentative
2171       && (lp == 0 || lp->total > 1)
2172       && expr)
2173     expr->evaluate(tentative, ref, result, pos);
2174 }
2175
2176 void separator_expr::evaluate(int tentative, const reference &ref,
2177                               string &result, substring_position &pos)
2178 {
2179   int start_length = result.length();
2180   int is_first = pos.start < 0;
2181   if (expr)
2182     expr->evaluate(tentative, ref, result, pos);
2183   if (is_first) {
2184     pos.start = start_length;
2185     pos.length = result.length() - start_length;
2186   }
2187 }
2188
2189 void map_expr::evaluate(int tentative, const reference &ref,
2190                         string &result, substring_position &)
2191 {
2192   if (expr) {
2193     string temp;
2194     substring_position temp_pos;
2195     expr->evaluate(tentative, ref, temp, temp_pos);
2196     (*func)(temp.contents(), temp.contents() + temp.length(), result);
2197   }
2198 }
2199
2200 void extractor_expr::evaluate(int tentative, const reference &ref,
2201                               string &result, substring_position &)
2202 {
2203   if (expr) {
2204     string temp;
2205     substring_position temp_pos;
2206     expr->evaluate(tentative, ref, temp, temp_pos);
2207     const char *end, *start = (*func)(temp.contents(),
2208                                       temp.contents() + temp.length(),
2209                                       &end);
2210     switch (part) {
2211     case BEFORE:
2212       if (start)
2213         result.append(temp.contents(), start - temp.contents());
2214       else
2215         result += temp;
2216       break;
2217     case MATCH:
2218       if (start)
2219         result.append(start, end - start);
2220       break;
2221     case AFTER:
2222       if (start)
2223         result.append(end, temp.contents() + temp.length() - end);
2224       break;
2225     default:
2226       assert(0);
2227     }
2228   }
2229 }
2230
2231 static void first_part(int len, const char *ptr, const char *end,
2232                           string &result)
2233 {
2234   for (;;) {
2235     const char *token_start = ptr;
2236     if (!get_token(&ptr, end))
2237       break;
2238     const token_info *ti = lookup_token(token_start, ptr);
2239     int counts = ti->sortify_non_empty(token_start, ptr);
2240     if (counts && --len < 0)
2241       break;
2242     if (counts || ti->is_accent())
2243       result.append(token_start, ptr - token_start);
2244   }
2245 }
2246
2247 static void last_part(int len, const char *ptr, const char *end,
2248                       string &result)
2249 {
2250   const char *start = ptr;
2251   int count = 0;
2252   for (;;) {
2253     const char *token_start = ptr;
2254     if (!get_token(&ptr, end))
2255       break;
2256     const token_info *ti = lookup_token(token_start, ptr);
2257     if (ti->sortify_non_empty(token_start, ptr))
2258       count++;
2259   }
2260   ptr = start;
2261   int skip = count - len;
2262   if (skip > 0) {
2263     for (;;) {
2264       const char *token_start = ptr;
2265       if (!get_token(&ptr, end))
2266         assert(0);
2267       const token_info *ti = lookup_token(token_start, ptr);
2268       if (ti->sortify_non_empty(token_start, ptr) && --skip < 0) {
2269         ptr = token_start;
2270         break;
2271       }
2272     }
2273   }
2274   first_part(len, ptr, end, result);
2275 }
2276
2277 void truncate_expr::evaluate(int tentative, const reference &ref,
2278                              string &result, substring_position &)
2279 {
2280   if (expr) {
2281     string temp;
2282     substring_position temp_pos;
2283     expr->evaluate(tentative, ref, temp, temp_pos);
2284     const char *start = temp.contents();
2285     const char *end = start + temp.length();
2286     if (n > 0)
2287       first_part(n, start, end, result);
2288     else if (n < 0)
2289       last_part(-n, start, end, result);
2290   }
2291 }
2292
2293 void alternative_expr::evaluate(int tentative, const reference &ref,
2294                                 string &result, substring_position &pos)
2295 {
2296   int start_length = result.length();
2297   if (expr1)
2298     expr1->evaluate(tentative, ref, result, pos);
2299   if (result.length() == start_length && expr2)
2300     expr2->evaluate(tentative, ref, result, pos);
2301 }
2302
2303 void list_expr::evaluate(int tentative, const reference &ref,
2304                          string &result, substring_position &pos)
2305 {
2306   if (expr1)
2307     expr1->evaluate(tentative, ref, result, pos);
2308   if (expr2)
2309     expr2->evaluate(tentative, ref, result, pos);
2310 }
2311
2312 void substitute_expr::evaluate(int tentative, const reference &ref,
2313                                string &result, substring_position &pos)
2314 {
2315   int start_length = result.length();
2316   if (expr1)
2317     expr1->evaluate(tentative, ref, result, pos);
2318   if (result.length() > start_length && result[result.length() - 1] == '-') {
2319     // ought to see if pos covers the -
2320     result.set_length(result.length() - 1);
2321     if (expr2)
2322       expr2->evaluate(tentative, ref, result, pos);
2323   }
2324 }
2325
2326 void conditional_expr::evaluate(int tentative, const reference &ref,
2327                                 string &result, substring_position &pos)
2328 {
2329   string temp;
2330   substring_position temp_pos;
2331   if (expr1)
2332     expr1->evaluate(tentative, ref, temp, temp_pos);
2333   if (temp.length() > 0) {
2334     if (expr2)
2335       expr2->evaluate(tentative, ref, result, pos);
2336   }
2337   else {
2338     if (expr3)
2339       expr3->evaluate(tentative, ref, result, pos);
2340   }
2341 }
2342
2343 void reference::pre_compute_label()
2344 {
2345   if (parsed_label != 0
2346       && (parsed_label->analyze() & expression::CONTAINS_VARIABLE)) {
2347     label.clear();
2348     substring_position temp_pos;
2349     parsed_label->evaluate(1, *this, label, temp_pos);
2350     label_ptr = lookup_label(label);
2351   }
2352 }
2353
2354 void reference::compute_label()
2355 {
2356   label.clear();
2357   if (parsed_label)
2358     parsed_label->evaluate(0, *this, label, separator_pos);
2359   if (short_label_flag && parsed_short_label)
2360     parsed_short_label->evaluate(0, *this, short_label, short_separator_pos);
2361   if (date_as_label) {
2362     string new_date;
2363     if (parsed_date_label) {
2364       substring_position temp_pos;
2365       parsed_date_label->evaluate(0, *this, new_date, temp_pos);
2366     }
2367     set_date(new_date);
2368   }
2369   if (label_ptr)
2370     label_ptr->count += 1;
2371 }
2372
2373 void reference::immediate_compute_label()
2374 {
2375   if (label_ptr)
2376     label_ptr->total = 2;       // force use of disambiguator
2377   compute_label();
2378 }
2379
2380 int reference::merge_labels(reference **v, int n, label_type type,
2381                             string &result)
2382 {
2383   if (abbreviate_label_ranges)
2384     return merge_labels_by_number(v, n, type, result);
2385   else
2386     return merge_labels_by_parts(v, n, type, result);
2387 }
2388
2389 int reference::merge_labels_by_number(reference **v, int n, label_type type,
2390                                       string &result)
2391 {
2392   if (n <= 1)
2393     return 0;
2394   int num = get_number();
2395   // Only merge three or more labels.
2396   if (v[0]->get_number() != num + 1
2397       || v[1]->get_number() != num + 2)
2398     return 0;
2399   int i;
2400   for (i = 2; i < n; i++)
2401     if (v[i]->get_number() != num + i + 1)
2402       break;
2403   result = get_label(type);
2404   result += label_range_indicator;
2405   result += v[i - 1]->get_label(type);
2406   return i;
2407 }
2408
2409 const substring_position &reference::get_separator_pos(label_type type) const
2410 {
2411   if (type == SHORT_LABEL && short_label_flag)
2412     return short_separator_pos;
2413   else
2414     return separator_pos;
2415 }
2416
2417 const string &reference::get_label(label_type type) const
2418 {
2419   if (type == SHORT_LABEL && short_label_flag)
2420     return short_label; 
2421   else
2422     return label;
2423 }
2424
2425 int reference::merge_labels_by_parts(reference **v, int n, label_type type,
2426                                      string &result)
2427 {
2428   if (n <= 0)
2429     return 0;
2430   const string &lb = get_label(type);
2431   const substring_position &sp = get_separator_pos(type);
2432   if (sp.start < 0
2433       || sp.start != v[0]->get_separator_pos(type).start 
2434       || memcmp(lb.contents(), v[0]->get_label(type).contents(),
2435                 sp.start) != 0)
2436     return 0;
2437   result = lb;
2438   int i = 0;
2439   do {
2440     result += separate_label_second_parts;
2441     const substring_position &s = v[i]->get_separator_pos(type);
2442     int sep_end_pos = s.start + s.length;
2443     result.append(v[i]->get_label(type).contents() + sep_end_pos,
2444                   v[i]->get_label(type).length() - sep_end_pos);
2445   } while (++i < n
2446            && sp.start == v[i]->get_separator_pos(type).start
2447            && memcmp(lb.contents(), v[i]->get_label(type).contents(),
2448                      sp.start) == 0);
2449   return i;
2450 }
2451
2452 string label_pool;
2453
2454 label_info::label_info(const string &s)
2455 : start(label_pool.length()), length(s.length()), count(0), total(1)
2456 {
2457   label_pool += s;
2458 }
2459
2460 static label_info **label_table = 0;
2461 static int label_table_size = 0;
2462 static int label_table_used = 0;
2463
2464 label_info *lookup_label(const string &label)
2465 {
2466   if (label_table == 0) {
2467     label_table = new label_info *[17];
2468     label_table_size = 17;
2469     for (int i = 0; i < 17; i++)
2470       label_table[i] = 0;
2471   }
2472   unsigned h = hash_string(label.contents(), label.length()) % label_table_size;
2473   label_info **ptr;
2474   for (ptr = label_table + h;
2475        *ptr != 0;
2476        (ptr == label_table)
2477        ? (ptr = label_table + label_table_size - 1)
2478        : ptr--)
2479     if ((*ptr)->length == label.length()
2480         && memcmp(label_pool.contents() + (*ptr)->start, label.contents(),
2481                   label.length()) == 0) {
2482       (*ptr)->total += 1;
2483       return *ptr;
2484     }
2485   label_info *result = *ptr = new label_info(label);
2486   if (++label_table_used * 2 > label_table_size) {
2487     // Rehash the table.
2488     label_info **old_table = label_table;
2489     int old_size = label_table_size;
2490     label_table_size = next_size(label_table_size);
2491     label_table = new label_info *[label_table_size];
2492     int i;
2493     for (i = 0; i < label_table_size; i++)
2494       label_table[i] = 0;
2495     for (i = 0; i < old_size; i++)
2496       if (old_table[i]) {
2497         h = hash_string(label_pool.contents() + old_table[i]->start,
2498                         old_table[i]->length);
2499         label_info **p;
2500         for (p = label_table + (h % label_table_size);
2501              *p != 0;
2502              (p == label_table)
2503              ? (p = label_table + label_table_size - 1)
2504              : --p)
2505             ;
2506         *p = old_table[i];
2507         }
2508     a_delete old_table;
2509   }
2510   return result;
2511 }
2512
2513 void clear_labels()
2514 {
2515   for (int i = 0; i < label_table_size; i++) {
2516     delete label_table[i];
2517     label_table[i] = 0;
2518   }
2519   label_table_used = 0;
2520   label_pool.clear();
2521 }
2522
2523 static void consider_authors(reference **start, reference **end, int i);
2524
2525 void compute_labels(reference **v, int n)
2526 {
2527   if (parsed_label
2528       && (parsed_label->analyze() & expression::CONTAINS_AT)
2529       && sort_fields.length() >= 2
2530       && sort_fields[0] == 'A'
2531       && sort_fields[1] == '+')
2532     consider_authors(v, v + n, 0);
2533   for (int i = 0; i < n; i++)
2534     v[i]->compute_label();
2535 }
2536
2537
2538 /* A reference with a list of authors <A0,A1,...,AN> _needs_ author i
2539 where 0 <= i <= N if there exists a reference with a list of authors
2540 <B0,B1,...,BM> such that <A0,A1,...,AN> != <B0,B1,...,BM> and M >= i
2541 and Aj = Bj for 0 <= j < i. In this case if we can't say ``A0,
2542 A1,...,A(i-1) et al'' because this would match both <A0,A1,...,AN> and
2543 <B0,B1,...,BM>.  If a reference needs author i we only have to call
2544 need_author(j) for some j >= i such that the reference also needs
2545 author j. */
2546
2547 /* This function handles 2 tasks:
2548 determine which authors are needed (cannot be elided with et al.);
2549 determine which authors can have only last names in the labels.
2550
2551 References >= start and < end have the same first i author names.
2552 Also they're sorted by A+. */
2553
2554 static void consider_authors(reference **start, reference **end, int i)
2555 {
2556   if (start >= end)
2557     return;
2558   reference **p = start;
2559   if (i >= (*p)->get_nauthors()) {
2560     for (++p; p < end && i >= (*p)->get_nauthors(); p++)
2561       ;
2562     if (p < end && i > 0) {
2563       // If we have an author list <A B C> and an author list <A B C D>,
2564       // then both lists need C.
2565       for (reference **q = start; q < end; q++)
2566         (*q)->need_author(i - 1);
2567     }
2568     start = p;
2569   }
2570   while (p < end) {
2571     reference **last_name_start = p;
2572     reference **name_start = p;
2573     for (++p;
2574          p < end && i < (*p)->get_nauthors()
2575          && same_author_last_name(**last_name_start, **p, i);
2576          p++) {
2577       if (!same_author_name(**name_start, **p, i)) {
2578         consider_authors(name_start, p, i + 1);
2579         name_start = p;
2580       }
2581     }
2582     consider_authors(name_start, p, i + 1);
2583     if (last_name_start == name_start) {
2584       for (reference **q = last_name_start; q < p; q++)
2585         (*q)->set_last_name_unambiguous(i);
2586     }
2587     // If we have an author list <A B C D> and <A B C E>, then the lists
2588     // need author D and E respectively.
2589     if (name_start > start || p < end) {
2590       for (reference **q = last_name_start; q < p; q++)
2591         (*q)->need_author(i);
2592     }
2593   }
2594 }
2595
2596 int same_author_last_name(const reference &r1, const reference &r2, int n)
2597 {
2598   const char *ae1;
2599   const char *as1 = r1.get_sort_field(0, n, 0, &ae1);
2600   const char *ae2;
2601   const char *as2 = r2.get_sort_field(0, n, 0, &ae2);
2602   if (!as1 && !as2) return 1;   // they are the same
2603   if (!as1 || !as2) return 0;
2604   return ae1 - as1 == ae2 - as2 && memcmp(as1, as2, ae1 - as1) == 0;
2605 }
2606
2607 int same_author_name(const reference &r1, const reference &r2, int n)
2608 {
2609   const char *ae1;
2610   const char *as1 = r1.get_sort_field(0, n, -1, &ae1);
2611   const char *ae2;
2612   const char *as2 = r2.get_sort_field(0, n, -1, &ae2);
2613   if (!as1 && !as2) return 1;   // they are the same
2614   if (!as1 || !as2) return 0;
2615   return ae1 - as1 == ae2 - as2 && memcmp(as1, as2, ae1 - as1) == 0;
2616 }
2617
2618
2619 void int_set::set(int i)
2620 {
2621   assert(i >= 0);
2622   int bytei = i >> 3;
2623   if (bytei >= v.length()) {
2624     int old_length = v.length();
2625     v.set_length(bytei + 1);
2626     for (int j = old_length; j <= bytei; j++)
2627       v[j] = 0;
2628   }
2629   v[bytei] |= 1 << (i & 7);
2630 }
2631
2632 int int_set::get(int i) const
2633 {
2634   assert(i >= 0);
2635   int bytei = i >> 3;
2636   return bytei >= v.length() ? 0 : (v[bytei] & (1 << (i & 7))) != 0;
2637 }
2638
2639 void reference::set_last_name_unambiguous(int i)
2640 {
2641   last_name_unambiguous.set(i);
2642 }
2643
2644 void reference::need_author(int n)
2645 {
2646   if (n > last_needed_author)
2647     last_needed_author = n;
2648 }
2649
2650 const char *reference::get_authors(const char **end) const
2651 {
2652   if (!computed_authors) {
2653     ((reference *)this)->computed_authors = 1;
2654     string &result = ((reference *)this)->authors;
2655     int na = get_nauthors();
2656     result.clear();
2657     for (int i = 0; i < na; i++) {
2658       if (last_name_unambiguous.get(i)) {
2659         const char *e, *start = get_author_last_name(i, &e);
2660         assert(start != 0);
2661         result.append(start, e - start);
2662       }
2663       else {
2664         const char *e, *start = get_author(i, &e);
2665         assert(start != 0);
2666         result.append(start, e - start);
2667       }
2668       if (i == last_needed_author
2669           && et_al.length() > 0
2670           && et_al_min_elide > 0
2671           && last_needed_author + et_al_min_elide < na
2672           && na >= et_al_min_total) {
2673         result += et_al;
2674         break;
2675       }
2676       if (i < na - 1) {
2677         if (na == 2)
2678           result += join_authors_exactly_two;
2679         else if (i < na - 2)
2680           result += join_authors_default;
2681         else
2682           result += join_authors_last_two;
2683       }
2684     }
2685   }
2686   const char *start = authors.contents();
2687   *end = start + authors.length();
2688   return start;
2689 }
2690
2691 int reference::get_nauthors() const
2692 {
2693   if (nauthors < 0) {
2694     const char *dummy;
2695     int na;
2696     for (na = 0; get_author(na, &dummy) != 0; na++)
2697       ;
2698     ((reference *)this)->nauthors = na;
2699   }
2700   return nauthors;
2701 }