Berkeley YACC  1993-03-03
Berkeley's version of Yet Another Compiler Compiler
 All Data Structures Files Functions Variables Typedefs Macros Groups
main.c File Reference
#include <signal.h>
#include "defs.h"
+ Include dependency graph for main.c:

Go to the source code of this file.

Functions

char * mktemp ()
 
char * getenv ()
 
 done (int k)
 Shutdown function. More...
 
 onintr ()
 Signal handler callback. More...
 
 set_signals ()
 Sets the appropriate signal handlers up. More...
 
 usage ()
 
 getargs (int argc, argv)
 
char * allocate (unsigned n)
 
 create_file_names ()
 
 open_files ()
 
int main (int argc, argv)
 

Variables

char dflag
 
char lflag
 
char rflag
 
char tflag
 
char vflag
 
char * symbol_prefix
 
char * file_prefix = "y"
 
char * myname = "yacc"
 
char * temp_form = "yacc.XXXXXXX"
 
int lineno
 
int outline
 
char * action_file_name
 
char * code_file_name
 
char * defines_file_name
 
char * input_file_name = ""
 
char * output_file_name
 
char * text_file_name
 
char * union_file_name
 
char * verbose_file_name
 
FILE * action_file
 
FILE * code_file
 
FILE * defines_file
 
FILE * input_file
 
FILE * output_file
 
FILE * text_file
 
FILE * union_file
 
FILE * verbose_file
 
int nitems
 
int nrules
 The number of rules in the grammar. More...
 
int nsyms
 The number of symbols (terminals + non-terminals) in the grammar. More...
 
int ntokens
 The number of tokens (terminals) in the grammar. More...
 
int nvars
 The number of variables (non-terminals) in the grammar. More...
 
int start_symbol
 Index of the starting symbol of the grammar. More...
 
char ** symbol_name
 Array of symbol names. More...
 
short * symbol_value
 
short * symbol_prec
 
char * symbol_assoc
 
short * ritem
 Representation of all productions (and items) More...
 
short * rlhs
 List of left-hand sides of all rules. More...
 
short * rrhs
 List of right-hand sides of all rules. More...
 
short * rprec
 
char * rassoc
 
short ** derives
 List of rules that derive each non-terminal. More...
 
char * nullable
 

Function Documentation

char* allocate ( unsigned  n)

Definition at line 315 of file main.c.

References CALLOC, and no_space().

317 {
318  register char *p;
319 
320  p = NULL;
321  if (n)
322  {
323  p = CALLOC(1, n);
324  if (!p) no_space();
325  }
326  return (p);
327 }
#define CALLOC(k, n)
Definition: defs.h:101
no_space()
Definition: error.c:27

+ Here is the call graph for this function:

create_file_names ( )

Definition at line 330 of file main.c.

References action_file_name, code_file_name, CODE_SUFFIX, defines_file_name, DEFINES_SUFFIX, dflag, file_prefix, getenv(), MALLOC, mktemp(), no_space(), output_file_name, OUTPUT_SUFFIX, rflag, strcpy(), temp_form, text_file_name, union_file_name, verbose_file_name, VERBOSE_SUFFIX, and vflag.

Referenced by open_files().

331 {
332  int i, len;
333  char *tmpdir;
334 
335  tmpdir = getenv("TMPDIR");
336  if (tmpdir == 0) tmpdir = "/tmp";
337 
338  len = strlen(tmpdir);
339  i = len + 13;
340  if (len && tmpdir[len-1] != '/')
341  ++i;
342 
344  if (action_file_name == 0) no_space();
345  text_file_name = MALLOC(i);
346  if (text_file_name == 0) no_space();
347  union_file_name = MALLOC(i);
348  if (union_file_name == 0) no_space();
349 
350  strcpy(action_file_name, tmpdir);
351  strcpy(text_file_name, tmpdir);
352  strcpy(union_file_name, tmpdir);
353 
354  if (len && tmpdir[len - 1] != '/')
355  {
356  action_file_name[len] = '/';
357  text_file_name[len] = '/';
358  union_file_name[len] = '/';
359  ++len;
360  }
361 
365 
366  action_file_name[len + 5] = 'a';
367  text_file_name[len + 5] = 't';
368  union_file_name[len + 5] = 'u';
369 
373 
374  len = strlen(file_prefix);
375 
376  output_file_name = MALLOC(len + 7);
377  if (output_file_name == 0)
378  no_space();
381 
382  if (rflag)
383  {
384  code_file_name = MALLOC(len + 8);
385  if (code_file_name == 0)
386  no_space();
389  }
390  else
392 
393  if (dflag)
394  {
395  defines_file_name = MALLOC(len + 7);
396  if (defines_file_name == 0)
397  no_space();
400  }
401 
402  if (vflag)
403  {
404  verbose_file_name = MALLOC(len + 8);
405  if (verbose_file_name == 0)
406  no_space();
409  }
410 }
char * file_prefix
Definition: main.c:11
#define VERBOSE_SUFFIX
Definition: defs.h:51
char * defines_file_name
Definition: main.c:20
#define OUTPUT_SUFFIX
Definition: defs.h:50
#define MALLOC(n)
Definition: defs.h:103
char * output_file_name
Definition: main.c:22
char * action_file_name
Definition: main.c:18
char * code_file_name
Definition: main.c:19
char rflag
Definition: main.c:6
char * text_file_name
Definition: main.c:23
char * union_file_name
Definition: main.c:24
char * temp_form
Definition: main.c:13
no_space()
Definition: error.c:27
char * getenv()
char * mktemp()
char vflag
Definition: main.c:8
char * verbose_file_name
Definition: main.c:25
char * strcpy()
#define CODE_SUFFIX
Definition: defs.h:48
char dflag
Definition: main.c:4
#define DEFINES_SUFFIX
Definition: defs.h:49

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

