4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1988 AT&T */
27 /* All Rights Reserved */
33 #define IDENTIFIER 257
42 #define C_IDENTIFIER 265 /* name followed by colon */
49 #define LHS_TEXT_LEN 80 /* length of lhstext */
50 #define RHS_TEXT_LEN 640 /* length of rhstext */
51 /* communication variables between various I/O routines */
55 #define DEFAULT_PREFIX "y"
57 char *infile
; /* input file name */
58 static int numbval
; /* value of an input number */
59 static int toksize
= NAMESIZE
;
60 static wchar_t *tokname
; /* input token name */
61 char *parser
= PARSER
; /* location of common parser */
63 static void finact(void);
64 static wchar_t *cstash(wchar_t *);
65 static void defout(void);
66 static void cpyunion(void);
67 static void cpycode(void);
68 static void cpyact(int);
69 static void lhsfill(wchar_t *);
70 static void rhsfill(wchar_t *);
71 static void lrprnt(void);
72 static void beg_debug(void);
73 static void end_toks(void);
74 static void end_debug(void);
75 static void exp_tokname(void);
76 static void exp_prod(void);
77 static void exp_ntok(void);
78 static void exp_nonterm(void);
79 static int defin(int, wchar_t *);
80 static int gettok(void);
81 static int chfind(int, wchar_t *);
82 static int skipcom(void);
83 static int findchtok(int);
84 static void put_prefix_define(char *);
87 /* storage of names */
90 * initial block to place token and
91 * nonterminal names are stored
92 * points to initial block - more space
93 * is allocated as needed.
95 static wchar_t cnamesblk0
[CNAMSZ
];
96 static wchar_t *cnames
= cnamesblk0
;
98 /* place where next name is to be put in */
99 static wchar_t *cnamp
= cnamesblk0
;
101 /* number of defined symbols output */
102 static int ndefout
= 3;
104 /* storage of types */
105 static int defunion
= 0; /* union of types defined? */
106 static int ntypes
= 0; /* number of types defined */
107 static wchar_t *typeset
[NTYPES
]; /* pointers to type tags */
109 /* symbol tables for tokens and nonterminals */
118 int nnontersz
= NNONTERM
;
120 static int start
; /* start symbol */
122 /* assigned token type values */
123 static int extval
= 0;
125 /* input and output file descriptors */
127 FILE *finput
; /* yacc input file */
128 FILE *faction
; /* file for saving actions */
129 FILE *fdefine
; /* file for # defines */
130 FILE *ftable
; /* y.tab.c file */
131 FILE *ftemp
; /* tempfile to pass 2 */
132 FILE *fdebug
; /* where the strings for debugging are stored */
133 FILE *foutput
; /* y.output file */
137 static wchar_t *lhstext
;
138 static wchar_t *rhstext
;
140 /* storage for grammar rules */
142 int *mem0
; /* production storage */
145 extern int *optimmem
;
146 int new_memsize
= MEMSIZE
;
147 int nprod
= 1; /* number of productions */
154 /* flag for generating the # line's default is yes */
158 /* flag for whether to include runtime debugging */
159 static int gen_testing
= 0;
161 /* flag for version stamping--default turned off */
162 static char *v_stmp
= "n";
164 int nmbchars
= 0; /* number of mb literals in mbchars */
165 MBCLIT
*mbchars
= NULL
; /* array of mb literals */
166 int nmbcharsz
= 0; /* allocated space for mbchars */
172 { int ii
, i
, j
, lev
, t
, ty
;
173 /* ty is the sequencial number of token name in tokset */
178 unsigned int options
= 0;
179 char *file_prefix
= DEFAULT_PREFIX
;
180 char *sym_prefix
= "";
181 #define F_NAME_LENGTH 128
182 char fname
[F_NAME_LENGTH
+1];
188 tokname
= (wchar_t *)malloc(sizeof (wchar_t) * toksize
);
189 tokset
= (TOKSYMB
*)malloc(sizeof (TOKSYMB
) * ntoksz
);
190 toklev
= (int *)malloc(sizeof (int) * ntoksz
);
191 nontrst
= (NTSYMB
*)malloc(sizeof (NTSYMB
) * nnontersz
);
192 mem0
= (int *)malloc(sizeof (int) * new_memsize
);
193 prdptr
= (int **)malloc(sizeof (int *) * (nprodsz
+2));
194 levprd
= (int *)malloc(sizeof (int) * (nprodsz
+2));
195 had_act
= (wchar_t *)calloc((nprodsz
+ 2), sizeof (wchar_t));
196 lhstext
= (wchar_t *)calloc(1, sizeof (wchar_t) * LHS_TEXT_LEN
);
197 rhstext
= (wchar_t *)calloc(1, sizeof (wchar_t) * RHS_TEXT_LEN
);
198 aryfil(toklev
, ntoksz
, 0);
199 aryfil(levprd
, nprodsz
, 0);
200 for (ii
= 0; ii
< ntoksz
; ++ii
)
201 tokset
[ii
].value
= 0;
202 for (ii
= 0; ii
< nnontersz
; ++ii
)
203 nontrst
[ii
].tvalue
= 0;
204 aryfil(mem0
, new_memsize
, 0);
208 while ((c
= getopt(argc
, argv
, "vVdltp:Q:Y:P:b:")) != EOF
)
214 (void) fprintf(stderr
, "yacc: %s %s\n",
215 (const char *)SGU_PKG
,
216 (const char *)SGU_REL
);
220 if (*v_stmp
!= 'y' && *v_stmp
!= 'n')
222 * TRANSLATION_NOTE -- This is a message from yacc.
223 * This message is passed to error() function.
224 * Do not translate -Q and [y/n].
227 "yacc: -Q should be followed by [y/n]"));
233 gen_lines
= 0; /* don't gen #lines */
236 gen_testing
= 1; /* set YYDEBUG on */
239 cp
= (char *)malloc(strlen(optarg
)+
240 sizeof ("/yaccpar") + 1);
241 cp
= strcpy(cp
, optarg
);
242 parser
= strcat(cp
, "/yaccpar");
248 if (strcmp(optarg
, "yy") != 0)
254 file_prefix
= optarg
;
259 * TRANSLATION_NOTE -- This is a message from yacc.
260 * This message is passed to error() function.
261 * This is a usage message. The translate should be
262 * consistent with man page translation.
264 (void) fprintf(stderr
, gettext(
265 "Usage: yacc [-vVdltY] [-Q(y/n)] [-b file_prefix] [-p sym_prefix]"
266 " [-P parser] file\n"));
270 * Open y.output if -v is specified
272 if (options
& v_FLAG
) {
273 (void) strncpy(fname
,
275 F_NAME_LENGTH
-strlen(".output"));
276 (void) strcat(fname
, ".output");
277 foutput
= fopen(fname
, "w");
280 "cannot open y.output"));
284 * Open y.tab.h if -d is specified
286 if (options
& d_FLAG
) {
287 (void) strncpy(fname
,
289 F_NAME_LENGTH
-strlen(".tab.h"));
290 (void) strcat(fname
, ".tab.h");
291 fdefine
= fopen(fname
, "w");
294 "cannot open y.tab.h"));
297 fdebug
= fopen(DEBUGNAME
, "w");
300 * TRANSLATION_NOTE -- This is a message from yacc.
301 * This message is passed to error() function.
302 * Do not translate yacc.debug.
305 "cannot open yacc.debug"));
309 (void) strncpy(fname
,
311 F_NAME_LENGTH
-strlen(".tab.c"));
312 (void) strcat(fname
, ".tab.c");
313 ftable
= fopen(fname
, "w");
316 "cannot open %s"), fname
);
318 ftemp
= fopen(TEMPNAME
, "w");
319 faction
= fopen(ACTNAME
, "w");
320 if (ftemp
== NULL
|| faction
== NULL
)
322 * TRANSLATION_NOTE -- This is a message from yacc.
323 * This message is passed to error() function.
324 * The message means: "Could not open a temporary file."
327 "cannot open temp file"));
329 if ((finput
= fopen(infile
= argv
[optind
], "r")) == NULL
)
331 * TRANSLATION_NOTE -- This is a message from yacc.
332 * This message is passed to error() function.
335 "cannot open input file"));
339 (void) defin(0, L
"$end");
341 (void) defin(0, L
"error");
342 (void) defin(1, L
"$accept");
347 beg_debug(); /* initialize fdebug file */
350 * sorry -- no yacc parser here.....
351 * we must bootstrap somehow...
356 (void) fprintf(ftable
, "#ident\t\"yacc: %s %s\"\n",
357 (const char *)SGU_PKG
, (const char *)SGU_REL
);
358 for (; t
!= MARK
&& t
!= ENDFILE
; ) {
367 if ((t
= gettok()) != IDENTIFIER
) {
368 error("bad %%start construction");
370 start
= chfind(1, tokname
);
376 if ((t
= gettok()) != TYPENAME
)
378 * TRANSLATION_NOTE -- This is a message from yacc.
379 * This message is passed to error() function.
380 * Do not translate %%type.
383 "bad syntax in %%type"));
391 * The following lines are idented to left.
394 if ((t
= chfind(1, tokname
)) < NTBASE
) {
396 if (j
!= 0 && j
!= ty
) {
398 * TRANSLATION_NOTE -- This is a message from yacc.
399 * This message is passed to error() function.
402 "type redeclaration of token %ws"),
406 SETTYPE(toklev
[t
], ty
);
408 j
= nontrst
[t
-NTBASE
].tvalue
;
409 if (j
!= 0 && j
!= ty
) {
411 * TRANSLATION_NOTE -- This is a message from yacc.
412 * This message is passed to error() function.
413 * Check how nonterminal is translated in translated
414 * yacc man page or yacc user's document.
417 "type redeclaration of nonterminal %ws"),
418 nontrst
[t
-NTBASE
].name
);
421 nontrst
[t
-NTBASE
].tvalue
= ty
;
438 * TRANSLATION_NOTE -- This is a message from yacc.
439 * This message is passed to error() function.
442 "missing tokens or illegal tokens"));
448 /* copy the union declaration to the output */
462 /* nonzero means new prec. and assoc. */
466 /* get identifiers so defined */
469 if (t
== TYPENAME
) { /* there is a type defined */
486 j
= chfind(0, tokname
);
489 * TRANSLATION_NOTE -- This is a message from yacc.
490 * This message is passed to error() function.
493 "%ws is not a token."),
497 if (ASSOC(toklev
[j
]) & ~04)
499 * TRANSLATION_NOTE -- This is a message from yacc.
500 * This message is passed to error() function.
503 "redeclaration of precedence of %ws"),
505 SETASC(toklev
[j
], lev
);
506 SETPLEV(toklev
[j
], i
);
508 if (ASSOC(toklev
[j
]))
509 (void) warning(1, gettext(
510 "redeclaration of precedence of %ws."),
512 SETASC(toklev
[j
], lev
);
518 * TRANSLATION_NOTE -- This is a message from yacc.
519 * This message is passed to error() function.
521 "redeclaration of type of %ws"),
523 SETTYPE(toklev
[j
], ty
);
525 if ((t
= gettok()) == NUMBER
) {
526 tokset
[j
].value
= numbval
;
527 if (j
< ndefout
&& j
> 2) {
529 * TRANSLATION_NOTE -- This is a message from yacc.
530 * This message is passed to error() function.
533 "type number of %ws should be defined earlier"),
536 if (numbval
>= -YYFLAG1
) {
538 * TRANSLATION_NOTE -- This is a message from yacc.
539 * This message is passed to error() function.
542 "token numbers must be less than %d"),
552 * TRANSLATION_NOTE -- This is a message from yacc.
553 * This message is passed to error() function.
556 "missing tokens or illegal tokens"));
568 error("syntax error");
576 * TRANSLATION_NOTE -- This is a message from yacc.
577 * This message is passed to error() function.
578 * Do not translate %%%%.
580 error("unexpected EOF before %%%%");
586 end_toks(); /* all tokens dumped - get ready for reductions */
588 (void) fprintf(ftable
, "\n#include <inttypes.h>\n");
589 (void) fprintf(ftable
, "\n#ifdef __STDC__\n");
590 (void) fprintf(ftable
, "#include <stdlib.h>\n");
591 (void) fprintf(ftable
, "#include <string.h>\n");
592 (void) fprintf(ftable
, "#define YYCONST const\n");
593 (void) fprintf(ftable
, "#else\n");
594 (void) fprintf(ftable
, "#include <malloc.h>\n");
595 (void) fprintf(ftable
, "#include <memory.h>\n");
596 (void) fprintf(ftable
, "#define YYCONST\n");
597 (void) fprintf(ftable
, "#endif\n");
598 (void) fprintf(ftable
, "\n#include <values.h>\n");
600 if (sym_prefix
[0] != '\0')
601 put_prefix_define(sym_prefix
);
603 (void) fprintf(ftable
,
604 "\n#if defined(__cplusplus) || defined(__STDC__)\n");
605 (void) fprintf(ftable
,
606 "\n#if defined(__cplusplus) && defined(__EXTERN_C__)\n");
607 (void) fprintf(ftable
, "extern \"C\" {\n");
608 (void) fprintf(ftable
, "#endif\n");
609 (void) fprintf(ftable
, "#ifndef yyerror\n");
610 (void) fprintf(ftable
, "#if defined(__cplusplus)\n");
611 (void) fprintf(ftable
, " void yyerror(YYCONST char *);\n");
612 (void) fprintf(ftable
, "#endif\n");
613 (void) fprintf(ftable
, "#endif\n");
614 (void) fprintf(ftable
, "#ifndef yylex\n");
615 (void) fprintf(ftable
, " int yylex(void);\n");
616 (void) fprintf(ftable
, "#endif\n");
617 (void) fprintf(ftable
, " int yyparse(void);\n");
618 (void) fprintf(ftable
,
619 "#if defined(__cplusplus) && defined(__EXTERN_C__)\n");
620 (void) fprintf(ftable
, "}\n");
621 (void) fprintf(ftable
, "#endif\n");
622 (void) fprintf(ftable
, "\n#endif\n\n");
624 (void) fprintf(ftable
, "#define yyclearin yychar = -1\n");
625 (void) fprintf(ftable
, "#define yyerrok yyerrflag = 0\n");
626 (void) fprintf(ftable
, "extern int yychar;\nextern int yyerrflag;\n");
627 if (!(defunion
|| ntypes
))
628 (void) fprintf(ftable
,
629 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
630 (void) fprintf(ftable
, "YYSTYPE yylval;\n");
631 (void) fprintf(ftable
, "YYSTYPE yyval;\n");
632 (void) fprintf(ftable
, "typedef int yytabelem;\n");
633 (void) fprintf(ftable
,
634 "#ifndef YYMAXDEPTH\n#define YYMAXDEPTH 150\n#endif\n");
635 (void) fprintf(ftable
, "#if YYMAXDEPTH > 0\n");
636 (void) fprintf(ftable
, "int yy_yys[YYMAXDEPTH], *yys = yy_yys;\n");
637 (void) fprintf(ftable
, "YYSTYPE yy_yyv[YYMAXDEPTH], *yyv = yy_yyv;\n");
638 (void) fprintf(ftable
, "#else /* user does initial allocation */\n");
639 (void) fprintf(ftable
, "int *yys;\nYYSTYPE *yyv;\n#endif\n");
640 (void) fprintf(ftable
, "static int yymaxdepth = YYMAXDEPTH;\n");
643 /* added production */
646 /* if start is 0, we will overwrite with the lhs of the first rule */
652 while ((t
= gettok()) == LCURLY
)
655 if (t
!= C_IDENTIFIER
)
656 error("bad syntax on first rule");
659 prdptr
[0][1] = chfind(1, tokname
);
663 while (t
!= MARK
&& t
!= ENDFILE
) {
668 rhsfill((wchar_t *)0); /* restart fill of rhs */
669 *mem
= *prdptr
[nprod
-1];
670 if (++mem
>= &tracemem
[new_memsize
])
672 } else if (t
== C_IDENTIFIER
) {
673 *mem
= chfind(1, tokname
);
676 * TRANSLATION_NOTE -- This is a message from yacc.
677 * This message is passed to error() function.
678 * Check how nonterminal is translated.
681 "illegal nonterminal in grammar rule"));
682 if (++mem
>= &tracemem
[new_memsize
])
684 lhsfill(tokname
); /* new rule: restart strings */
687 * TRANSLATION_NOTE -- This is a message from yacc.
688 * This message is passed to error() function.
691 "illegal rule: missing semicolon or | ?"));
698 while (t
== IDENTIFIER
) {
699 *mem
= chfind(1, tokname
);
701 levprd
[nprod
] = toklev
[*mem
]& ~04;
702 if (++mem
>= &tracemem
[new_memsize
])
704 rhsfill(tokname
); /* add to rhs string */
709 if (gettok() != IDENTIFIER
)
711 * TRANSLATION_NOTE -- This is a message from yacc.
712 * This message is passed to error() function.
713 * Do not translate %%prec.
716 "illegal %%prec syntax"));
717 j
= chfind(2, tokname
);
720 * TRANSLATION_NOTE -- This is a message from yacc.
721 * This message is passed to error() function.
722 * Do not translate %%prec.
725 "nonterminal %ws illegal after %%prec"),
726 nontrst
[j
-NTBASE
].name
);
727 levprd
[nprod
] = toklev
[j
] & ~04;
733 levprd
[nprod
] |= ACTFLAG
;
734 (void) fprintf(faction
, "\ncase %d:", nprod
);
735 cpyact(mem
-prdptr
[nprod
] - 1);
736 (void) fprintf(faction
, " break;");
737 if ((t
= gettok()) == IDENTIFIER
) {
738 /* action within rule... */
740 lrprnt(); /* dump lhs, rhs */
741 (void) wsprintf(actname
, "$$%d", nprod
);
743 * make it nonterminal
745 j
= chfind(1, actname
);
748 * the current rule will become rule
749 * number nprod+1 move the contents down,
750 * and make room for the null
753 if (mem
+ 2 >= &tracemem
[new_memsize
])
755 for (p
= mem
; p
>= prdptr
[nprod
]; --p
)
759 /* enter null production for action */
766 /* update the production information */
768 levprd
[nprod
+1] = levprd
[nprod
] & ~ACTFLAG
;
769 levprd
[nprod
] = ACTFLAG
;
771 if (++nprod
>= nprodsz
)
776 * make the action appear in
780 if (mem
>= &tracemem
[new_memsize
])
782 /* get some more of the rule */
789 if (mem
>= &tracemem
[new_memsize
])
792 /* check that default action is reasonable */
794 if (ntypes
&& !(levprd
[nprod
] & ACTFLAG
) &&
795 nontrst
[*prdptr
[nprod
]-NTBASE
].tvalue
) {
796 /* no explicit action, LHS has value */
798 tempty
= prdptr
[nprod
][1];
801 * TRANSLATION_NOTE -- This is a message from yacc.
802 * This message is passed to error() function.
803 * LHS means Left Hand Side. It does not need to be translated.
806 "must return a value, since LHS has a type"));
807 else if (tempty
>= NTBASE
)
808 tempty
= nontrst
[tempty
-NTBASE
].tvalue
;
810 tempty
= TYPE(toklev
[tempty
]);
811 if (tempty
!= nontrst
[*prdptr
[nprod
]-NTBASE
].tvalue
) {
813 * TRANSLATION_NOTE -- This is a message from yacc.
814 * This message is passed to error() function.
815 * Check how action is transltated in yacc man page or documents.
818 "default action causes potential type clash"));
822 if (++nprod
>= nprodsz
)
827 /* end of all rules */
829 end_debug(); /* finish fdebug file's input */
833 (void) fprintf(ftable
, "\n# line %d \"%s\"\n",
835 while ((c
= getwc(finput
)) != EOF
)
836 (void) putwc(c
, ftable
);
838 (void) fclose(finput
);
844 /* finish action routine */
845 (void) fclose(faction
);
846 (void) fprintf(ftable
, "# define YYERRCODE %d\n", tokset
[2].value
);
855 static int used_save
= 0;
856 static int exp_cname
= CNAMSZ
;
861 * Don't need to expand the table, just allocate new space.
864 while (len
>= (exp_cname
- used_save
)) {
867 free((char *)cnames
);
868 if ((cnames
= (wchar_t *)
869 malloc(sizeof (wchar_t)*exp_cname
)) == NULL
)
871 * TRANSLATION_NOTE -- This is a message from yacc.
872 * This message is passed to error() function.
874 * You may just translate this as:
875 * 'Could not allocate internally used memory.'
878 "cannot expand string dump"));
887 used
+= cnamp
- temp
;
892 defin(int t
, wchar_t *s
)
894 /* define s to be a terminal if t=0 or a nonterminal if t=1 */
899 if (++nnonter
>= nnontersz
)
901 nontrst
[nnonter
].name
= cstash(s
);
902 return (NTBASE
+ nnonter
);
904 /* must be a token */
905 if (++ntokens
>= ntoksz
)
907 tokset
[ntokens
].name
= cstash(s
);
909 /* establish value for token */
911 if (s
[0] == L
' ' && s
[2] == 0) { /* single character literal */
912 val
= findchtok(s
[1]);
913 } else if (s
[0] == L
' ' && s
[1] == L
'\\') { /* escape sequence */
914 if (s
[3] == 0) { /* single character escape sequence */
916 /* character which is escaped */
918 (void) warning(1, gettext(
920 * TRANSLATION_NOTE -- This is a message from yacc.
921 * This message is passed to warning() function.
922 * Do not trasnlate ANSI C, \\a.
924 "\\a is ANSI C \"alert\" character"));
925 #if __STDC__ - 1 == 0
932 case L
'v': val
= L
'\v'; break;
933 case L
'n': val
= L
'\n'; break;
934 case L
'r': val
= L
'\r'; break;
935 case L
'b': val
= L
'\b'; break;
936 case L
't': val
= L
'\t'; break;
937 case L
'f': val
= L
'\f'; break;
938 case L
'\'': val
= L
'\''; break;
939 case L
'"': val
= L
'"'; break;
940 case L
'?': val
= L
'?'; break;
941 case L
'\\': val
= L
'\\'; break;
943 * TRANSLATION_NOTE -- This is a message from yacc.
944 * This message is passed to error() function.
946 default: error(gettext(
949 } else if (s
[2] <= L
'7' && s
[2] >= L
'0') { /* \nnn sequence */
952 while (iswdigit(s
[i
]) && i
<= 4) {
953 if (s
[i
] >= L
'0' && s
[i
] <= L
'7')
954 val
= val
* 8 + s
[i
] - L
'0';
957 * TRANSLATION_NOTE -- This is a message from yacc.
958 * This message is passed to error() function.
961 "illegal octal number"));
966 * TRANSLATION_NOTE -- This is a message from yacc.
967 * This message is passed to error() function.
968 * Do not translate \\nnn.
971 "illegal \\nnn construction"));
974 * TRANSLATION_NOTE -- This is a message from yacc.
975 * This message is passed to error() function.
977 * \\nnn, \\xnnnnnnnn.
980 "\\nnn exceed \\377; use \\xnnnnnnnn for wchar_t value of multibyte char");
981 if (val
== 0 && i
>= 4)
983 * TRANSLATION_NOTE -- This is a message from yacc.
984 * This message is passed to error() function.
985 * Do not translate \\000.
988 "'\\000' is illegal"));
989 } else if (s
[2] == L
'x') { /* hexadecimal \xnnn sequence */
993 * TRANSLATION_NOTE -- This is a message from yacc.
994 * This message is passed to warning() function.
995 * Do not translate \\x, ANSI C.
997 (void) warning(1, gettext(
998 "\\x is ANSI C hex escape"));
1000 while (iswxdigit(s
[i
])) {
1003 tmpval
= s
[i
] - L
'0';
1004 else if (s
[i
] >= L
'a')
1005 tmpval
= s
[i
] - L
'a' + 10;
1007 tmpval
= s
[i
] - L
'A' + 10;
1008 val
= 16 * val
+ tmpval
;
1013 "illegal hexadecimal number"));
1016 * TRANSLATION_NOTE -- This is a message from yacc.
1017 * This message is passed to error() function.
1018 * Do not translate \\xnn.
1021 "illegal \\xnn construction"));
1022 #define LWCHAR_MAX 0x7fffffff
1023 if ((unsigned)val
> LWCHAR_MAX
)
1025 * TRANSLATION_NOTE -- This is a message from yacc.
1026 * This message is passed to error() function.
1027 * Do not translate \\xnnnnnnnn and %#x.
1030 " \\xnnnnnnnn exceed %#x"),
1034 * TRANSLATION_NOTE -- This is a message from yacc.
1035 * This message is passed to error() function.
1036 * Do not translate \\x00.
1039 "'\\x00' is illegal"));
1040 val
= findchtok(val
);
1047 tokset
[ntokens
].value
= val
;
1048 toklev
[ntokens
] = 0;
1055 /* write out the defines (at the end of the declaration section) */
1058 register wchar_t *cp
;
1060 for (i
= ndefout
; i
<= ntokens
; ++i
) {
1062 cp
= tokset
[i
].name
;
1063 if (*cp
== L
' ') /* literals */
1065 (void) fprintf(fdebug
, WSFMT("\t\"%ws\",\t%d,\n"),
1066 tokset
[i
].name
+ 1, tokset
[i
].value
);
1067 continue; /* was cp++ */
1070 for (; (c
= *cp
) != 0; ++cp
) {
1071 if (iswlower(c
) || iswupper(c
) ||
1072 iswdigit(c
) || c
== L
'_')
1078 (void) fprintf(fdebug
,
1079 WSFMT("\t\"%ws\",\t%d,\n"), tokset
[i
].name
,
1081 (void) fprintf(ftable
,
1082 WSFMT("# define %ws %d\n"), tokset
[i
].name
,
1084 if (fdefine
!= NULL
)
1085 (void) fprintf(fdefine
,
1086 WSFMT("# define %ws %d\n"),
1092 ndefout
= ntokens
+1;
1099 static int peekline
; /* number of '\n' seen in lookahead */
1100 int c
, match
, reserve
;
1107 * while (c == ' ' || c == '\n' || c == '\t' || c == '\f') {
1109 while (iswspace(c
)) {
1114 if (c
== L
'/') { /* skip comment */
1115 lineno
+= skipcom();
1124 (void) ungetwc(c
, finput
);
1125 return (L
'='); /* action ... */
1126 case L
'<': /* get, and look up, a type name (union member name) */
1128 while ((c
= getwc(finput
)) != L
'>' &&
1129 c
!= EOF
&& c
!= L
'\n') {
1136 "unterminated < ... > clause"));
1139 error("missing type name in < ... > clause");
1140 for (i
= 1; i
<= ntypes
; ++i
) {
1141 if (!wscmp(typeset
[i
], tokname
)) {
1146 typeset
[numbval
= ++ntypes
] = cstash(tokname
);
1156 if (c
== L
'\n' || c
== EOF
)
1158 "illegal or missing ' or \""));
1164 } else if (c
== match
) break;
1174 switch (c
= getwc(finput
)) {
1176 case L
'0': return (TERM
);
1177 case L
'<': return (LEFT
);
1178 case L
'2': return (BINARY
);
1179 case L
'>': return (RIGHT
);
1181 case L
'\\': return (MARK
);
1182 case L
'=': return (PREC
);
1183 case L
'{': return (LCURLY
);
1184 default: reserve
= 1;
1189 if (iswdigit(c
)) { /* number */
1191 base
= (c
== L
'0') ? 8 : 10;
1192 for (c
= getwc(finput
);
1194 c
= getwc(finput
)) {
1195 numbval
= numbval
*base
+ c
- L
'0';
1197 (void) ungetwc(c
, finput
);
1199 } else if (iswlower(c
) || iswupper(c
) ||
1200 c
== L
'_' || c
== L
'.' ||
1203 while (iswlower(c
) || iswupper(c
) ||
1204 iswdigit(c
) || c
== L
'_' ||
1205 c
== L
'.' || c
== L
'$') {
1207 if (reserve
&& iswupper(c
))
1208 tokname
[i
] = towlower(c
);
1217 (void) ungetwc(c
, finput
);
1222 if (reserve
) { /* find a reserved word */
1223 if (!wscmp(tokname
, L
"term"))
1225 if (!wscmp(tokname
, L
"token"))
1227 if (!wscmp(tokname
, L
"left"))
1229 if (!wscmp(tokname
, L
"nonassoc"))
1231 if (!wscmp(tokname
, L
"binary"))
1233 if (!wscmp(tokname
, L
"right"))
1235 if (!wscmp(tokname
, L
"prec"))
1237 if (!wscmp(tokname
, L
"start"))
1239 if (!wscmp(tokname
, L
"type"))
1241 if (!wscmp(tokname
, L
"union"))
1244 "invalid escape, or illegal reserved word: %ws"),
1248 /* look ahead to distinguish IDENTIFIER from C_IDENTIFIER */
1252 * while (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '/')
1255 while (iswspace(c
) || c
== L
'/') {
1258 } else if (c
== L
'/') { /* look for comments */
1259 peekline
+= skipcom();
1264 return (C_IDENTIFIER
);
1265 (void) ungetwc(c
, finput
);
1266 return (IDENTIFIER
);
1272 /* determine the type of a symbol */
1275 v
= nontrst
[t
-NTBASE
].tvalue
;
1277 v
= TYPE(toklev
[t
]);
1280 "must specify type for %ws"),
1281 (t
>= NTBASE
) ? nontrst
[t
-NTBASE
].name
:
1287 chfind(int t
, wchar_t *s
)
1294 if (!wscmp(s
, tokset
[i
].name
)) {
1299 if (!wscmp(s
, nontrst
[i
].name
)) {
1300 return (i
+ NTBASE
);
1303 /* cannot find name */
1306 "%ws should have been defined earlier"), s
);
1307 return (defin(t
, s
));
1314 * copy the union declaration to the output,
1315 * and the define file if present
1319 (void) fprintf(ftable
, "\n# line %d \"%s\"\n", lineno
, infile
);
1320 (void) fprintf(ftable
, "typedef union\n");
1322 (void) fprintf(fdefine
, "\ntypedef union\n");
1323 (void) fprintf(ftable
, "#ifdef __cplusplus\n\tYYSTYPE\n#endif\n");
1325 (void) fprintf(fdefine
,
1326 "#ifdef __cplusplus\n\tYYSTYPE\n#endif\n");
1330 if ((c
= getwc(finput
)) == EOF
)
1332 * TRANSLATION_NOTE -- This is a message from yacc.
1333 * This message is passed to error() function.
1334 * EOF - End Of File.
1335 * Do not translate %%union.
1338 "EOF encountered while processing %%union"));
1339 (void) putwc(c
, ftable
);
1341 (void) putwc(c
, fdefine
);
1355 if (level
== 0) { /* we are finished copying */
1356 (void) fprintf(ftable
, " YYSTYPE;\n");
1358 (void) fprintf(fdefine
,
1359 " YYSTYPE;\nextern YYSTYPE yylval;\n");
1369 /* copies code between \{ and \} */
1378 (void) fprintf(ftable
, "\n# line %d \"%s\"\n", lineno
, infile
);
1381 if ((c
= getwc(finput
)) == L
'}')
1384 (void) putwc(L
'\\', ftable
);
1385 } else if (c
== L
'%') {
1386 if ((c
= getwc(finput
)) == L
'}')
1389 (void) putwc(L
'%', ftable
);
1391 (void) putwc(c
, ftable
);
1397 * TRANSLATION_NOTE -- This is a message from yacc.
1398 * This message is passed to error() function.
1399 * Do not translate %%}.
1408 /* skip over comments */
1409 int c
, i
= 0; /* i is the number of lines skipped */
1411 /* skipcom is called after reading a / */
1413 if (getwc(finput
) != L
'*')
1415 "illegal comment"));
1419 if ((c
= getwc(finput
)) == L
'/')
1427 * TRANSLATION_NOTE -- This is a message from yacc.
1428 * This message is passed to error() function.
1429 * EOF -- End Of File.
1432 "EOF inside comment"));
1440 /* copy C action to the next ; or closing } */
1441 int brac
, c
, match
, i
, t
, j
, s
, tok
, argument
, m
;
1442 wchar_t id_name
[NAMESIZE
+1];
1446 (void) fprintf(faction
, "\n# line %d \"%s\"\n", lineno
, infile
);
1457 (void) putwc(c
, faction
);
1468 while ((c
= getwc(finput
)) == L
' ' || c
== L
'\t')
1470 if (c
== L
'<') { /* type description */
1471 (void) ungetwc(c
, finput
);
1472 if (gettok() != TYPENAME
)
1474 * TRANSLATION_NOTE -- This is a message from yacc.
1475 * This message is passed to error() function.
1476 * Do not translate $<ident>
1479 "bad syntax on $<ident> clause"));
1484 (void) fprintf(faction
, "yyval");
1485 if (ntypes
) { /* put out the proper tag... */
1487 tok
= fdtype(*prdptr
[nprod
]);
1488 (void) fprintf(faction
,
1489 WSFMT(".%ws"), typeset
[tok
]);
1496 (void) ungetwc(c
, finput
);
1497 if (gettok() != IDENTIFIER
)
1499 * TRANSLATION_NOTE -- This is a message from yacc.
1500 * This message is passed to error() function.
1501 * Check how action is translated in yacc man page/document.
1504 "bad action format"));
1506 * Save the number of non-terminal
1509 t
= chfind(1, tokname
);
1511 * Check if the identifier is added as a non-terminal
1513 if (id_sw
!= nnonter
)
1517 while ((c
= getwc(finput
)) == L
' ' ||
1521 while ((c
= getwc(finput
)) == L
' ' ||
1526 while (iswdigit(c
)) {
1533 "illegal character \"#\""));
1537 * TRANSLATION_NOTE -- This is a message from yacc.
1538 * This message is passed to error() function.
1539 * Check how action is translated in yacc man page/document.
1542 "illegal action argument no."));
1543 for (i
= 1; i
<= offset
; ++i
)
1544 if (prdptr
[nprod
][i
] == t
)
1545 if (++same
== argument
) {
1546 (void) fprintf(faction
,
1547 "yypvt[-%d]", offset
-i
);
1552 fdtype(prdptr
[nprod
][i
]);
1553 (void) fprintf(faction
,
1560 * This used to be handled as error.
1561 * Treat this as a valid C statement.
1562 * (Likely id with $ in.)
1563 * If non-terminal is added, remove it from the list.
1565 (void) fprintf(faction
, WSFMT("$%ws"), tokname
);
1567 * TRANSLATION_NOTE -- This is a message from yacc.
1568 * This message is passed to warning() function.
1569 * Do not translate Ansi C.
1572 "Illegal character '$' in Ansi C symbol: %ws$%ws."),
1585 while (iswdigit(c
)) {
1586 j
= j
*10 + c
- L
'0';
1592 * TRANSLATION_NOTE -- This is a message from yacc.
1593 * This message is passed to error() function.
1594 * Do not translate $%d.
1597 "Illegal use of $%d"),
1600 (void) fprintf(faction
, "yypvt[-%d]", -j
);
1601 if (ntypes
) { /* put out the proper tag */
1602 if (j
+ offset
<= 0 && tok
< 0)
1604 * TRANSLATION_NOTE -- This is a message from yacc.
1605 * This message is passed to error() function.
1606 * Do not translate $%d.
1609 "must specify type of $%d"),
1612 tok
= fdtype(prdptr
[nprod
][j
+offset
]);
1613 (void) fprintf(faction
,
1614 WSFMT(".%ws"), typeset
[tok
]);
1618 (void) putwc(L
'$', faction
);
1620 (void) putwc(L
'-', faction
);
1625 (void) putwc(c
, faction
);
1627 case L
'/': /* look for comments */
1628 (void) putwc(c
, faction
);
1632 /* it really is a comment */
1633 (void) putwc(c
, faction
);
1637 (void) putwc(c
, faction
);
1638 if ((c
= getwc(finput
)) == L
'/')
1641 (void) putwc(c
, faction
);
1646 error("EOF inside comment");
1648 case L
'\'': /* character constant */
1649 case L
'"': /* character string */
1651 (void) putwc(c
, faction
);
1652 while ((c
= getwc(finput
)) != EOF
) {
1654 (void) putwc(c
, faction
);
1658 } else if (c
== match
)
1660 else if (c
== L
'\n')
1662 * TRANSLATION_NOTE -- This is a message from yacc.
1663 * This message is passed to error() function.
1664 * This error message is issued when
1665 * quoted string has multiple lines.
1668 "newline in string or char. const."));
1669 (void) putwc(c
, faction
);
1672 "EOF in string or character constant"));
1676 * TRANSLATION_NOTE -- This is a message from yacc.
1677 * This message is passed to error() function.
1678 * Check how 'action' is translated in yacc mapage/document.
1681 "action does not terminate"));
1688 (void) putwc(c
, faction
);
1690 * Save the possible identifier name.
1691 * Used to print out a warning message.
1693 if (id_idx
>= NAMESIZE
) {
1695 * Error. Silently ignore.
1700 * If c has a possibility to be a
1701 * part of identifier, save it.
1703 else if (iswalnum(c
) || c
== L
'_') {
1704 id_name
[id_idx
++] = c
;
1705 id_name
[id_idx
] = 0;
1708 id_name
[id_idx
] = 0;
1714 lhsfill(s
) /* new rule, dump old (if exists), restart strings */
1717 static int lhs_len
= LHS_TEXT_LEN
;
1718 int s_lhs
= wslen(s
);
1719 if (s_lhs
>= lhs_len
) {
1720 lhs_len
= s_lhs
+ 2;
1721 lhstext
= (wchar_t *)
1722 realloc((char *)lhstext
, sizeof (wchar_t)*lhs_len
);
1723 if (lhstext
== NULL
)
1725 * TRANSLATION_NOTE -- This is a message from yacc.
1726 * This message is passed to error() function.
1727 * LHS -- Left Hand Side.
1730 "couldn't expanded LHS length"));
1732 rhsfill((wchar_t *)0);
1733 (void) wscpy(lhstext
, s
); /* don't worry about too long of a name */
1738 wchar_t *s
; /* either name or 0 */
1740 static wchar_t *loc
; /* next free location in rhstext */
1741 static int rhs_len
= RHS_TEXT_LEN
;
1742 static int used
= 0;
1743 int s_rhs
= (s
== NULL
? 0 : wslen(s
));
1744 register wchar_t *p
;
1746 if (!s
) /* print out and erase old text */
1748 if (*lhstext
) /* there was an old rule - dump it */
1750 (loc
= rhstext
)[0] = 0;
1753 /* add to stuff in rhstext */
1756 used
= loc
- rhstext
;
1757 if ((s_rhs
+ 3) >= (rhs_len
- used
)) {
1758 static wchar_t *textbase
;
1760 rhs_len
+= s_rhs
+ RHS_TEXT_LEN
;
1761 rhstext
= (wchar_t *)
1762 realloc((char *)rhstext
, sizeof (wchar_t)*rhs_len
);
1763 if (rhstext
== NULL
)
1765 * TRANSLATION_NOTE -- This is a message from yacc.
1766 * This message is passed to error() function.
1767 * RHS -- Right Hand Side.
1770 "couldn't expanded RHS length"));
1771 loc
= loc
- textbase
+ rhstext
;
1775 if (*s
== L
' ') /* special quoted symbol */
1777 *loc
++ = L
'\''; /* add first quote */
1781 if (loc
++ > &rhstext
[ RHS_TEXT_LEN
] - 3)
1786 *loc
= 0; /* terminate the string */
1790 lrprnt() /* print out the left and right hand sides */
1793 wchar_t *m_rhs
= NULL
;
1795 if (!*rhstext
) /* empty rhs - print usual comment */
1796 rhs
= L
" /* empty */";
1798 int idx1
; /* tmp idx used to find if there are d_quotes */
1799 int idx2
; /* tmp idx used to generate escaped string */
1802 * Check if there are any double quote in RHS.
1804 for (idx1
= 0; rhstext
[idx1
] != 0; idx1
++) {
1805 if (rhstext
[idx1
] == L
'"') {
1807 * A double quote is found.
1809 idx2
= wslen(rhstext
)*2;
1810 p
= m_rhs
= (wchar_t *)
1811 malloc((idx2
+ 1)*sizeof (wchar_t));
1814 * TRANSLATION_NOTE -- This is a message from yacc.
1815 * This message is passed to error() function.
1816 * RHS - Right Hand Side.
1818 * You may just translate this as:
1819 * 'Could not allocate internally used memory.'
1822 "Couldn't allocate memory for RHS."));
1826 for (idx2
= 0; rhstext
[idx2
] != 0; idx2
++) {
1828 * Check if this quote is escaped or not
1830 if (rhstext
[idx2
] == L
'"') {
1833 while (tmp_l
>= 0 &&
1834 rhstext
[tmp_l
] == '\\') {
1839 * If quote is not escaped,
1845 *p
++ = rhstext
[idx2
];
1849 * Break from the loop
1859 (void) fprintf(fdebug
, WSFMT("\t\"%ws :%ws\",\n"), lhstext
, rhs
);
1865 beg_debug() /* dump initial sequence for fdebug file */
1867 (void) fprintf(fdebug
,
1868 "typedef struct\n");
1869 (void) fprintf(fdebug
,
1870 "#ifdef __cplusplus\n\tyytoktype\n");
1871 (void) fprintf(fdebug
, "#endif\n{\n");
1872 (void) fprintf(fdebug
,
1873 "#ifdef __cplusplus\nconst\n#endif\n");
1874 (void) fprintf(fdebug
, "char *t_name; int t_val; } yytoktype;\n");
1875 (void) fprintf(fdebug
,
1876 "#ifndef YYDEBUG\n#\tdefine YYDEBUG\t%d", gen_testing
);
1877 (void) fprintf(fdebug
, "\t/*%sallow debugging */\n#endif\n\n",
1878 gen_testing
? " " : " don't ");
1879 (void) fprintf(fdebug
, "#if YYDEBUG\n\nyytoktype yytoks[] =\n{\n");
1884 end_toks() /* finish yytoks array, get ready for yyred's strings */
1886 (void) fprintf(fdebug
, "\t\"-unknown-\",\t-1\t/* ends search */\n");
1887 (void) fprintf(fdebug
, "};\n\n");
1888 (void) fprintf(fdebug
,
1889 "#ifdef __cplusplus\nconst\n#endif\n");
1890 (void) fprintf(fdebug
, "char * yyreds[] =\n{\n");
1891 (void) fprintf(fdebug
, "\t\"-no such reduction-\",\n");
1896 end_debug() /* finish yyred array, close file */
1898 lrprnt(); /* dump last lhs, rhs */
1899 (void) fprintf(fdebug
, "};\n#endif /* YYDEBUG */\n");
1900 (void) fclose(fdebug
);
1906 * The normal length for token sizes is NAMESIZE - If a token is
1907 * seen that has a longer length, expand "tokname" by NAMESIZE.
1912 toksize
+= NAMESIZE
;
1913 tokname
= (wchar_t *)
1914 reallocarray(tokname
, toksize
, sizeof (wchar_t));
1928 prdptr
= reallocarray(prdptr
, nprodsz
+ 2, sizeof (int *));
1929 levprd
= reallocarray(levprd
, nprodsz
+ 2, sizeof (int));
1930 had_act
= (wchar_t *)
1931 reallocarray(had_act
, nprodsz
+ 2, sizeof (wchar_t));
1932 for (i
= nprodsz
-NPROD
; i
< nprodsz
+2; ++i
)
1935 if ((*prdptr
== NULL
) || (levprd
== NULL
) || (had_act
== NULL
))
1937 * TRANSLATION_NOTE -- This is a message from yacc.
1938 * This message is passed to error() function.
1940 * You may just translate this as:
1941 * 'Could not allocate internally used memory.'
1944 "couldn't expand productions"));
1949 * Expand the number of terminals. Initially there are NTERMS;
1950 * each time space runs out, the size is increased by NTERMS.
1951 * The total size, however, cannot exceed MAXTERMS because of
1952 * the way LOOKSETS(struct looksets) is set up.
1954 * tokset, toklev : increased to ntoksz
1956 * tables with initial dimensions of TEMPSIZE must be changed if
1957 * (ntoksz + NNONTERM) >= TEMPSIZE : temp1[]
1964 tokset
= reallocarray((char *)tokset
, ntoksz
, sizeof (TOKSYMB
));
1965 toklev
= reallocarray(toklev
, ntoksz
, sizeof (int));
1967 if ((tokset
== NULL
) || (toklev
== NULL
))
1969 * TRANSLATION_NOTE -- This is a message from yacc.
1970 * This message is passed to error() function.
1971 * Do not translate NTERMS.
1973 * You may just translate this as:
1974 * 'Could not allocate internally used memory.'
1977 "couldn't expand NTERMS"));
1984 nnontersz
+= NNONTERM
;
1986 nontrst
= (NTSYMB
*)
1987 reallocarray(nontrst
, nnontersz
, sizeof (TOKSYMB
));
1988 if (nontrst
== NULL
)
1990 * TRANSLATION_NOTE -- This is a message from yacc.
1991 * This message is passed to error() function.
1992 * Do not translate NTERMS.
1994 * You may just translate this as:
1995 * 'Could not allocate internally used memory.'
1998 "couldn't expand NNONTERM"));
2006 static int *membase
;
2007 new_memsize
+= MEMSIZE
;
2011 realloc((char *)tracemem
, sizeof (int) * new_memsize
);
2012 if (tracemem
== NULL
)
2014 * TRANSLATION_NOTE -- This is a message from yacc.
2015 * This message is passed to error() function.
2017 * You may just translate this as:
2018 * 'Could not allocate internally used memory.'
2021 "couldn't expand mem table"));
2023 for (i
= 0; i
<= nprod
; ++i
)
2024 prdptr
[i
] = prdptr
[i
] - membase
+ tracemem
;
2025 mem
= mem
- membase
+ tracemem
;
2028 temp1
= (int *)realloc((char *)temp1
, sizeof (int)*size
);
2029 optimmem
= optimmem
- membase
+ tracemem
;
2037 * findchtok(chlit) returns the token number for a character literal
2038 * chlit that is "bigger" than 255 -- the max char value that the
2039 * original yacc was build for. This yacc treate them as though
2040 * an ordinary token.
2046 return (chlit
); /* single-byte char */
2047 for (i
= 0; i
< nmbchars
; ++i
) {
2048 if (mbchars
->character
== chlit
)
2049 return (mbchars
->tvalue
);
2052 /* Not found. Register it! */
2053 if (++nmbchars
> nmbcharsz
) { /* Make sure there's enough space */
2054 nmbcharsz
+= NMBCHARSZ
;
2055 mbchars
= (MBCLIT
*)
2056 realloc((char *)mbchars
, sizeof (MBCLIT
)*nmbcharsz
);
2057 if (mbchars
== NULL
)
2059 "too many character literals"));
2061 mbchars
[nmbchars
-1].character
= chlit
;
2062 return (mbchars
[nmbchars
-1].tvalue
= extval
++);
2063 /* Return the newly assigned token. */
2067 * When -p is specified, symbol prefix for
2068 * yy{parse, lex, error}(),
2069 * yy{lval, val, char, debug, errflag, nerrs}
2070 * are defined to the specified name.
2073 put_prefix_define(char *pre
)
2090 for (i
= 0; syms
[i
]; i
++)
2091 (void) fprintf(ftable
, "#define\tyy%s\t%s%s\n",
2092 syms
[i
], pre
, syms
[i
]);