blob: f775f9638172b3e9897ecaf1fcf2d368258b6f6e [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Grammar interface */
3
Fred Drakeea9cb5a2000-07-09 00:20:36 +00004#ifndef Py_GRAMMAR_H
5#define Py_GRAMMAR_H
6#ifdef __cplusplus
7extern "C" {
8#endif
9
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010#include "bitset.h" /* Sigh... */
11
12/* A label of an arc */
13
Guido van Rossumb6775db1994-08-01 11:34:53 +000014typedef struct {
Fred Drakeea9cb5a2000-07-09 00:20:36 +000015 int lb_type;
16 char *lb_str;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017} label;
18
19#define EMPTY 0 /* Label number 0 is by definition the empty label */
20
21/* A list of labels */
22
Guido van Rossumb6775db1994-08-01 11:34:53 +000023typedef struct {
Fred Drakeea9cb5a2000-07-09 00:20:36 +000024 int ll_nlabels;
25 label *ll_label;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026} labellist;
27
28/* An arc from one state to another */
29
Guido van Rossumb6775db1994-08-01 11:34:53 +000030typedef struct {
Fred Drakeea9cb5a2000-07-09 00:20:36 +000031 short a_lbl; /* Label of this arc */
32 short a_arrow; /* State where this arc goes to */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033} arc;
34
35/* A state in a DFA */
36
Guido van Rossumb6775db1994-08-01 11:34:53 +000037typedef struct {
Fred Drakeea9cb5a2000-07-09 00:20:36 +000038 int s_narcs;
39 arc *s_arc; /* Array of arcs */
Serhiy Storchakaf4027752015-03-21 09:25:53 +020040
Fred Drakeea9cb5a2000-07-09 00:20:36 +000041 /* 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 Rossum85a5fbb1990-10-14 12:07:46 +000046} state;
47
48/* A DFA */
49
Guido van Rossumb6775db1994-08-01 11:34:53 +000050typedef struct {
Fred Drakeea9cb5a2000-07-09 00:20:36 +000051 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 Rossum85a5fbb1990-10-14 12:07:46 +000057} dfa;
58
59/* A grammar */
60
Guido van Rossumb6775db1994-08-01 11:34:53 +000061typedef struct {
Fred Drakeea9cb5a2000-07-09 00:20:36 +000062 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 Rossum85a5fbb1990-10-14 12:07:46 +000067} grammar;
68
69/* FUNCTIONS */
70
Fred Drakeea9cb5a2000-07-09 00:20:36 +000071grammar *newgrammar(int start);
Benjamin Peterson9ac11a72016-09-18 18:00:25 -070072void freegrammar(grammar *g);
Serhiy Storchakac6792272013-10-19 21:03:34 +030073dfa *adddfa(grammar *g, int type, const char *name);
Fred Drakeea9cb5a2000-07-09 00:20:36 +000074int addstate(dfa *d);
75void addarc(dfa *d, int from, int to, int lbl);
76dfa *PyGrammar_FindDFA(grammar *g, int type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077
Serhiy Storchakac6792272013-10-19 21:03:34 +030078int addlabel(labellist *ll, int type, const char *str);
79int findlabel(labellist *ll, int type, const char *str);
Benjamin Peterson742b2f82012-10-31 13:36:13 -040080const char *PyGrammar_LabelRepr(label *lb);
Fred Drakeea9cb5a2000-07-09 00:20:36 +000081void translatelabels(grammar *g);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082
Fred Drakeea9cb5a2000-07-09 00:20:36 +000083void addfirstsets(grammar *g);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084
Fred Drakeea9cb5a2000-07-09 00:20:36 +000085void PyGrammar_AddAccelerators(grammar *g);
86void PyGrammar_RemoveAccelerators(grammar *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000087
Fred Drakeea9cb5a2000-07-09 00:20:36 +000088void printgrammar(grammar *g, FILE *fp);
89void printnonterminals(grammar *g, FILE *fp);
Guido van Rossuma3309961993-07-28 09:05:47 +000090
91#ifdef __cplusplus
92}
93#endif
94#endif /* !Py_GRAMMAR_H */