-
Notifications
You must be signed in to change notification settings - Fork 1
/
s.txt
227 lines (220 loc) · 9.05 KB
/
s.txt
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
CompUnit → [ CompUnit ] ( Decl | FuncDef )
Decl → ConstDecl | VarDecl
ConstDecl → 'const' BType ConstDef { ',' ConstDef } ';'
BType → 'int' | 'float'
ConstDef → Ident { '[' ConstExp ']' } '=' ConstInitVal
ConstInitVal → ConstExp | '{' [ ConstInitVal { ',' ConstInitVal } ] '}'
VarDecl → BType VarDef { ',' VarDef } ';'
VarDef → Ident { '[' ConstExp ']' } | Ident { '[' ConstExp ']' } '=' InitVal
InitVal → Exp | '{' [ InitVal { ',' InitVal } ] '}'
FuncDef → FuncType Ident '(' [FuncFParams] ')' Block
FuncType → 'void' | 'int' | 'float'
FuncFParams → FuncFParam { ',' FuncFParam }
FuncFParam → BType Ident ['[' ']' { '[' Exp ']' }]
Block → '{' { BlockItem } '}'
BlockItem → Decl | Stmt
Stmt → LVal '=' Exp ';' | [Exp] ';' | Block | 'if' '(' Cond ')' Stmt [ 'else' Stmt ] | 'while' '(' Cond ')' Stmt | 'break' ';' | 'continue' ';' | 'return' [Exp] ';'
Exp → AddExp
Cond → LOrExp
LVal → Ident {'[' Exp ']'}
PrimaryExp → '(' Exp ')' | LVal | Number
Number → IntConst | floatConst
UnaryExp → PrimaryExp | Ident '(' [FuncRParams] ')' | UnaryOp UnaryExp
UnaryOp → '+' | '−' | '!'
FuncRParams → Exp { ',' Exp }
MulExp → UnaryExp | MulExp ('*' | '/' | '%') UnaryExp
AddExp → MulExp | AddExp ('+' | '−') MulExp
RelExp → AddExp | RelExp ('<' | '>' | '<=' | '>=') AddExp
EqExp → RelExp | EqExp ('==' | '!=') RelExp
LAndExp → EqExp | LAndExp '&&' EqExp
LOrExp → LAndExp | LOrExp '||' LAndExp
ConstExp → AddExp
CompUnit → [ CompUnit' ] ;
CompUnit' → Decl | FuncDef ;
Decl → ConstDecl | VarDecl ;
ConstDecl → 'const' BType ConstDef { ',' ConstDef } ';' ;
BType → 'int' | 'float' ;
ConstDef → Ident { '[' ConstExp ']' } '=' ConstInitVal ;
ConstInitVal → ConstExp | '{' [ ConstInitVal { ',' ConstInitVal } ] '}' ;
VarDecl → BType VarDef { ',' VarDef } ';' ;
VarDef → Ident { '[' ConstExp ']' } [ '=' InitVal ] ;
InitVal → Exp | '{' [ InitVal { ',' InitVal } ] '}' ;
FuncDef → FuncType Ident '(' [FuncFParams] ')' Block ;
FuncType → 'void' | 'int' | 'float' ;
FuncFParams → FuncFParam { ',' FuncFParam } ;
FuncFParam → BType Ident ['[' ']' { '[' Exp ']' }] ;
Block → '{' { BlockItem } '}' ;
BlockItem → Decl | Stmt ;
Stmt → LVal '=' Exp ';' | [Exp] ';' | Block | 'if' '(' Cond ')' Stmt [ 'else' Stmt ] | 'while' '(' Cond ')' Stmt | 'break' ';' | 'continue' ';' | 'return' [Exp] ';' ;
Exp → AddExp ;
Cond → LOrExp ;
LVal → Ident {'[' Exp ']'} ;
PrimaryExp → '(' Exp ')' | LVal | Number ;
Number → IntConst | floatConst ;
UnaryExp → PrimaryExp | Ident '(' [FuncRParams] ')' | UnaryOp UnaryExp ;
UnaryOp → '+' | '−' | '!' ;
FuncRParams → Exp { ',' Exp } ;
MulExp → UnaryExp MulExp' ;
MulExp' → ('*' | '/' | '%') UnaryExp MulExp' | ε ;
AddExp → MulExp AddExp' ;
AddExp' → ('+' | '−') MulExp AddExp' | ε ;
RelExp → AddExp RelExp' ;
RelExp' → ('<' | '>' | '<=' | '>=') AddExp RelExp' | ε ;
EqExp → RelExp EqExp' ;
EqExp' → ('==' | '!=') RelExp EqExp' | ε ;
LAndExp → EqExp LAndExp' ;
LAndExp' → '&&' EqExp LAndExp' | ε ;
LOrExp → LAndExp LOrExp' ;
LOrExp' → '||' LAndExp LOrExp' | ε ;
ConstExp → AddExp ;
CompUnit → CompUnit' ;
CompUnit' → Decl CompUnit' | FuncDef CompUnit' | ε ;
Decl → ConstDecl | VarDecl ;
ConstDecl → 'const' BType ConstDef ConstDefList ';' ;
BType → 'int' | 'float' ;
ConstDef → Ident ConstExpList '=' ConstInitVal ;
ConstDefList → ',' ConstDef ConstDefList | ε ;
ConstExpList → '[' ConstExp ']' ConstExpList | ε ;
ConstInitVal → ConstExp | '{' ConstInitValList '}' ;
ConstInitValList → ConstInitVal ConstInitValListTail | ε ;
ConstInitValListTail → ',' ConstInitVal ConstInitValListTail | ε ;
VarDecl → BType VarDef VarDefList ';' ;
VarDef → Ident ConstExpList OptionalInitVal ;
VarDefList → ',' VarDef VarDefList | ε ;
OptionalInitVal → '=' InitVal | ε ;
InitVal → Exp | '{' InitValList '}' ;
InitValList → InitVal InitValListTail | ε ;
InitValListTail → ',' InitVal InitValListTail | ε ;
FuncDef → FuncType Ident '(' OptionalFuncFParams ')' Block ;
FuncType → 'void' | 'int' | 'float' ;
OptionalFuncFParams → FuncFParams | ε ;
FuncFParams → FuncFParam FuncFParamList ;
FuncFParamList → ',' FuncFParam FuncFParamList | ε ;
FuncFParam → BType Ident OptionalBracketExpList ;
OptionalBracketExpList → '[' ']' BracketExpList | ε ;
BracketExpList → '[' Exp ']' BracketExpList | ε ;
Block → '{' BlockItemList '}' ;
BlockItemList → BlockItem BlockItemList | ε ;
BlockItem → Decl | Stmt ;
Stmt → LVal '=' Exp ';' | OptionalExp ';' | Block | 'if' '(' Cond ')' Stmt OptionalElse | 'while' '(' Cond ')' Stmt | 'break' ';' | 'continue' ';' | 'return' OptionalExp ';' ;
OptionalExp → Exp | ε ;
OptionalElse → 'else' Stmt | ε ;
Exp → AddExp ;
Cond → LOrExp ;
LVal → Ident ExpList ;
ExpList → '[' Exp ']' ExpList | ε ;
PrimaryExp → '(' Exp ')' | LVal | Number ;
Number → IntConst | floatConst ;
UnaryExp → PrimaryExp | Ident '(' OptionalFuncRParams ')' | UnaryOp UnaryExp ;
OptionalFuncRParams → FuncRParams | ε ;
FuncRParams → Exp ExpListTail ;
ExpListTail → ',' Exp ExpListTail | ε ;
UnaryOp → '+' | '−' | '!' ;
MulExp → UnaryExp MulExp' ;
MulExp' → ('*' | '/' | '%') UnaryExp MulExp' | ε ;
AddExp → MulExp AddExp' ;
AddExp' → ('+' | '−') MulExp AddExp' | ε ;
RelExp → AddExp RelExp' ;
RelExp' → ('<' | '>' | '<=' | '>=') AddExp RelExp' | ε ;
EqExp → RelExp EqExp' ;
EqExp' → ('==' | '!=') RelExp EqExp' | ε ;
LAndExp → EqExp LAndExp' ;
LAndExp' → '&&' EqExp LAndExp' | ε ;
LOrExp → LAndExp LOrExp' ;
LOrExp' → '||' LAndExp LOrExp' | ε ;
ConstExp → AddExp ;
%token 'const' 'int' 'float' 'Ident' '[' ']' '=' '{' '}' ',' ';' void '(' ')' 'if' 'while' 'break' 'continue' 'return' '+' '−' '!' '*' '/' '%' '<' '>' '<=' '>=' '==' '!=' '&&' '||'
{
CompUnit':CompUnit
CompUnit:Decl CompUnit|FuncDef CompUnit|ε
Decl:ConstDecl|VarDecl
ConstDecl:'const' BType ConstDef ConstDefList ';'
BType:'int'|'float'
ConstDef:Ident ConstExpList '=' ConstInitVal
ConstDefList:',' ConstDef ConstDefList|ε
ConstExpList:'[' ConstExp ']' ConstExpList|ε
ConstInitVal:ConstExp|'{' ConstInitValList '}'
ConstInitValList:ConstInitVal ConstInitValListTail|ε
ConstInitValListTail:',' ConstInitVal ConstInitValListTail|ε
VarDecl:BType VarDef VarDefList ';'
VarDef:Ident ConstExpList OptionalInitVal
VarDefList:',' VarDef VarDefList|ε
OptionalInitVal:'=' InitVal|ε
InitVal:Exp|'{' InitValList '}'
InitValList:InitVal InitValListTail|ε
InitValListTail:',' InitVal InitValListTail|ε
FuncDef:FuncType Ident '(' OptionalFuncFParams ')' Block
FuncType:'void'|'int'|'float'
OptionalFuncFParams:FuncFParams|ε
FuncFParams:FuncFParam FuncFParamList
FuncFParamList:',' FuncFParam FuncFParamList|ε
FuncFParam:BType Ident OptionalBracketExpList
OptionalBracketExpList:'[' ']' BracketExpList|ε
BracketExpList:'[' Exp ']' BracketExpList|ε
Block:'{' BlockItemList '}'
BlockItemList:BlockItem BlockItemList|ε
BlockItem:Decl|Stmt
Stmt:LVal '=' Exp ';'|OptionalExp ';'|Block|'if' '(' Cond ')' Stmt OptionalElse|'while' '(' Cond ')' Stmt|'break' ';'|'continue' ';'|'return' OptionalExp ';'
OptionalExp:Exp|ε
OptionalElse:'else' Stmt|ε
Exp:AddExp
Cond:LOrExp
LVal:Ident ExpList
ExpList:'[' Exp ']' ExpList|ε
PrimaryExp:'(' Exp ')'|LVal|Number
Number:IntConst|floatConst
UnaryExp:PrimaryExp|Ident '(' OptionalFuncRParams ')'|UnaryOp UnaryExp
OptionalFuncRParams:FuncRParams|ε
FuncRParams:Exp ExpListTail
ExpListTail:',' Exp ExpListTail|ε
UnaryOp:'+'|'−'|'!'
MulExp:UnaryExp MulExp'
MulExp':('*'|'/'|'%') UnaryExp MulExp'|ε
AddExp:MulExp AddExp'
AddExp':('+'|'−') MulExp AddExp'|ε
RelExp:AddExp RelExp'
RelExp':('<'|'>'|'<='|'>=') AddExp RelExp'|ε
EqExp:RelExp EqExp'
EqExp':('=='|'!=') RelExp EqExp'|ε
LAndExp:EqExp LAndExp'
LAndExp':'&&' EqExp LAndExp'|ε
LOrExp:LAndExp LOrExp'
LOrExp':'||' LAndExp LOrExp'|ε
ConstExp:AddExp
}
// pub fn compute_lr1_item_sets(&mut self, grammar: &Grammar, START_SYMBOL: &Element) {
// let mut item_sets = Vec::<HashSet<LR1Item>>::new();
//
// let mut initial_set = HashSet::<LR1Item>::new();
// let initial_item = LR1Item {
// head: START_SYMBOL.clone(),
// body: grammar.pro_list.get(START_SYMBOL).unwrap()[0].clone(),
// dot: 0,
// lookahead: Element::Terminal("#".to_string()),
// };
// initial_set.insert(initial_item);
// item_sets.push(self.closure(grammar, &initial_set));
//
// let mut changed = true;
// while changed {
// changed = false;
//
// let mut new_item_set = Vec::<HashSet<LR1Item>>::new();
// // let mut new_item_set = Vec::<HashSet<LR1Item>>::new();
// for item_set in &item_sets {
// for item in item_set {
// let dot_position = item.dot;
// if dot_position < item.body.len() {
// let next_symbol = &item.body[item.dot];
// let goto_set = self.goto(grammar, item_set, next_symbol);
// if !item_sets.contains(&goto_set) && !new_item_set.contains(&goto_set) {
// new_item_set.push(goto_set);
// changed = true;
// }
// }
// }
// }
// item_sets.extend(new_item_set.into_iter());
// }
// self.lr1_sets = item_sets;
// }