1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1989 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Robert Paul Corbett.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-12-10 21:13:08 +00:00
|
|
|
#if 0
|
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)main.c 5.5 (Berkeley) 5/24/93";
|
|
|
|
#endif
|
|
|
|
#endif
|
2001-12-02 21:24:03 +00:00
|
|
|
|
2001-12-10 21:13:08 +00:00
|
|
|
#include <sys/cdefs.h>
|
2001-12-02 21:24:03 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-04 22:15:13 +00:00
|
|
|
#include <paths.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <signal.h>
|
1997-01-12 21:16:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
char dflag;
|
|
|
|
char lflag;
|
|
|
|
char rflag;
|
|
|
|
char tflag;
|
|
|
|
char vflag;
|
|
|
|
|
2001-12-01 17:34:42 +00:00
|
|
|
const char *symbol_prefix;
|
|
|
|
const char *file_prefix = "y";
|
|
|
|
char temp_form[] = "yacc.XXXXXXXXXXX";
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int lineno;
|
|
|
|
int outline;
|
|
|
|
|
|
|
|
char *action_file_name;
|
|
|
|
char *code_file_name;
|
|
|
|
char *defines_file_name;
|
2001-12-01 17:34:42 +00:00
|
|
|
const char *input_file_name = "";
|
1994-05-27 12:33:43 +00:00
|
|
|
char *output_file_name;
|
|
|
|
char *text_file_name;
|
|
|
|
char *union_file_name;
|
|
|
|
char *verbose_file_name;
|
|
|
|
|
|
|
|
FILE *action_file; /* a temp file, used to save actions associated */
|
|
|
|
/* with rules until the parser is written */
|
|
|
|
FILE *code_file; /* y.code.c (used when the -r option is specified) */
|
|
|
|
FILE *defines_file; /* y.tab.h */
|
|
|
|
FILE *input_file; /* the input file */
|
|
|
|
FILE *output_file; /* y.tab.c */
|
|
|
|
FILE *text_file; /* a temp file, used to save text until all */
|
|
|
|
/* symbols have been defined */
|
|
|
|
FILE *union_file; /* a temp file, used to save the union */
|
|
|
|
/* definition until all symbol have been */
|
|
|
|
/* defined */
|
|
|
|
FILE *verbose_file; /* y.output */
|
|
|
|
|
|
|
|
int nitems;
|
|
|
|
int nrules;
|
|
|
|
int nsyms;
|
|
|
|
int ntokens;
|
|
|
|
int nvars;
|
|
|
|
|
|
|
|
int start_symbol;
|
|
|
|
char **symbol_name;
|
|
|
|
short *symbol_value;
|
|
|
|
short *symbol_prec;
|
|
|
|
char *symbol_assoc;
|
|
|
|
|
|
|
|
short *ritem;
|
|
|
|
short *rlhs;
|
|
|
|
short *rrhs;
|
|
|
|
short *rprec;
|
|
|
|
char *rassoc;
|
|
|
|
short **derives;
|
|
|
|
char *nullable;
|
|
|
|
|
2002-03-22 01:42:45 +00:00
|
|
|
static void create_file_names(void);
|
|
|
|
static void getargs(int, char **);
|
|
|
|
static void onintr(int);
|
|
|
|
static void open_files(void);
|
|
|
|
static void set_signals(void);
|
|
|
|
static void usage(void);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-04 22:15:13 +00:00
|
|
|
volatile sig_atomic_t sigdie;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-04 22:15:13 +00:00
|
|
|
__dead2 void
|
2010-11-07 22:51:54 +00:00
|
|
|
done(int k)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
if (action_file) { fclose(action_file); unlink(action_file_name); }
|
|
|
|
if (text_file) { fclose(text_file); unlink(text_file_name); }
|
|
|
|
if (union_file) { fclose(union_file); unlink(union_file_name); }
|
2002-04-04 22:15:13 +00:00
|
|
|
if (sigdie) { _exit(k); }
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-01-12 21:16:25 +00:00
|
|
|
static void
|
2010-11-07 22:51:54 +00:00
|
|
|
onintr(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-04 22:15:13 +00:00
|
|
|
sigdie = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
done(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-01-12 21:16:25 +00:00
|
|
|
static void
|
2010-11-07 22:51:54 +00:00
|
|
|
set_signals(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
#ifdef SIGINT
|
|
|
|
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
|
|
|
|
signal(SIGINT, onintr);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
|
|
|
|
signal(SIGTERM, onintr);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGHUP
|
|
|
|
if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
|
|
|
|
signal(SIGHUP, onintr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-01-12 21:16:25 +00:00
|
|
|
static void
|
2010-11-07 22:51:54 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
1997-08-28 06:33:53 +00:00
|
|
|
fprintf(stderr, "%s\n%s\n",
|
|
|
|
"usage: yacc [-dlrtv] [-b file_prefix] [-o output_filename]",
|
|
|
|
" [-p symbol_prefix] filename");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-01-12 21:16:25 +00:00
|
|
|
static void
|
2010-11-07 22:51:54 +00:00
|
|
|
getargs(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-03-05 01:52:09 +00:00
|
|
|
int ch;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2010-11-07 23:44:40 +00:00
|
|
|
while ((ch = getopt(argc, argv, "b:dlo:p:rtvy")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-03-05 01:52:09 +00:00
|
|
|
switch (ch)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
case 'b':
|
2004-03-05 01:52:09 +00:00
|
|
|
file_prefix = optarg;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
|
|
|
|
1997-02-16 01:50:25 +00:00
|
|
|
case 'o':
|
2004-03-05 01:52:09 +00:00
|
|
|
output_file_name = optarg;
|
|
|
|
break;
|
1997-02-16 01:50:25 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'p':
|
2004-03-05 01:52:09 +00:00
|
|
|
symbol_prefix = optarg;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
tflag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
vflag = 1;
|
|
|
|
break;
|
|
|
|
|
2010-11-07 23:44:40 +00:00
|
|
|
case 'y':
|
|
|
|
/* for bison compatibility -- byacc is already POSIX compatible */
|
|
|
|
break;
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-05 01:52:09 +00:00
|
|
|
if (optind + 1 != argc)
|
|
|
|
usage();
|
|
|
|
if (strcmp(argv[optind], "-") == 0)
|
|
|
|
input_file = stdin;
|
|
|
|
else
|
|
|
|
input_file_name = argv[optind];
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-07 23:22:42 +00:00
|
|
|
void *
|
|
|
|
allocate(size_t n)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2010-11-07 23:22:42 +00:00
|
|
|
void *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
p = NULL;
|
|
|
|
if (n)
|
|
|
|
{
|
2010-11-07 23:22:42 +00:00
|
|
|
p = calloc(1, n);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!p) no_space();
|
|
|
|
}
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-01-12 21:16:25 +00:00
|
|
|
static void
|
2010-11-07 22:51:54 +00:00
|
|
|
create_file_names(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int i, len;
|
2001-12-01 17:34:42 +00:00
|
|
|
const char *tmpdir;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-04 22:15:13 +00:00
|
|
|
if (!(tmpdir = getenv("TMPDIR")))
|
|
|
|
tmpdir = _PATH_TMP;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
len = strlen(tmpdir);
|
2002-04-04 22:15:13 +00:00
|
|
|
i = len + strlen(temp_form) + 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
if (len && tmpdir[len-1] != '/')
|
|
|
|
++i;
|
|
|
|
|
2010-11-07 23:22:42 +00:00
|
|
|
action_file_name = malloc(i);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (action_file_name == 0) no_space();
|
2010-11-07 23:22:42 +00:00
|
|
|
text_file_name = malloc(i);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (text_file_name == 0) no_space();
|
2010-11-07 23:22:42 +00:00
|
|
|
union_file_name = malloc(i);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (union_file_name == 0) no_space();
|
|
|
|
|
|
|
|
strcpy(action_file_name, tmpdir);
|
|
|
|
strcpy(text_file_name, tmpdir);
|
|
|
|
strcpy(union_file_name, tmpdir);
|
|
|
|
|
|
|
|
if (len && tmpdir[len - 1] != '/')
|
|
|
|
{
|
|
|
|
action_file_name[len] = '/';
|
|
|
|
text_file_name[len] = '/';
|
|
|
|
union_file_name[len] = '/';
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(action_file_name + len, temp_form);
|
|
|
|
strcpy(text_file_name + len, temp_form);
|
|
|
|
strcpy(union_file_name + len, temp_form);
|
|
|
|
|
|
|
|
action_file_name[len + 5] = 'a';
|
|
|
|
text_file_name[len + 5] = 't';
|
|
|
|
union_file_name[len + 5] = 'u';
|
|
|
|
|
1997-02-16 01:50:25 +00:00
|
|
|
if (output_file_name != 0)
|
|
|
|
{
|
|
|
|
file_prefix = output_file_name;
|
|
|
|
len = strlen(file_prefix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = strlen(file_prefix);
|
2010-11-07 23:22:42 +00:00
|
|
|
output_file_name = malloc(len + 7);
|
1997-02-16 01:50:25 +00:00
|
|
|
if (output_file_name == 0)
|
|
|
|
no_space();
|
|
|
|
strcpy(output_file_name, file_prefix);
|
|
|
|
strcpy(output_file_name + len, OUTPUT_SUFFIX);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (rflag)
|
|
|
|
{
|
2010-11-07 23:22:42 +00:00
|
|
|
code_file_name = malloc(len + 8);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (code_file_name == 0)
|
|
|
|
no_space();
|
|
|
|
strcpy(code_file_name, file_prefix);
|
1997-02-16 01:50:25 +00:00
|
|
|
if (file_prefix == output_file_name)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* XXX ".tab.c" here is OUTPUT_SUFFIX, but since its length is
|
|
|
|
* in various magic numbers, don't bother using the macro.
|
|
|
|
*/
|
|
|
|
if (len >= 6 && strcmp(code_file_name + len - 6, ".tab.c") == 0)
|
|
|
|
strcpy(code_file_name + len - 6, CODE_SUFFIX);
|
|
|
|
else if (len >= 2 && strcmp(code_file_name + len - 2, ".c") == 0)
|
|
|
|
strcpy(code_file_name + len - 2, CODE_SUFFIX);
|
|
|
|
else
|
|
|
|
strcpy(code_file_name + len, CODE_SUFFIX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(code_file_name + len, CODE_SUFFIX);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
code_file_name = output_file_name;
|
|
|
|
|
|
|
|
if (dflag)
|
|
|
|
{
|
2010-11-07 23:22:42 +00:00
|
|
|
defines_file_name = malloc(len + 7);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (defines_file_name == 0)
|
|
|
|
no_space();
|
|
|
|
strcpy(defines_file_name, file_prefix);
|
1997-02-16 01:50:25 +00:00
|
|
|
if (file_prefix == output_file_name)
|
|
|
|
{
|
|
|
|
#define BISON_DEFINES_SUFFIX ".h"
|
|
|
|
if (len >= 2 && strcmp(defines_file_name + len - 2, ".c") == 0)
|
|
|
|
strcpy(defines_file_name + len - 2, BISON_DEFINES_SUFFIX);
|
|
|
|
else
|
|
|
|
strcpy(defines_file_name + len, BISON_DEFINES_SUFFIX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(defines_file_name + len, DEFINES_SUFFIX);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vflag)
|
|
|
|
{
|
2010-11-07 23:22:42 +00:00
|
|
|
verbose_file_name = malloc(len + 8);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (verbose_file_name == 0)
|
|
|
|
no_space();
|
|
|
|
strcpy(verbose_file_name, file_prefix);
|
1997-02-16 01:50:25 +00:00
|
|
|
if (file_prefix == output_file_name)
|
|
|
|
{
|
|
|
|
if (len >= 6 && strcmp(verbose_file_name + len - 6, ".tab.c") == 0)
|
|
|
|
strcpy(verbose_file_name + len - 6, VERBOSE_SUFFIX);
|
|
|
|
else if (len >= 2 && strcmp(verbose_file_name + len - 2, ".c") == 0)
|
|
|
|
strcpy(verbose_file_name + len - 2, VERBOSE_SUFFIX);
|
|
|
|
else
|
|
|
|
strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-01-12 21:16:25 +00:00
|
|
|
static void
|
2010-11-07 22:51:54 +00:00
|
|
|
open_files(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
1998-06-09 04:20:51 +00:00
|
|
|
int fd;
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
create_file_names();
|
|
|
|
|
|
|
|
if (input_file == 0)
|
|
|
|
{
|
|
|
|
input_file = fopen(input_file_name, "r");
|
|
|
|
if (input_file == 0)
|
|
|
|
open_error(input_file_name);
|
|
|
|
}
|
|
|
|
|
1998-06-09 04:20:51 +00:00
|
|
|
fd = mkstemp(action_file_name);
|
|
|
|
if (fd < 0 || (action_file = fdopen(fd, "w")) == NULL) {
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
1994-05-27 12:33:43 +00:00
|
|
|
open_error(action_file_name);
|
1998-06-09 04:20:51 +00:00
|
|
|
}
|
|
|
|
fd = mkstemp(text_file_name);
|
|
|
|
if (fd < 0 || (text_file = fdopen(fd, "w")) == NULL) {
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
open_error(text_file_name);
|
|
|
|
}
|
|
|
|
fd = mkstemp(union_file_name);
|
|
|
|
if (fd < 0 || (union_file = fdopen(fd, "w")) == NULL) {
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
open_error(union_file_name);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
text_file = fopen(text_file_name, "w");
|
|
|
|
if (text_file == 0)
|
|
|
|
open_error(text_file_name);
|
|
|
|
|
|
|
|
if (vflag)
|
|
|
|
{
|
|
|
|
verbose_file = fopen(verbose_file_name, "w");
|
|
|
|
if (verbose_file == 0)
|
|
|
|
open_error(verbose_file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dflag)
|
|
|
|
{
|
|
|
|
defines_file = fopen(defines_file_name, "w");
|
|
|
|
if (defines_file == 0)
|
|
|
|
open_error(defines_file_name);
|
|
|
|
union_file = fopen(union_file_name, "w");
|
|
|
|
if (union_file == 0)
|
|
|
|
open_error(union_file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
output_file = fopen(output_file_name, "w");
|
|
|
|
if (output_file == 0)
|
|
|
|
open_error(output_file_name);
|
|
|
|
|
|
|
|
if (rflag)
|
|
|
|
{
|
|
|
|
code_file = fopen(code_file_name, "w");
|
|
|
|
if (code_file == 0)
|
|
|
|
open_error(code_file_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
code_file = output_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2002-04-04 22:15:56 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
set_signals();
|
|
|
|
getargs(argc, argv);
|
|
|
|
open_files();
|
|
|
|
reader();
|
|
|
|
lr0();
|
|
|
|
lalr();
|
|
|
|
make_parser();
|
|
|
|
verbose();
|
|
|
|
output();
|
|
|
|
done(0);
|
|
|
|
/*NOTREACHED*/
|
1997-01-12 21:16:25 +00:00
|
|
|
return (0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|