11590Srgrimes/*
21590Srgrimes * Copyright (c) 1989 The Regents of the University of California.
31590Srgrimes * All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Robert Paul Corbett.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 4. Neither the name of the University nor the names of its contributors
171590Srgrimes *    may be used to endorse or promote products derived from this software
181590Srgrimes *    without specific prior written permission.
191590Srgrimes *
201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301590Srgrimes * SUCH DAMAGE.
311590Srgrimes *
321590Srgrimes *	@(#)defs.h	5.6 (Berkeley) 5/24/93
3350477Speter * $FreeBSD$
341590Srgrimes */
351590Srgrimes
361590Srgrimes#include <assert.h>
371590Srgrimes#include <ctype.h>
3828856Scharnier#include <err.h>
391590Srgrimes#include <stdio.h>
401590Srgrimes
411590Srgrimes
421590Srgrimes/*  machine-dependent definitions			*/
431590Srgrimes/*  the following definitions are for the Tahoe		*/
441590Srgrimes/*  they might have to be changed for other machines	*/
451590Srgrimes
461590Srgrimes/*  MAXTABLE is the maximum table size			*/
471590Srgrimes/*  BITS_PER_WORD is the number of bits in a C unsigned	*/
481590Srgrimes/*  WORDSIZE computes the number of words needed to	*/
491590Srgrimes/*	store n bits					*/
501590Srgrimes/*  BIT returns the value of the n-th bit starting	*/
511590Srgrimes/*	from r (0-indexed)				*/
521590Srgrimes/*  SETBIT sets the n-th bit starting from r		*/
531590Srgrimes
541590Srgrimes#define MAXTABLE	32500
551590Srgrimes#define BITS_PER_WORD	32
561590Srgrimes#define	WORDSIZE(n)	(((n)+(BITS_PER_WORD-1))/BITS_PER_WORD)
571590Srgrimes#define	BIT(r, n)	((((r)[(n)>>5])>>((n)&31))&1)
581590Srgrimes#define	SETBIT(r, n)	((r)[(n)>>5]|=((unsigned)1<<((n)&31)))
591590Srgrimes
601590Srgrimes
611590Srgrimes/*  character names  */
621590Srgrimes
631590Srgrimes#define	NUL		'\0'    /*  the null character  */
641590Srgrimes#define	NEWLINE		'\n'    /*  line feed  */
651590Srgrimes#define	SP		' '     /*  space  */
661590Srgrimes#define	BS		'\b'    /*  backspace  */
671590Srgrimes#define	HT		'\t'    /*  horizontal tab  */
681590Srgrimes#define	VT		'\013'  /*  vertical tab  */
691590Srgrimes#define	CR		'\r'    /*  carriage return  */
701590Srgrimes#define	FF		'\f'    /*  form feed  */
711590Srgrimes#define	QUOTE		'\''    /*  single quote  */
721590Srgrimes#define	DOUBLE_QUOTE	'\"'    /*  double quote  */
731590Srgrimes#define	BACKSLASH	'\\'    /*  backslash  */
741590Srgrimes
751590Srgrimes
761590Srgrimes/* defines for constructing filenames */
771590Srgrimes
781590Srgrimes#define CODE_SUFFIX	".code.c"
791590Srgrimes#define	DEFINES_SUFFIX	".tab.h"
801590Srgrimes#define	OUTPUT_SUFFIX	".tab.c"
811590Srgrimes#define	VERBOSE_SUFFIX	".output"
821590Srgrimes
831590Srgrimes
841590Srgrimes/* keyword codes */
851590Srgrimes
861590Srgrimes#define TOKEN 0
871590Srgrimes#define LEFT 1
881590Srgrimes#define RIGHT 2
891590Srgrimes#define NONASSOC 3
901590Srgrimes#define MARK 4
911590Srgrimes#define TEXT 5
921590Srgrimes#define TYPE 6
931590Srgrimes#define START 7
941590Srgrimes#define UNION 8
951590Srgrimes#define IDENT 9
9649208Sobrien#define EXPECT 10
971590Srgrimes
981590Srgrimes
991590Srgrimes/*  symbol classes  */
1001590Srgrimes
1011590Srgrimes#define UNKNOWN 0
1021590Srgrimes#define TERM 1
1031590Srgrimes#define NONTERM 2
1041590Srgrimes
1051590Srgrimes
1061590Srgrimes/*  the undefined value  */
1071590Srgrimes
1081590Srgrimes#define UNDEFINED (-1)
1091590Srgrimes
1101590Srgrimes
1111590Srgrimes/*  action codes  */
1121590Srgrimes
1131590Srgrimes#define SHIFT 1
1141590Srgrimes#define REDUCE 2
1151590Srgrimes
1161590Srgrimes
1171590Srgrimes/*  character macros  */
1181590Srgrimes
1191590Srgrimes#define IS_IDENT(c)	(isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$')
1201590Srgrimes#define	IS_OCTAL(c)	((c) >= '0' && (c) <= '7')
1211590Srgrimes#define	NUMERIC_VALUE(c)	((c) - '0')
1221590Srgrimes
1231590Srgrimes
1241590Srgrimes/*  symbol macros  */
1251590Srgrimes
1261590Srgrimes#define ISTOKEN(s)	((s) < start_symbol)
1271590Srgrimes#define ISVAR(s)	((s) >= start_symbol)
1281590Srgrimes
1291590Srgrimes
1301590Srgrimes/*  storage allocation macros  */
1311590Srgrimes
1321590Srgrimes#define	NEW(t)		((t*)allocate(sizeof(t)))
133214961Sobrien#define	NEW2(n,t)	((t*)allocate((n)*sizeof(t)))
1341590Srgrimes
1351590Srgrimes
1361590Srgrimes/*  the structure of a symbol table entry  */
1371590Srgrimes
1381590Srgrimestypedef struct bucket bucket;
1391590Srgrimesstruct bucket
1401590Srgrimes{
1411590Srgrimes    struct bucket *link;
1421590Srgrimes    struct bucket *next;
1431590Srgrimes    char *name;
1441590Srgrimes    char *tag;
1451590Srgrimes    short value;
1461590Srgrimes    short index;
1471590Srgrimes    short prec;
1481590Srgrimes    char class;
1491590Srgrimes    char assoc;
1501590Srgrimes};
1511590Srgrimes
1521590Srgrimes
1531590Srgrimes/*  the structure of the LR(0) state machine  */
1541590Srgrimes
1551590Srgrimestypedef struct core core;
1561590Srgrimesstruct core
1571590Srgrimes{
1581590Srgrimes    struct core *next;
1591590Srgrimes    struct core *link;
1601590Srgrimes    short number;
1611590Srgrimes    short accessing_symbol;
1621590Srgrimes    short nitems;
1631590Srgrimes    short items[1];
1641590Srgrimes};
1651590Srgrimes
1661590Srgrimes
1671590Srgrimes/*  the structure used to record shifts  */
1681590Srgrimes
1691590Srgrimestypedef struct shifts shifts;
1701590Srgrimesstruct shifts
1711590Srgrimes{
1721590Srgrimes    struct shifts *next;
1731590Srgrimes    short number;
1741590Srgrimes    short nshifts;
1751590Srgrimes    short shift[1];
1761590Srgrimes};
1771590Srgrimes
1781590Srgrimes
1791590Srgrimes/*  the structure used to store reductions  */
1801590Srgrimes
1811590Srgrimestypedef struct reductions reductions;
1821590Srgrimesstruct reductions
1831590Srgrimes{
1841590Srgrimes    struct reductions *next;
1851590Srgrimes    short number;
1861590Srgrimes    short nreds;
1871590Srgrimes    short rules[1];
1881590Srgrimes};
1891590Srgrimes
1901590Srgrimes
1911590Srgrimes/*  the structure used to represent parser actions  */
1921590Srgrimes
1931590Srgrimestypedef struct action action;
1941590Srgrimesstruct action
1951590Srgrimes{
1961590Srgrimes    struct action *next;
1971590Srgrimes    short symbol;
1981590Srgrimes    short number;
1991590Srgrimes    short prec;
2001590Srgrimes    char action_code;
2011590Srgrimes    char assoc;
2021590Srgrimes    char suppressed;
2031590Srgrimes};
2041590Srgrimes
2051590Srgrimes
2061590Srgrimes/* global variables */
2071590Srgrimes
2081590Srgrimesextern char dflag;
2091590Srgrimesextern char lflag;
2101590Srgrimesextern char rflag;
2111590Srgrimesextern char tflag;
2121590Srgrimesextern char vflag;
21387171Smarkmextern const char *symbol_prefix;
2141590Srgrimes
2151590Srgrimesextern char *cptr;
2161590Srgrimesextern char *line;
2171590Srgrimesextern int lineno;
2181590Srgrimesextern int outline;
2191590Srgrimes
22087171Smarkmextern const char *banner[];
22187171Smarkmextern const char *tables[];
22287171Smarkmextern const char *header[];
22387171Smarkmextern const char *body[];
22487171Smarkmextern const char *trailer[];
2251590Srgrimes
2261590Srgrimesextern char *action_file_name;
2271590Srgrimesextern char *code_file_name;
2281590Srgrimesextern char *defines_file_name;
22987171Smarkmextern const char *input_file_name;
2301590Srgrimesextern char *output_file_name;
2311590Srgrimesextern char *text_file_name;
2321590Srgrimesextern char *union_file_name;
2331590Srgrimesextern char *verbose_file_name;
2341590Srgrimes
2351590Srgrimesextern FILE *action_file;
2361590Srgrimesextern FILE *code_file;
2371590Srgrimesextern FILE *defines_file;
2381590Srgrimesextern FILE *input_file;
2391590Srgrimesextern FILE *output_file;
2401590Srgrimesextern FILE *text_file;
2411590Srgrimesextern FILE *union_file;
2421590Srgrimesextern FILE *verbose_file;
2431590Srgrimes
2441590Srgrimesextern int nitems;
2451590Srgrimesextern int nrules;
2461590Srgrimesextern int nsyms;
2471590Srgrimesextern int ntokens;
2481590Srgrimesextern int nvars;
2491590Srgrimesextern int ntags;
2501590Srgrimes
2511590Srgrimesextern char unionized;
2521590Srgrimes
2531590Srgrimesextern int   start_symbol;
2541590Srgrimesextern char  **symbol_name;
2551590Srgrimesextern short *symbol_value;
2561590Srgrimesextern short *symbol_prec;
2571590Srgrimesextern char  *symbol_assoc;
2581590Srgrimes
2591590Srgrimesextern short *ritem;
2601590Srgrimesextern short *rlhs;
2611590Srgrimesextern short *rrhs;
2621590Srgrimesextern short *rprec;
2631590Srgrimesextern char  *rassoc;
2641590Srgrimes
2651590Srgrimesextern short **derives;
2661590Srgrimesextern char *nullable;
2671590Srgrimes
2681590Srgrimesextern bucket *first_symbol;
2691590Srgrimesextern bucket *last_symbol;
2701590Srgrimes
2711590Srgrimesextern int nstates;
2721590Srgrimesextern core *first_state;
2731590Srgrimesextern shifts *first_shift;
2741590Srgrimesextern reductions *first_reduction;
2751590Srgrimesextern short *accessing_symbol;
2761590Srgrimesextern core **state_table;
2771590Srgrimesextern shifts **shift_table;
2781590Srgrimesextern reductions **reduction_table;
2791590Srgrimesextern unsigned *LA;
2801590Srgrimesextern short *LAruleno;
2811590Srgrimesextern short *lookaheads;
2821590Srgrimesextern short *goto_map;
2831590Srgrimesextern short *from_state;
2841590Srgrimesextern short *to_state;
2851590Srgrimes
2861590Srgrimesextern action **parser;
28749208Sobrienextern int SRexpect;
2881590Srgrimesextern int SRtotal;
2891590Srgrimesextern int RRtotal;
2901590Srgrimesextern short *SRconflicts;
2911590Srgrimesextern short *RRconflicts;
2921590Srgrimesextern short *defred;
2931590Srgrimesextern short *rules_used;
2941590Srgrimesextern short nunused;
2951590Srgrimesextern short final_state;
2961590Srgrimes
2971590Srgrimes/* global functions */
2981590Srgrimes
299214961Sobrienvoid *allocate(size_t);
30092922Simpvoid closure(short *, int);
30192922Simpvoid create_symbol_table(void);
30292922Simpvoid default_action_warning(void);
303181269Scpercivavoid dollar_error(int, char *, char *) __dead2;
30492922Simpvoid dollar_warning(int, int);
305181269Scpercivavoid done(int) __dead2;
306181269Scpercivavoid fatal(const char *msg) __dead2;
30792922Simpvoid finalize_closure(void);
30892922Simpvoid free_parser(void);
30992922Simpvoid free_symbols(void);
31092922Simpvoid free_symbol_table(void);
311181269Scpercivavoid illegal_character(char *) __dead2;
312181269Scpercivavoid illegal_tag(int, char *, char *) __dead2;
31392922Simpvoid lalr(void);
31492922Simpbucket *lookup(char *);
31592922Simpvoid lr0(void);
31692922Simpbucket *make_bucket(const char *);
31792922Simpvoid make_parser(void);
318181269Scpercivavoid no_grammar(void) __dead2;
319181269Scpercivavoid no_space(void) __dead2;
320181269Scpercivavoid open_error(const char *) __dead2;
32192922Simpvoid output(void);
322181269Scpercivavoid over_unionized(char *) __dead2;
32392922Simpvoid prec_redeclared(void);
32492922Simpvoid reader(void);
32592922Simpvoid reflexive_transitive_closure(unsigned *, int);
32692922Simpvoid reprec_warning(char *);
32792922Simpvoid restarted_warning(void);
32892922Simpvoid retyped_warning(char *);
32992922Simpvoid revalued_warning(char *);
33092922Simpvoid set_first_derives(void);
331181269Scpercivavoid syntax_error(int, char *, char *) __dead2;
332181269Scpercivavoid terminal_lhs(int) __dead2;
333181269Scpercivavoid terminal_start(char *) __dead2;
334181269Scpercivavoid tokenized_start(char *) __dead2;
335181269Scpercivavoid undefined_goal(char *) __dead2;
33692922Simpvoid undefined_symbol_warning(char *);
337181269Scpercivavoid unexpected_EOF(void) __dead2;
338181269Scpercivavoid unknown_rhs(int) __dead2;
339181269Scpercivavoid unterminated_action(int, char *, char *) __dead2;
340181269Scpercivavoid unterminated_comment(int, char *, char *) __dead2;
341181269Scpercivavoid unterminated_string(int, char *, char *) __dead2;
342181269Scpercivavoid unterminated_text(int, char *, char *) __dead2;
343181269Scpercivavoid unterminated_union(int, char *, char *) __dead2;
344181269Scpercivavoid untyped_lhs(void) __dead2;
345181269Scpercivavoid untyped_rhs(int, char *) __dead2;
346181269Scpercivavoid used_reserved(char *) __dead2;
34792922Simpvoid verbose(void);
34892922Simpvoid write_section(const char **);
349