tagged release 0.6.4
[parrot.git] / compilers / pirc / macro / macroparser.c
blobd5818156e9f6e2dca5667a0ceef9781625b91766
1 /* A Bison parser, made by GNU Bison 2.3. */
3 /* Skeleton implementation for Bison's Yacc-like parsers in C
5 Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
6 Free Software Foundation, Inc.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* As a special exception, you may create a larger work that contains
24 part or all of the Bison parser skeleton and distribute that work
25 under terms of your choice, so long as that work isn't itself a
26 parser generator using the skeleton or a modified version thereof
27 as a parser skeleton. Alternatively, if you modify or redistribute
28 the parser skeleton itself, you may (at your option) remove this
29 special exception, which will cause the skeleton and the resulting
30 Bison output files to be licensed under the GNU General Public
31 License without this special exception.
33 This special exception was added by the Free Software Foundation in
34 version 2.2 of Bison. */
36 /* C LALR(1) parser skeleton written by Richard Stallman, by
37 simplifying the original so-called "semantic" parser. */
39 /* All symbols defined below should begin with yy or YY, to avoid
40 infringing on user name space. This should be done even for local
41 variables, as they might otherwise be expanded by user macros.
42 There are some unavoidable exceptions within include files to
43 define necessary library symbols; they are noted "INFRINGES ON
44 USER NAME SPACE" below. */
46 /* Identify Bison output. */
47 #define YYBISON 1
49 /* Bison version. */
50 #define YYBISON_VERSION "2.3"
52 /* Skeleton name. */
53 #define YYSKELETON_NAME "yacc.c"
55 /* Pure parsers. */
56 #define YYPURE 1
58 /* Using locations. */
59 #define YYLSP_NEEDED 0
61 /* Substitute the variable and function names. */
62 #define yyparse macroparse
63 #define yylex macrolex
64 #define yyerror macroerror
65 #define yylval macrolval
66 #define yychar macrochar
67 #define yydebug macrodebug
68 #define yynerrs macronerrs
71 /* Tokens. */
72 #ifndef YYTOKENTYPE
73 # define YYTOKENTYPE
74 /* Put the tokens into the symbol table, so that GDB and other debuggers
75 know about them. */
76 enum yytokentype {
77 TK_MACRO = 258,
78 TK_NL = 259,
79 TK_ENDM = 260,
80 TK_INCLUDE = 261,
81 TK_MACRO_CONST = 262,
82 TK_MACRO_LOCAL = 263,
83 TK_MACRO_LABEL = 264,
84 TK_LINE = 265,
85 TK_INT = 266,
86 TK_NUM = 267,
87 TK_STRING = 268,
88 TK_PMC = 269,
89 TK_IDENT = 270,
90 TK_ANY = 271,
91 TK_BODY = 272,
92 TK_DOT_IDENT = 273,
93 TK_LABEL_ID = 274,
94 TK_LOCAL_ID = 275,
95 TK_VAR_EXPANSION = 276,
96 TK_LABEL_EXPANSION = 277,
97 TK_UNIQUE_LABEL = 278,
98 TK_UNIQUE_LOCAL = 279,
99 TK_STRINGC = 280,
100 TK_NUMC = 281,
101 TK_INTC = 282
103 #endif
104 /* Tokens. */
105 #define TK_MACRO 258
106 #define TK_NL 259
107 #define TK_ENDM 260
108 #define TK_INCLUDE 261
109 #define TK_MACRO_CONST 262
110 #define TK_MACRO_LOCAL 263
111 #define TK_MACRO_LABEL 264
112 #define TK_LINE 265
113 #define TK_INT 266
114 #define TK_NUM 267
115 #define TK_STRING 268
116 #define TK_PMC 269
117 #define TK_IDENT 270
118 #define TK_ANY 271
119 #define TK_BODY 272
120 #define TK_DOT_IDENT 273
121 #define TK_LABEL_ID 274
122 #define TK_LOCAL_ID 275
123 #define TK_VAR_EXPANSION 276
124 #define TK_LABEL_EXPANSION 277
125 #define TK_UNIQUE_LABEL 278
126 #define TK_UNIQUE_LOCAL 279
127 #define TK_STRINGC 280
128 #define TK_NUMC 281
129 #define TK_INTC 282
134 /* Copy the first part of user declarations. */
135 #line 1 "macro.y"
139 * $Id$
140 * Copyright (C) 2007, The Perl Foundation.
144 #include <stdio.h>
145 #include <string.h>
146 #include <stdlib.h>
147 #include <assert.h>
148 #include <math.h>
149 #include "macroparser.h"
150 #include "macro.h"
151 #include "lexer.h"
153 /* prevent inclusion of <unistd.h> on windows */
154 #define YY_NO_UNISTD_H
156 /* declare yylex prototype BEFORE inclusion of lexer header file. */
157 #define YY_DECL int macrolex(YYSTYPE *yylval, yyscan_t yyscanner)
159 /* inlude flex-generated lexer header file. */
160 #include "macrolexer.h"
162 /* declare yylex. */
163 extern YY_DECL;
165 /* declare yyerror */
166 extern int yyerror(yyscan_t yyscanner, lexer_state *lexer, char *message);
168 #define YYDEBUG 1
173 static void process_file(char *filename, lexer_state *lexer);
174 static void process_string(char *buffer, lexer_state *lexer);
175 static void include_file(char *filename, lexer_state *lexer);
176 static void expand(macro_def *macro, list *args, lexer_state *lexer);
177 static void define_constant(constant_table *table, char *name, char *value);
178 static void define_macro(constant_table *table, char *name, list *parameters, char *body);
179 static void emit(char *str);
180 static list *new_list(char *first_item);
181 static list *add_item(list *L, char *item);
183 static char *munge_id(char *label_id, int is_label_declaration, lexer_state *lexer);
184 static constant_table *new_constant_table(constant_table *current, lexer_state *lexer);
185 static constant_table *pop_constant_table(lexer_state *lexer);
186 static void delete_constant_table(constant_table *table);
187 static void update_unique_id(lexer_state *lexer);
189 macro_def *find_macro(constant_table *table, char *name);
191 extern char *dupstr(char *str);
193 char *concat(char *str1, char *str2, int insert_space);
198 /* Enabling traces. */
199 #ifndef YYDEBUG
200 # define YYDEBUG 0
201 #endif
203 /* Enabling verbose error messages. */
204 #ifdef YYERROR_VERBOSE
205 # undef YYERROR_VERBOSE
206 # define YYERROR_VERBOSE 1
207 #else
208 # define YYERROR_VERBOSE 1
209 #endif
211 /* Enabling the token table. */
212 #ifndef YYTOKEN_TABLE
213 # define YYTOKEN_TABLE 0
214 #endif
216 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
217 typedef union YYSTYPE
218 #line 63 "macro.y"
220 char *sval;
221 struct list *lval;
222 struct macro_def *mval;
225 /* Line 187 of yacc.c. */
226 #line 227 "macroparser.c"
227 YYSTYPE;
228 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
229 # define YYSTYPE_IS_DECLARED 1
230 # define YYSTYPE_IS_TRIVIAL 1
231 #endif
235 /* Copy the second part of user declarations. */
238 /* Line 216 of yacc.c. */
239 #line 240 "macroparser.c"
241 #ifdef short
242 # undef short
243 #endif
245 #ifdef YYTYPE_UINT8
246 typedef YYTYPE_UINT8 yytype_uint8;
247 #else
248 typedef unsigned char yytype_uint8;
249 #endif
251 #ifdef YYTYPE_INT8
252 typedef YYTYPE_INT8 yytype_int8;
253 #elif (defined __STDC__ || defined __C99__FUNC__ \
254 || defined __cplusplus || defined _MSC_VER)
255 typedef signed char yytype_int8;
256 #else
257 typedef short int yytype_int8;
258 #endif
260 #ifdef YYTYPE_UINT16
261 typedef YYTYPE_UINT16 yytype_uint16;
262 #else
263 typedef unsigned short int yytype_uint16;
264 #endif
266 #ifdef YYTYPE_INT16
267 typedef YYTYPE_INT16 yytype_int16;
268 #else
269 typedef short int yytype_int16;
270 #endif
272 #ifndef YYSIZE_T
273 # ifdef __SIZE_TYPE__
274 # define YYSIZE_T __SIZE_TYPE__
275 # elif defined size_t
276 # define YYSIZE_T size_t
277 # elif ! defined YYSIZE_T && (defined __STDC__ || defined __C99__FUNC__ \
278 || defined __cplusplus || defined _MSC_VER)
279 # include <stddef.h> /* INFRINGES ON USER NAME SPACE */
280 # define YYSIZE_T size_t
281 # else
282 # define YYSIZE_T unsigned int
283 # endif
284 #endif
286 #define YYSIZE_MAXIMUM ((YYSIZE_T) -1)
288 #ifndef YY_
289 # if YYENABLE_NLS
290 # if ENABLE_NLS
291 # include <libintl.h> /* INFRINGES ON USER NAME SPACE */
292 # define YY_(msgid) dgettext ("bison-runtime", msgid)
293 # endif
294 # endif
295 # ifndef YY_
296 # define YY_(msgid) msgid
297 # endif
298 #endif
300 /* Suppress unused-variable warnings by "using" E. */
301 #if ! defined lint || defined __GNUC__
302 # define YYUSE(e) ((void) (e))
303 #else
304 # define YYUSE(e) /* empty */
305 #endif
307 /* Identity function, used to suppress warnings about constant conditions. */
308 #ifndef lint
309 # define YYID(n) (n)
310 #else
311 #if (defined __STDC__ || defined __C99__FUNC__ \
312 || defined __cplusplus || defined _MSC_VER)
313 static int
314 YYID (int i)
315 #else
316 static int
317 YYID (i)
318 int i;
319 #endif
321 return i;
323 #endif
325 #if ! defined yyoverflow || YYERROR_VERBOSE
327 /* The parser invokes alloca or malloc; define the necessary symbols. */
329 # ifdef YYSTACK_USE_ALLOCA
330 # if YYSTACK_USE_ALLOCA
331 # ifdef __GNUC__
332 # define YYSTACK_ALLOC __builtin_alloca
333 # elif defined __BUILTIN_VA_ARG_INCR
334 # include <alloca.h> /* INFRINGES ON USER NAME SPACE */
335 # elif defined _AIX
336 # define YYSTACK_ALLOC __alloca
337 # elif defined _MSC_VER
338 # include <malloc.h> /* INFRINGES ON USER NAME SPACE */
339 # define alloca _alloca
340 # else
341 # define YYSTACK_ALLOC alloca
342 # if ! defined _ALLOCA_H && ! defined _STDLIB_H && (defined __STDC__ || defined __C99__FUNC__ \
343 || defined __cplusplus || defined _MSC_VER)
344 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
345 # ifndef _STDLIB_H
346 # define _STDLIB_H 1
347 # endif
348 # endif
349 # endif
350 # endif
351 # endif
353 # ifdef YYSTACK_ALLOC
354 /* Pacify GCC's `empty if-body' warning. */
355 # define YYSTACK_FREE(Ptr) do { /* empty */; } while (YYID (0))
356 # ifndef YYSTACK_ALLOC_MAXIMUM
357 /* The OS might guarantee only one guard page at the bottom of the stack,
358 and a page size can be as small as 4096 bytes. So we cannot safely
359 invoke alloca (N) if N exceeds 4096. Use a slightly smaller number
360 to allow for a few compiler-allocated temporary stack slots. */
361 # define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
362 # endif
363 # else
364 # define YYSTACK_ALLOC YYMALLOC
365 # define YYSTACK_FREE YYFREE
366 # ifndef YYSTACK_ALLOC_MAXIMUM
367 # define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
368 # endif
369 # if (defined __cplusplus && ! defined _STDLIB_H \
370 && ! ((defined YYMALLOC || defined malloc) \
371 && (defined YYFREE || defined free)))
372 # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
373 # ifndef _STDLIB_H
374 # define _STDLIB_H 1
375 # endif
376 # endif
377 # ifndef YYMALLOC
378 # define YYMALLOC malloc
379 # if ! defined malloc && ! defined _STDLIB_H && (defined __STDC__ || defined __C99__FUNC__ \
380 || defined __cplusplus || defined _MSC_VER)
381 void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
382 # endif
383 # endif
384 # ifndef YYFREE
385 # define YYFREE free
386 # if ! defined free && ! defined _STDLIB_H && (defined __STDC__ || defined __C99__FUNC__ \
387 || defined __cplusplus || defined _MSC_VER)
388 void free (void *); /* INFRINGES ON USER NAME SPACE */
389 # endif
390 # endif
391 # endif
392 #endif /* ! defined yyoverflow || YYERROR_VERBOSE */
395 #if (! defined yyoverflow \
396 && (! defined __cplusplus \
397 || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
399 /* A type that is properly aligned for any stack member. */
400 union yyalloc
402 yytype_int16 yyss;
403 YYSTYPE yyvs;
406 /* The size of the maximum gap between one aligned stack and the next. */
407 # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
409 /* The size of an array large to enough to hold all stacks, each with
410 N elements. */
411 # define YYSTACK_BYTES(N) \
412 ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
413 + YYSTACK_GAP_MAXIMUM)
415 /* Copy COUNT objects from FROM to TO. The source and destination do
416 not overlap. */
417 # ifndef YYCOPY
418 # if defined __GNUC__ && 1 < __GNUC__
419 # define YYCOPY(To, From, Count) \
420 __builtin_memcpy (To, From, (Count) * sizeof (*(From)))
421 # else
422 # define YYCOPY(To, From, Count) \
423 do \
425 YYSIZE_T yyi; \
426 for (yyi = 0; yyi < (Count); yyi++) \
427 (To)[yyi] = (From)[yyi]; \
429 while (YYID (0))
430 # endif
431 # endif
433 /* Relocate STACK from its old location to the new one. The
434 local variables YYSIZE and YYSTACKSIZE give the old and new number of
435 elements in the stack, and YYPTR gives the new location of the
436 stack. Advance YYPTR to a properly aligned location for the next
437 stack. */
438 # define YYSTACK_RELOCATE(Stack) \
439 do \
441 YYSIZE_T yynewbytes; \
442 YYCOPY (&yyptr->Stack, Stack, yysize); \
443 Stack = &yyptr->Stack; \
444 yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
445 yyptr += yynewbytes / sizeof (*yyptr); \
447 while (YYID (0))
449 #endif
451 /* YYFINAL -- State number of the termination state. */
452 #define YYFINAL 4
453 /* YYLAST -- Last index in YYTABLE. */
454 #define YYLAST 63
456 /* YYNTOKENS -- Number of terminals. */
457 #define YYNTOKENS 33
458 /* YYNNTS -- Number of nonterminals. */
459 #define YYNNTS 29
460 /* YYNRULES -- Number of rules. */
461 #define YYNRULES 61
462 /* YYNRULES -- Number of states. */
463 #define YYNSTATES 80
465 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
466 #define YYUNDEFTOK 2
467 #define YYMAXUTOK 282
469 #define YYTRANSLATE(YYX) \
470 ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
472 /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX. */
473 static const yytype_uint8 yytranslate[] =
475 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
476 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
477 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
478 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
479 29, 30, 2, 2, 28, 2, 2, 2, 2, 2,
480 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
481 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
482 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
483 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
484 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
485 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
486 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
487 2, 2, 2, 31, 2, 32, 2, 2, 2, 2,
488 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
489 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
490 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
491 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
492 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
493 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
494 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
495 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
496 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
497 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
498 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
499 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
500 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
501 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
502 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
503 25, 26, 27
506 #if YYDEBUG
507 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
508 YYRHS. */
509 static const yytype_uint8 yyprhs[] =
511 0, 0, 3, 4, 8, 9, 11, 13, 17, 19,
512 21, 23, 25, 27, 29, 33, 34, 37, 39, 42,
513 44, 47, 49, 51, 53, 55, 58, 62, 69, 70,
514 72, 74, 77, 79, 81, 83, 86, 90, 92, 94,
515 96, 98, 99, 103, 104, 106, 108, 112, 113, 117,
516 118, 120, 122, 126, 128, 130, 134, 135, 138, 140,
517 142, 144
520 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
521 static const yytype_int8 yyrhs[] =
523 34, 0, -1, -1, 35, 36, 35, -1, -1, 4,
524 -1, 38, -1, 36, 37, 38, -1, 4, -1, 45,
525 -1, 43, -1, 44, -1, 39, -1, 41, -1, 10,
526 27, 40, -1, -1, 28, 25, -1, 42, -1, 41,
527 42, -1, 16, -1, 18, 55, -1, 22, -1, 23,
528 -1, 24, -1, 21, -1, 6, 25, -1, 7, 15,
529 61, -1, 3, 15, 52, 4, 46, 5, -1, -1,
530 47, -1, 48, -1, 47, 48, -1, 16, -1, 49,
531 -1, 50, -1, 9, 19, -1, 8, 51, 20, -1,
532 11, -1, 14, -1, 12, -1, 13, -1, -1, 29,
533 53, 30, -1, -1, 54, -1, 15, -1, 54, 28,
534 15, -1, -1, 29, 56, 30, -1, -1, 57, -1,
535 58, -1, 57, 28, 58, -1, 61, -1, 59, -1,
536 31, 60, 32, -1, -1, 60, 16, -1, 15, -1,
537 26, -1, 27, -1, 25, -1
540 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
541 static const yytype_uint16 yyrline[] =
543 0, 130, 130, 131, 134, 135, 138, 139, 143, 150,
544 151, 152, 153, 154, 157, 163, 164, 172, 173, 176,
545 177, 178, 181, 184, 187, 193, 197, 203, 210, 211,
546 214, 215, 218, 219, 220, 223, 227, 235, 236, 237,
547 238, 241, 242, 245, 246, 249, 250, 253, 254, 257,
548 258, 261, 262, 265, 266, 269, 272, 273, 279, 280,
549 281, 282
551 #endif
553 #if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
554 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
555 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
556 static const char *const yytname[] =
558 "$end", "error", "$undefined", "\".macro\"", "\"\\n\"", "\".endm\"",
559 "\".include\"", "\".macro_const\"", "\".macro_local\"",
560 "\".macro_label\"", "\".line\"", "\"int\"", "\"num\"", "\"string\"",
561 "\"pmc\"", "\"identifier\"", "\"any token\"", "\"macro body\"",
562 "\".identifier\"", "\"label\"", "\"$identifier\"", "\"var expansion\"",
563 "\"label target expansion\"", "\"unique label\"", "\"unique local\"",
564 "\"string constant\"", "\"number constant\"", "\"integer constant\"",
565 "','", "'('", "')'", "'{'", "'}'", "$accept", "program", "opt_nl",
566 "statements", "newline", "statement", "line_directive", "opt_filename",
567 "anything", "any", "include_statement", "macro_const_definition",
568 "macro_definition", "opt_macro_body", "macro_body", "body_token",
569 "label_declaration", "local_declaration", "type", "parameters",
570 "opt_param_list", "param_list", "arguments", "opt_arg_list", "arg_list",
571 "arg", "braced_arg", "long_arg", "expression", 0
573 #endif
575 # ifdef YYPRINT
576 /* YYTOKNUM[YYLEX-NUM] -- Internal token number corresponding to
577 token YYLEX-NUM. */
578 static const yytype_uint16 yytoknum[] =
580 0, 256, 257, 258, 259, 260, 261, 262, 263, 264,
581 265, 266, 267, 268, 269, 270, 271, 272, 273, 274,
582 275, 276, 277, 278, 279, 280, 281, 282, 44, 40,
583 41, 123, 125
585 # endif
587 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
588 static const yytype_uint8 yyr1[] =
590 0, 33, 34, 34, 35, 35, 36, 36, 37, 38,
591 38, 38, 38, 38, 39, 40, 40, 41, 41, 42,
592 42, 42, 42, 42, 42, 43, 44, 45, 46, 46,
593 47, 47, 48, 48, 48, 49, 50, 51, 51, 51,
594 51, 52, 52, 53, 53, 54, 54, 55, 55, 56,
595 56, 57, 57, 58, 58, 59, 60, 60, 61, 61,
596 61, 61
599 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
600 static const yytype_uint8 yyr2[] =
602 0, 2, 0, 3, 0, 1, 1, 3, 1, 1,
603 1, 1, 1, 1, 3, 0, 2, 1, 2, 1,
604 2, 1, 1, 1, 1, 2, 3, 6, 0, 1,
605 1, 2, 1, 1, 1, 2, 3, 1, 1, 1,
606 1, 0, 3, 0, 1, 1, 3, 0, 3, 0,
607 1, 1, 3, 1, 1, 3, 0, 2, 1, 1,
608 1, 1
611 /* YYDEFACT[STATE-NAME] -- Default rule to reduce with in state
612 STATE-NUM when YYTABLE doesn't specify something else to do. Zero
613 means the default is an error. */
614 static const yytype_uint8 yydefact[] =
616 4, 5, 0, 0, 1, 0, 0, 0, 0, 19,
617 47, 24, 21, 22, 23, 4, 6, 12, 13, 17,
618 10, 11, 9, 41, 25, 0, 15, 49, 20, 8,
619 3, 0, 18, 43, 0, 58, 61, 59, 60, 26,
620 0, 14, 56, 0, 50, 51, 54, 53, 7, 45,
621 0, 44, 28, 16, 0, 48, 0, 42, 0, 0,
622 0, 32, 0, 29, 30, 33, 34, 57, 55, 52,
623 46, 37, 39, 40, 38, 0, 35, 27, 31, 36
626 /* YYDEFGOTO[NTERM-NUM]. */
627 static const yytype_int8 yydefgoto[] =
629 -1, 2, 3, 15, 31, 16, 17, 41, 18, 19,
630 20, 21, 22, 62, 63, 64, 65, 66, 75, 34,
631 50, 51, 28, 43, 44, 45, 46, 54, 47
634 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
635 STATE-NUM. */
636 #define YYPACT_NINF -18
637 static const yytype_int8 yypact[] =
639 2, -18, 5, -3, -18, -5, -14, 8, 1, -18,
640 -17, -18, -18, -18, -18, 23, -18, -18, 19, -18,
641 -18, -18, -18, 9, -18, 7, 3, -1, -18, 29,
642 -18, -3, -18, 21, 35, -18, -18, -18, -18, -18,
643 24, -18, -18, 18, 22, -18, -18, -18, -18, -18,
644 25, 26, 0, -18, -15, -18, -1, -18, 36, 33,
645 34, -18, 47, 0, -18, -18, -18, -18, -18, -18,
646 -18, -18, -18, -18, -18, 37, -18, -18, -18, -18
649 /* YYPGOTO[NTERM-NUM]. */
650 static const yytype_int8 yypgoto[] =
652 -18, -18, 41, -18, -18, 27, -18, -18, -18, 42,
653 -18, -18, -18, -18, -18, -4, -18, -18, -18, -18,
654 -18, -18, -18, -18, -18, 6, -18, -18, 38
657 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
658 positive, shift that token. If negative, reduce the rule which
659 number is the opposite. If zero, do what YYDEFACT says.
660 If YYTABLE_NINF, syntax error. */
661 #define YYTABLE_NINF -6
662 static const yytype_int8 yytable[] =
664 5, 67, -2, 6, 7, 4, 1, 8, 59, 60,
665 23, 24, 27, 9, 35, 10, 61, 68, 11, 12,
666 13, 14, 35, 25, 36, 37, 38, 29, 26, -5,
667 42, 40, 36, 37, 38, 9, 49, 10, 33, 52,
668 11, 12, 13, 14, 71, 72, 73, 74, 55, 53,
669 56, 70, 77, 76, 58, 57, 30, 79, 48, 78,
670 32, 0, 69, 39
673 static const yytype_int8 yycheck[] =
675 3, 16, 0, 6, 7, 0, 4, 10, 8, 9,
676 15, 25, 29, 16, 15, 18, 16, 32, 21, 22,
677 23, 24, 15, 15, 25, 26, 27, 4, 27, 0,
678 31, 28, 25, 26, 27, 16, 15, 18, 29, 4,
679 21, 22, 23, 24, 11, 12, 13, 14, 30, 25,
680 28, 15, 5, 19, 28, 30, 15, 20, 31, 63,
681 18, -1, 56, 25
684 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
685 symbol of state STATE-NUM. */
686 static const yytype_uint8 yystos[] =
688 0, 4, 34, 35, 0, 3, 6, 7, 10, 16,
689 18, 21, 22, 23, 24, 36, 38, 39, 41, 42,
690 43, 44, 45, 15, 25, 15, 27, 29, 55, 4,
691 35, 37, 42, 29, 52, 15, 25, 26, 27, 61,
692 28, 40, 31, 56, 57, 58, 59, 61, 38, 15,
693 53, 54, 4, 25, 60, 30, 28, 30, 28, 8,
694 9, 16, 46, 47, 48, 49, 50, 16, 32, 58,
695 15, 11, 12, 13, 14, 51, 19, 5, 48, 20
698 #define yyerrok (yyerrstatus = 0)
699 #define yyclearin (yychar = YYEMPTY)
700 #define YYEMPTY (-2)
701 #define YYEOF 0
703 #define YYACCEPT goto yyacceptlab
704 #define YYABORT goto yyabortlab
705 #define YYERROR goto yyerrorlab
708 /* Like YYERROR except do call yyerror. This remains here temporarily
709 to ease the transition to the new meaning of YYERROR, for GCC.
710 Once GCC version 2 has supplanted version 1, this can go. */
712 #define YYFAIL goto yyerrlab
714 #define YYRECOVERING() (!!yyerrstatus)
716 #define YYBACKUP(Token, Value) \
717 do \
718 if (yychar == YYEMPTY && yylen == 1) \
720 yychar = (Token); \
721 yylval = (Value); \
722 yytoken = YYTRANSLATE (yychar); \
723 YYPOPSTACK (1); \
724 goto yybackup; \
726 else \
728 yyerror (yyscanner, lexer, YY_("syntax error: cannot back up")); \
729 YYERROR; \
731 while (YYID (0))
734 #define YYTERROR 1
735 #define YYERRCODE 256
738 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
739 If N is 0, then set CURRENT to the empty location which ends
740 the previous symbol: RHS[0] (always defined). */
742 #define YYRHSLOC(Rhs, K) ((Rhs)[K])
743 #ifndef YYLLOC_DEFAULT
744 # define YYLLOC_DEFAULT(Current, Rhs, N) \
745 do \
746 if (YYID (N)) \
748 (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
749 (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
750 (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
751 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
753 else \
755 (Current).first_line = (Current).last_line = \
756 YYRHSLOC (Rhs, 0).last_line; \
757 (Current).first_column = (Current).last_column = \
758 YYRHSLOC (Rhs, 0).last_column; \
760 while (YYID (0))
761 #endif
764 /* YY_LOCATION_PRINT -- Print the location on the stream.
765 This macro was not mandated originally: define only if we know
766 we won't break user code: when these are the locations we know. */
768 #ifndef YY_LOCATION_PRINT
769 # if YYLTYPE_IS_TRIVIAL
770 # define YY_LOCATION_PRINT(File, Loc) \
771 fprintf (File, "%d.%d-%d.%d", \
772 (Loc).first_line, (Loc).first_column, \
773 (Loc).last_line, (Loc).last_column)
774 # else
775 # define YY_LOCATION_PRINT(File, Loc) ((void) 0)
776 # endif
777 #endif
780 /* YYLEX -- calling `yylex' with the right arguments. */
782 #ifdef YYLEX_PARAM
783 # define YYLEX yylex (&yylval, YYLEX_PARAM)
784 #else
785 # define YYLEX yylex (&yylval, yyscanner)
786 #endif
788 /* Enable debugging if requested. */
789 #if YYDEBUG
791 # ifndef YYFPRINTF
792 # include <stdio.h> /* INFRINGES ON USER NAME SPACE */
793 # define YYFPRINTF fprintf
794 # endif
796 # define YYDPRINTF(Args) \
797 do { \
798 if (yydebug) \
799 YYFPRINTF Args; \
800 } while (YYID (0))
802 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
803 do { \
804 if (yydebug) \
806 YYFPRINTF (stderr, "%s ", Title); \
807 yy_symbol_print (stderr, \
808 Type, Value, yyscanner, lexer); \
809 YYFPRINTF (stderr, "\n"); \
811 } while (YYID (0))
814 /*--------------------------------.
815 | Print this symbol on YYOUTPUT. |
816 `--------------------------------*/
818 /*ARGSUSED*/
819 #if (defined __STDC__ || defined __C99__FUNC__ \
820 || defined __cplusplus || defined _MSC_VER)
821 static void
822 yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep, yyscan_t yyscanner, struct lexer_state *lexer)
823 #else
824 static void
825 yy_symbol_value_print (yyoutput, yytype, yyvaluep, yyscanner, lexer)
826 FILE *yyoutput;
827 int yytype;
828 YYSTYPE const * const yyvaluep;
829 yyscan_t yyscanner;
830 struct lexer_state *lexer;
831 #endif
833 if (!yyvaluep)
834 return;
835 YYUSE (yyscanner);
836 YYUSE (lexer);
837 # ifdef YYPRINT
838 if (yytype < YYNTOKENS)
839 YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
840 # else
841 YYUSE (yyoutput);
842 # endif
843 switch (yytype)
845 default:
846 break;
851 /*--------------------------------.
852 | Print this symbol on YYOUTPUT. |
853 `--------------------------------*/
855 #if (defined __STDC__ || defined __C99__FUNC__ \
856 || defined __cplusplus || defined _MSC_VER)
857 static void
858 yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep, yyscan_t yyscanner, struct lexer_state *lexer)
859 #else
860 static void
861 yy_symbol_print (yyoutput, yytype, yyvaluep, yyscanner, lexer)
862 FILE *yyoutput;
863 int yytype;
864 YYSTYPE const * const yyvaluep;
865 yyscan_t yyscanner;
866 struct lexer_state *lexer;
867 #endif
869 if (yytype < YYNTOKENS)
870 YYFPRINTF (yyoutput, "token %s (", yytname[yytype]);
871 else
872 YYFPRINTF (yyoutput, "nterm %s (", yytname[yytype]);
874 yy_symbol_value_print (yyoutput, yytype, yyvaluep, yyscanner, lexer);
875 YYFPRINTF (yyoutput, ")");
878 /*------------------------------------------------------------------.
879 | yy_stack_print -- Print the state stack from its BOTTOM up to its |
880 | TOP (included). |
881 `------------------------------------------------------------------*/
883 #if (defined __STDC__ || defined __C99__FUNC__ \
884 || defined __cplusplus || defined _MSC_VER)
885 static void
886 yy_stack_print (yytype_int16 *bottom, yytype_int16 *top)
887 #else
888 static void
889 yy_stack_print (bottom, top)
890 yytype_int16 *bottom;
891 yytype_int16 *top;
892 #endif
894 YYFPRINTF (stderr, "Stack now");
895 for (; bottom <= top; ++bottom)
896 YYFPRINTF (stderr, " %d", *bottom);
897 YYFPRINTF (stderr, "\n");
900 # define YY_STACK_PRINT(Bottom, Top) \
901 do { \
902 if (yydebug) \
903 yy_stack_print ((Bottom), (Top)); \
904 } while (YYID (0))
907 /*------------------------------------------------.
908 | Report that the YYRULE is going to be reduced. |
909 `------------------------------------------------*/
911 #if (defined __STDC__ || defined __C99__FUNC__ \
912 || defined __cplusplus || defined _MSC_VER)
913 static void
914 yy_reduce_print (YYSTYPE *yyvsp, int yyrule, yyscan_t yyscanner, struct lexer_state *lexer)
915 #else
916 static void
917 yy_reduce_print (yyvsp, yyrule, yyscanner, lexer)
918 YYSTYPE *yyvsp;
919 int yyrule;
920 yyscan_t yyscanner;
921 struct lexer_state *lexer;
922 #endif
924 int yynrhs = yyr2[yyrule];
925 int yyi;
926 unsigned long int yylno = yyrline[yyrule];
927 YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n",
928 yyrule - 1, yylno);
929 /* The symbols being reduced. */
930 for (yyi = 0; yyi < yynrhs; yyi++)
932 fprintf (stderr, " $%d = ", yyi + 1);
933 yy_symbol_print (stderr, yyrhs[yyprhs[yyrule] + yyi],
934 &(yyvsp[(yyi + 1) - (yynrhs)])
935 , yyscanner, lexer);
936 fprintf (stderr, "\n");
940 # define YY_REDUCE_PRINT(Rule) \
941 do { \
942 if (yydebug) \
943 yy_reduce_print (yyvsp, Rule, yyscanner, lexer); \
944 } while (YYID (0))
946 /* Nonzero means print parse trace. It is left uninitialized so that
947 multiple parsers can coexist. */
948 int yydebug;
949 #else /* !YYDEBUG */
950 # define YYDPRINTF(Args)
951 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
952 # define YY_STACK_PRINT(Bottom, Top)
953 # define YY_REDUCE_PRINT(Rule)
954 #endif /* !YYDEBUG */
957 /* YYINITDEPTH -- initial size of the parser's stacks. */
958 #ifndef YYINITDEPTH
959 # define YYINITDEPTH 200
960 #endif
962 /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
963 if the built-in stack extension method is used).
965 Do not make this value too large; the results are undefined if
966 YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
967 evaluated with infinite-precision integer arithmetic. */
969 #ifndef YYMAXDEPTH
970 # define YYMAXDEPTH 10000
971 #endif
975 #if YYERROR_VERBOSE
977 # ifndef yystrlen
978 # if defined __GLIBC__ && defined _STRING_H
979 # define yystrlen strlen
980 # else
981 /* Return the length of YYSTR. */
982 #if (defined __STDC__ || defined __C99__FUNC__ \
983 || defined __cplusplus || defined _MSC_VER)
984 static YYSIZE_T
985 yystrlen (const char *yystr)
986 #else
987 static YYSIZE_T
988 yystrlen (yystr)
989 const char *yystr;
990 #endif
992 YYSIZE_T yylen;
993 for (yylen = 0; yystr[yylen]; yylen++)
994 continue;
995 return yylen;
997 # endif
998 # endif
1000 # ifndef yystpcpy
1001 # if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
1002 # define yystpcpy stpcpy
1003 # else
1004 /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
1005 YYDEST. */
1006 #if (defined __STDC__ || defined __C99__FUNC__ \
1007 || defined __cplusplus || defined _MSC_VER)
1008 static char *
1009 yystpcpy (char *yydest, const char *yysrc)
1010 #else
1011 static char *
1012 yystpcpy (yydest, yysrc)
1013 char *yydest;
1014 const char *yysrc;
1015 #endif
1017 char *yyd = yydest;
1018 const char *yys = yysrc;
1020 while ((*yyd++ = *yys++) != '\0')
1021 continue;
1023 return yyd - 1;
1025 # endif
1026 # endif
1028 # ifndef yytnamerr
1029 /* Copy to YYRES the contents of YYSTR after stripping away unnecessary
1030 quotes and backslashes, so that it's suitable for yyerror. The
1031 heuristic is that double-quoting is unnecessary unless the string
1032 contains an apostrophe, a comma, or backslash (other than
1033 backslash-backslash). YYSTR is taken from yytname. If YYRES is
1034 null, do not copy; instead, return the length of what the result
1035 would have been. */
1036 static YYSIZE_T
1037 yytnamerr (char *yyres, const char *yystr)
1039 if (*yystr == '"')
1041 YYSIZE_T yyn = 0;
1042 char const *yyp = yystr;
1044 for (;;)
1045 switch (*++yyp)
1047 case '\'':
1048 case ',':
1049 goto do_not_strip_quotes;
1051 case '\\':
1052 if (*++yyp != '\\')
1053 goto do_not_strip_quotes;
1054 /* Fall through. */
1055 default:
1056 if (yyres)
1057 yyres[yyn] = *yyp;
1058 yyn++;
1059 break;
1061 case '"':
1062 if (yyres)
1063 yyres[yyn] = '\0';
1064 return yyn;
1066 do_not_strip_quotes: ;
1069 if (! yyres)
1070 return yystrlen (yystr);
1072 return yystpcpy (yyres, yystr) - yyres;
1074 # endif
1076 /* Copy into YYRESULT an error message about the unexpected token
1077 YYCHAR while in state YYSTATE. Return the number of bytes copied,
1078 including the terminating null byte. If YYRESULT is null, do not
1079 copy anything; just return the number of bytes that would be
1080 copied. As a special case, return 0 if an ordinary "syntax error"
1081 message will do. Return YYSIZE_MAXIMUM if overflow occurs during
1082 size calculation. */
1083 static YYSIZE_T
1084 yysyntax_error (char *yyresult, int yystate, int yychar)
1086 int yyn = yypact[yystate];
1088 if (! (YYPACT_NINF < yyn && yyn <= YYLAST))
1089 return 0;
1090 else
1092 int yytype = YYTRANSLATE (yychar);
1093 YYSIZE_T yysize0 = yytnamerr (0, yytname[yytype]);
1094 YYSIZE_T yysize = yysize0;
1095 YYSIZE_T yysize1;
1096 int yysize_overflow = 0;
1097 enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
1098 char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
1099 int yyx;
1101 # if 0
1102 /* This is so xgettext sees the translatable formats that are
1103 constructed on the fly. */
1104 YY_("syntax error, unexpected %s");
1105 YY_("syntax error, unexpected %s, expecting %s");
1106 YY_("syntax error, unexpected %s, expecting %s or %s");
1107 YY_("syntax error, unexpected %s, expecting %s or %s or %s");
1108 YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s");
1109 # endif
1110 char *yyfmt;
1111 char const *yyf;
1112 static char const yyunexpected[] = "syntax error, unexpected %s";
1113 static char const yyexpecting[] = ", expecting %s";
1114 static char const yyor[] = " or %s";
1115 char yyformat[sizeof yyunexpected
1116 + sizeof yyexpecting - 1
1117 + ((YYERROR_VERBOSE_ARGS_MAXIMUM - 2)
1118 * (sizeof yyor - 1))];
1119 char const *yyprefix = yyexpecting;
1121 /* Start YYX at -YYN if negative to avoid negative indexes in
1122 YYCHECK. */
1123 int yyxbegin = yyn < 0 ? -yyn : 0;
1125 /* Stay within bounds of both yycheck and yytname. */
1126 int yychecklim = YYLAST - yyn + 1;
1127 int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
1128 int yycount = 1;
1130 yyarg[0] = yytname[yytype];
1131 yyfmt = yystpcpy (yyformat, yyunexpected);
1133 for (yyx = yyxbegin; yyx < yyxend; ++yyx)
1134 if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
1136 if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
1138 yycount = 1;
1139 yysize = yysize0;
1140 yyformat[sizeof yyunexpected - 1] = '\0';
1141 break;
1143 yyarg[yycount++] = yytname[yyx];
1144 yysize1 = yysize + yytnamerr (0, yytname[yyx]);
1145 yysize_overflow |= (yysize1 < yysize);
1146 yysize = yysize1;
1147 yyfmt = yystpcpy (yyfmt, yyprefix);
1148 yyprefix = yyor;
1151 yyf = YY_(yyformat);
1152 yysize1 = yysize + yystrlen (yyf);
1153 yysize_overflow |= (yysize1 < yysize);
1154 yysize = yysize1;
1156 if (yysize_overflow)
1157 return YYSIZE_MAXIMUM;
1159 if (yyresult)
1161 /* Avoid sprintf, as that infringes on the user's name space.
1162 Don't have undefined behavior even if the translation
1163 produced a string with the wrong number of "%s"s. */
1164 char *yyp = yyresult;
1165 int yyi = 0;
1166 while ((*yyp = *yyf) != '\0')
1168 if (*yyp == '%' && yyf[1] == 's' && yyi < yycount)
1170 yyp += yytnamerr (yyp, yyarg[yyi++]);
1171 yyf += 2;
1173 else
1175 yyp++;
1176 yyf++;
1180 return yysize;
1183 #endif /* YYERROR_VERBOSE */
1186 /*-----------------------------------------------.
1187 | Release the memory associated to this symbol. |
1188 `-----------------------------------------------*/
1190 /*ARGSUSED*/
1191 #if (defined __STDC__ || defined __C99__FUNC__ \
1192 || defined __cplusplus || defined _MSC_VER)
1193 static void
1194 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep, yyscan_t yyscanner, struct lexer_state *lexer)
1195 #else
1196 static void
1197 yydestruct (yymsg, yytype, yyvaluep, yyscanner, lexer)
1198 const char *yymsg;
1199 int yytype;
1200 YYSTYPE *yyvaluep;
1201 yyscan_t yyscanner;
1202 struct lexer_state *lexer;
1203 #endif
1205 YYUSE (yyvaluep);
1206 YYUSE (yyscanner);
1207 YYUSE (lexer);
1209 if (!yymsg)
1210 yymsg = "Deleting";
1211 YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
1213 switch (yytype)
1216 default:
1217 break;
1222 /* Prevent warnings from -Wmissing-prototypes. */
1224 #ifdef YYPARSE_PARAM
1225 #if defined __STDC__ || defined __cplusplus
1226 int yyparse (void *YYPARSE_PARAM);
1227 #else
1228 int yyparse ();
1229 #endif
1230 #else /* ! YYPARSE_PARAM */
1231 #if defined __STDC__ || defined __cplusplus
1232 int yyparse (yyscan_t yyscanner, struct lexer_state *lexer);
1233 #else
1234 int yyparse ();
1235 #endif
1236 #endif /* ! YYPARSE_PARAM */
1243 /*----------.
1244 | yyparse. |
1245 `----------*/
1247 #ifdef YYPARSE_PARAM
1248 #if (defined __STDC__ || defined __C99__FUNC__ \
1249 || defined __cplusplus || defined _MSC_VER)
1251 yyparse (void *YYPARSE_PARAM)
1252 #else
1254 yyparse (YYPARSE_PARAM)
1255 void *YYPARSE_PARAM;
1256 #endif
1257 #else /* ! YYPARSE_PARAM */
1258 #if (defined __STDC__ || defined __C99__FUNC__ \
1259 || defined __cplusplus || defined _MSC_VER)
1261 yyparse (yyscan_t yyscanner, struct lexer_state *lexer)
1262 #else
1264 yyparse (yyscanner, lexer)
1265 yyscan_t yyscanner;
1266 struct lexer_state *lexer;
1267 #endif
1268 #endif
1270 /* The look-ahead symbol. */
1271 int yychar;
1273 /* The semantic value of the look-ahead symbol. */
1274 YYSTYPE yylval;
1276 /* Number of syntax errors so far. */
1277 int yynerrs;
1279 int yystate;
1280 int yyn;
1281 int yyresult;
1282 /* Number of tokens to shift before error messages enabled. */
1283 int yyerrstatus;
1284 /* Look-ahead token as an internal (translated) token number. */
1285 int yytoken = 0;
1286 #if YYERROR_VERBOSE
1287 /* Buffer for error messages, and its allocated size. */
1288 char yymsgbuf[128];
1289 char *yymsg = yymsgbuf;
1290 YYSIZE_T yymsg_alloc = sizeof yymsgbuf;
1291 #endif
1293 /* Three stacks and their tools:
1294 `yyss': related to states,
1295 `yyvs': related to semantic values,
1296 `yyls': related to locations.
1298 Refer to the stacks thru separate pointers, to allow yyoverflow
1299 to reallocate them elsewhere. */
1301 /* The state stack. */
1302 yytype_int16 yyssa[YYINITDEPTH];
1303 yytype_int16 *yyss = yyssa;
1304 yytype_int16 *yyssp;
1306 /* The semantic value stack. */
1307 YYSTYPE yyvsa[YYINITDEPTH];
1308 YYSTYPE *yyvs = yyvsa;
1309 YYSTYPE *yyvsp;
1313 #define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N))
1315 YYSIZE_T yystacksize = YYINITDEPTH;
1317 /* The variables used to return semantic value and location from the
1318 action routines. */
1319 YYSTYPE yyval;
1322 /* The number of symbols on the RHS of the reduced rule.
1323 Keep to zero when no symbol should be popped. */
1324 int yylen = 0;
1326 YYDPRINTF ((stderr, "Starting parse\n"));
1328 yystate = 0;
1329 yyerrstatus = 0;
1330 yynerrs = 0;
1331 yychar = YYEMPTY; /* Cause a token to be read. */
1333 /* Initialize stack pointers.
1334 Waste one element of value and location stack
1335 so that they stay on the same level as the state stack.
1336 The wasted elements are never initialized. */
1338 yyssp = yyss;
1339 yyvsp = yyvs;
1341 goto yysetstate;
1343 /*------------------------------------------------------------.
1344 | yynewstate -- Push a new state, which is found in yystate. |
1345 `------------------------------------------------------------*/
1346 yynewstate:
1347 /* In all cases, when you get here, the value and location stacks
1348 have just been pushed. So pushing a state here evens the stacks. */
1349 yyssp++;
1351 yysetstate:
1352 *yyssp = yystate;
1354 if (yyss + yystacksize - 1 <= yyssp)
1356 /* Get the current used size of the three stacks, in elements. */
1357 YYSIZE_T yysize = yyssp - yyss + 1;
1359 #ifdef yyoverflow
1361 /* Give user a chance to reallocate the stack. Use copies of
1362 these so that the &'s don't force the real ones into
1363 memory. */
1364 YYSTYPE *yyvs1 = yyvs;
1365 yytype_int16 *yyss1 = yyss;
1368 /* Each stack pointer address is followed by the size of the
1369 data in use in that stack, in bytes. This used to be a
1370 conditional around just the two extra args, but that might
1371 be undefined if yyoverflow is a macro. */
1372 yyoverflow (YY_("memory exhausted"),
1373 &yyss1, yysize * sizeof (*yyssp),
1374 &yyvs1, yysize * sizeof (*yyvsp),
1376 &yystacksize);
1378 yyss = yyss1;
1379 yyvs = yyvs1;
1381 #else /* no yyoverflow */
1382 # ifndef YYSTACK_RELOCATE
1383 goto yyexhaustedlab;
1384 # else
1385 /* Extend the stack our own way. */
1386 if (YYMAXDEPTH <= yystacksize)
1387 goto yyexhaustedlab;
1388 yystacksize *= 2;
1389 if (YYMAXDEPTH < yystacksize)
1390 yystacksize = YYMAXDEPTH;
1393 yytype_int16 *yyss1 = yyss;
1394 union yyalloc *yyptr =
1395 (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
1396 if (! yyptr)
1397 goto yyexhaustedlab;
1398 YYSTACK_RELOCATE (yyss);
1399 YYSTACK_RELOCATE (yyvs);
1401 # undef YYSTACK_RELOCATE
1402 if (yyss1 != yyssa)
1403 YYSTACK_FREE (yyss1);
1405 # endif
1406 #endif /* no yyoverflow */
1408 yyssp = yyss + yysize - 1;
1409 yyvsp = yyvs + yysize - 1;
1412 YYDPRINTF ((stderr, "Stack size increased to %lu\n",
1413 (unsigned long int) yystacksize));
1415 if (yyss + yystacksize - 1 <= yyssp)
1416 YYABORT;
1419 YYDPRINTF ((stderr, "Entering state %d\n", yystate));
1421 goto yybackup;
1423 /*-----------.
1424 | yybackup. |
1425 `-----------*/
1426 yybackup:
1428 /* Do appropriate processing given the current state. Read a
1429 look-ahead token if we need one and don't already have one. */
1431 /* First try to decide what to do without reference to look-ahead token. */
1432 yyn = yypact[yystate];
1433 if (yyn == YYPACT_NINF)
1434 goto yydefault;
1436 /* Not known => get a look-ahead token if don't already have one. */
1438 /* YYCHAR is either YYEMPTY or YYEOF or a valid look-ahead symbol. */
1439 if (yychar == YYEMPTY)
1441 YYDPRINTF ((stderr, "Reading a token: "));
1442 yychar = YYLEX;
1445 if (yychar <= YYEOF)
1447 yychar = yytoken = YYEOF;
1448 YYDPRINTF ((stderr, "Now at end of input.\n"));
1450 else
1452 yytoken = YYTRANSLATE (yychar);
1453 YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
1456 /* If the proper action on seeing token YYTOKEN is to reduce or to
1457 detect an error, take that action. */
1458 yyn += yytoken;
1459 if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
1460 goto yydefault;
1461 yyn = yytable[yyn];
1462 if (yyn <= 0)
1464 if (yyn == 0 || yyn == YYTABLE_NINF)
1465 goto yyerrlab;
1466 yyn = -yyn;
1467 goto yyreduce;
1470 if (yyn == YYFINAL)
1471 YYACCEPT;
1473 /* Count tokens shifted since error; after three, turn off error
1474 status. */
1475 if (yyerrstatus)
1476 yyerrstatus--;
1478 /* Shift the look-ahead token. */
1479 YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
1481 /* Discard the shifted token unless it is eof. */
1482 if (yychar != YYEOF)
1483 yychar = YYEMPTY;
1485 yystate = yyn;
1486 *++yyvsp = yylval;
1488 goto yynewstate;
1491 /*-----------------------------------------------------------.
1492 | yydefault -- do the default action for the current state. |
1493 `-----------------------------------------------------------*/
1494 yydefault:
1495 yyn = yydefact[yystate];
1496 if (yyn == 0)
1497 goto yyerrlab;
1498 goto yyreduce;
1501 /*-----------------------------.
1502 | yyreduce -- Do a reduction. |
1503 `-----------------------------*/
1504 yyreduce:
1505 /* yyn is the number of a rule to reduce with. */
1506 yylen = yyr2[yyn];
1508 /* If YYLEN is nonzero, implement the default value of the action:
1509 `$$ = $1'.
1511 Otherwise, the following line sets YYVAL to garbage.
1512 This behavior is undocumented and Bison
1513 users should not rely upon it. Assigning to YYVAL
1514 unconditionally makes the parser a bit smaller, and it avoids a
1515 GCC warning that YYVAL may be used uninitialized. */
1516 yyval = yyvsp[1-yylen];
1519 YY_REDUCE_PRINT (yyn);
1520 switch (yyn)
1522 case 8:
1523 #line 144 "macro.y"
1524 { emit("\n"); /* after each statement, emit a newline */ ;}
1525 break;
1527 case 14:
1528 #line 158 "macro.y"
1529 { emit("setline");
1530 emit((yyvsp[(2) - (3)].sval));
1532 break;
1534 case 16:
1535 #line 165 "macro.y"
1536 { emit("setfile");
1537 emit((yyvsp[(2) - (2)].sval));
1538 emit("\n");
1540 break;
1542 case 19:
1543 #line 176 "macro.y"
1544 { emit((yyvsp[(1) - (1)].sval)); ;}
1545 break;
1547 case 20:
1548 #line 177 "macro.y"
1549 { expand((yyvsp[(1) - (2)].mval), (yyvsp[(2) - (2)].lval), lexer); ;}
1550 break;
1552 case 21:
1553 #line 178 "macro.y"
1554 { char *label = munge_id((yyvsp[(1) - (1)].sval), 1, lexer);
1555 emit(label);
1557 break;
1559 case 22:
1560 #line 181 "macro.y"
1561 { char *label = munge_id((yyvsp[(1) - (1)].sval), 1, lexer);
1562 emit(label);
1564 break;
1566 case 23:
1567 #line 184 "macro.y"
1568 { char *local = munge_id((yyvsp[(1) - (1)].sval), 0, lexer);
1569 emit(local);
1571 break;
1573 case 24:
1574 #line 187 "macro.y"
1575 { char *label = munge_id((yyvsp[(1) - (1)].sval), 0, lexer);
1576 emit(label);
1578 break;
1580 case 25:
1581 #line 194 "macro.y"
1582 { include_file((yyvsp[(2) - (2)].sval), lexer); ;}
1583 break;
1585 case 26:
1586 #line 198 "macro.y"
1587 { define_constant(lexer->globaldefinitions, (yyvsp[(2) - (3)].sval), (yyvsp[(3) - (3)].sval)); ;}
1588 break;
1590 case 27:
1591 #line 207 "macro.y"
1592 { define_macro(lexer->globaldefinitions, (yyvsp[(2) - (6)].sval), (yyvsp[(3) - (6)].lval), (yyvsp[(5) - (6)].sval)); ;}
1593 break;
1595 case 28:
1596 #line 210 "macro.y"
1597 { (yyval.sval) = ""; ;}
1598 break;
1600 case 29:
1601 #line 211 "macro.y"
1602 { (yyval.sval) = (yyvsp[(1) - (1)].sval); ;}
1603 break;
1605 case 30:
1606 #line 214 "macro.y"
1607 { (yyval.sval) = (yyvsp[(1) - (1)].sval); ;}
1608 break;
1610 case 31:
1611 #line 215 "macro.y"
1612 { (yyval.sval) = concat((yyvsp[(1) - (2)].sval), (yyvsp[(2) - (2)].sval), 1); ;}
1613 break;
1615 case 32:
1616 #line 218 "macro.y"
1617 { (yyval.sval) = (yyvsp[(1) - (1)].sval); ;}
1618 break;
1620 case 33:
1621 #line 219 "macro.y"
1622 { (yyval.sval) = (yyvsp[(1) - (1)].sval); ;}
1623 break;
1625 case 34:
1626 #line 220 "macro.y"
1627 { (yyval.sval) = (yyvsp[(1) - (1)].sval); ;}
1628 break;
1630 case 35:
1631 #line 224 "macro.y"
1632 { (yyval.sval) = (yyvsp[(2) - (2)].sval); ;}
1633 break;
1635 case 36:
1636 #line 228 "macro.y"
1637 { /* create a string like ".local <type> <id>" */
1638 (yyval.sval) = dupstr(".local");
1639 (yyval.sval) = concat((yyval.sval), (yyvsp[(2) - (3)].sval), 1);
1640 (yyval.sval) = concat((yyval.sval), (yyvsp[(3) - (3)].sval), 1);
1642 break;
1644 case 41:
1645 #line 241 "macro.y"
1646 { (yyval.lval) = NULL; ;}
1647 break;
1649 case 42:
1650 #line 242 "macro.y"
1651 { (yyval.lval) = (yyvsp[(2) - (3)].lval); ;}
1652 break;
1654 case 43:
1655 #line 245 "macro.y"
1656 { (yyval.lval) = NULL; ;}
1657 break;
1659 case 44:
1660 #line 246 "macro.y"
1661 { (yyval.lval) = (yyvsp[(1) - (1)].lval); ;}
1662 break;
1664 case 45:
1665 #line 249 "macro.y"
1666 { (yyval.lval) = new_list((yyvsp[(1) - (1)].sval)); ;}
1667 break;
1669 case 46:
1670 #line 250 "macro.y"
1671 { (yyval.lval) = add_item((yyvsp[(1) - (3)].lval), (yyvsp[(3) - (3)].sval)); ;}
1672 break;
1674 case 47:
1675 #line 253 "macro.y"
1676 { (yyval.lval) = NULL; ;}
1677 break;
1679 case 48:
1680 #line 254 "macro.y"
1681 { (yyval.lval) = (yyvsp[(2) - (3)].lval); ;}
1682 break;
1684 case 49:
1685 #line 257 "macro.y"
1686 { (yyval.lval) = NULL; ;}
1687 break;
1689 case 50:
1690 #line 258 "macro.y"
1691 { (yyval.lval) = (yyvsp[(1) - (1)].lval); ;}
1692 break;
1694 case 51:
1695 #line 261 "macro.y"
1696 { (yyval.lval) = new_list((yyvsp[(1) - (1)].sval)); ;}
1697 break;
1699 case 52:
1700 #line 262 "macro.y"
1701 { (yyval.lval) = add_item((yyvsp[(1) - (3)].lval), (yyvsp[(3) - (3)].sval)); ;}
1702 break;
1704 case 53:
1705 #line 265 "macro.y"
1706 { (yyval.sval) = (yyvsp[(1) - (1)].sval); ;}
1707 break;
1709 case 54:
1710 #line 266 "macro.y"
1711 { (yyval.sval) = (yyvsp[(1) - (1)].sval); ;}
1712 break;
1714 case 55:
1715 #line 269 "macro.y"
1716 { (yyval.sval) = (yyvsp[(2) - (3)].sval); ;}
1717 break;
1719 case 56:
1720 #line 272 "macro.y"
1721 { (yyval.sval) = ""; ;}
1722 break;
1724 case 57:
1725 #line 273 "macro.y"
1726 { (yyval.sval) = concat((yyvsp[(1) - (2)].sval), (yyvsp[(2) - (2)].sval), 0); ;}
1727 break;
1730 /* Line 1267 of yacc.c. */
1731 #line 1732 "macroparser.c"
1732 default: break;
1734 YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
1736 YYPOPSTACK (yylen);
1737 yylen = 0;
1738 YY_STACK_PRINT (yyss, yyssp);
1740 *++yyvsp = yyval;
1743 /* Now `shift' the result of the reduction. Determine what state
1744 that goes to, based on the state we popped back to and the rule
1745 number reduced by. */
1747 yyn = yyr1[yyn];
1749 yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
1750 if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
1751 yystate = yytable[yystate];
1752 else
1753 yystate = yydefgoto[yyn - YYNTOKENS];
1755 goto yynewstate;
1758 /*------------------------------------.
1759 | yyerrlab -- here on detecting error |
1760 `------------------------------------*/
1761 yyerrlab:
1762 /* If not already recovering from an error, report this error. */
1763 if (!yyerrstatus)
1765 ++yynerrs;
1766 #if ! YYERROR_VERBOSE
1767 yyerror (yyscanner, lexer, YY_("syntax error"));
1768 #else
1770 YYSIZE_T yysize = yysyntax_error (0, yystate, yychar);
1771 if (yymsg_alloc < yysize && yymsg_alloc < YYSTACK_ALLOC_MAXIMUM)
1773 YYSIZE_T yyalloc = 2 * yysize;
1774 if (! (yysize <= yyalloc && yyalloc <= YYSTACK_ALLOC_MAXIMUM))
1775 yyalloc = YYSTACK_ALLOC_MAXIMUM;
1776 if (yymsg != yymsgbuf)
1777 YYSTACK_FREE (yymsg);
1778 yymsg = (char *) YYSTACK_ALLOC (yyalloc);
1779 if (yymsg)
1780 yymsg_alloc = yyalloc;
1781 else
1783 yymsg = yymsgbuf;
1784 yymsg_alloc = sizeof yymsgbuf;
1788 if (0 < yysize && yysize <= yymsg_alloc)
1790 (void) yysyntax_error (yymsg, yystate, yychar);
1791 yyerror (yyscanner, lexer, yymsg);
1793 else
1795 yyerror (yyscanner, lexer, YY_("syntax error"));
1796 if (yysize != 0)
1797 goto yyexhaustedlab;
1800 #endif
1805 if (yyerrstatus == 3)
1807 /* If just tried and failed to reuse look-ahead token after an
1808 error, discard it. */
1810 if (yychar <= YYEOF)
1812 /* Return failure if at end of input. */
1813 if (yychar == YYEOF)
1814 YYABORT;
1816 else
1818 yydestruct ("Error: discarding",
1819 yytoken, &yylval, yyscanner, lexer);
1820 yychar = YYEMPTY;
1824 /* Else will try to reuse look-ahead token after shifting the error
1825 token. */
1826 goto yyerrlab1;
1829 /*---------------------------------------------------.
1830 | yyerrorlab -- error raised explicitly by YYERROR. |
1831 `---------------------------------------------------*/
1832 yyerrorlab:
1834 /* Pacify compilers like GCC when the user code never invokes
1835 YYERROR and the label yyerrorlab therefore never appears in user
1836 code. */
1837 if (/*CONSTCOND*/ 0)
1838 goto yyerrorlab;
1840 /* Do not reclaim the symbols of the rule which action triggered
1841 this YYERROR. */
1842 YYPOPSTACK (yylen);
1843 yylen = 0;
1844 YY_STACK_PRINT (yyss, yyssp);
1845 yystate = *yyssp;
1846 goto yyerrlab1;
1849 /*-------------------------------------------------------------.
1850 | yyerrlab1 -- common code for both syntax error and YYERROR. |
1851 `-------------------------------------------------------------*/
1852 yyerrlab1:
1853 yyerrstatus = 3; /* Each real token shifted decrements this. */
1855 for (;;)
1857 yyn = yypact[yystate];
1858 if (yyn != YYPACT_NINF)
1860 yyn += YYTERROR;
1861 if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
1863 yyn = yytable[yyn];
1864 if (0 < yyn)
1865 break;
1869 /* Pop the current state because it cannot handle the error token. */
1870 if (yyssp == yyss)
1871 YYABORT;
1874 yydestruct ("Error: popping",
1875 yystos[yystate], yyvsp, yyscanner, lexer);
1876 YYPOPSTACK (1);
1877 yystate = *yyssp;
1878 YY_STACK_PRINT (yyss, yyssp);
1881 if (yyn == YYFINAL)
1882 YYACCEPT;
1884 *++yyvsp = yylval;
1887 /* Shift the error token. */
1888 YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
1890 yystate = yyn;
1891 goto yynewstate;
1894 /*-------------------------------------.
1895 | yyacceptlab -- YYACCEPT comes here. |
1896 `-------------------------------------*/
1897 yyacceptlab:
1898 yyresult = 0;
1899 goto yyreturn;
1901 /*-----------------------------------.
1902 | yyabortlab -- YYABORT comes here. |
1903 `-----------------------------------*/
1904 yyabortlab:
1905 yyresult = 1;
1906 goto yyreturn;
1908 #ifndef yyoverflow
1909 /*-------------------------------------------------.
1910 | yyexhaustedlab -- memory exhaustion comes here. |
1911 `-------------------------------------------------*/
1912 yyexhaustedlab:
1913 yyerror (yyscanner, lexer, YY_("memory exhausted"));
1914 yyresult = 2;
1915 /* Fall through. */
1916 #endif
1918 yyreturn:
1919 if (yychar != YYEOF && yychar != YYEMPTY)
1920 yydestruct ("Cleanup: discarding lookahead",
1921 yytoken, &yylval, yyscanner, lexer);
1922 /* Do not reclaim the symbols of the rule which action triggered
1923 this YYABORT or YYACCEPT. */
1924 YYPOPSTACK (yylen);
1925 YY_STACK_PRINT (yyss, yyssp);
1926 while (yyssp != yyss)
1928 yydestruct ("Cleanup: popping",
1929 yystos[*yyssp], yyvsp, yyscanner, lexer);
1930 YYPOPSTACK (1);
1932 #ifndef yyoverflow
1933 if (yyss != yyssa)
1934 YYSTACK_FREE (yyss);
1935 #endif
1936 #if YYERROR_VERBOSE
1937 if (yymsg != yymsgbuf)
1938 YYSTACK_FREE (yymsg);
1939 #endif
1940 /* Make sure YYID is used. */
1941 return YYID (yyresult);
1945 #line 287 "macro.y"
1951 =head1 Functions
1953 =over 4
1955 =item C<new_list>
1957 Create a new list node. The specified item is assigned to the node's value.
1958 Returns the newly created node.
1960 =cut
1963 static list *
1964 new_list(char *first_item) {
1965 list *L = (list *)malloc(sizeof (list));
1966 assert(L != NULL);
1967 memset(L, 0, sizeof (list));
1968 L->item = first_item;
1969 return L;
1974 =item C<add_item>
1976 Add a new item to the specified list. The item is added
1977 at the back of the list, so items added are kept in order.
1979 A pointer to the beginning of the list is returned (the
1980 same as was specified).
1982 =cut
1985 static list *
1986 add_item(list *L, char *item) {
1987 list *iter = L;
1988 assert(iter != NULL);
1990 /* the item is added at the end of the list. This
1991 * can be done more efficiently, but for now this works.
1993 while (iter->next != NULL) {
1994 iter = iter->next;
1996 /* iter->next is now NULL, link the new node onto it. */
1997 iter->next = new_list(item);
1999 return L;
2004 =item C<include_file>
2006 Process the specified file.
2009 =cut
2012 static void
2013 include_file(char *filename, lexer_state *lexer) {
2014 assert(filename != NULL);
2015 fprintf(stderr, "including: %s\n", filename);
2016 /* remove closing quote */
2017 filename[strlen(filename) - 1] = '\0';
2018 /* give address of string, skipping opening quote */
2019 process_file(filename + 1, lexer);
2024 =item C<update_unique_id>
2026 =cut
2029 static void
2030 update_unique_id(lexer_state *lexer) {
2031 /* each expansion has a unique id that is used for label/local munging */
2032 lexer->id_gen++;
2033 /* Count number of digits:
2034 * log10 returns a double, get the part before the dot (so, "3.14" -> "3")
2035 * using the floor() function.
2036 * log10(1000) -> 3, so add 1 more digit.
2038 lexer->num_digits = floor(log10(lexer->id_gen)) + 1;
2044 =item C<expand>
2046 Expand the specified macro (or constant).
2048 =cut
2051 static void
2052 expand(macro_def *macro, list *args, lexer_state *lexer) {
2053 /* construct a map data structure that maps the argument values to the parameter names */
2054 /* enter the parameters as temporary symbols (.macro_const) */
2055 constant_table *macro_params = new_constant_table(lexer->globaldefinitions, lexer);
2056 list *params = macro->parameters;
2058 int current_scope_nr;
2059 char *current_macro_id;
2061 while (params && args) {
2062 define_constant(macro_params, params->item, args->item);
2063 params = params->next;
2064 args = args->next;
2067 /* check for both conditions; either can be non-null, indicating an error.
2068 * If both are null, then all went ok.
2070 if (params != NULL) { /* args must be null, so too few arguments */
2071 fprintf(stderr, "Too few arguments for macro expansion %s.\n", macro->name);
2073 if (args != NULL) { /* params must be null, so too many arguments */
2074 fprintf(stderr, "Too many arguments for macro expansion %s.\n", macro->name);
2077 fprintf(stderr, "expanding '%s'\n", macro->name);
2078 fprintf(stderr, "[%s]\n", macro->body);
2080 /* parse the macro body */
2083 fprintf(stderr, "expansion '%s' starting\n", macro->name);
2085 current_macro_id = lexer->macro_id;
2086 lexer->macro_id = macro->name;
2087 /* save current scope id */
2088 current_scope_nr = lexer->unique_id;
2089 update_unique_id(lexer);
2090 lexer->unique_id = lexer->id_gen;
2091 process_string(macro->body, lexer);
2093 /* restore current scope id */
2094 lexer->unique_id = current_scope_nr;
2095 lexer->macro_id = current_macro_id;
2097 fprintf(stderr, "expansion '%s' done\n", macro->name);
2100 /* now remove the temporary constant definitions */
2101 pop_constant_table(lexer);
2102 delete_constant_table(macro_params);
2104 update_unique_id(lexer);
2110 =item C<define_constant>
2112 Define the specified name as an alias for the specified value.
2114 =cut
2117 static void
2118 define_constant(constant_table *table, char *name, char *value) {
2119 macro_def *def = (macro_def *)malloc(sizeof (macro_def));
2120 assert(def != NULL);
2121 memset(def, 0, sizeof (macro_def));
2123 def->name = name;
2124 def->body = value;
2126 def->next = table->definitions;
2127 table->definitions = def;
2134 =item C<define_macro>
2136 Define a macro by the given name, parameters and body.
2138 =cut
2141 static void
2142 define_macro(constant_table *table, char *name, list *parameters, char *body) {
2143 macro_def *macro = (macro_def *)malloc(sizeof (macro_def));
2144 assert(macro != NULL);
2145 memset(macro, 0, sizeof (macro_def));
2147 /* initialize the fields */
2148 macro->name = name;
2149 macro->body = body;
2150 macro->parameters = parameters;
2152 /* link the macro in the list */
2153 macro->next = table->definitions;
2154 table->definitions = macro;
2160 =item C<find_macro>
2162 Find the specified macro. If the specified macro does not exist,
2163 NULL is returned.
2165 =cut
2168 macro_def *
2169 find_macro(constant_table *table, char *name) {
2170 macro_def *iter = table->definitions;
2171 assert(name != NULL);
2173 /* iterate over the list and compare each node's name */
2174 while (iter != NULL) {
2175 if (strcmp(iter->name, name) == 0)
2176 return iter;
2177 iter = iter->next;
2180 if (table->prev)
2181 return find_macro(table->prev, name);
2183 return NULL;
2188 =item C<concat>
2190 Concatenate two strings, and return the result. If the first string is NULL, then
2191 the result consists of the second string. If need_space is true, a space is
2192 inserted between the two strings.
2194 =cut
2197 char *
2198 concat(char *str1, char *str2, int need_space) {
2199 assert (str2 != NULL);
2200 if (str1 == NULL) {
2201 return str2;
2203 else {
2204 /* allocate a new buffer large enough to hold both strings, a space, and the NULL char. */
2205 /* TODO: make this more efficient; don't malloc every time, just allocate a big enough
2206 * buffer, and only increase it if it's full. For now this is the easiest solution.
2208 int strlen1 = strlen(str1);
2209 char *newbuffer = (char *)calloc(strlen1 + strlen(str2) + 1 + (need_space ? 1 : 0), sizeof (char));
2211 assert(newbuffer != NULL);
2212 sprintf(newbuffer, "%s%s%s", str1, need_space ? " " : "", str2);
2215 free(str1);
2216 free(str2);
2218 return newbuffer;
2222 /* short-cut to check for a label; last character must be ":" */
2223 #define is_label(X) (X[strlen(X) - 1] == ':')
2225 /* short-cut to check for token equality */
2226 #define is_token(X,Y) (strcmp(X,Y) == 0)
2231 =item C<emit>
2233 Emit the specified string. This function will be the "gateway" to the
2234 output file. All tokens except C<.sub>, C<.end> and C<.namespace> are indented.
2235 All tokens are separated with a space, C<)>, C<]>, C<,>.
2237 =cut
2240 static void
2241 emit(char *str) {
2242 FILE *output = stdout;
2243 /* globals! */
2244 static int just_nl = 1;
2245 static int need_space = 1;
2247 /* emit a newline between .subs and .namespaces */
2248 if (is_token(str, ".sub") || is_token(str, ".namespace"))
2249 fprintf(output, "\n");
2251 /* if the token needs indention, all except .sub, .end and .namespace. */
2252 if (!is_token(str, ".sub") && !is_token(str, ".end") && !is_token(str, ".namespace")) {
2253 if (just_nl) { /* just a newline means we need some indention */
2254 if (is_label(str)) { /* indent labels a bit less than other tokens */
2255 fprintf(output, " ");
2257 else { /* not a label */
2258 fprintf(output, " ");
2262 /* print the token; if it's a token like a comma, that shouldn't be prefixed with space,
2263 * remove the space (backspace)
2266 if (is_token(str, ",") || is_token(str, ")") || is_token(str, "]")) {
2267 fprintf(output, "%c", '\b');
2271 if (is_token(str, "(") || is_token(str, "[")) {
2272 need_space = 0; /* no separation needed after these tokens */
2274 else { /* all normal cases emit a separation space for tokens */
2275 need_space = 1;
2277 /* print the token */
2278 fprintf(output, "%s", str);
2279 /* check for newline */
2280 if (is_token(str, "\n"))
2281 just_nl = 1;
2282 else {
2283 just_nl = 0;
2284 if (need_space)
2285 fprintf(output, " ");
2290 void
2291 emit_int(int val) {
2292 FILE *output = stdout;
2293 fprintf(output, "%d ", val);
2298 =item C<new_constant_table>
2300 =cut
2303 static constant_table *
2304 new_constant_table(constant_table *current, lexer_state *lexer) {
2305 constant_table *table = (constant_table *)malloc(sizeof (constant_table));
2306 assert(table != NULL);
2307 table->definitions = NULL;
2308 table->prev = current;
2310 lexer->globaldefinitions = table;
2311 return table;
2317 =item C<pop_constant_table>
2319 =cut
2322 static constant_table *
2323 pop_constant_table(lexer_state *lexer) {
2324 constant_table *popped = lexer->globaldefinitions;
2325 lexer->globaldefinitions = popped->prev;
2326 return popped;
2331 =item C<delete_constant_table>
2333 =cut
2336 static void
2337 delete_constant_table(constant_table *table) {
2338 /* destroy all definitions */
2339 macro_def *iter = table->definitions;
2340 while (iter != NULL) {
2341 macro_def *temp = iter;
2342 iter = iter->next;
2343 free(temp);
2345 free(table);
2352 =item C<munge_id>
2354 Generate an identifier based on a macro label or locaal declaration, or a macro label
2355 or local expansion.
2357 A label declaration looks like: ".label $LABEL:", from which a normal
2358 PIR label is created, formatted as: "_unique_MACRO_LABEL_?:".
2360 A label expansion looks like ".$LABEL", from which a label identifier is
2361 generated, formatted as: "_unique_MACRO_LABEL_?"
2362 (note the difference, there is no colon at the end.
2364 The same works for local identifiers, but of course the is_label_declaration
2365 argument must be 0 for that.
2367 =cut
2370 static char *
2371 munge_id(char *id, int is_label_declaration, lexer_state *lexer) {
2372 /* the format of the generated label: */
2373 char const * const format = "_unique_%s_%s_%d%s";
2374 int const format_length = strlen(format);
2376 /* calculate length of the generated label: length of macro name,
2377 * plus length of label name.
2379 int length = format_length + strlen(lexer->macro_id) + lexer->num_digits;
2380 char *munged_id = NULL;
2382 length += strlen(id);
2384 if (is_label_declaration)
2385 length++; /* reserve 1 more byte for the ":" */
2387 munged_id = (char *)calloc(length + 1, sizeof (char));
2388 assert(munged_id != NULL);
2389 /* generate the identifier; if it's a declaration, then add the colon. */
2390 sprintf(munged_id, format, lexer->macro_id, id, lexer->unique_id,
2391 is_label_declaration ? ":" : "");
2392 return munged_id;
2399 =item C<process_string>
2401 Process the string stored in C<buffer>. First a new yyscan_t
2402 object is created, initialized, after which the specified
2403 buffer is parsed. Afterwards the yyscan_t object is destroyed.
2405 =cut
2408 void
2409 process_string(char *buffer, lexer_state *lexer) {
2410 /* initialize a yyscan_t object */
2411 yyscan_t yyscanner;
2412 macrolex_init(&yyscanner);
2414 macroset_debug(lexer->flexdebug, yyscanner);
2415 macroset_extra(lexer, yyscanner);
2416 assert(buffer != NULL);
2417 /* set the scanner to a string buffer and go parse */
2418 macro_scan_string(buffer, yyscanner);
2419 yyparse(yyscanner, lexer);
2420 /* clean up after playing */
2421 macrolex_destroy(yyscanner);
2426 =item C<process_file>
2428 Process the specified file.
2430 =cut
2433 void
2434 process_file(char *filename, lexer_state *lexer) {
2435 FILE *fp = NULL;
2436 yyscan_t yyscanner;
2438 if (filename == NULL) { /* no file name means reading from stdin. */
2439 fp = stdin;
2441 else { /* open the specified file */
2442 fp = fopen(filename, "r");
2445 if (fp == NULL) {
2446 fprintf(stderr, "Failed to open file %s\n", filename);
2448 else {
2449 /* save current state of lexer, these are overwritten, so that
2450 * error messages indicate an error in the string (macro body).
2452 int temp_line = lexer->line;
2453 char *temp_file = lexer->currentfile;
2455 /* construct a yylex_t object */
2456 macrolex_init(&yyscanner);
2457 macroset_in(fp, yyscanner);
2458 macroset_debug(lexer->flexdebug, yyscanner);
2459 macroset_extra(lexer, yyscanner);
2461 /* emit directives that set the file/line */
2462 /* they must be within compilation unit; this doesn't happen right now...
2463 emit("setfile");
2464 emit(filename);
2465 emit("setline");
2466 emit_int(1);
2469 /* go parse the file */
2470 yyparse(yyscanner, lexer);
2471 /* and clean up */
2472 macrolex_destroy(yyscanner);
2474 /* restore state of lexer */
2475 lexer->line = temp_line;
2476 lexer->currentfile = temp_file;
2482 =item C<print_help>
2484 =cut
2487 static void
2488 print_help(char const * const programname) {
2489 fprintf(stderr, "Usage: %s [options] [input]\n", programname);
2490 fprintf(stderr, "where options are:\n");
2491 fprintf(stderr, " -d debug the generated parser\n");
2492 fprintf(stderr, " -f debug the generated lexer\n\n");
2493 fprintf(stderr, "If no input is specified, the program reads from stdin.\n");
2499 =item C<yyerror>
2501 Function for syntax error handling.
2503 =cut
2507 yyerror(yyscan_t yyscanner, lexer_state *lexer, char *message) {
2508 fprintf(stderr, "Error in '%s' (line %d): %s\n", lexer->currentfile, lexer->line, message);
2509 lexer->errors++;
2510 return 0;
2516 =item C<main>
2518 Pre-processor main function.
2520 =cut
2524 main(int argc, char *argv[]) {
2525 lexer_state *lexer = NULL;
2526 char const * const programname = argv[0];
2528 /* skip program name */
2529 argc--;
2530 argv++;
2532 lexer = (lexer_state *)malloc(sizeof (lexer_state));
2533 memset(lexer, 0, sizeof (lexer_state));
2534 assert(lexer != NULL);
2535 lexer->unique_id = 0;
2536 lexer->line = 1;
2537 lexer->errors = 0;
2538 lexer->flexdebug = 0;
2539 lexer->macro_id = NULL;
2540 lexer->globaldefinitions = new_constant_table(NULL, lexer);
2543 /* very basic argument handling; I'm too lazy to check out
2544 * the standard funtion for that, right now. This is a TODO.
2546 while (argc > 0 && argv[0][0] == '-') {
2547 switch (argv[0][1]) {
2548 /* Only allow for debug flag if the generated parser supports it */
2549 #ifdef YYDEBUG
2550 case 'd':
2551 yydebug = 1;
2552 break;
2553 #endif
2554 case 'f':
2555 lexer->flexdebug = 1;
2556 break;
2557 case 'h':
2558 print_help(programname);
2559 exit(EXIT_SUCCESS); /* asking for help doesn't make you a failure */
2560 /* break; */
2561 default:
2562 fprintf(stderr, "Unknown option: '%c'\n", argv[0][1]);
2563 break;
2565 /* goto next command line argument */
2566 argv++;
2567 argc--;
2570 if (argc > 0) { /* a command line argument was given */
2571 lexer->currentfile = argv[0];
2572 process_file(argv[0], lexer);
2574 else { /* no filename */
2575 lexer->currentfile = "STDIN";
2576 process_file(NULL, lexer);
2579 if (lexer->errors > 0)
2580 fprintf(stderr, "There were %d error(s)\n", lexer->errors);
2583 /* clean up and go home */
2584 delete_constant_table(lexer->globaldefinitions);
2585 free(lexer);
2587 return 0;
2593 =back
2595 =cut
2601 * Local variables:
2602 * c-file-style: "parrot"
2603 * End:
2604 * vim: expandtab shiftwidth=4: