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