getargs ( int  argc,
argv   
)

Definition at line 209 of file main.c.

References dflag, file_prefix, input_file, input_file_name, lflag, myname, rflag, symbol_prefix, tflag, usage(), and vflag.

Referenced by main().

212 {
213  register int i;
214  register char *s;
215 
216  if (argc > 0) myname = argv[0];
217  for (i = 1; i < argc; ++i)
218  {
219  s = argv[i];
220  if (*s != '-') break;
221  switch (*++s)
222  {
223  case '\0':
224  input_file = stdin;
225  if (i + 1 < argc) usage();
226  return;
227 
228  case '-':
229  ++i;
230  goto no_more_options;
231 
232  case 'b':
233  if (*++s)
234  file_prefix = s;
235  else if (++i < argc)
236  file_prefix = argv[i];
237  else
238  usage();
239  continue;
240 
241  case 'd':
242  dflag = 1;
243  break;
244 
245  case 'l':
246  lflag = 1;
247  break;
248 
249  case 'p':
250  if (*++s)
251  symbol_prefix = s;
252  else if (++i < argc)
253  symbol_prefix = argv[i];
254  else
255  usage();
256  continue;
257 
258  case 'r':
259  rflag = 1;
260  break;
261 
262  case 't':
263  tflag = 1;
264  break;
265 
266  case 'v':
267  vflag = 1;
268  break;
269 
270  default:
271  usage();
272  }
273 
274  for (;;)
275  {
276  switch (*++s)
277  {
278  case '\0':
279  goto end_of_option;
280 
281  case 'd':
282  dflag = 1;
283  break;
284 
285  case 'l':
286  lflag = 1;
287  break;
288 
289  case 'r':
290  rflag = 1;
291  break;
292 
293  case 't':
294  tflag = 1;
295  break;
296 
297  case 'v':
298  vflag = 1;
299  break;
300 
301  default:
302  usage();
303  }
304  }
305 end_of_option:;
306  }
307 
308 no_more_options:;
309  if (i + 1 != argc) usage();
310  input_file_name = argv[i];
311 }
char * file_prefix
Definition: main.c:11
char * symbol_prefix
Definition: main.c:10
char lflag
Definition: main.c:5
char * input_file_name
Definition: main.c:21
char tflag
Definition: main.c:7
char rflag
Definition: main.c:6
usage()
Definition: main.c:202
char vflag
Definition: main.c:8
char * myname
Definition: main.c:12
FILE * input_file
Definition: main.c:31
char dflag
Definition: main.c:4

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char* getenv ( )

Referenced by create_file_names().

+ Here is the caller graph for this function:

int main ( int  argc,
argv   
)

Definition at line 465 of file main.c.

References done(), getargs(), lalr(), lr0(), make_parser(), open_files(), output(), reader(), set_signals(), and verbose().

468 {
469  set_signals();
470  getargs(argc, argv);
471  open_files();
472  reader();
473  lr0();
474  lalr();
475  make_parser();
476  verbose();
477  output();
478  done(0);
479  /*NOTREACHED*/
480 }
lalr()
Definition: lalr.c:37
set_signals()
Sets the appropriate signal handlers up.
Definition: main.c:185
done(int k)
Shutdown function.
Definition: main.c:156
lr0()
Definition: lr0.c:593
getargs(int argc, argv)
Definition: main.c:209
verbose()
Definition: verbose.c:7
open_files()
Definition: main.c:413
make_parser()
Definition: mkpar.c:23
output()
Definition: output.c:20
reader()
Definition: reader.c:1775

+ Here is the call graph for this function:

char* mktemp ( )

Referenced by create_file_names().

