-
Notifications
You must be signed in to change notification settings - Fork 1
/
cparse.h
92 lines (73 loc) · 1.64 KB
/
cparse.h
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
#ifndef CPARSE_H
#define CPARSE_H
#include "clex/clex.h"
#include <stdbool.h>
#define ARRAY_CAPACITY 2048
typedef struct SetItem {
char *key;
char **values;
} SetItem;
typedef struct Rule {
char *left;
char **right;
} Rule;
typedef struct Grammar {
Rule **rules;
char **terminals;
char **nonterminals;
char *start;
SetItem **first;
SetItem **follow;
} Grammar;
typedef struct LR1Item {
char **lookaheads;
char *left;
char **right;
int dot;
} LR1Item;
typedef struct LR1State LR1State;
typedef struct LR1Transition {
char *value;
LR1State *state;
} LR1Transition;
typedef struct LR1State {
LR1Item **items;
LR1Transition **transitions;
} LR1State;
typedef struct GoToNode {
char *nonterminal;
int state;
} GoToNode;
typedef enum ActionType {
ACCEPT,
SHIFT,
REDUCE
} ActionType;
typedef struct Action {
ActionType type;
int operand;
} Action;
typedef struct ActionNode {
char *terminal;
Action *action;
} ActionNode;
typedef struct LR1Parser {
Grammar *grammar;
LR1State **collection;
GoToNode ***goToTable;
ActionNode ***actionTable;
const char * const *tokenKindStr;
} LR1Parser;
typedef LR1Parser LALR1Parser;
typedef struct ParseTreeNode ParseTreeNode;
typedef struct ParseTreeNode {
char *value;
Token token;
ParseTreeNode **children;
} ParseTreeNode;
Grammar *cparseGrammar(char *grammarString);
LR1Parser *cparseCreateLR1Parser(Grammar *grammar, const char * const *tokenKindStr);
LALR1Parser *cparseCreateLALR1Parser(Grammar *grammar, const char * const *tokenKindStr);
bool cparseAccept(LR1Parser *parser, char *input);
ParseTreeNode *cparse(LR1Parser *parser, char *input);
#endif