blob: a469ca51c6c25548b16d0b115d1d0c44aaa471ab [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 Rossum5799b521995-01-04 19:06:22 +00008Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
9The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000010
11 All Rights Reserved
12
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000013Copyright (c) 2000, BeOpen.com.
14Copyright (c) 1995-2000, Corporation for National Research Initiatives.
15Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
16All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000017
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000018See the file "Misc/COPYRIGHT" for information on usage and
19redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000020
21******************************************************************/
22
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000023/* Grammar interface */
24
25#include "bitset.h" /* Sigh... */
26
27/* A label of an arc */
28
Guido van Rossumb6775db1994-08-01 11:34:53 +000029typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030 int lb_type;
31 char *lb_str;
32} label;
33
34#define EMPTY 0 /* Label number 0 is by definition the empty label */
35
36/* A list of labels */
37
Guido van Rossumb6775db1994-08-01 11:34:53 +000038typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039 int ll_nlabels;
40 label *ll_label;
41} labellist;
42
43/* An arc from one state to another */
44
Guido van Rossumb6775db1994-08-01 11:34:53 +000045typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 short a_lbl; /* Label of this arc */
47 short a_arrow; /* State where this arc goes to */
48} arc;
49
50/* A state in a DFA */
51
Guido van Rossumb6775db1994-08-01 11:34:53 +000052typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053 int s_narcs;
54 arc *s_arc; /* Array of arcs */
55
56 /* Optional accelerators */
57 int s_lower; /* Lowest label index */
58 int s_upper; /* Highest label index */
59 int *s_accel; /* Accelerator */
60 int s_accept; /* Nonzero for accepting state */
61} state;
62
63/* A DFA */
64
Guido van Rossumb6775db1994-08-01 11:34:53 +000065typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 int d_type; /* Non-terminal this represents */
67 char *d_name; /* For printing */
68 int d_initial; /* Initial state */
69 int d_nstates;
70 state *d_state; /* Array of states */
71 bitset d_first;
72} dfa;
73
74/* A grammar */
75
Guido van Rossumb6775db1994-08-01 11:34:53 +000076typedef struct {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077 int g_ndfas;
78 dfa *g_dfa; /* Array of DFAs */
79 labellist g_ll;
80 int g_start; /* Start symbol of the grammar */
81 int g_accel; /* Set if accelerators present */
82} grammar;
83
84/* FUNCTIONS */
85
Guido van Rossumcaa63801995-01-12 11:45:45 +000086grammar *newgrammar Py_PROTO((int start));
87dfa *adddfa Py_PROTO((grammar *g, int type, char *name));
88int addstate Py_PROTO((dfa *d));
89void addarc Py_PROTO((dfa *d, int from, int to, int lbl));
90dfa *PyGrammar_FindDFA Py_PROTO((grammar *g, int type));
91char *typename Py_PROTO((grammar *g, int lbl));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092
Guido van Rossumcaa63801995-01-12 11:45:45 +000093int addlabel Py_PROTO((labellist *ll, int type, char *str));
94int findlabel Py_PROTO((labellist *ll, int type, char *str));
95char *PyGrammar_LabelRepr Py_PROTO((label *lb));
96void translatelabels Py_PROTO((grammar *g));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097
Guido van Rossumcaa63801995-01-12 11:45:45 +000098void addfirstsets Py_PROTO((grammar *g));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099
Guido van Rossumcaa63801995-01-12 11:45:45 +0000100void PyGrammar_AddAccelerators Py_PROTO((grammar *g));
Guido van Rossumbb301c51997-08-12 14:57:08 +0000101void PyGrammar_RemoveAccelerators Py_PROTO((grammar *));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102
Guido van Rossumcaa63801995-01-12 11:45:45 +0000103void printgrammar Py_PROTO((grammar *g, FILE *fp));
104void printnonterminals Py_PROTO((grammar *g, FILE *fp));
Guido van Rossuma3309961993-07-28 09:05:47 +0000105
106#ifdef __cplusplus
107}
108#endif
109#endif /* !Py_GRAMMAR_H */