синтаксис и семантика. 01.02.2015 парсер не понимает директивы, но в остальном самоприменяется

  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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
$EXTERN Lexer, Put, Arg, Open, Prout;
$ENTRY Go {
= <Open 'w' 2 <Arg 2>><Put 2 <Parser : <Lexer <Arg 1>>>>;
/*= <Open 'w' 2 <Arg 2>><PrintTree <Semantic <Parser : <Lexer <Arg 1>>>> >;*/
/* = <Open 'w' 2 <Arg 2>><PrintTree <Parser : <Lexer <Arg 1>>> >;*/
}
Parser {
: eTokens =
<Program
() /* список ошибок */
() /* список функций */
: eTokens
>;
}
/*----------------------- Print Tree -----------*/
PrintTree {
errorList eFunctionList =
<PrintErrors errorList> <PrintFunctions : eFunctionList>;
}
PrintErrors {
() = <Prout 'No errors \n'>;
term :eTerms = <Prout term '\n'><PrintErrors :eTerms >;
= ;
}
PrintFunctions {
(head : eTail) : eTerms = <PrintBody head : eTail ><PrintFunctions :eTerms >;
atom :eTerms = <Prout atom '\n'><PrintFunctions :eTerms >;
= ;
}
PrintBody {
name type position eSentences =
<Prout name type position '\n'> <PrintSentences : eSentences>;
= ;
}
PrintSentences {
eSentense :eTerms = <PrintPR : eSentense '\n' ><PrintSentences :eTerms >;
CALL :eTerms = <Prout '\tCALL\n'> <PrintSentences :eTerms >;
BRACKETS :eTerms = <Prout '\tBRACKETS\n'><PrintSentences :eTerms >;
atom :eTerms = <Prout '\t\t' atom '\n'><PrintSentences :eTerms >;
= ;
}
PrintPR {
pattern result : eTail =
<Prout '\t\t' pattern '\n'>
<Prout '\t\t' result '\n'>;
= ;
atom = <Prout '\t\t' atom '\n'>;
}
/*------------------------------------------------------------------------*/
/*
Program =
[$ENTRY] Name FunctionBody Program
| $EXTERN ExternList Program
| пусто .
*/
Program {
tErrorList tFunctions (Directive tPosDir '$ENTRY') (Name tPosName : eName) : eTokens =
<Program-Function
tFunctions eName tPosName Entry
<FunctionBody tErrorList ()() : eTokens>
>;
tErrorList tFunctions (Directive tPosDir '$ENTRY') tOtherToken : eTokens =
<Program
/* Добавляет сообщение */
/* «позиция: unexpected token токен, expected function name» */
/* Позиция и значение неожиданного токена выбирается из tOtherToken */
<AddUnexpectedToken tErrorList tOtherToken 'function name'>
tFunctions tOtherToken : eTokens
>;
tErrorList tFunctions (Name tPos : eName) : eTokens =
<Program-Function
tFunctions eName tPos Local
<FunctionBody tErrorList : eTokens>
>;
tErrorList tFunctions (Directive tPos '$EXTERN') : eTokens =
<ExternList tErrorList tFunctions : eTokens>;
tErrorList tFunctions (EOF tPos '!') =
/* Корректное завершение */
tErrorList <Reverse tFunctions>; /* REVERSE */
tErrorList tFunctions tOtherToken : eTokens =
<Program
<AddUnexpectedToken
tErrorList tOtherToken 'function definion or $EXTERN'
>
tFunctions : eTokens
>;
}
FunctionBody {
tErrorList (OpenBlock tposOpenBlock '{') : eTokens =
<Pattern tErrorList () () () : eTokens >;
tErrorList token : eTokens =
<Pattern <AddExpectedCharacter tErrorList token 'open function body'> () () () token : eTokens >; /* ErrorList Sentences Stack Scanned :Terms */
}
AddToken {
tPattern eToken =
tPattern : eToken; /* раз такой порядок, значит не нужно будет делать reverse */
}
/* ErrorList Sentences Stack Scanned :eTerms */
Pattern {
tErrorList sentences openBrackets eScanned (Name posName : eName) : eTokens =
<Prout 'Name'><Pattern tErrorList sentences openBrackets ((Name posName : eName) :eScanned) : eTokens >;
tErrorList sentences openBrackets eScanned (Char posChar : eChar) : eTokens =
<Prout 'Char'><Pattern tErrorList sentences openBrackets ((Char posChar : eChar) : eScanned) : eTokens>;
tErrorList sentences openBrackets eScanned (Number posNumber : eNumber) : eTokens =
<Prout 'Number'><Pattern tErrorList sentences openBrackets ((Number posNumber : eNumber) : eScanned) : eTokens>;
tErrorList sentences openBrackets eScanned (Variable posVariable : eVariable) : eTokens =
<Prout 'Variable' eVariable><Pattern tErrorList sentences openBrackets ((Variable posVariable : eVariable) : eScanned) : eTokens>;
tErrorList sentences openBrackets eScanned (Colon posCol : eCol) : eTokens =
<Prout 'Colon' ><Pattern tErrorList sentences openBrackets ((Colon posCol : eCol) : eScanned) : eTokens>;
tErrorList sentences eOpenBrackets scanned (OpenBracket posBracket '(') : eTokens =
<Prout 'OpenBracket'><Pattern tErrorList sentences (scanned : eOpenBrackets) (BRACKETS) : eTokens >;
tErrorList sentences (eScanned : eOpenBrackets) inBrackets (CloseBracket posBracket ')') : eTokens =
<Prout 'CloseBracket'><Pattern tErrorList sentences eOpenBrackets (<ReverseCheckColon inBrackets> : eScanned) : eTokens >;
/* Если образец закончился, то есть мы встретили '=' и стек пуст: ()
значит начинаем разбирать результат */
tErrorList sentences () scanned (Assign posAssgn '=') : eTokens =
<Prout 'Assign'><Result tErrorList sentences <Reverse scanned> () () : eTokens >; /* вызываем Result и ждем пока обработается результат, чтоб все это прикрепить к предложениям */
tErrorList eSentences () () (CloseBlock posCloseBlock '}') : eTokens =
<Prout 'CloseBlock'><CompleteFunclion tErrorList eSentences : eTokens >;
}
Result {
tErrorList sentences pattern openBrackets eScanned (Name posName : eName) : eTokens =
<Result tErrorList sentences pattern openBrackets ((Name posName : eName) : eScanned) : eTokens>;
tErrorList sentences pattern openBrackets eScanned (Char posChar : eChar) : eTokens =
<Result tErrorList sentences pattern openBrackets ((Char posChar : eChar) : eScanned) : eTokens>;
tErrorList sentences pattern openBrackets eScanned (Number posNumber : eNumber) : eTokens =
<Result tErrorList sentences pattern openBrackets ((Number posNumber : eNumber) : eScanned) : eTokens>;
tErrorList sentences pattern openBrackets eScanned (Variable posVariable : eVariable) : eTokens =
<Result tErrorList sentences pattern openBrackets ((Variable posVariable : eVariable) : eScanned) : eTokens>;
tErrorList sentences pattern openBrackets eScanned (Colon posCol : eCol) : eTokens =
<Result tErrorList sentences pattern openBrackets ((Colon posCol : eCol) : eScanned) : eTokens>;
tErrorList sentences pattern eOpenBrackets scanned (OpenBracket posBracket '(') : eTokens =
<Result tErrorList sentences pattern (scanned : eOpenBrackets) (BRACKETS) : eTokens >;
tErrorList sentences pattern (eScanned : eOpenBrackets) inBrackets (CloseBracket posBracket ')') : eTokens =
<Result tErrorList sentences pattern eOpenBrackets (<ReverseCheckColon inBrackets> : eScanned) : eTokens >;
tErrorList sentences pattern eOpenBrackets scanned (OpenCall posBracket '<') : eTokens =
<Result tErrorList sentences pattern (scanned : eOpenBrackets) (CALL ) : eTokens >;
tErrorList sentences pattern (eScanned : eOpenBrackets) inBrackets (CloseCall posBracket '>') : eTokens =
<Result tErrorList sentences pattern eOpenBrackets (<ReverseCheckColon inBrackets> : eScanned) : eTokens >;
tErrorList eSentences pattern () scanned (Semicolon posSemicolon ';') : eTokens =
<Pattern tErrorList ((pattern <Reverse scanned>) : eSentences) () () : eTokens >; /* ErrorList Sentences Stack Scanned :Terms */
}
CompleteFunclion {
tErrorList sentences : eTokens =
tErrorList <Reverse sentences> : eTokens;
}
ReverseCheckColon {
( term (Colon pos ':') :eTerms) =
<DoReverseCheck eTerms (LAST term)>;
terms = <DoReverseCheck terms (NIL)>;
/*tErrorList( term (Colon pos ':') :eTerms) =
<DoReverseCheck tErrorList eTerms (LAST term)>;
tErrorList terms = <DoReverseCheck tErrorList terms (NIL)>;*/
}
DoReverseCheck {
/* tErrorList ((Colon pos ':') : eTerms) reversed =
<DoReverseCheck
<AddUnexpectedToken tErrorList (Colon pos ':') 'colon'>
eTerms
reversed
>;*/
(term : eTerms) eReversed =
<DoReverseCheck eTerms (term : eReversed)>;
() reversed = reversed;
}
Reverse {
(term :eTerms) =
<DoReverse eTerms (LAST term)>;
terms = <DoReverse terms (NIL)>;
}
DoReverse {
(term : eTerms) eReversed =
<DoReverse eTerms (term : eReversed)>;
() reversed = reversed;
}
Program-Function {
eFunctions tName tPos sAccess
tErrorList tFunctionBody : eTokens =
<Program
tErrorList
((tName sAccess tPos tFunctionBody) : eFunctions) /* PUSH */
: eTokens
>;
}
/*
ExternList = FunctionName "," ExternList | ";"
*/
ExternList {
tErrorList eFunctions (Name tPosName tName) (Comma tPosComma ',') : eTokens =
<ExternList
tErrorList
((tName Entry tPosName NoBody) : eFunctions) /* PUSH */
: eTokens
>;
tErrorList tFunctions (Semicolon tPos ';') : eTokens =
<Program tErrorList tFunctions : eTokens>;
/*тут ещё будет обработка ошибок (нет имени, нет запятой, внезапный EOF, нет точки с запятой)*/
}
AddUnexpectedToken {
((token : eMessage) : errorList) token : eMessage =
((token : eMessage) : errorList);
errorList token : eMessage =
((token : eMessage) : errorList);
}
AddExpectedCharacter {
errorList token : eMessage =
((token : eMessage) : errorList);
}
/*===================================== Semantic ======================================*/
Semantic {
myErorList tree =
<SemanticAnalysis myErorList tree tree>;
}
/* SemanticAnalysis(errorList 'tree to analyse' 'tree to compare') */
SemanticAnalysis {
myErorList ((functionName mode pos eFunctionBody) : eOtherFunctions) tree =
<Prout 'begin semantic ' :eFunctionBody>< SemanticAnalysis
<AnalyseFunction myErorList :eFunctionBody tree>
eOtherFunctions
tree
>;
myErorList (otherElem : eOtherFunctions) tree =
<Prout 'continue semantic'><SemanticAnalysis myErorList eOtherFunctions tree>;
myErrorList () tree =
myErrorList tree;
}
AnalyseFunction {
myErorList (pattern result) :eOtherSentences tree =
<Prout 'see pattern and result'><AnalyseFunction
<CheckVariable myErorList pattern result tree>
:eOtherSentences
tree
>;
myErorList atom :eOtherSentences tree =
<Prout 'see atom'><AnalyseFunction
myErorList
:eOtherSentences
tree
>;
myErorList tree = <Prout 'ok'>myErorList;
}
CheckVariable {
T myErrors :eOtherArgs =
<DoCheckVariable myErrors :eOtherArgs>;
myErrors :eOtherArgs =
<DoCheckVariable myErrors :eOtherArgs>;
}
/* CheckVariables(errorList pattern result tree) */
DoCheckVariable {
myErorList pattern ((Variable pos :eName) :eTali) tree =
<Prout 'Start CheckVariable'><CheckVariable
<CheckPattern myErorList pattern (Variable pos :eName) tree>
pattern
eTali
tree
>;
myErorList pattern ((BRACKETS :eBody) :eTali) tree =
<Prout 'CheckVariable see brackets'><CheckVariable
<CheckVariable myErorList pattern (BRACKETS :eBody) tree>
pattern
eTali
tree
>;
myErorList pattern ((CALL functionName :eBody) :eTali) tree =
<Prout 'CheckVariable see call'><CheckVariable
<CheckVariable
<CheckFunction myErorList functionName tree>
pattern
(CALL :eBody)
tree
>
pattern
eTali
tree
>;
myErorList pattern (otherToken :eTali) tree =
<Prout 'CheckVariable see other'><CheckVariable myErorList pattern eTali tree>;
myErorList pattern () tree =
<Prout 'stop CheckVariable'>myErorList;
}
/* CheckPattern(errorList pattern variable tree) */
CheckPattern {
T myErrorList:eOther =
T myErrorList;
myErorList ((Variable pos1 :eName) :eTali) (Variable pos2 :eName) tree
= <Prout '###' (Variable pos1 :eName) '--'(Variable pos2 :eName)'####'> T myErorList;
myErorList ((BRACKETS :eBody) :eTali) (Variable pos :eName) tree =
<Prout '************** is it posible to get in BRACKETS?? ' eBody '--' (Variable pos :eName) '\n'> <CheckPattern
<CheckPattern myErorList eBody (Variable pos :eName) tree>
eTali
(Variable pos :eName)
tree
>;
myErorList (otherToken :eTali) (Variable pos :eName) tree =
<CheckPattern
myErorList
eTali
(Variable pos :eName)
tree
>;
myErorList () (Variable pos :eName) tree =
<AddUnexpectedToken myErorList (Variable pos :eName) 'undeclared variable in result on pos'>;
}
/* FunctionName (errorList (functionName) tree) */
CheckFunction {
myErorList functionName () =
<AddUnexpectedToken myErorList functionName 'undeclared function on pos'>;
myErorList (Name pos :eFname) (( eFname : eFunctionBody ) : eFunctions) =
<Prout eFname ' its found!\n'>myErorList;
myErorList functionName (term : eFunctions) =
<CheckFunction myErorList functionName eFunctions>;
}
/*======================================================================================*/