1
//-----------------------------parser.h---------------------------------
2
#ifndef PARSER_H
3
#define PARSER_H
4
5
#include "scanner.h"
6
7
typedef double (*FuncPtr)(double);
8
struct ExprNode // 语法树节点类型
9
#endif
2
3
4
5
6
7
8
9
1
//-------------------------parser.cpp-----------------------------
2
3
#include "parser.h"
4
5
#define PARSER_DEBUG
6
7
#ifndef PARSER_DEBUG
8
#include "semantic.h"
9
#endif
10
11
#ifdef PARSER_DEBUG
12
#define enter(x) printf("enter in "); printf(x); printf("\n")
13
#else
14
#define enter(x)
15
#endif
16
17
#ifdef PARSER_DEBUG
18
#define back(x) printf("exit from "); printf(x); printf("\n")
19
#else
20
#define back(x)
21
#endif
22
23
#ifdef PARSER_DERBUG
24
#define call_match(x) printf("match token "); printf(x); printf("\n")
25
#else
26
#define call_match(x)
27
#endif
28
29
#ifdef PARSER_DEBUG
30
#define Tree_trace(x) PrintSyntaxTree(x, 1);
31
#else
32
#define Tree_trace
33
#endif
34
35
#ifdef PARSER_DEBUG
36
double Parameter = 0; //参数T的存储空间
37
#else
38
double Parameter = 0, //参数存储空间
39
Origin_x = 0, Origin_y = 0, //横纵坐标平移距离
40
Scale_x = 1,Scale_y = 1, //横纵比例因子
41
Rot_angle = 0; //旋转角度
42
#endif
43
44
static Token token; //记号
45
46
47
// ------------辅助函数声明
48
static void FetchToken();
49
static void MatchToken(enum Token_Type AToken);
50
static void SyntaxError(int case_of);
51
static void ErrMsg(unsigned LineNo, char *descrip, char *string);
52
static void PrintSyntaxTree(struct ExprNode *root, int indent);
53
54
// ------------非终结符的递归子程序声明
55
static void Program();
56
static void Statement();
57
static void OriginStatement();
58
static void RotStatement();
59
static void ScaleStatement();
60
static void ForStatement();
61
static struct ExprNode *Expression();
62
static struct ExprNode *Term();
63
static struct ExprNode *Factor();
64
static struct ExprNode *Component();
65
static struct ExprNode *Atom();
66
67
// -------------外部接口与语法树构造函数声明
68
extern void Parser(char *SrcFilePtr);
69
static struct ExprNode *MakeExprNode(enum Token_Type opcode,
);
70
71
// -------------通过词法分析器接口GetToken获取一个记号
72
static void FetchToken()
73
}
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
1
//----------------------------parsermain.cpp-----------------------
2
3
#include <stdio.h>
4
#include "parser.h"
5
6
extern void Parser(char *SrcFilePtr);
7
8
int main()
9
}
2
3
4
5
6
7
8
9
注:要想正常运行需要把词法分析器的那部分也加入到工程中