Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | /* Grammar interface */ |
| 12 | |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 13 | #ifndef Py_GRAMMAR_H |
| 14 | #define Py_GRAMMAR_H |
| 15 | #ifdef __cplusplus |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 19 | #include "bitset.h" /* Sigh... */ |
| 20 | |
| 21 | /* A label of an arc */ |
| 22 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 23 | typedef struct { |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 24 | int lb_type; |
| 25 | char *lb_str; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 26 | } label; |
| 27 | |
| 28 | #define EMPTY 0 /* Label number 0 is by definition the empty label */ |
| 29 | |
| 30 | /* A list of labels */ |
| 31 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 32 | typedef struct { |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 33 | int ll_nlabels; |
| 34 | label *ll_label; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | } labellist; |
| 36 | |
| 37 | /* An arc from one state to another */ |
| 38 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 39 | typedef struct { |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 40 | short a_lbl; /* Label of this arc */ |
| 41 | short a_arrow; /* State where this arc goes to */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | } arc; |
| 43 | |
| 44 | /* A state in a DFA */ |
| 45 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | typedef struct { |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 47 | int s_narcs; |
| 48 | arc *s_arc; /* Array of arcs */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 50 | /* Optional accelerators */ |
| 51 | int s_lower; /* Lowest label index */ |
| 52 | int s_upper; /* Highest label index */ |
| 53 | int *s_accel; /* Accelerator */ |
| 54 | int s_accept; /* Nonzero for accepting state */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | } state; |
| 56 | |
| 57 | /* A DFA */ |
| 58 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 59 | typedef struct { |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 60 | int d_type; /* Non-terminal this represents */ |
| 61 | char *d_name; /* For printing */ |
| 62 | int d_initial; /* Initial state */ |
| 63 | int d_nstates; |
| 64 | state *d_state; /* Array of states */ |
| 65 | bitset d_first; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | } dfa; |
| 67 | |
| 68 | /* A grammar */ |
| 69 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 70 | typedef struct { |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 71 | int g_ndfas; |
| 72 | dfa *g_dfa; /* Array of DFAs */ |
| 73 | labellist g_ll; |
| 74 | int g_start; /* Start symbol of the grammar */ |
| 75 | int g_accel; /* Set if accelerators present */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | } grammar; |
| 77 | |
| 78 | /* FUNCTIONS */ |
| 79 | |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 80 | grammar *newgrammar(int start); |
| 81 | dfa *adddfa(grammar *g, int type, char *name); |
| 82 | int addstate(dfa *d); |
| 83 | void addarc(dfa *d, int from, int to, int lbl); |
| 84 | dfa *PyGrammar_FindDFA(grammar *g, int type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 86 | int addlabel(labellist *ll, int type, char *str); |
| 87 | int findlabel(labellist *ll, int type, char *str); |
| 88 | char *PyGrammar_LabelRepr(label *lb); |
| 89 | void translatelabels(grammar *g); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 91 | void addfirstsets(grammar *g); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 93 | void PyGrammar_AddAccelerators(grammar *g); |
| 94 | void PyGrammar_RemoveAccelerators(grammar *); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 95 | |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 96 | void printgrammar(grammar *g, FILE *fp); |
| 97 | void printnonterminals(grammar *g, FILE *fp); |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 98 | |
| 99 | #ifdef __cplusplus |
| 100 | } |
| 101 | #endif |
| 102 | #endif /* !Py_GRAMMAR_H */ |