-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathParser.cpp
More file actions
397 lines (362 loc) · 12.3 KB
/
Parser.cpp
File metadata and controls
397 lines (362 loc) · 12.3 KB
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
#include "Parser.h"
#include "Exp.h"
#include "FuncDef.h"
#include "Program.h"
#include "Stmt.h"
#include "TokenStream.h"
namespace {
// Exceptions are used internally by the parser to simplify error checking.
// Any parse error is caught by the top-level parsing routine, which reports
// an error and returns an error status.
class ParseError : public std::runtime_error
{
public:
explicit ParseError( const std::string& msg )
: std::runtime_error( msg )
{
}
};
// Forward declarations
ExpPtr parseExp( TokenStream& tokens );
std::vector<ExpPtr> parseArgs( TokenStream& tokens );
SeqStmtPtr parseSeq( TokenStream& tokens );
ExpPtr parseRemainingExp( ExpPtr leftExp, int leftPrecedence, TokenStream& tokens );
int getPrecedence( const Token& token );
// Skip the specified token, throwing ParseError if it's not present.
void skipToken( const Token& expected, TokenStream& tokens )
{
Token token( *tokens++ );
if( token != expected )
throw ParseError( std::string( "Expected '" ) + expected.ToString() + "'" );
}
// PrimaryExp -> true | false
// | Num
// | Id
// | Id ( Args )
// | ( Exp )
// | UnaryOp PrimaryExp
ExpPtr parsePrimaryExp( TokenStream& tokens )
{
// Fetch the next token, advancing the token stream. (Note that this
// dereferences, then increments the TokenStream.)
Token token( *tokens++ );
switch( token.GetTag() )
{
// Boolean constant?
case kTokenTrue:
return std::make_unique<BoolExp>( true );
case kTokenFalse:
return std::make_unique<BoolExp>( false );
// Integer constant?
case kTokenNum:
return std::make_unique<IntExp>( token.GetNum() );
// An identifier might be a variable or the start of a function call.
case kTokenId:
{
// If the next token is a left paren, it's a function call.
if( *tokens == kTokenLparen )
// Parse argument expressions and construct CallExp.
return std::make_unique<CallExp>( token.GetId(), parseArgs( tokens ) );
else
// Construct VarExp
return std::make_unique<VarExp>( token.GetId() );
}
// Type conversion?
case kTokenBool:
case kTokenInt:
{
return std::make_unique<CallExp>( token.ToString(), parseArgs( tokens ) );
}
// Parenthesized expression?
case kTokenLparen:
{
skipToken( kTokenLparen, tokens );
ExpPtr exp( parseExp( tokens ) );
skipToken( kTokenRparen, tokens );
return exp;
}
// Prefix minus?
case kTokenMinus:
{
Token unaryOp( *tokens++ );
ExpPtr exp( parsePrimaryExp( tokens ) );
return std::make_unique<CallExp>( unaryOp.ToString(), std::move( exp ) );
}
default:
throw ParseError( std::string( "Unexpected token: " ) + token.ToString() );
}
}
// Args -> ( ArgList )
// ArgList -> Exp
// | Exp , ArgList
std::vector<ExpPtr> parseArgs( TokenStream& tokens )
{
skipToken( kTokenLparen, tokens );
std::vector<ExpPtr> exps;
if( *tokens != kTokenRparen )
{
exps.push_back( parseExp( tokens ) );
while( *tokens == kTokenComma )
{
exps.push_back( parseExp( ++tokens ) );
}
}
skipToken( kTokenRparen, tokens );
return exps;
}
// Parse an expression with infix operators.
ExpPtr parseExp( TokenStream& tokens )
{
// First, parse a primary expression, which contains no infix operators.
ExpPtr leftExp( parsePrimaryExp( tokens ) );
// The next token might be an operator. Call a helper routine
// to parse the remainder of the expression.
return parseRemainingExp( std::move( leftExp ), 0 /*initial precedence*/, tokens );
}
// This routine implements an operator precedence expression parser.
// It assembles primary expressions into call expressions based
// on the precedence of the operators it encounters. For example,
// "1 + 2 * 3" is parsed as "1 + (2 * 3)" because multiplication has
// higher precedence than addition.
//
// After parsing an expression (leftExp) whose operator has the given
// precedence (or zero if it has no operator), parse the remainder of
// the expression from the given token stream.
ExpPtr parseRemainingExp( ExpPtr leftExp, int leftPrecedence, TokenStream& tokens )
{
while( true )
{
// If the previous operator has higher precedence than the current one,
// it claims the prevously parsed expression.
int precedence = getPrecedence( *tokens );
if( leftPrecedence > precedence )
return leftExp;
// Parse the current operator and the current primary expression.
Token opToken( *tokens++ );
ExpPtr rightExp = parsePrimaryExp( tokens );
// If the next operator has higher precedence, it claims the current expression.
int rightPrecedence = getPrecedence( *tokens );
if( rightPrecedence > precedence )
{
rightExp = parseRemainingExp( std::move( rightExp ), precedence + 1, tokens );
}
// Construct a call expression with the left and right expressions.
leftExp = std::make_unique<CallExp>( opToken.ToString(),
std::move( leftExp ), std::move( rightExp ) );
}
}
// If the given token is an operator, return its precedence (from 0 to 5).
// Otherwise return -1.
int getPrecedence( const Token& token )
{
switch( token.GetTag() )
{
case kTokenTimes:
case kTokenDiv:
return 5;
case kTokenMod:
case kTokenPlus:
case kTokenMinus:
return 4;
case kTokenLT:
case kTokenLE:
case kTokenGT:
case kTokenGE:
return 3;
case kTokenEQ:
case kTokenNE:
return 2;
case kTokenAnd:
return 1;
case kTokenOr:
return 0;
default:
return -1;
}
}
// Type -> bool | int
Type parseType( TokenStream& tokens )
{
Token typeName( *tokens++ );
switch( typeName.GetTag() )
{
case kTokenBool:
return kTypeBool;
case kTokenInt:
return kTypeInt;
default:
throw ParseError( "Expected type name" );
}
}
// Parse an identifier.
std::string parseId( TokenStream& tokens )
{
Token id( *tokens++ );
if( id.GetTag() != kTokenId )
throw ParseError( "Invalid declaration (expected identifier)" );
return id.GetId();
}
// VarDecl -> Type Id
VarDeclPtr parseVarDecl( VarDecl::Kind kind, TokenStream& tokens )
{
Type type( parseType( tokens ) );
std::string id( parseId( tokens ) );
return std::make_unique<VarDecl>( kind, type, id );
}
// Stmt -> Id = Exp ;
// | Id ( Args ) ;
// | VarDecl ;
// | Seq
// | return Exp ;
// | if ( Exp ) Stmt
// | if ( Exp ) Stmt else Stmt
// | while ( Exp ) Stmt
StmtPtr parseStmt( TokenStream& tokens )
{
Token token( *tokens );
switch( token.GetTag() )
{
case kTokenId:
{
Token id( *tokens++ );
if( *tokens == kTokenAssign )
{
// Assignment
ExpPtr rvalue( parseExp( ++tokens ) );
skipToken( kTokenSemicolon, tokens );
return std::make_unique<AssignStmt>( id.GetId(), std::move( rvalue ) );
}
else
{
// Call
std::vector<ExpPtr> args( parseArgs( tokens ) );
CallExpPtr callExp( std::make_unique<CallExp>( id.GetId(), std::move( args ) ) );
skipToken( kTokenSemicolon, tokens );
return std::make_unique<CallStmt>( std::move( callExp ) );
}
}
case kTokenInt:
case kTokenBool:
{
// Declaration
VarDeclPtr varDecl( parseVarDecl( VarDecl::kLocal, tokens ) );
ExpPtr initExp;
if( *tokens == kTokenAssign )
{
initExp = parseExp( ++tokens );
}
skipToken( kTokenSemicolon, tokens );
return std::make_unique<DeclStmt>( std::move( varDecl ), std::move( initExp ) );
}
case kTokenLbrace:
{
// Sequence
return parseSeq( tokens );
}
case kTokenReturn:
{
++tokens; // skip "return"
ExpPtr returnExp( parseExp( tokens ) );
skipToken( kTokenSemicolon, tokens );
return std::make_unique<ReturnStmt>( std::move( returnExp ) );
}
case kTokenIf:
{
++tokens; // skip "if"
skipToken( kTokenLparen, tokens );
ExpPtr condExp( parseExp( tokens ) );
skipToken( kTokenRparen, tokens );
StmtPtr thenStmt( parseStmt( tokens ) );
StmtPtr elseStmt;
if( *tokens == kTokenElse )
{
++tokens; // skip "else"
elseStmt = parseStmt( tokens );
}
return std::make_unique<IfStmt>( std::move( condExp ), std::move( thenStmt ), std::move( elseStmt ) );
}
case kTokenWhile:
{
++tokens; // skip "while"
skipToken( kTokenLparen, tokens );
ExpPtr condExp( parseExp( tokens ) );
skipToken( kTokenRparen, tokens );
StmtPtr bodyStmt( parseStmt( tokens ) );
return std::make_unique<WhileStmt>( std::move( condExp ), std::move( bodyStmt ) );
}
default:
throw ParseError( std::string( "Unexpected token: " ) + token.ToString() );
}
}
// Seq -> { Stmt* }
SeqStmtPtr parseSeq( TokenStream& tokens )
{
skipToken( kTokenLbrace, tokens );
std::vector<StmtPtr> stmts;
while( *tokens != kTokenRbrace )
{
stmts.push_back( parseStmt( tokens ) );
}
skipToken( kTokenRbrace, tokens );
return std::make_unique<SeqStmt>( std::move( stmts ) );
}
// FuncId -> Id | operator BinaryOp
std::string parseFuncId( TokenStream& tokens )
{
if( *tokens == kTokenOperator )
{
++tokens;
Token op( *tokens++ );
if( !op.IsOperator() )
throw ParseError( "Invalid operator" );
return op.ToString();
}
else
return parseId( tokens );
}
// FuncDef -> Type Id ( VarDecl* ) Seq
FuncDefPtr parseFuncDef( TokenStream& tokens )
{
// Parse return type and function id.
Type returnType( parseType( tokens ) );
std::string id( parseFuncId( tokens ) );
// Parse parameter declarations
skipToken( kTokenLparen, tokens );
std::vector<VarDeclPtr> params;
if( *tokens != kTokenRparen )
{
params.push_back( parseVarDecl( VarDecl::kParam, tokens ) );
while( *tokens != kTokenRparen )
{
skipToken( kTokenComma, tokens );
params.push_back( parseVarDecl( VarDecl::kParam, tokens ) );
}
}
skipToken( kTokenRparen, tokens );
// Parse function body (if any);
SeqStmtPtr body;
if( *tokens == kTokenLbrace )
body = parseSeq( tokens );
else
skipToken( kTokenSemicolon, tokens );
return std::make_unique<FuncDef>( returnType, id, std::move( params ), std::move( body ) );
}
} // anonymouse namespace
// Parse the given tokens, adding function definitions to the given program.
// Returns zero for success (otherwise an error message is reported).
int ParseProgram( TokenStream& tokens, Program* program )
{
try
{
do {
FuncDefPtr function( parseFuncDef( tokens ) );
program->GetFunctions().push_back( std::move( function ) );
} while( *tokens != kTokenEOF );
return 0;
}
catch( const ParseError& error )
{
std::cerr << "Error: " << error.what() << std::endl;
return -1;
}
}