xref: /csrg-svn/usr.bin/yacc/defs.h (revision 42698)
139774Sbostic /*
239774Sbostic  * Copyright (c) 1989 The Regents of the University of California.
339774Sbostic  * All rights reserved.
439774Sbostic  *
539774Sbostic  * This code is derived from software contributed to Berkeley by
639774Sbostic  * Robert Paul Corbett.
739774Sbostic  *
8*42698Sbostic  * %sccs.include.redist.c%
939774Sbostic  *
10*42698Sbostic  *	@(#)defs.h	5.4 (Berkeley) 06/01/90
1139774Sbostic  */
1239774Sbostic 
1339774Sbostic #include <assert.h>
1439774Sbostic #include <ctype.h>
1539774Sbostic #include <stdio.h>
1639774Sbostic 
1739774Sbostic 
1839774Sbostic /*  machine dependent definitions			*/
1939774Sbostic /*  the following definitions are for the VAX		*/
2039774Sbostic /*  they might have to be changed for other machines	*/
2139774Sbostic 
2239774Sbostic /*  MAXCHAR is the largest character value		*/
2339774Sbostic /*  MAXSHORT is the largest value of a C short		*/
2439774Sbostic /*  MINSHORT is the most negative value of a C short	*/
2539774Sbostic /*  MAXTABLE is the maximum table size			*/
2639774Sbostic /*  BITS_PER_WORD is the number of bits in a C unsigned	*/
2739774Sbostic /*  WORDSIZE computes the number of words needed to	*/
2839774Sbostic /*	store n bits					*/
2939774Sbostic /*  BIT returns the value of the n-th bit starting	*/
3039774Sbostic /*	from r (0-indexed)				*/
3139774Sbostic /*  SETBIT sets the n-th bit starting from r		*/
3239774Sbostic 
3339774Sbostic #define	MAXCHAR		255
3439774Sbostic #define	MAXSHORT	32767
3539774Sbostic #define MINSHORT	-32768
3639774Sbostic #define MAXTABLE	32500
3739774Sbostic #define BITS_PER_WORD	32
3839774Sbostic #define	WORDSIZE(n)	(((n)+(BITS_PER_WORD-1))/BITS_PER_WORD)
3939774Sbostic #define	BIT(r, n)	((((r)[(n) >> 5]) >> ((n) & 31)) & 1)
4039774Sbostic #define	SETBIT(r, n)	((r)[(n) >> 5] |= (1 << ((n) & 31)))
4139774Sbostic 
4239774Sbostic 
4339774Sbostic /*  character names  */
4439774Sbostic 
4539774Sbostic #define	NUL		'\0'    /*  the null character  */
4639774Sbostic #define	NEWLINE		'\n'    /*  line feed  */
4739774Sbostic #define	SP		' '     /*  space  */
4839774Sbostic #define	BS		'\b'    /*  backspace  */
4939774Sbostic #define	HT		'\t'    /*  horizontal tab  */
5039774Sbostic #define	VT		'\013'  /*  vertical tab  */
5139774Sbostic #define	CR		'\r'    /*  carriage return  */
5239774Sbostic #define	FF		'\f'    /*  form feed  */
5339774Sbostic #define	QUOTE		'\''    /*  single quote  */
5439774Sbostic #define	DOUBLE_QUOTE	'\"'    /*  double quote  */
5539774Sbostic #define	BACKSLASH	'\\'    /*  backslash  */
5639774Sbostic 
5739774Sbostic 
5839774Sbostic /* defines for constructing filenames */
5939774Sbostic 
6039774Sbostic #define	DEFINES_SUFFIX	".tab.h"
6139774Sbostic #define	OUTPUT_SUFFIX	".tab.c"
6239774Sbostic #define	VERBOSE_SUFFIX	".output"
6339774Sbostic 
6439774Sbostic 
6539774Sbostic /* keyword codes */
6639774Sbostic 
6739774Sbostic #define TOKEN 0
6839774Sbostic #define LEFT 1
6939774Sbostic #define RIGHT 2
7039774Sbostic #define NONASSOC 3
7139774Sbostic #define MARK 4
7239774Sbostic #define TEXT 5
7339774Sbostic #define TYPE 6
7439774Sbostic #define START 7
7539774Sbostic #define UNION 8
7639774Sbostic #define IDENT 9
7739774Sbostic 
7839774Sbostic 
7939774Sbostic /*  symbol classes  */
8039774Sbostic 
8139774Sbostic #define UNKNOWN 0
8239774Sbostic #define TERM 1
8339774Sbostic #define NONTERM 2
8439774Sbostic 
8539774Sbostic 
8639774Sbostic /*  the undefined value  */
8739774Sbostic 
8839774Sbostic #define UNDEFINED (-1)
8939774Sbostic 
9039774Sbostic 
9139774Sbostic /*  action codes  */
9239774Sbostic 
9339774Sbostic #define SHIFT 1
9439774Sbostic #define REDUCE 2
9539774Sbostic #define ERROR 3
9639774Sbostic 
9739774Sbostic 
9839774Sbostic /*  character macros  */
9939774Sbostic 
10039774Sbostic #define IS_IDENT(c)	(isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$')
10139774Sbostic #define	IS_OCTAL(c)	((c) >= '0' && (c) <= '7')
10239774Sbostic #define	NUMERIC_VALUE(c)	((c) - '0')
10339774Sbostic 
10439774Sbostic 
10539774Sbostic /*  symbol macros  */
10639774Sbostic 
10739774Sbostic #define ISTOKEN(s)	((s) < start_symbol)
10839774Sbostic #define ISVAR(s)	((s) >= start_symbol)
10939774Sbostic 
11039774Sbostic 
11139774Sbostic /*  storage allocation macros  */
11239774Sbostic 
11339774Sbostic #define	FREE(x)		(free((char*)(x)))
11439774Sbostic #define MALLOC(n)	(malloc((unsigned)(n)))
11539774Sbostic #define	NEW(t)		((t*)allocate(sizeof(t)))
11639774Sbostic #define	NEW2(n,t)	((t*)allocate((unsigned)((n)*sizeof(t))))
11739774Sbostic #define REALLOC(p,n)	(realloc((char*)(p),(unsigned)(n)))
11839774Sbostic 
11939774Sbostic 
12039774Sbostic /*  the structure of a symbol table entry  */
12139774Sbostic 
12239774Sbostic typedef struct bucket bucket;
12339774Sbostic struct bucket
12439774Sbostic {
12539774Sbostic     struct bucket *link;
12639774Sbostic     struct bucket *next;
12739774Sbostic     char *name;
12839774Sbostic     char *tag;
12939774Sbostic     short value;
13039774Sbostic     short index;
13139774Sbostic     short prec;
13239774Sbostic     char class;
13339774Sbostic     char assoc;
13439774Sbostic };
13539774Sbostic 
13639774Sbostic 
13739774Sbostic /*  the structure of the LR(0) state machine  */
13839774Sbostic 
13939774Sbostic typedef struct core core;
14039774Sbostic struct core
14139774Sbostic {
14239774Sbostic     struct core *next;
14339774Sbostic     struct core *link;
14439774Sbostic     short number;
14539774Sbostic     short accessing_symbol;
14639774Sbostic     short nitems;
14739774Sbostic     short items[1];
14839774Sbostic };
14939774Sbostic 
15039774Sbostic 
15139774Sbostic /*  the structure used to record shifts  */
15239774Sbostic 
15339774Sbostic typedef struct shifts shifts;
15439774Sbostic struct shifts
15539774Sbostic {
15639774Sbostic     struct shifts *next;
15739774Sbostic     short number;
15839774Sbostic     short nshifts;
15939774Sbostic     short shift[1];
16039774Sbostic };
16139774Sbostic 
16239774Sbostic 
16339774Sbostic /*  the structure used to store reductions  */
16439774Sbostic 
16539774Sbostic typedef struct reductions reductions;
16639774Sbostic struct reductions
16739774Sbostic {
16839774Sbostic     struct reductions *next;
16939774Sbostic     short number;
17039774Sbostic     short nreds;
17139774Sbostic     short rules[1];
17239774Sbostic };
17339774Sbostic 
17439774Sbostic 
17539774Sbostic /*  the structure used to represent parser actions  */
17639774Sbostic 
17739774Sbostic typedef struct action action;
17839774Sbostic struct action
17939774Sbostic {
18039774Sbostic     struct action *next;
18139774Sbostic     short symbol;
18239774Sbostic     short number;
18339774Sbostic     short prec;
18439774Sbostic     char action_code;
18539774Sbostic     char assoc;
18639774Sbostic     char suppressed;
18739774Sbostic };
18839774Sbostic 
18939774Sbostic 
19039774Sbostic /* global variables */
19139774Sbostic 
19239774Sbostic extern char dflag;
19339774Sbostic extern char lflag;
19439774Sbostic extern char tflag;
19539774Sbostic extern char vflag;
19639774Sbostic 
19739774Sbostic extern char *myname;
19839774Sbostic extern char *cptr;
19939774Sbostic extern char *line;
20039774Sbostic extern int lineno;
20139774Sbostic extern int outline;
20239774Sbostic 
20339774Sbostic extern char *banner[];
20439774Sbostic extern char *header[];
20539774Sbostic extern char *body[];
20639774Sbostic extern char *trailer[];
20739774Sbostic 
20839774Sbostic extern char *action_file_name;
20939774Sbostic extern char *defines_file_name;
21039774Sbostic extern char *input_file_name;
21139774Sbostic extern char *output_file_name;
21239774Sbostic extern char *text_file_name;
21339774Sbostic extern char *union_file_name;
21439774Sbostic extern char *verbose_file_name;
21539774Sbostic 
21639774Sbostic extern FILE *action_file;
21739774Sbostic extern FILE *defines_file;
21839774Sbostic extern FILE *input_file;
21939774Sbostic extern FILE *output_file;
22039774Sbostic extern FILE *text_file;
22139774Sbostic extern FILE *union_file;
22239774Sbostic extern FILE *verbose_file;
22339774Sbostic 
22439774Sbostic extern int nitems;
22539774Sbostic extern int nrules;
22639774Sbostic extern int nsyms;
22739774Sbostic extern int ntokens;
22839774Sbostic extern int nvars;
22940085Sbostic extern int ntags;
23039774Sbostic 
23139774Sbostic extern char unionized;
23239774Sbostic extern char line_format[];
23339774Sbostic 
23439774Sbostic extern int   start_symbol;
23539774Sbostic extern char  **symbol_name;
23639774Sbostic extern short *symbol_value;
23739774Sbostic extern short *symbol_prec;
23839774Sbostic extern char  *symbol_assoc;
23939774Sbostic 
24039774Sbostic extern short *ritem;
24139774Sbostic extern short *rlhs;
24239774Sbostic extern short *rrhs;
24339774Sbostic extern short *rprec;
24439774Sbostic extern char  *rassoc;
24539774Sbostic 
24639774Sbostic extern short **derives;
24739774Sbostic extern char *nullable;
24839774Sbostic 
24939774Sbostic extern bucket *first_symbol;
25039774Sbostic extern bucket *last_symbol;
25139774Sbostic 
25239774Sbostic extern int nstates;
25339774Sbostic extern core *first_state;
25439774Sbostic extern shifts *first_shift;
25539774Sbostic extern reductions *first_reduction;
25639774Sbostic extern short *accessing_symbol;
25739774Sbostic extern core **state_table;
25839774Sbostic extern shifts **shift_table;
25939774Sbostic extern reductions **reduction_table;
26039774Sbostic extern unsigned *LA;
26139774Sbostic extern short *LAruleno;
26239774Sbostic extern short *lookaheads;
26339774Sbostic extern short *goto_map;
26439774Sbostic extern short *from_state;
26539774Sbostic extern short *to_state;
26639774Sbostic 
26739774Sbostic extern action **parser;
26839774Sbostic extern int SRtotal;
26939774Sbostic extern int RRtotal;
27039774Sbostic extern short *SRconflicts;
27139774Sbostic extern short *RRconflicts;
27239774Sbostic extern short *defred;
27339774Sbostic extern short *rules_used;
27439774Sbostic extern short nunused;
27539774Sbostic extern short final_state;
27639774Sbostic 
27739774Sbostic /* global functions */
27839774Sbostic 
27939774Sbostic extern char *allocate();
28039774Sbostic extern bucket *lookup();
28139774Sbostic extern bucket *make_bucket();
28239774Sbostic 
28339774Sbostic 
28439774Sbostic /* system variables */
28539774Sbostic 
28639774Sbostic extern int errno;
28739774Sbostic 
28839774Sbostic 
28939774Sbostic /* system functions */
29039774Sbostic 
29139774Sbostic extern void free();
29239774Sbostic extern char *calloc();
29339774Sbostic extern char *malloc();
29439774Sbostic extern char *realloc();
29539774Sbostic extern char *strcpy();
296