-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntaxparser.hpp
107 lines (83 loc) · 2.01 KB
/
syntaxparser.hpp
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
#ifndef SYNTAX_PARSER_HPP
#define SYNTAX_PARSER_HPP
#include "reader.hpp"
#include <vector>
#include <map>
/*Grammar of how to define gramar
F' -> .F& ? r0
.F -> .L $ r1
.F -> .L\nF $ r2
.L -> .I>E \n, $ r3
.E -> .IE \n, $ r4
.E -> .I \n, $ r5
.I -> ./D >, /, \n, $ r6
.D -> .[0-9]D >, /, \n, $ r7
.D -> .[0-9] >, /, \n, $ r8
lalrtable:
/ [0-9] > \n $ | F L E I D
1 s8 | g2 g3 g5
2 a |
3 s4 r1 |
4 s8 | g11 g3 g5
5 s6 |
6 s8 | g7
7 s8 r5 r5 |
8 s9 | g10
9 r8 s9 r8 r8 r8 | g13
10 r6 r6 r6 r6 |
11 r2 |
12 r3 r3 |
13 r7 r7 r7 r7 |
14 r4 r4 |
*/
#define NUM_RULES 9
#define FAIL 0
#define SHIFT 1
#define GOTO 2
#define REDUCE 3
#define ACCEPT 4
#define NA (createAction(FAIL, 0))
#define S(a) (createAction(SHIFT, a))
#define G(a) (createAction(GOTO, a))
#define R(a) (createAction(REDUCE, a))
#define ACK (createAction(ACCEPT, 0))
using namespace std;
typedef struct action {
int type;
int num;
} action;
action createAction(int a, int b);
typedef struct File File;
typedef struct Line Line;
typedef struct Id Id;
typedef struct Exp Exp;
typedef struct Digits Digits;
struct File
{
Line *line;
File *next = NULL;
};
struct Line
{
Exp *exp;
Id *id;
};
struct Id
{
Digits *digits;
};
struct Exp
{
Id *id;
Exp *next = NULL;
};
struct Digits
{
int val;
Digits *next = NULL;
};
File *parse(Reader *reader);
//test purpose
File *parse(Reader *reader, vector<vector<action>> lrtable, map<int, int> mapping);
int id2int(Id *id);
#endif