Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 5113f5f | 1992-04-05 14:20:22 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 3 | Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Grammar interface */ |
| 26 | |
| 27 | #include "bitset.h" /* Sigh... */ |
| 28 | |
| 29 | /* A label of an arc */ |
| 30 | |
| 31 | typedef 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 | |
| 40 | typedef struct _labellist { |
| 41 | int ll_nlabels; |
| 42 | label *ll_label; |
| 43 | } labellist; |
| 44 | |
| 45 | /* An arc from one state to another */ |
| 46 | |
| 47 | typedef 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 | |
| 54 | typedef 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 | |
| 67 | typedef 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 | |
| 78 | typedef 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 | |
| 88 | grammar *newgrammar PROTO((int start)); |
| 89 | dfa *adddfa PROTO((grammar *g, int type, char *name)); |
| 90 | int addstate PROTO((dfa *d)); |
| 91 | void addarc PROTO((dfa *d, int from, int to, int lbl)); |
| 92 | dfa *finddfa PROTO((grammar *g, int type)); |
| 93 | char *typename PROTO((grammar *g, int lbl)); |
| 94 | |
| 95 | int addlabel PROTO((labellist *ll, int type, char *str)); |
| 96 | int findlabel PROTO((labellist *ll, int type, char *str)); |
| 97 | char *labelrepr PROTO((label *lb)); |
| 98 | void translatelabels PROTO((grammar *g)); |
| 99 | |
| 100 | void addfirstsets PROTO((grammar *g)); |
| 101 | |
Guido van Rossum | 3702284 | 1992-09-03 20:46:06 +0000 | [diff] [blame] | 102 | void addaccelerators PROTO((grammar *g)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 103 | |
| 104 | void printgrammar PROTO((grammar *g, FILE *fp)); |
| 105 | void printnonterminals PROTO((grammar *g, FILE *fp)); |