нужно исправить все где в результате больше одного терма (в том числе функция CompliteFunction и 'T' в анализе переменных в результате)

  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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
$EXTERN Lexer, Put, Arg, Open, Prout;
$ENTRY Go {
/*= <Open 'w' 2 <Arg 2>><Put 2 <Parser : <Lexer <Arg 1>>>>;*/
= <GenereateCode : <Semantic : <Parser : <Lexer <Arg 1>>>> >;
/* = <Open 'w' 2 <Arg 2>><PrintTree <Parser : <Lexer <Arg 1>>> >;*/
}
Parser {
: eTokens =
<Program
() /* список ошибок */
() /* список функций */
: eTokens
>;
}
/*
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
>;
}
/*
ExternList = FunctionName "," ExternList | ";"
*/
ExternList {
tErrorList eFunctions (Name tPosName :eName) (Comma tPosComma ',') : eTokens =
<ExternList
tErrorList
((eName Extern tPosName NoBody) : eFunctions) : eTokens
>;
tErrorList eFunctions (Name tPosName :eName)(Semicolon tPos ';') : eTokens =
<Program tErrorList ((eName Extern tPosName NoBody) : eFunctions) : eTokens>;
/*тут ещё будет обработка ошибок (нет имени, нет запятой, внезапный EOF, нет точки с запятой)*/
}
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 */
}
/* ErrorList Sentences Stack Scanned :eTerms */
Pattern {
tErrorList sentences openBrackets eScanned (Name posName : eName) : eTokens =
<Pattern tErrorList sentences openBrackets ((Name posName : eName) :eScanned) : eTokens >;
tErrorList sentences openBrackets eScanned (Ref posRef '&') (Name posName : eName) : eTokens =
<Pattern tErrorList sentences openBrackets ((Func posName : eName) :eScanned) : eTokens >;
tErrorList sentences openBrackets eScanned (Char posChar : eChar) : eTokens =
<Pattern tErrorList sentences openBrackets ((Char posChar : eChar) : eScanned) : eTokens>;
tErrorList sentences openBrackets eScanned (Number posNumber : eNumber) : eTokens =
<Pattern tErrorList sentences openBrackets ((Number posNumber : eNumber) : eScanned) : eTokens>;
tErrorList sentences openBrackets eScanned (Variable posVariable : eVariable) : eTokens =
<Pattern tErrorList sentences openBrackets ((Variable posVariable : eVariable) : eScanned) : eTokens>;
tErrorList sentences openBrackets eScanned (Colon posCol : eCol) : eTokens =
<Pattern tErrorList sentences openBrackets ((Colon posCol : eCol) : eScanned) : eTokens>;
tErrorList sentences eOpenBrackets scanned (OpenBracket posBracket '(') : eTokens =
<Pattern tErrorList sentences (scanned : eOpenBrackets) (BRACKETS) : eTokens >;
tErrorList sentences (eScanned : eOpenBrackets) inBrackets (CloseBracket posBracket ')') : eTokens =
<Pattern tErrorList sentences eOpenBrackets (<ReverseCheckColon inBrackets> : eScanned) : eTokens >;
/* Если образец закончился, то есть мы встретили '=' и стек пуст: ()
значит начинаем разбирать результат */
tErrorList sentences () scanned (Assign posAssgn '=') : eTokens =
<Result tErrorList sentences <ReverseCheckColon scanned> () () : eTokens >; /* вызываем Result и ждем пока обработается результат, чтоб все это прикрепить к предложениям */
tErrorList eSentences () () (CloseBlock posCloseBlock '}') : eTokens =
<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 <ReverseCheckColon 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 (term)>;
terms = <DoReverse terms ()>;
}
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
>;
}
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 pos mode eFunctionBody) : eOtherFunctions) tree =
< SemanticAnalysis
<AnalyseFunction myErorList :eFunctionBody tree>
eOtherFunctions
tree
>;
myErorList (otherElem : eOtherFunctions) tree =
<SemanticAnalysis myErorList eOtherFunctions tree>;
myErrorList () tree =
(myErrorList tree);
}
AnalyseFunction {
myErorList (pattern result) :eOtherSentences tree =
<AnalyseFunction
<CheckVariable myErorList pattern result tree>
:eOtherSentences
tree
>;
myErorList atom :eOtherSentences tree =
<AnalyseFunction
myErorList
:eOtherSentences
tree
>;
myErorList tree = 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 =
<CheckVariable
<CheckPattern myErorList pattern (Variable pos :eName) tree>
pattern
eTali
tree
>;
myErorList pattern ((BRACKETS :eBody) :eTali) tree =
<CheckVariable
<CheckVariable myErorList pattern (BRACKETS :eBody) tree>
pattern
eTali
tree
>;
myErorList pattern ((CALL functionName :eBody) :eTali) tree =
<CheckVariable
<CheckVariable
<CheckFunction myErorList functionName tree>
pattern
(CALL :eBody)
tree
>
pattern
eTali
tree
>;
myErorList pattern (otherToken :eTali) tree =
<CheckVariable myErorList pattern eTali tree>;
myErorList pattern () tree =
myErorList;
}
/* CheckPattern(errorList pattern variable tree) */
CheckPattern {
T myErrorList:eOther =
T myErrorList;
myErorList ((Variable pos1 :eName) :eTali) (Variable pos2 :eName) tree
= T myErorList;
myErorList ((BRACKETS :eBody) :eTali) (Variable pos :eName) tree =
<CheckPattern
<CheckPatternInside 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'>;
myErorList () (Variable pos :eName) tree =
myErorList;
}
CheckPatternInside {
T myErrorList :eOther =
T myErrorList;
myErorList ((Variable pos1 :eName) :eTali) (Variable pos2 :eName) tree
= T myErorList;
myErorList ((BRACKETS :eBody) :eTali) (Variable pos :eName) tree =
<CheckPattern
<CheckPatternInside myErorList eBody (Variable pos :eName) tree>
eTali
(Variable pos :eName)
tree
>;
myErorList (otherToken :eTali) (Variable pos :eName) tree =
<CheckPatternInside
myErorList
eTali
(Variable pos :eName)
tree
>;
myErorList () (Variable pos :eName) tree =
myErorList;
}
/* FunctionName (errorList (functionName) tree) */
CheckFunction {
myErorList functionName () =
<AddUnexpectedToken myErorList functionName 'undeclared function on pos'>;
myErorList (Name pos :eFname) (( eFname : eFunctionBody ) : eFunctions) =
myErorList;
myErorList functionName (term : eFunctions) =
<CheckFunction myErorList functionName eFunctions>;
}
/*======================================================================================*/
/*===================================== GenereateCode =====================================*/
GenereateCode {
() tree =
<AnalyseTree tree>;
myErrors tree =
/*<AnalyseTree tree>;*/
<PrintErrors myErrors>;
}
AnalyseTree {
((fName :eBody) : eOtherFunctions) =
(<GenerateFunction (fName :eBody)> : <AnalyseTree eOtherFunctions>);
(Atom : eOtherFunctions) =
<AnalyseTree eOtherFunctions>;
() = ();
}
GenerateFunction {
(eName Local pos body) =
(<Prout 'Fn' : eName '{'> <GenerateBody body> <Prout '}'> );
(eName Extern pos body) =
(<Prout '$EXTERN Fn' : eName ';'>);
(eName Entry pos body) =
(<Prout '$ENTRY Fn' : eName '{'> <GenerateBody body> <Prout '}'> );
}
Begin {
first next : elast = <Begin next : elast>;
last_term = last_term;
}
GenerateBody {
((pattern result) : eBodyTail) =
<Begin
/* <Prout pattern result>*/
<Prout :
<Concat
<PrintSentence-New <PrepareConsTree-Pat pattern>>
(' = ')
<PrintSentence-New <PrepareConsTree-Res result>>
(';')
>
>
<GenerateBody eBodyTail>
>;
() = ();
}
PrepareConsTree-Pat {
(LAST term) = <PrepareConsTree term>;
(head LAST tail) = (Cons <PrepareConsTree head> <PrepareConsTree tail>);
(NIL) = NIL;
(head : etail) = (Cons <PrepareConsTree head> <PrepareConsTree (BRACKETS : etail)>);
}
PrepareConsTree-Res {
(result NIL) = <PrepareConsTree result>;
(NIL) = NIL;
}
/*
<PrepareConsTree ?>
== tree
tree ::= (Cons tree1 tree2) | (Var : name) | (Char char) |
(Number num) | (Name : name) | (Func : ename) | NIL |
(CallCons name arg)
*/
PrepareConsTree {
(BRACKETS head LAST tail) =
(Cons <PrepareConsTree head> <PrepareConsTree tail>);
(BRACKETS head NIL) =
(Cons <PrepareConsTree head> NIL);
(BRACKETS NIL) = NIL;
(BRACKETS head : etail) =
(Cons <PrepareConsTree head> <PrepareConsTree (BRACKETS : etail)>);
(CALL (Name npos : ename) LAST tail) =
(CallCons ename <PrepareConsTree tail>);
(CALL (Name npos : ename) NIL) =
(CallCons ename NIL);
(CALL (Name npos : ename) head : etail) =
(CallCons ename <PrepareConsTree (BRACKETS head : etail)>);
(Variable pos 't' ename) = (Var : ename);
(Char pos char) = (Char char);
(Number pos number) = (Number number);
(Func pos : ename) = (Func : ename);
(Name pos : ename) = (Name : ename);
x = (Error x);
}
PrintSentence-New {
NIL = ('()');
(Cons t1 t2) = <Concat ('(') <PrintSentence-New t1> <PrintSentence-New t2> (')')>;
(CallCons ename arg) =
<Concat ('<Fn' : ename) <PrintSentence-New arg> ('>')>;
(Char char) = ('\'' char '\' ');
(Number number) = (number);
(Func : ename) = <Concat ('Fn' : ename) (' ')>;
(Name : ename) = <Concat ('Id' : ename) (' ')>;
(Var : ename) = <Concat ('t.' : ename) (' ')>;
x = x;
}
Concat {
= ();
(first : elast) : expected = (first : <Concat elast : expected>);
() : expected = <Concat : expected>;
}
PrintErrors {
() = <Prout 'No errors \n'>;
aEerror : eErrors= <Begin (<Prout aEerror '\n'><PrintErrors :eErrors >)>;
= ;
}
/*==========================================================================*/