xref: /csrg-svn/usr.bin/yacc/defs.h (revision 60321)
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  *
842698Sbostic  * %sccs.include.redist.c%
939774Sbostic  *
10*60321Sbostic  *	@(#)defs.h	5.6 (Berkeley) 05/24/93
1139774Sbostic  */
1239774Sbostic 
1339774Sbostic #include <assert.h>
1439774Sbostic #include <ctype.h>
1539774Sbostic #include <stdio.h>
1639774Sbostic 
1739774Sbostic 
1846060Scorbett /*  machine-dependent definitions			*/
1946060Scorbett /*  the following definitions are for the Tahoe		*/
2039774Sbostic /*  they might have to be changed for other machines	*/
2139774Sbostic 
2246060Scorbett /*  MAXCHAR is the largest unsigned 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)
3946060Scorbett #define	BIT(r, n)	((((r)[(n)>>5])>>((n)&31))&1)
4046060Scorbett #define	SETBIT(r, n)	((r)[(n)>>5]|=((unsigned)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 
6046060Scorbett #define CODE_SUFFIX	".code.c"
6139774Sbostic #define	DEFINES_SUFFIX	".tab.h"
6239774Sbostic #define	OUTPUT_SUFFIX	".tab.c"
6339774Sbostic #define	VERBOSE_SUFFIX	".output"
6439774Sbostic 
6539774Sbostic 
6639774Sbostic /* keyword codes */
6739774Sbostic 
6839774Sbostic #define TOKEN 0
6939774Sbostic #define LEFT 1
7039774Sbostic #define RIGHT 2
7139774Sbostic #define NONASSOC 3
7239774Sbostic #define MARK 4
7339774Sbostic #define TEXT 5
7439774Sbostic #define TYPE 6
7539774Sbostic #define START 7
7639774Sbostic #define UNION 8
7739774Sbostic #define IDENT 9
7839774Sbostic 
7939774Sbostic 
8039774Sbostic /*  symbol classes  */
8139774Sbostic 
8239774Sbostic #define UNKNOWN 0
8339774Sbostic #define TERM 1
8439774Sbostic #define NONTERM 2
8539774Sbostic 
8639774Sbostic 
8739774Sbostic /*  the undefined value  */
8839774Sbostic 
8939774Sbostic #define UNDEFINED (-1)
9039774Sbostic 
9139774Sbostic 
9239774Sbostic /*  action codes  */
9339774Sbostic 
9439774Sbostic #define SHIFT 1
9539774Sbostic #define REDUCE 2
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 
11346060Scorbett #define CALLOC(k,n)	(calloc((unsigned)(k),(unsigned)(n)))
11439774Sbostic #define	FREE(x)		(free((char*)(x)))
11539774Sbostic #define MALLOC(n)	(malloc((unsigned)(n)))
11639774Sbostic #define	NEW(t)		((t*)allocate(sizeof(t)))
11739774Sbostic #define	NEW2(n,t)	((t*)allocate((unsigned)((n)*sizeof(t))))
11839774Sbostic #define REALLOC(p,n)	(realloc((char*)(p),(unsigned)(n)))
11939774Sbostic 
12039774Sbostic 
12139774Sbostic /*  the structure of a symbol table entry  */
12239774Sbostic 
12339774Sbostic typedef struct bucket bucket;
12439774Sbostic struct bucket
12539774Sbostic {
12639774Sbostic     struct bucket *link;
12739774Sbostic     struct bucket *next;
12839774Sbostic     char *name;
12939774Sbostic     char *tag;
13039774Sbostic     short value;
13139774Sbostic     short index;
13239774Sbostic     short prec;
13339774Sbostic     char class;
13439774Sbostic     char assoc;
13539774Sbostic };
13639774Sbostic 
13739774Sbostic 
13839774Sbostic /*  the structure of the LR(0) state machine  */
13939774Sbostic 
14039774Sbostic typedef struct core core;
14139774Sbostic struct core
14239774Sbostic {
14339774Sbostic     struct core *next;
14439774Sbostic     struct core *link;
14539774Sbostic     short number;
14639774Sbostic     short accessing_symbol;
14739774Sbostic     short nitems;
14839774Sbostic     short items[1];
14939774Sbostic };
15039774Sbostic 
15139774Sbostic 
15239774Sbostic /*  the structure used to record shifts  */
15339774Sbostic 
15439774Sbostic typedef struct shifts shifts;
15539774Sbostic struct shifts
15639774Sbostic {
15739774Sbostic     struct shifts *next;
15839774Sbostic     short number;
15939774Sbostic     short nshifts;
16039774Sbostic     short shift[1];
16139774Sbostic };
16239774Sbostic 
16339774Sbostic 
16439774Sbostic /*  the structure used to store reductions  */
16539774Sbostic 
16639774Sbostic typedef struct reductions reductions;
16739774Sbostic struct reductions
16839774Sbostic {
16939774Sbostic     struct reductions *next;
17039774Sbostic     short number;
17139774Sbostic     short nreds;
17239774Sbostic     short rules[1];
17339774Sbostic };
17439774Sbostic 
17539774Sbostic 
17639774Sbostic /*  the structure used to represent parser actions  */
17739774Sbostic 
17839774Sbostic typedef struct action action;
17939774Sbostic struct action
18039774Sbostic {
18139774Sbostic     struct action *next;
18239774Sbostic     short symbol;
18339774Sbostic     short number;
18439774Sbostic     short prec;
18539774Sbostic     char action_code;
18639774Sbostic     char assoc;
18739774Sbostic     char suppressed;
18839774Sbostic };
18939774Sbostic 
19039774Sbostic 
19139774Sbostic /* global variables */
19239774Sbostic 
19339774Sbostic extern char dflag;
19439774Sbostic extern char lflag;
19546060Scorbett extern char rflag;
19639774Sbostic extern char tflag;
19739774Sbostic extern char vflag;
198*60321Sbostic extern char *symbol_prefix;
19939774Sbostic 
20039774Sbostic extern char *myname;
20139774Sbostic extern char *cptr;
20239774Sbostic extern char *line;
20339774Sbostic extern int lineno;
20439774Sbostic extern int outline;
20539774Sbostic 
20639774Sbostic extern char *banner[];
20746060Scorbett extern char *tables[];
20839774Sbostic extern char *header[];
20939774Sbostic extern char *body[];
21039774Sbostic extern char *trailer[];
21139774Sbostic 
21239774Sbostic extern char *action_file_name;
21346060Scorbett extern char *code_file_name;
21439774Sbostic extern char *defines_file_name;
21539774Sbostic extern char *input_file_name;
21639774Sbostic extern char *output_file_name;
21739774Sbostic extern char *text_file_name;
21839774Sbostic extern char *union_file_name;
21939774Sbostic extern char *verbose_file_name;
22039774Sbostic 
22139774Sbostic extern FILE *action_file;
22246060Scorbett extern FILE *code_file;
22339774Sbostic extern FILE *defines_file;
22439774Sbostic extern FILE *input_file;
22539774Sbostic extern FILE *output_file;
22639774Sbostic extern FILE *text_file;
22739774Sbostic extern FILE *union_file;
22839774Sbostic extern FILE *verbose_file;
22939774Sbostic 
23039774Sbostic extern int nitems;
23139774Sbostic extern int nrules;
23239774Sbostic extern int nsyms;
23339774Sbostic extern int ntokens;
23439774Sbostic extern int nvars;
23540085Sbostic extern int ntags;
23639774Sbostic 
23739774Sbostic extern char unionized;
23839774Sbostic extern char line_format[];
23939774Sbostic 
24039774Sbostic extern int   start_symbol;
24139774Sbostic extern char  **symbol_name;
24239774Sbostic extern short *symbol_value;
24339774Sbostic extern short *symbol_prec;
24439774Sbostic extern char  *symbol_assoc;
24539774Sbostic 
24639774Sbostic extern short *ritem;
24739774Sbostic extern short *rlhs;
24839774Sbostic extern short *rrhs;
24939774Sbostic extern short *rprec;
25039774Sbostic extern char  *rassoc;
25139774Sbostic 
25239774Sbostic extern short **derives;
25339774Sbostic extern char *nullable;
25439774Sbostic 
25539774Sbostic extern bucket *first_symbol;
25639774Sbostic extern bucket *last_symbol;
25739774Sbostic 
25839774Sbostic extern int nstates;
25939774Sbostic extern core *first_state;
26039774Sbostic extern shifts *first_shift;
26139774Sbostic extern reductions *first_reduction;
26239774Sbostic extern short *accessing_symbol;
26339774Sbostic extern core **state_table;
26439774Sbostic extern shifts **shift_table;
26539774Sbostic extern reductions **reduction_table;
26639774Sbostic extern unsigned *LA;
26739774Sbostic extern short *LAruleno;
26839774Sbostic extern short *lookaheads;
26939774Sbostic extern short *goto_map;
27039774Sbostic extern short *from_state;
27139774Sbostic extern short *to_state;
27239774Sbostic 
27339774Sbostic extern action **parser;
27439774Sbostic extern int SRtotal;
27539774Sbostic extern int RRtotal;
27639774Sbostic extern short *SRconflicts;
27739774Sbostic extern short *RRconflicts;
27839774Sbostic extern short *defred;
27939774Sbostic extern short *rules_used;
28039774Sbostic extern short nunused;
28139774Sbostic extern short final_state;
28239774Sbostic 
28339774Sbostic /* global functions */
28439774Sbostic 
28539774Sbostic extern char *allocate();
28639774Sbostic extern bucket *lookup();
28739774Sbostic extern bucket *make_bucket();
28839774Sbostic 
28939774Sbostic 
29039774Sbostic /* system variables */
29139774Sbostic 
29239774Sbostic extern int errno;
29339774Sbostic 
29439774Sbostic 
29539774Sbostic /* system functions */
29639774Sbostic 
29739774Sbostic extern void free();
29839774Sbostic extern char *calloc();
29939774Sbostic extern char *malloc();
30039774Sbostic extern char *realloc();
30139774Sbostic extern char *strcpy();
302