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