+ Here is the caller graph for this function:

open_files ( )

Definition at line 413 of file main.c.

References action_file, action_file_name, code_file, code_file_name, create_file_names(), defines_file, defines_file_name, dflag, input_file, input_file_name, open_error(), output_file, output_file_name, rflag, text_file, text_file_name, union_file, union_file_name, verbose_file, verbose_file_name, and vflag.

Referenced by main().

414 {
416 
417  if (input_file == 0)
418  {
419  input_file = fopen(input_file_name, "r");
420  if (input_file == 0)
422  }
423 
424  action_file = fopen(action_file_name, "w");
425  if (action_file == 0)
427 
428  text_file = fopen(text_file_name, "w");
429  if (text_file == 0)
431 
432  if (vflag)
433  {
434  verbose_file = fopen(verbose_file_name, "w");
435  if (verbose_file == 0)
437  }
438 
439  if (dflag)
440  {
441  defines_file = fopen(defines_file_name, "w");
442  if (defines_file == 0)
444  union_file = fopen(union_file_name, "w");
445  if (union_file == 0)
447  }
448 
449  output_file = fopen(output_file_name, "w");
450  if (output_file == 0)
452 
453  if (rflag)
454  {
455  code_file = fopen(code_file_name, "w");
456  if (code_file == 0)
458  }
459  else
461 }
FILE * code_file
Definition: main.c:29
create_file_names()
Definition: main.c:330
open_error(char *filename)
Definition: error.c:34
char * defines_file_name
Definition: main.c:20
FILE * text_file
Definition: main.c:33
char * output_file_name
Definition: main.c:22
char * action_file_name
Definition: main.c:18
char * code_file_name
Definition: main.c:19
char * input_file_name
Definition: main.c:21
char rflag
Definition: main.c:6
char * text_file_name
Definition: main.c:23
char * union_file_name
Definition: main.c:24
FILE * verbose_file
Definition: main.c:38
FILE * action_file
Definition: main.c:27
FILE * union_file
Definition: main.c:35
char vflag
Definition: main.c:8
FILE * defines_file
Definition: main.c:30
char * verbose_file_name
Definition: main.c:25
FILE * input_file
Definition: main.c:31
FILE * output_file
Definition: main.c:32
char dflag
Definition: main.c:4

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

usage ( )

Definition at line 202 of file main.c.

References myname.

Referenced by getargs().

203 {
204  fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] [-p symbol_prefix] filename\n", myname);
205  exit(1);
206 }
char * myname
Definition: main.c:12

+ Here is the caller graph for this function:

Variable Documentation

FILE* action_file

Definition at line 27 of file main.c.

Referenced by copy_action(), done(), open_files(), and output_semantic_actions().

char* action_file_name

Definition at line 18 of file main.c.

Referenced by create_file_names(), done(), open_files(), and output_semantic_actions().

char* code_file_name
FILE* defines_file

Definition at line 30 of file main.c.

Referenced by open_files(), and output_defines().

char* defines_file_name

Definition at line 20 of file main.c.

Referenced by create_file_names(), and open_files().

short** derives

List of rules that derive each non-terminal.

Array of pointers that associates to each symbol the list of productions that have it as the left-hand side. Each item in those lists is the identifying number of a rule. The first ntokens entries (the ones for terminals) are not set and should not be accessed. They are only present to make the indexes consistent with other arrays (i.e. this array can be indexed using the symbol number).

Allocated and filled in set_first_derives().

Definition at line 140 of file main.c.

Referenced by build_relations(), free_derives(), initialize_states(), set_derives(), set_EFF(), and set_first_derives().

char dflag

Definition at line 4 of file main.c.

Referenced by copy_union(), create_file_names(), getargs(), open_files(), and output_defines().

char* file_prefix = "y"

Definition at line 11 of file main.c.

Referenced by create_file_names(), and getargs().

FILE* input_file

Definition at line 31 of file main.c.

Referenced by get_line(), getargs(), open_files(), and output_trailing_text().

int nsyms

The number of symbols (terminals + non-terminals) in the grammar.

All symbols can be uniquely represented using integers in the range [0, nsyms - 1], which is obtained by joining the range of terminals [0, ntokens - 1] with the range of non-terminals [ntokens, nsyms - 1].

It holds that nsyms = ntokens + nvars.

Definition at line 55 of file main.c.

Referenced by allocate_itemsets(), allocate_storage(), goto_actions(), new_itemsets(), pack_symbols(), set_derives(), set_EFF(), set_first_derives(), set_goto_map(), and set_nullable().

int ntokens

The number of tokens (terminals) in the grammar.

All tokens can be uniquely represented using integers in the range [0, ntokens - 1].

It holds that nsyms = ntokens + nvars.

Definition at line 64 of file main.c.

