Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_GRAMMAR_H |
| 2 | #define Py_GRAMMAR_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 7 | /*********************************************************** |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 8 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 9 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 10 | |
| 11 | All Rights Reserved |
| 12 | |
| 13 | Permission to use, copy, modify, and distribute this software and its |
| 14 | documentation for any purpose and without fee is hereby granted, |
| 15 | provided that the above copyright notice appear in all copies and that |
| 16 | both that copyright notice and this permission notice appear in |
| 17 | supporting documentation, and that the names of Stichting Mathematisch |
| 18 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 19 | distribution of the software without specific, written prior permission. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 22 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 23 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 24 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 25 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 26 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 27 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 28 | |
| 29 | ******************************************************************/ |
| 30 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | /* Grammar interface */ |
| 32 | |
| 33 | #include "bitset.h" /* Sigh... */ |
| 34 | |
| 35 | /* A label of an arc */ |
| 36 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 37 | typedef struct { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | 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 Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | typedef struct { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | int ll_nlabels; |
| 48 | label *ll_label; |
| 49 | } labellist; |
| 50 | |
| 51 | /* An arc from one state to another */ |
| 52 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 53 | typedef struct { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | 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 Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | typedef struct { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | 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 Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 73 | typedef struct { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | 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 Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 84 | typedef struct { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | 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 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame^] | 94 | grammar *newgrammar Py_PROTO((int start)); |
| 95 | dfa *adddfa Py_PROTO((grammar *g, int type, char *name)); |
| 96 | int addstate Py_PROTO((dfa *d)); |
| 97 | void addarc Py_PROTO((dfa *d, int from, int to, int lbl)); |
| 98 | dfa *PyGrammar_FindDFA Py_PROTO((grammar *g, int type)); |
| 99 | char *typename Py_PROTO((grammar *g, int lbl)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame^] | 101 | int addlabel Py_PROTO((labellist *ll, int type, char *str)); |
| 102 | int findlabel Py_PROTO((labellist *ll, int type, char *str)); |
| 103 | char *PyGrammar_LabelRepr Py_PROTO((label *lb)); |
| 104 | void translatelabels Py_PROTO((grammar *g)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame^] | 106 | void addfirstsets Py_PROTO((grammar *g)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame^] | 108 | void PyGrammar_AddAccelerators Py_PROTO((grammar *g)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame^] | 110 | void printgrammar Py_PROTO((grammar *g, FILE *fp)); |
| 111 | void printnonterminals Py_PROTO((grammar *g, FILE *fp)); |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 112 | |
| 113 | #ifdef __cplusplus |
| 114 | } |
| 115 | #endif |
| 116 | #endif /* !Py_GRAMMAR_H */ |