blob: a15316eca280829cd2c1fe694db056b1b3096e35 [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_GRAMMAR_H
2#define Py_GRAMMAR_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Guido van Rossumf70e43a1991-02-19 12:39:46 +00007/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00008Copyright (c) 2000, BeOpen.com.
9Copyright (c) 1995-2000, Corporation for National Research Initiatives.
10Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
11All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000012
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000013See the file "Misc/COPYRIGHT" for information on usage and
14redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000015******************************************************************/
16
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017/* Grammar interface */
18
19#include "bitset.h" /* Sigh... */
20
21/* A label of an arc */
22
Guido van Rossumb6775db1994-08-01 11:34:53 +000023typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024 int lb_type;
25 char *lb_str;
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 Rossumb6775db1994-08-01 11:34:53 +000032typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033 int ll_nlabels;
34 label *ll_label;
35} labellist;
36
37/* An arc from one state to another */
38
Guido van Rossumb6775db1994-08-01 11:34:53 +000039typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040 short a_lbl; /* Label of this arc */
41 short a_arrow; /* State where this arc goes to */
42} arc;
43
44/* A state in a DFA */
45
Guido van Rossumb6775db1994-08-01 11:34:53 +000046typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047 int s_narcs;
48 arc *s_arc; /* Array of arcs */
49
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 */
55} state;
56
57/* A DFA */
58
Guido van Rossumb6775db1994-08-01 11:34:53 +000059typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060 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;
66} dfa;
67
68/* A grammar */
69
Guido van Rossumb6775db1994-08-01 11:34:53 +000070typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071 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 */
76} grammar;
77
78/* FUNCTIONS */
79
Guido van Rossumcaa63801995-01-12 11:45:45 +000080grammar *newgrammar Py_PROTO((int start));
81dfa *adddfa Py_PROTO((grammar *g, int type, char *name));
82int addstate Py_PROTO((dfa *d));
83void addarc Py_PROTO((dfa *d, int from, int to, int lbl));
84dfa *PyGrammar_FindDFA Py_PROTO((grammar *g, int type));
85char *typename Py_PROTO((grammar *g, int lbl));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086
Guido van Rossumcaa63801995-01-12 11:45:45 +000087int addlabel Py_PROTO((labellist *ll, int type, char *str));
88int findlabel Py_PROTO((labellist *ll, int type, char *str));
89char *PyGrammar_LabelRepr Py_PROTO((label *lb));
90void translatelabels Py_PROTO((grammar *g));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091
Guido van Rossumcaa63801995-01-12 11:45:45 +000092void addfirstsets Py_PROTO((grammar *g));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093
Guido van Rossumcaa63801995-01-12 11:45:45 +000094void PyGrammar_AddAccelerators Py_PROTO((grammar *g));
Guido van Rossumbb301c51997-08-12 14:57:08 +000095void PyGrammar_RemoveAccelerators Py_PROTO((grammar *));
Guido van Rossum3f5da241990-12-20 15:06:42 +000096
Guido van Rossumcaa63801995-01-12 11:45:45 +000097void printgrammar Py_PROTO((grammar *g, FILE *fp));
98void printnonterminals Py_PROTO((grammar *g, FILE *fp));
Guido van Rossuma3309961993-07-28 09:05:47 +000099
100#ifdef __cplusplus
101}
102#endif
103#endif /* !Py_GRAMMAR_H */