Referenced by add_reductions(), finalize_closure(), lalr(), output_actions(), output_debug(), output_defines(), pack_symbols(), set_first_derives(), set_goto_map(), token_actions(), and verbose().

char* nullable

Definition at line 141 of file main.c.

Referenced by build_relations(), free_nullable(), initialize_F(), and set_nullable().

int nvars

The number of variables (non-terminals) in the grammar.

All variables can be uniquely represented using integers in the range [ntokens, nsyms - 1], which is equivalent to the range [ntokens, ntokens + nvars - 1].

It holds that nsyms = ntokens + nvars.

Definition at line 74 of file main.c.

Referenced by output_actions(), pack_symbols(), set_derives(), set_EFF(), set_first_derives(), set_goto_map(), and verbose().

char* output_file_name

Definition at line 22 of file main.c.

Referenced by create_file_names(), and open_files().

char* rassoc
short* ritem

Representation of all productions (and items)

Typical shape: [1, 12, 21, -1, 2, 3, -2, 1, 4, -3, ...]

All productions are represented in this array as the list of their right-hand side symbols followed by the negation of their index. So the symbols on the right-hand side of the first rule are followed by -1, then there are the right-hand side symbols of the second rule and then -2, and so on.

Indices (using short integers) inside this array can represent rules (in that case they point to the first element after a negative one) or items, in which case they can point to any position; the element in that position is taken to be the first element after the "point" of the item. If the index points to a negative number, then it represents a reduction item for the rule whose number is the absolute value of the pointed element.

The array called rrhs contains indices inside this list and is used to associate to each rule the beginning of its production (that is, the closure item for that rule).

Definition at line 112 of file main.c.

Referenced by allocate_itemsets(), build_relations(), closure(), find_final_state(), log_unused(), new_itemsets(), output_debug(), pack_grammar(), print_core(), print_grammar(), save_reductions(), set_EFF(), set_maxrhs(), set_nullable(), show_cores(), and show_ritems().

short* rlhs

List of left-hand sides of all rules.

This array associates to each production its left-hand side symbol.

Definition at line 119 of file main.c.

Referenced by log_unused(), output_debug(), output_rule_data(), pack_grammar(), print_core(), print_grammar(), print_nulls(), set_derives(), set_nullable(), and show_cores().

short* rprec
short* rrhs

List of right-hand sides of all rules.

Array of indices inside ritem. It is used record the position inside ritem where each rule begins.

Definition at line 126 of file main.c.

Referenced by build_relations(), closure(), initialize_states(), log_unused(), output_debug(), output_rule_data(), pack_grammar(), print_core(), print_nulls(), set_EFF(), show_cores(), and show_rrhs().

int start_symbol

Index of the starting symbol of the grammar.

It holds that start_symbol = ntokens. In fact, the starting symbol is always placed at the beginning of the non-terminal range [ntokens, nsyms - 1].

Definition at line 82 of file main.c.

Referenced by free_derives(), goto_actions(), initialize_states(), output_rule_data(), pack_grammar(), pack_symbols(), set_derives(), set_EFF(), and set_first_derives().

char* symbol_assoc

Definition at line 93 of file main.c.

Referenced by get_shifts(), and pack_symbols().

char** symbol_name

Array of symbol names.

Array of strings representing the names of all symbols. All names are allocated in a single contiguous block of memory to improve locality.

Definition at line 90 of file main.c.

Referenced by log_unused(), output_debug(), output_defines(), pack_symbols(), print_conflicts(), print_core(), print_gotos(), print_grammar(), print_nulls(), print_reductions(), print_shifts(), set_nullable(), and show_cores().

short* symbol_prec

Definition at line 92 of file main.c.

Referenced by get_shifts(), and pack_symbols().

short* symbol_value
char* temp_form = "yacc.XXXXXXX"

Definition at line 13 of file main.c.

Referenced by create_file_names().

FILE* text_file

Definition at line 33 of file main.c.

Referenced by copy_text(), copy_union(), done(), open_files(), and output_stored_text().

char* text_file_name

Definition at line 23 of file main.c.

Referenced by create_file_names(), done(), open_files(), and output_stored_text().

char tflag

Definition at line 7 of file main.c.

Referenced by getargs(), and output_debug().

FILE* union_file

Definition at line 35 of file main.c.

Referenced by copy_union(), done(), open_files(), and output_defines().

char* union_file_name

Definition at line 24 of file main.c.

Referenced by create_file_names(), done(), open_files(), and output_defines().

char* verbose_file_name

Definition at line 25 of file main.c.

Referenced by create_file_names(), and open_files().

char vflag

Definition at line 8 of file main.c.

Referenced by create_file_names(), getargs(), open_files(), print_grammar(), and verbose().