синтаксический анализ. Нет обработки вызовов функций

  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
$EXTERN Lexer, Put, Arg, Open;
$ENTRY Go {
= <Open 'w' 2 <Arg 2>><Put 2 <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 '!') =
/* Корректное завершение */
<SemanticAnalysis 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 {
errorList sentences openBrackets eScanned (Name posName : eName) : eTokens =
<Pattern errorList sentences openBrackets ((Name posName : eName) :eScanned)>;
errorList sentences openBrackets eScanned (Char posChar : eChar) : eTokens =
<Pattern errorList sentences openBrackets ((Char posChar : eChar) : eScanned) : eTokens>;
errorList sentences openBrackets eScanned (Number posNumber : eNumber) : eTokens =
<Pattern errorList sentences openBrackets ((Number posNumber : eNumber) : eScanned) : eTokens>;
errorList sentences openBrackets eScanned (Variable posVariable : eVariable) : eTokens =
<Pattern errorList sentences openBrackets ((Variable posVariable : eVariable) : eScanned) : eTokens>;
errorList sentences openBrackets eScanned (OpenBracket posBracket '(') : eTokens =
<Pattern errorList sentences ((OpenBracket posBracket '(') : eScanned) () : eTokens >;
errorList sentences (eScanned : eOpenBrackets) inBrackets (CloseBracket posBracket ')') : eTokens =
<Pattern errorList sentences eOpenBrackets (<ReverseCheckColon inBrackets> : eScanned) : eTokens >;
/* Если образец закончился, то есть мы встретили '=' и стек пуст: ()
значит начинаем разбирать результат */
errorList sentences () scanned (Assign posAssgn '=') : eTokens =
<Result errorList sentences <Reverse scanned> () () : eTokens >; /* вызываем Result и ждем пока обработается результат, чтоб все это прикрепить к предложениям */
errorList eSentences () (CloseBlock posCloseBlock '}') : eTokens =
<CompleteFunclion errorList eSentences : eTokens >;
}
Result {
errorList sentences pattern openBrackets eScanned (Name posName : eName) : eTokens =
<Result errorList sentences pattern openBrackets ((Name posName : eName) : eScanned) : eTokens>;
errorList sentences pattern openBrackets eScanned (Char posChar : eChar) : eTokens =
<Result errorList sentences pattern openBrackets ((Char posChar : eChar) : eScanned) : eTokens>;
errorList sentences pattern openBrackets eScanned (Number posNumber : eNumber) : eTokens =
<Result errorList sentences pattern openBrackets ((Number posNumber : eNumber) : eScanned) : eTokens>;
errorList sentences pattern openBrackets eScanned (Variable posVariable : eVariable) : eTokens =
<Result errorList sentences pattern openBrackets ((Variable posVariable : eVariable) : eScanned) : eTokens>;
errorList sentences pattern openBrackets eScanned (OpenBracket posBracket '(') : eTokens =
<Result errorList sentences ((OpenBracket posBracket '(') : eScanned) : eTokens >;
errorList sentences pattern (eScanned : eOpenBrackets) inBrackets (CloseBracket posBracket ')') : eTokens =
<Pattern errorList sentences pattern eOpenBrackets (<ReverseCheckColon inBrackets> : eScanned) : eTokens >;
errorList eSentences pattern () scanned (Semicolon posSemicolon ';') : eTokens =
<Pattern errorList ((pattern <Reverse scanned>) : eSentences) () : eTokens >;
}
CompleteFunclion {
errorList sentences : eTokens =
errorList <Reverse sentences> : eTokens;
}
ReverseCheckColon {
errorList( term (Colon pos ':') :eTerms) =
<DoReverseCheck errorList eTerms (LAST term)>;
errorList terms = <DoReverseCheck errorList terms (NIL)>;
}
DoReverseCheck {
errorList ((Colon pos ':') : eTerms) reversed =
<DoReverseCheck
<AddUnexpectedToken errorList (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;
}
ParseResult {
tErrorList tPattern tResult (Char tCharPos tChar) : eTokens =
<ParseResult tErrorList tPattern <AddToken tResult (Char tChar)> : eTokens >;
/*---- other tokens ----*/
tErrorList tPattern tResult (CloseBlock tposCloseBlock ';') : eTokens =
<FunctionBody tErrorList tPattern tResult : eTokens >;
tErrorList tPattern tResult (CloseBlock tposCloseBlock '}') : eTokens =
tErrorList (tPattern tResult) : eTokens;
}
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, нет точки с запятой)*/
}
SemanticAnalysis {
errorList functions = errorList functions;
}
AddUnexpectedToken {
errorList token : eMessage =
((token : eMessage) : errorList);
}
AddExpectedCharacter {
errorList token : eMessage =
((token : eMessage) : errorList);
}