-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinclude.h
204 lines (176 loc) · 5.79 KB
/
include.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifndef _INCLUDE_H_
#define _INCLUDE_H_
#include <stdio.h>
#include <stdlib.h>
// 定义常量
#define MAX_TOKEN_LEN 100 // 最大单词长度
#define MAX_IDENT_LEN 32 // 最大关键词长度
#define MAX_NUM_LEN 1024 // 最多读入单词数目
#define MAX_TERMINAL_NUM 64 // 语法定义时直接定义的终结符数目最大值
#define MAX_NON_TERMINAL_NUM 1024 // 最多非终结符数目
#define MAX_STATE_STACK_NUM 256 // 状态栈最大大小
#define MAX_GEN_CODE_NUM 256 // 最大中间代码数目
// 定义文件路径
#define DEFAULT_INPUT_FILE "../input.txt"
#define DEFAULT_GRAMMAR_FILE "../Language.grammar"
// 定义状态类型
typedef enum {
END_OF_FILE_TOKEN,
KEYWORD_TOKEN,
IDENTIFIER_TOKEN,
NUMBER_TOKEN,
OPERATOR_TOKEN,
ERROR_TOKEN
} TokenType;
typedef struct {
TokenType type;
char str[MAX_TOKEN_LEN];
} Token;
typedef struct {
int id; // 表示这条产生式是第几条
int production;
int generative[10];
int gen_nums;
} Production, *pProduction;
typedef struct {
int *items;
int item_nums;
}FIRST, FOLLOW, *pFIRST, *pFOLLOW;
typedef struct {
pProduction productions;
int production_nums;
int item_nums; // first集合数目,也是语法符号表中符号的数目
pFIRST firsts; // first集合,注意因为first集合包含所有符号,所以索引向右平移了terminal_nums位
pFOLLOW follows; // 但是follow集合只包含非终结符,所以索引不需要平移
int follow_nums; // follow集合数目,也是非终极符号表的符号数目
} Grammar, *pGrammar;
typedef struct {
Production production;
int dot_pos;
} PosProduction, *pPosProduction;
typedef struct {
pPosProduction pos_productions;
int pos_production_nums;
int* non_core_productions;
int non_core_production_nums;
} State, *pState;
typedef struct {
pState states;
int state_nums;
} AutomatonStates;
typedef struct {
enum{
EMPTY_STATE = 0, // 空状态
GOTO_STATE, // 用GOTO表转移
SHIFT_STATE, // 直接ACTION表转移
REDUCE_STATE, // 使用某条语法规则规约
ACCEPT_STATE, // 接受状态
ERROR_STATE // 错误状态
} action_type;
int value;
} Action, *pAction;
typedef struct {
pAction actions; // 前terminal_nums个是action表(第一维是终止符),后non_terminal_nums个是goto表
int action_nums; // 元素总数
int state_nums; // 状态数,也是表格的行数
} ActionTable, *pActionTable;
typedef union { // 符号属性
struct {
int ins_pos;
} auxiliary_elem;
struct {
int type_id;
} terminal_elem;
struct {
int type_id;
char str[MAX_TOKEN_LEN];
} variable_elem;
struct {
int * true_list;
int * false_list;
int true_nums;
int false_nums;
} condition_elem;
struct {
int * next_list;
int next_nums;
} statement_elem;
} Attribute, *pAttribute;
// 栈
typedef struct {
int state_id; // 状态栈id
int token_id; // 符号栈id
Attribute attribute; // 符号属性
} StackElem, *pStackElem;
typedef struct {
pStackElem pool;
int top;
}Stack, *pStack;
typedef struct {
enum {
JUMP,
CONDITIONAL_JUMP,
ASSIGNMENT,
} code_type;
struct {
int jump_target;
int op_id; // 取值和语法符号表中的id一致
char params[3][MAX_TOKEN_LEN];
} code;
} GenerateCode, *pGenerateCode;
typedef struct {
pGenerateCode codes;
int code_nums;
} GenerateCodes, *pGenerateCodes;
// 定义全局变量
// 定义字符表(数字和字母的字符表没用)
extern const char number_table[];
extern const char alpha_table[];
extern const char keyword_table[][MAX_IDENT_LEN];
extern const char operator_elem_table[];
extern const char operator_table[][MAX_IDENT_LEN];
// 定义符号表
extern Token terminal_table[MAX_TERMINAL_NUM + 1];
extern char non_terminal_table[MAX_NON_TERMINAL_NUM + 1];
extern int terminal_nums;
extern int non_terminal_nums;
// 共有函数定义
// LexicalAnalysis
Token get_token(FILE *fp);
void print_error(const char *msg);
void print_token(Token t);
int is_keyword(const char *str);
int is_operator_elem(char ch);
int is_operator(const char *str);
int is_blank(char ch);
// GrammarAnalysis
int get_id(const char *lex);
void get_lex(int id, char temp[MAX_TOKEN_LEN]);
void processGrammar(FILE *fp, pGrammar grammar);
int productEpsilon(Grammar grammar, const int* string, int str_nums);
int mergeSet(int *a, int* a_size, const int *b, int b_size, int* epsilon_pos, int epsilon_val);
void calcFIRSTSet(pGrammar grammar);
void calcFOLLOWSet(pGrammar grammar);
Grammar generateGrammar(FILE* fp);
void printGrammar(Grammar grammar);
// ActionTableGeneration
State mergeState(State a, State b);
int sameState(State a, State b);
State closure(PosProduction pos_production, Grammar grammar);
State gotoState(State state, int ch, Grammar grammar);
void printState(State state);
void printStates(AutomatonStates automaton_states);
void getActionTable(Grammar grammar, AutomatonStates* automaton_states, ActionTable *action_table);
void printActionTable(ActionTable action_table);
// SyntaxAnalysis
void freeStack(pStack stack);
void initialStack(pStack stack, int size);
void pushStack(pStack stack, StackElem item);
StackElem popStack(pStack stack);
StackElem topStack(pStack stack);
void printStack(Stack stack);
int ParseAndGenerate(FILE *fp, ActionTable action_table, Grammar grammar, pGenerateCodes generate_codes);
// GenerateCode
Attribute generateCode(pGenerateCodes generate_codes, Production production, pStackElem elems);
void assignAttribute(pAttribute attributes, Stack stack);
#endif //_INCLUDE_H_