[go: up one dir, main page]

Menu

[r377]: / libpetey / grammar_t.h  Maximize  Restore  History

Download this file

99 lines (74 with data), 2.2 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include "string_petey.h"
#include "symbol_table.h"
#include "vector_e.h"
using namespace std;
using namespace libpetey;
class grammar_t;
class parse_tree;
class expr_t {
public:
expr_t();
virtual ~expr_t();
virtual parse_tree * parse(char *string, int ver)=0;
virtual int parse(char *string, parse_tree *pt, int depth=0)=0;
virtual int nver()=0;
virtual int ns(int v)=0;
virtual char *nameof()=0;
};
class primitive: public expr_t {
protected:
char val;
public:
primitive(char v1);
virtual ~primitive();
virtual parse_tree * parse(char *string, int ver);
virtual int parse(char *string, parse_tree *pt, int depth=0);
virtual int nver();
virtual int ns(int v);
virtual char *nameof();
};
class compound: public expr_t {
protected:
grammar_t *grammar; //each expression knows it's own identity
long id;
vector_e<expr_t **> sub; //list of sub-expressions
vector_e<int> nsub; //number of sub-expressions per version
int ntyp; //number of versions
public:
compound(grammar_t *g, long i);
virtual ~compound();
int add(expr_t **neex, int n);
virtual int nver();
virtual int ns(int v);
virtual char *nameof();
virtual parse_tree * parse(char *string, int ver);
virtual int parse(char *string, parse_tree *pt, int depth=0);
};
class parse_tree {
public:
expr_t *expr;
int ver; //which version of the expression
parse_tree **sub; //sub-trees
char *loc; //location where expression starts
int len; //total length of the expression
parse_tree();
parse_tree(expr_t *e, int v, char * l, int n, parse_tree **s);
~parse_tree();
void print(int depth=0);
char * value();
};
class grammar_t {
friend class compound;
protected:
symbol_table<string_petey> name;
vector_e<expr_t *> exprlist;
long last; //id of last expression defined
public:
grammar_t();
~grammar_t();
int add(char *nm, char *def); //add a new expression
int load(FILE *fs); //load a complete grammar
parse_tree * parse(char *expr_name, char *string);
parse_tree *parse(char *string);
};