blob: 5fd89158bc38de4513294a203bc607d32f9f5177 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum5113f5f1992-04-05 14:20:22 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumf70e43a1991-02-19 12:39:46 +00003Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Grammar interface */
26
27#include "bitset.h" /* Sigh... */
28
29/* A label of an arc */
30
31typedef struct _label {
32 int lb_type;
33 char *lb_str;
34} label;
35
36#define EMPTY 0 /* Label number 0 is by definition the empty label */
37
38/* A list of labels */
39
40typedef struct _labellist {
41 int ll_nlabels;
42 label *ll_label;
43} labellist;
44
45/* An arc from one state to another */
46
47typedef struct _arc {
48 short a_lbl; /* Label of this arc */
49 short a_arrow; /* State where this arc goes to */
50} arc;
51
52/* A state in a DFA */
53
54typedef struct _state {
55 int s_narcs;
56 arc *s_arc; /* Array of arcs */
57
58 /* Optional accelerators */
59 int s_lower; /* Lowest label index */
60 int s_upper; /* Highest label index */
61 int *s_accel; /* Accelerator */
62 int s_accept; /* Nonzero for accepting state */
63} state;
64
65/* A DFA */
66
67typedef struct _dfa {
68 int d_type; /* Non-terminal this represents */
69 char *d_name; /* For printing */
70 int d_initial; /* Initial state */
71 int d_nstates;
72 state *d_state; /* Array of states */
73 bitset d_first;
74} dfa;
75
76/* A grammar */
77
78typedef struct _grammar {
79 int g_ndfas;
80 dfa *g_dfa; /* Array of DFAs */
81 labellist g_ll;
82 int g_start; /* Start symbol of the grammar */
83 int g_accel; /* Set if accelerators present */
84} grammar;
85
86/* FUNCTIONS */
87
88grammar *newgrammar PROTO((int start));
89dfa *adddfa PROTO((grammar *g, int type, char *name));
90int addstate PROTO((dfa *d));
91void addarc PROTO((dfa *d, int from, int to, int lbl));
92dfa *finddfa PROTO((grammar *g, int type));
93char *typename PROTO((grammar *g, int lbl));
94
95int addlabel PROTO((labellist *ll, int type, char *str));
96int findlabel PROTO((labellist *ll, int type, char *str));
97char *labelrepr PROTO((label *lb));
98void translatelabels PROTO((grammar *g));
99
100void addfirstsets PROTO((grammar *g));
101
Guido van Rossum37022841992-09-03 20:46:06 +0000102void addaccelerators PROTO((grammar *g));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103
104void printgrammar PROTO((grammar *g, FILE *fp));
105void printnonterminals PROTO((grammar *g, FILE *fp));