1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
6 <title>Kaleidoscope: Implementing a Parser and AST</title>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8 <meta name="author" content="Chris Lattner">
9 <link rel="stylesheet" href="../llvm.css" type="text/css">
14 <div class="doc_title">Kaleidoscope: Implementing a Parser and AST</div>
17 <li><a href="index.html">Up to Tutorial Index</a></li>
20 <li><a href="#intro">Chapter 2 Introduction</a></li>
21 <li><a href="#ast">The Abstract Syntax Tree (AST)</a></li>
22 <li><a href="#parserbasics">Parser Basics</a></li>
23 <li><a href="#parserprimexprs">Basic Expression Parsing</a></li>
24 <li><a href="#parserbinops">Binary Expression Parsing</a></li>
25 <li><a href="#parsertop">Parsing the Rest</a></li>
26 <li><a href="#driver">The Driver</a></li>
27 <li><a href="#conclusions">Conclusions</a></li>
28 <li><a href="#code">Full Code Listing</a></li>
31 <li><a href="LangImpl3.html">Chapter 3</a>: Code generation to LLVM IR</li>
34 <div class="doc_author">
35 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
38 <!-- *********************************************************************** -->
39 <div class="doc_section"><a name="intro">Chapter 2 Introduction</a></div>
40 <!-- *********************************************************************** -->
42 <div class="doc_text">
44 <p>Welcome to Chapter 2 of the "<a href="index.html">Implementing a language
45 with LLVM</a>" tutorial. This chapter shows you how to use the <a
46 href="LangImpl1.html">Lexer built in Chapter 1</a> to build a full <a
47 href="http://en.wikipedia.org/wiki/Parsing">parser</a> for
48 our Kaleidoscope language and build an <a
49 href="http://en.wikipedia.org/wiki/Abstract_syntax_tree">Abstract Syntax
52 <p>The parser we will build uses a combination of <a
53 href="http://en.wikipedia.org/wiki/Recursive_descent_parser">Recursive Descent
54 Parsing</a> and <a href=
55 "http://en.wikipedia.org/wiki/Operator-precedence_parser">Operator-Precedence
56 Parsing</a> to parse the Kaleidoscope language (the later for binary expression
57 and the former for everything else). Before we get to parsing though, lets talk
58 about the output of the parser: the Abstract Syntax Tree.</p>
62 <!-- *********************************************************************** -->
63 <div class="doc_section"><a name="ast">The Abstract Syntax Tree (AST)</a></div>
64 <!-- *********************************************************************** -->
66 <div class="doc_text">
68 <p>The AST for a program captures its behavior in a way that it is easy for
69 later stages of the compiler (e.g. code generation) to interpret. We basically
70 want one object for each construct in the language, and the AST should closely
71 model the language. In Kaleidoscope, we have expressions, a prototype, and a
72 function object. We'll start with expressions first:</p>
74 <div class="doc_code">
76 /// ExprAST - Base class for all expression nodes.
82 /// NumberExprAST - Expression class for numeric literals like "1.0".
83 class NumberExprAST : public ExprAST {
86 explicit NumberExprAST(double val) : Val(val) {}
91 <p>The code above shows the definition of the base ExprAST class and one
92 subclass which we use for numeric literals. The important thing about this is
93 that the NumberExprAST class captures the numeric value of the literal in the
94 class, so that later phases of the compiler can know what it is.</p>
96 <p>Right now we only create the AST, so there are no useful accessor methods on
97 them. It would be very easy to add a virtual method to pretty print the code,
98 for example. Here are the other expression AST node definitions that we'll use
99 in the basic form of the Kaleidoscope language.
102 <div class="doc_code">
104 /// VariableExprAST - Expression class for referencing a variable, like "a".
105 class VariableExprAST : public ExprAST {
108 explicit VariableExprAST(const std::string &name) : Name(name) {}
111 /// BinaryExprAST - Expression class for a binary operator.
112 class BinaryExprAST : public ExprAST {
116 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
117 : Op(op), LHS(lhs), RHS(rhs) {}
120 /// CallExprAST - Expression class for function calls.
121 class CallExprAST : public ExprAST {
123 std::vector<ExprAST*> Args;
125 CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
126 : Callee(callee), Args(args) {}
131 <p>This is all (intentially) rather straight-forward: variables capture the
132 variable name, binary operators capture their opcode (e.g. '+'), and calls
133 capture a function name and list of argument expressions. One thing that is
134 nice about our AST is that it captures the language features without talking
135 about the syntax of the language. Note that there is no discussion about
136 precedence of binary operators, lexical structure etc.</p>
138 <p>For our basic language, these are all of the expression nodes we'll define.
139 Because it doesn't have conditional control flow, it isn't Turing-complete;
140 we'll fix that in a later installment. The two things we need next are a way
141 to talk about the interface to a function, and a way to talk about functions
144 <div class="doc_code">
146 /// PrototypeAST - This class represents the "prototype" for a function,
147 /// which captures its argument names as well as if it is an operator.
150 std::vector<std::string> Args;
152 PrototypeAST(const std::string &name, const std::vector<std::string> &args)
153 : Name(name), Args(args) {}
156 /// FunctionAST - This class represents a function definition itself.
161 FunctionAST(PrototypeAST *proto, ExprAST *body)
162 : Proto(proto), Body(body) {}
167 <p>In Kaleidoscope, functions are typed with just a count of their arguments.
168 Since all values are double precision floating point, this fact doesn't need to
169 be captured anywhere. In a more aggressive and realistic language, the
170 "ExprAST" class would probably have a type field.</p>
172 <p>With this scaffolding, we can now talk about parsing expressions and function
173 bodies in Kaleidoscope.</p>
177 <!-- *********************************************************************** -->
178 <div class="doc_section"><a name="parserbasics">Parser Basics</a></div>
179 <!-- *********************************************************************** -->
181 <div class="doc_text">
183 <p>Now that we have an AST to build, we need to define the parser code to build
184 it. The idea here is that we want to parse something like "x+y" (which is
185 returned as three tokens by the lexer) into an AST that could be generated with
188 <div class="doc_code">
190 ExprAST *X = new VariableExprAST("x");
191 ExprAST *Y = new VariableExprAST("y");
192 ExprAST *Result = new BinaryExprAST('+', X, Y);
196 <p>In order to do this, we'll start by defining some basic helper routines:</p>
198 <div class="doc_code">
200 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
201 /// token the parser it looking at. getNextToken reads another token from the
202 /// lexer and updates CurTok with its results.
204 static int getNextToken() {
205 return CurTok = gettok();
211 This implements a simple token buffer around the lexer. This allows
212 us to look one token ahead at what the lexer is returning. Every function in
213 our parser will assume that CurTok is the current token that needs to be
216 <p>Again, we define these with global variables; it would be better design to
217 wrap the entire parser in a class and use instance variables for these.
220 <div class="doc_code">
223 /// Error* - These are little helper functions for error handling.
224 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
225 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
226 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
231 The <tt>Error</tt> routines are simple helper routines that our parser will use
232 to handle errors. The error recovery in our parser will not be the best and
233 is not particular user-friendly, but it will be enough for our tutorial. These
234 routines make it easier to handle errors in routines that have various return
235 types: they always return null.</p>
237 <p>With these basic helper functions implemented, we can implement the first
238 piece of our grammar: we'll start with numeric literals.</p>
242 <!-- *********************************************************************** -->
243 <div class="doc_section"><a name="parserprimexprs">Basic Expression
245 <!-- *********************************************************************** -->
247 <div class="doc_text">
249 <p>We start with numeric literals, because they are the simplest to process.
250 For each production in our grammar, we'll define a function which parses that
251 production. For numeric literals, we have:
254 <div class="doc_code">
256 /// numberexpr ::= number
257 static ExprAST *ParseNumberExpr() {
258 ExprAST *Result = new NumberExprAST(NumVal);
259 getNextToken(); // consume the number
265 <p>This routine is very simple: it expects to be called when the current token
266 is a <tt>tok_number</tt> token. It takes the current number value, creates
267 a <tt>NumberExprAST</tt> node, advances the lexer to the next token, then
270 <p>There are some interesting aspects of this. The most important one is that
271 this routine eats all of the tokens that correspond to the production, and
272 returns the lexer buffer with the next token (which is not part of the grammar
273 production) ready to go. This is a fairly standard way to go for recursive
274 descent parsers. For a better example, the parenthesis operator is defined like
277 <div class="doc_code">
279 /// parenexpr ::= '(' expression ')'
280 static ExprAST *ParseParenExpr() {
281 getNextToken(); // eat (.
282 ExprAST *V = ParseExpression();
286 return Error("expected ')'");
287 getNextToken(); // eat ).
293 <p>This function illustrates a number of interesting things about the parser:
294 1) it shows how we use the Error routines. When called, this function expects
295 that the current token is a '(' token, but after parsing the subexpression, it
296 is possible that there is not a ')' waiting. For example, if the user types in
297 "(4 x" instead of "(4)", the parser should emit an error. Because errors can
298 occur, the parser needs a way to indicate that they happened: in our parser, we
299 return null on an error.</p>
301 <p>Another interesting aspect of this function is that it uses recursion by
302 calling <tt>ParseExpression</tt> (we will soon see that <tt>ParseExpression</tt> can call
303 <tt>ParseParenExpr</tt>). This is powerful because it allows us to handle
304 recursive grammars, and keeps each production very simple. Note that
305 parentheses do not cause construction of AST nodes themselves. While we could
306 do this, the most important role of parens are to guide the parser and provide
307 grouping. Once the parser constructs the AST, parens are not needed.</p>
309 <p>The next simple production is for handling variable references and function
312 <div class="doc_code">
316 /// ::= identifier '(' expression* ')'
317 static ExprAST *ParseIdentifierExpr() {
318 std::string IdName = IdentifierStr;
320 getNextToken(); // eat identifier.
322 if (CurTok != '(') // Simple variable ref.
323 return new VariableExprAST(IdName);
326 getNextToken(); // eat (
327 std::vector<ExprAST*> Args;
329 ExprAST *Arg = ParseExpression();
333 if (CurTok == ')') break;
336 return Error("Expected ')'");
343 return new CallExprAST(IdName, Args);
348 <p>This routine follows the same style as the other routines (it expects to be
349 called if the current token is a <tt>tok_identifier</tt> token). It also has
350 recursion and error handling. One interesting aspect of this is that it uses
351 <em>look-ahead</em> to determine if the current identifier is a stand alone
352 variable reference or if it is a function call expression. It handles this by
353 checking to see if the token after the identifier is a '(' token, and constructs
354 either a <tt>VariableExprAST</tt> or <tt>CallExprAST</tt> node as appropriate.
357 <p>Now that we have all of our simple expression parsing logic in place, we can
358 define a helper function to wrap them up in a class. We call this class of
359 expressions "primary" expressions, for reasons that will become more clear
360 later. In order to parse a primary expression, we need to determine what sort
361 of expression it is:</p>
363 <div class="doc_code">
366 /// ::= identifierexpr
369 static ExprAST *ParsePrimary() {
371 default: return Error("unknown token when expecting an expression");
372 case tok_identifier: return ParseIdentifierExpr();
373 case tok_number: return ParseNumberExpr();
374 case '(': return ParseParenExpr();
380 <p>Now that you see the definition of this function, it makes it more obvious
381 why we can assume the state of CurTok in the various functions. This uses
382 look-ahead to determine which sort of expression is being inspected, and parses
383 it with a function call.</p>
385 <p>Now that basic expressions are handled, we need to handle binary expressions,
386 which are a bit more complex.</p>
390 <!-- *********************************************************************** -->
391 <div class="doc_section"><a name="parserbinops">Binary Expression
393 <!-- *********************************************************************** -->
395 <div class="doc_text">
397 <p>Binary expressions are significantly harder to parse because they are often
398 ambiguous. For example, when given the string "x+y*z", the parser can choose
399 to parse it as either "(x+y)*z" or "x+(y*z)". With common definitions from
400 mathematics, we expect the later parse, because "*" (multiplication) has
401 higher <em>precedence</em> than "+" (addition).</p>
403 <p>There are many ways to handle this, but an elegant and efficient way is to
405 "http://en.wikipedia.org/wiki/Operator-precedence_parser">Operator-Precedence
406 Parsing</a>. This parsing technique uses the precedence of binary operators to
407 guide recursion. To start with, we need a table of precedences:</p>
409 <div class="doc_code">
411 /// BinopPrecedence - This holds the precedence for each binary operator that is
413 static std::map<char, int> BinopPrecedence;
415 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
416 static int GetTokPrecedence() {
417 if (!isascii(CurTok))
420 // Make sure it's a declared binop.
421 int TokPrec = BinopPrecedence[CurTok];
422 if (TokPrec <= 0) return -1;
427 // Install standard binary operators.
428 // 1 is lowest precedence.
429 BinopPrecedence['<'] = 10;
430 BinopPrecedence['+'] = 20;
431 BinopPrecedence['-'] = 20;
432 BinopPrecedence['*'] = 40; // highest.
438 <p>For the basic form of Kaleidoscope, we will only support 4 binary operators
439 (this can obviously be extended by you, the reader). The
440 <tt>GetTokPrecedence</tt> function returns the precedence for the current token,
441 or -1 if the token is not a binary operator. Having a map makes it easy to add
442 new operators and makes it clear that the algorithm doesn't depend on the
443 specific operators involved, but it would be easy enough to eliminate the map
444 and do the comparisons in the <tt>GetTokPrecedence</tt> function.</p>
446 <p>With the helper above defined, we can now start parsing binary expressions.
447 The basic idea of operator precedence parsing is to break down an expression
448 with potentially ambiguous binary operators into pieces. Consider for example
449 the expression "a+b+(c+d)*e*f+g". Operator precedence parsing considers this
450 as a stream of primary expressions separated by binary operators. As such,
451 it will first parse the leading primary expression "a", then it will see the
452 pairs [+, b] [+, (c+d)] [*, e] [*, f] and [+, g]. Note that because parentheses
453 are primary expressions, the binary expression parser doesn't need to worry
454 about nested subexpressions like (c+d) at all.
458 To start, an expression is a primary expression potentially followed by a
459 sequence of [binop,primaryexpr] pairs:</p>
461 <div class="doc_code">
464 /// ::= primary binoprhs
466 static ExprAST *ParseExpression() {
467 ExprAST *LHS = ParsePrimary();
470 return ParseBinOpRHS(0, LHS);
475 <p><tt>ParseBinOpRHS</tt> is the function that parses the sequence of pairs for
476 us. It takes a precedence and a pointer to an expression for the part parsed
477 so far. Note that "x" is a perfectly valid expression: As such, "binoprhs" is
478 allowed to be empty, in which case it returns the expression that is passed into
479 it. In our example above, the code passes the expression for "a" into
480 <tt>ParseBinOpRHS</tt> and the current token is "+".</p>
482 <p>The precedence value passed into <tt>ParseBinOpRHS</tt> indicates the <em>
483 minimal operator precedence</em> that the function is allowed to eat. For
484 example, if the current pair stream is [+, x] and <tt>ParseBinOpRHS</tt> is
485 passed in a precedence of 40, it will not consume any tokens (because the
486 precedence of '+' is only 20). With this in mind, <tt>ParseBinOpRHS</tt> starts
489 <div class="doc_code">
492 /// ::= ('+' primary)*
493 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
494 // If this is a binop, find its precedence.
496 int TokPrec = GetTokPrecedence();
498 // If this is a binop that binds at least as tightly as the current binop,
499 // consume it, otherwise we are done.
500 if (TokPrec < ExprPrec)
505 <p>This code gets the precedence of the current token and checks to see if if is
506 too low. Because we defined invalid tokens to have a precedence of -1, this
507 check implicitly knows that the pair-stream ends when the token stream runs out
508 of binary operators. If this check succeeds, we know that the token is a binary
509 operator and that it will be included in this expression:</p>
511 <div class="doc_code">
513 // Okay, we know this is a binop.
515 getNextToken(); // eat binop
517 // Parse the primary expression after the binary operator.
518 ExprAST *RHS = ParsePrimary();
523 <p>As such, this code eats (and remembers) the binary operator and then parses
524 the following primary expression. This builds up the whole pair, the first of
525 which is [+, b] for the running example.</p>
527 <p>Now that we parsed the left-hand side of an expression and one pair of the
528 RHS sequence, we have to decide which way the expression associates. In
529 particular, we could have "(a+b) binop unparsed" or "a + (b binop unparsed)".
530 To determine this, we look ahead at "binop" to determine its precedence and
531 compare it to BinOp's precedence (which is '+' in this case):</p>
533 <div class="doc_code">
535 // If BinOp binds less tightly with RHS than the operator after RHS, let
536 // the pending operator take RHS as its LHS.
537 int NextPrec = GetTokPrecedence();
538 if (TokPrec < NextPrec) {
542 <p>If the precedence of the binop to the right of "RHS" is lower or equal to the
543 precedence of our current operator, then we know that the parentheses associate
544 as "(a+b) binop ...". In our example, since the next operator is "+" and so is
545 our current one, we know that they have the same precedence. In this case we'll
546 create the AST node for "a+b", and then continue parsing:</p>
548 <div class="doc_code">
550 ... if body omitted ...
554 LHS = new BinaryExprAST(BinOp, LHS, RHS);
555 } // loop around to the top of the while loop.
560 <p>In our example above, this will turn "a+b+" into "(a+b)" and execute the next
561 iteration of the loop, with "+" as the current token. The code above will eat
562 and remember it and parse "(c+d)" as the primary expression, which makes the
563 current pair be [+, (c+d)]. It will then enter the 'if' above with "*" as the
564 binop to the right of the primary. In this case, the precedence of "*" is
565 higher than the precedence of "+" so the if condition will be entered.</p>
567 <p>The critical question left here is "how can the if condition parse the right
568 hand side in full"? In particular, to build the AST correctly for our example,
569 it needs to get all of "(c+d)*e*f" as the RHS expression variable. The code to
570 do this is surprisingly simple (code from the above two blocks duplicated for
573 <div class="doc_code">
575 // If BinOp binds less tightly with RHS than the operator after RHS, let
576 // the pending operator take RHS as its LHS.
577 int NextPrec = GetTokPrecedence();
578 if (TokPrec < NextPrec) {
579 RHS = ParseBinOpRHS(TokPrec+1, RHS);
580 if (RHS == 0) return 0;
583 LHS = new BinaryExprAST(BinOp, LHS, RHS);
584 } // loop around to the top of the while loop.
589 <p>At this point, we know that the binary operator to the RHS of our primary
590 has higher precedence than the binop we are currently parsing. As such, we know
591 that any sequence of pairs whose operators are all higher precedence than "+"
592 should be parsed together and returned as "RHS". To do this, we recursively
593 invoke the <tt>ParseBinOpRHS</tt> function specifying "TokPrec+1" as the minimum
594 precedence required for it to continue. In our example above, this will cause
595 it to return the AST node for "(c+d)*e*f" as RHS, which is then set as the RHS
596 of the '+' expression.</p>
598 <p>Finally, on the next iteration of the while loop, the "+g" piece is parsed.
599 and added to the AST. With this little bit of code (14 non-trivial lines), we
600 correctly handle fully general binary expression parsing in a very elegant way.
603 <p>This wraps up handling of expressions. At this point, we can point the
604 parser at an arbitrary token stream and build an expression from them, stopping
605 at the first token that is not part of the expression. Next up we need to
606 handle function definitions etc.</p>
610 <!-- *********************************************************************** -->
611 <div class="doc_section"><a name="parsertop">Parsing the Rest</a></div>
612 <!-- *********************************************************************** -->
614 <div class="doc_text">
617 The first basic thing missing is that of function prototypes. In Kaleidoscope,
618 these are used both for 'extern' function declarations as well as function body
619 definitions. The code to do this is straight-forward and not very interesting
620 (once you've survived expressions):
623 <div class="doc_code">
626 /// ::= id '(' id* ')'
627 static PrototypeAST *ParsePrototype() {
628 if (CurTok != tok_identifier)
629 return ErrorP("Expected function name in prototype");
631 std::string FnName = IdentifierStr;
635 return ErrorP("Expected '(' in prototype");
637 std::vector<std::string> ArgNames;
638 while (getNextToken() == tok_identifier)
639 ArgNames.push_back(IdentifierStr);
641 return ErrorP("Expected ')' in prototype");
644 getNextToken(); // eat ')'.
646 return new PrototypeAST(FnName, ArgNames);
651 <p>Given this, a function definition is very simple, just a prototype plus
652 an expression to implement the body:</p>
654 <div class="doc_code">
656 /// definition ::= 'def' prototype expression
657 static FunctionAST *ParseDefinition() {
658 getNextToken(); // eat def.
659 PrototypeAST *Proto = ParsePrototype();
660 if (Proto == 0) return 0;
662 if (ExprAST *E = ParseExpression())
663 return new FunctionAST(Proto, E);
669 <p>In addition, we support 'extern' to declare functions like 'sin' and 'cos' as
670 well as to support forward declaration of user functions. 'externs' are just
671 prototypes with no body:</p>
673 <div class="doc_code">
675 /// external ::= 'extern' prototype
676 static PrototypeAST *ParseExtern() {
677 getNextToken(); // eat extern.
678 return ParsePrototype();
683 <p>Finally, we'll also let the user type in arbitrary top-level expressions and
684 evaluate them on the fly. We will handle this by defining anonymous nullary
685 (zero argument) functions for them:</p>
687 <div class="doc_code">
689 /// toplevelexpr ::= expression
690 static FunctionAST *ParseTopLevelExpr() {
691 if (ExprAST *E = ParseExpression()) {
692 // Make an anonymous proto.
693 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
694 return new FunctionAST(Proto, E);
701 <p>Now that we have all the pieces, lets build a little driver that will let us
702 actually <em>execute</em> this code we've built!</p>
706 <!-- *********************************************************************** -->
707 <div class="doc_section"><a name="driver">The Driver</a></div>
708 <!-- *********************************************************************** -->
710 <div class="doc_text">
712 <p>The driver for this simply invokes all of the parsing pieces with a top-level
713 dispatch loop. There isn't much interesting here, so I'll just include the
714 top-level loop. See <a href="#code">below</a> for full code in the "Top-Level
715 Parsing" section.</p>
717 <div class="doc_code">
719 /// top ::= definition | external | expression | ';'
720 static void MainLoop() {
722 fprintf(stderr, "ready> ");
724 case tok_eof: return;
725 case ';': getNextToken(); break; // ignore top level semicolons.
726 case tok_def: HandleDefinition(); break;
727 case tok_extern: HandleExtern(); break;
728 default: HandleTopLevelExpression(); break;
735 <p>The most interesting part of this is that we ignore top-level semi colons.
736 Why is this, you ask? The basic reason is that if you type "4 + 5" at the
737 command line, the parser doesn't know that that is the end of what you will
738 type. For example, on the next line you could type "def foo..." in which case
739 4+5 is the end of a top-level expression. Alternatively you could type "* 6",
740 which would continue the expression. Having top-level semicolons allows you to
741 type "4+5;" and the parser will know you are done.</p>
745 <!-- *********************************************************************** -->
746 <div class="doc_section"><a name="conclusions">Conclusions</a></div>
747 <!-- *********************************************************************** -->
749 <div class="doc_text">
751 <p>With just under 400 lines of commented code, we fully defined our minimal
752 language, including a lexer, parser and AST builder. With this done, the
753 executable will validate code and tell us if it is gramatically invalid. For
754 example, here is a sample interaction:</p>
756 <div class="doc_code">
759 ready> def foo(x y) x+foo(y, 4.0);
760 ready> Parsed a function definition.
761 ready> def foo(x y) x+y y;
762 ready> Parsed a function definition.
763 ready> Parsed a top-level expr
764 ready> def foo(x y) x+y );
765 ready> Parsed a function definition.
766 ready> Error: unknown token when expecting an expression
767 ready> extern sin(a);
768 ready> Parsed an extern
774 <p>There is a lot of room for extension here. You can define new AST nodes,
775 extend the language in many ways, etc. In the <a href="LangImpl3.html">next
776 installment</a>, we will describe how to generate LLVM IR from the AST.</p>
780 <!-- *********************************************************************** -->
781 <div class="doc_section"><a name="code">Full Code Listing</a></div>
782 <!-- *********************************************************************** -->
784 <div class="doc_text">
787 Here is the complete code listing for this and the previous chapter.
788 Note that it is fully self-contained: you don't need LLVM or any external
789 libraries at all for this (other than the C and C++ standard libraries of
790 course). To build this, just compile with:</p>
792 <div class="doc_code">
801 <p>Here is the code:</p>
803 <div class="doc_code">
805 #include <cstdio>
806 #include <string>
808 #include <vector>
810 //===----------------------------------------------------------------------===//
812 //===----------------------------------------------------------------------===//
814 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
815 // of these for known things.
820 tok_def = -2, tok_extern = -3,
823 tok_identifier = -4, tok_number = -5,
826 static std::string IdentifierStr; // Filled in if tok_identifier
827 static double NumVal; // Filled in if tok_number
829 /// gettok - Return the next token from standard input.
830 static int gettok() {
831 static int LastChar = ' ';
833 // Skip any whitespace.
834 while (isspace(LastChar))
835 LastChar = getchar();
837 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
838 IdentifierStr = LastChar;
839 while (isalnum((LastChar = getchar())))
840 IdentifierStr += LastChar;
842 if (IdentifierStr == "def") return tok_def;
843 if (IdentifierStr == "extern") return tok_extern;
844 return tok_identifier;
847 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
851 LastChar = getchar();
852 } while (isdigit(LastChar) || LastChar == '.');
854 NumVal = strtod(NumStr.c_str(), 0);
858 if (LastChar == '#') {
859 // Comment until end of line.
860 do LastChar = getchar();
861 while (LastChar != EOF && LastChar != '\n' & LastChar != '\r');
867 // Check for end of file. Don't eat the EOF.
871 // Otherwise, just return the character as its ascii value.
872 int ThisChar = LastChar;
873 LastChar = getchar();
877 //===----------------------------------------------------------------------===//
878 // Abstract Syntax Tree (aka Parse Tree)
879 //===----------------------------------------------------------------------===//
881 /// ExprAST - Base class for all expression nodes.
884 virtual ~ExprAST() {}
887 /// NumberExprAST - Expression class for numeric literals like "1.0".
888 class NumberExprAST : public ExprAST {
891 explicit NumberExprAST(double val) : Val(val) {}
894 /// VariableExprAST - Expression class for referencing a variable, like "a".
895 class VariableExprAST : public ExprAST {
898 explicit VariableExprAST(const std::string &name) : Name(name) {}
901 /// BinaryExprAST - Expression class for a binary operator.
902 class BinaryExprAST : public ExprAST {
906 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
907 : Op(op), LHS(lhs), RHS(rhs) {}
910 /// CallExprAST - Expression class for function calls.
911 class CallExprAST : public ExprAST {
913 std::vector<ExprAST*> Args;
915 CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
916 : Callee(callee), Args(args) {}
919 /// PrototypeAST - This class represents the "prototype" for a function,
920 /// which captures its argument names as well as if it is an operator.
923 std::vector< Args;
925 PrototypeAST(const std::string &name, const std::vector<std::string> &args)
926 : Name(name), Args(args) {}
930 /// FunctionAST - This class represents a function definition itself.
935 FunctionAST(PrototypeAST *proto, ExprAST *body)
936 : Proto(proto), Body(body) {}
940 //===----------------------------------------------------------------------===//
942 //===----------------------------------------------------------------------===//
944 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
945 /// token the parser it looking at. getNextToken reads another token from the
946 /// lexer and updates CurTok with its results.
948 static int getNextToken() {
949 return CurTok = gettok();
952 /// BinopPrecedence - This holds the precedence for each binary operator that is
954 static std::map<char, int> BinopPrecedence;
956 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
957 static int GetTokPrecedence() {
958 if (!isascii(CurTok))
961 // Make sure it's a declared binop.
962 int TokPrec = BinopPrecedence[CurTok];
963 if (TokPrec <= 0) return -1;
967 /// Error* - These are little helper functions for error handling.
968 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
969 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
970 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
972 static ExprAST *ParseExpression();
976 /// ::= identifier '(' expression* ')'
977 static ExprAST *ParseIdentifierExpr() {
978 std::string IdName = IdentifierStr;
980 getNextToken(); // eat identifier.
982 if (CurTok != '(') // Simple variable ref.
983 return new VariableExprAST(IdName);
986 getNextToken(); // eat (
987 std::vector<ExprAST*> Args;
989 ExprAST *Arg = ParseExpression();
993 if (CurTok == ')') break;
996 return Error("Expected ')'");
1003 return new CallExprAST(IdName, Args);
1006 /// numberexpr ::= number
1007 static ExprAST *ParseNumberExpr() {
1008 ExprAST *Result = new NumberExprAST(NumVal);
1009 getNextToken(); // consume the number
1013 /// parenexpr ::= '(' expression ')'
1014 static ExprAST *ParseParenExpr() {
1015 getNextToken(); // eat (.
1016 ExprAST *V = ParseExpression();
1020 return Error("expected ')'");
1021 getNextToken(); // eat ).
1026 /// ::= identifierexpr
1029 static ExprAST *ParsePrimary() {
1031 default: return Error("unknown token when expecting an expression");
1032 case tok_identifier: return ParseIdentifierExpr();
1033 case tok_number: return ParseNumberExpr();
1034 case '(': return ParseParenExpr();
1039 /// ::= ('+' primary)*
1040 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
1041 // If this is a binop, find its precedence.
1043 int TokPrec = GetTokPrecedence();
1045 // If this is a binop that binds at least as tightly as the current binop,
1046 // consume it, otherwise we are done.
1047 if (TokPrec < ExprPrec)
1050 // Okay, we know this is a binop.
1052 getNextToken(); // eat binop
1054 // Parse the primary expression after the binary operator.
1055 ExprAST *RHS = ParsePrimary();
1058 // If BinOp binds less tightly with RHS than the operator after RHS, let
1059 // the pending operator take RHS as its LHS.
1060 int NextPrec = GetTokPrecedence();
1061 if (TokPrec < NextPrec) {
1062 RHS = ParseBinOpRHS(TokPrec+1, RHS);
1063 if (RHS == 0) return 0;
1067 LHS = new BinaryExprAST(BinOp, LHS, RHS);
1072 /// ::= primary binoprhs
1074 static ExprAST *ParseExpression() {
1075 ExprAST *LHS = ParsePrimary();
1078 return ParseBinOpRHS(0, LHS);
1082 /// ::= id '(' id* ')'
1083 static PrototypeAST *ParsePrototype() {
1084 if (CurTok != tok_identifier)
1085 return ErrorP("Expected function name in prototype");
1087 std::string FnName = IdentifierStr;
1091 return ErrorP("Expected '(' in prototype");
1093 std::vector<std::string> ArgNames;
1094 while (getNextToken() == tok_identifier)
1095 ArgNames.push_back(IdentifierStr);
1097 return ErrorP("Expected ')' in prototype");
1100 getNextToken(); // eat ')'.
1102 return new PrototypeAST(FnName, ArgNames);
1105 /// definition ::= 'def' prototype expression
1106 static FunctionAST *ParseDefinition() {
1107 getNextToken(); // eat def.
1108 PrototypeAST *Proto = ParsePrototype();
1109 if (Proto == 0) return 0;
1111 if (ExprAST *E = ParseExpression())
1112 return new FunctionAST(Proto, E);
1116 /// toplevelexpr ::= expression
1117 static FunctionAST *ParseTopLevelExpr() {
1118 if (ExprAST *E = ParseExpression()) {
1119 // Make an anonymous proto.
1120 PrototypeAST *Proto = new PrototypeAST("", std::vector<());
1121 return new FunctionAST(Proto, E);
1126 /// external ::= 'extern' prototype
1127 static PrototypeAST *ParseExtern() {
1128 getNextToken(); // eat extern.
1129 return ParsePrototype();
1132 //===----------------------------------------------------------------------===//
1133 // Top-Level parsing
1134 //===----------------------------------------------------------------------===//
1136 static void HandleDefinition() {
1137 if (FunctionAST *F = ParseDefinition()) {
1138 fprintf(stderr, "Parsed a function definition.\n");
1140 // Skip token for error recovery.
1145 static void HandleExtern() {
1146 if (PrototypeAST *P = ParseExtern()) {
1147 fprintf(stderr, "Parsed an extern\n");
1149 // Skip token for error recovery.
1154 static void HandleTopLevelExpression() {
1155 // Evaluate a top level expression into an anonymous function.
1156 if (FunctionAST *F = ParseTopLevelExpr()) {
1157 fprintf(stderr, "Parsed a top-level expr\n");
1159 // Skip token for error recovery.
1164 /// top ::= definition | external | expression | ';'
1165 static void MainLoop() {
1167 fprintf(stderr, "ready> ");
1169 case tok_eof: return;
1170 case ';': getNextToken(); break; // ignore top level semicolons.
1171 case tok_def: HandleDefinition(); break;
1172 case tok_extern: HandleExtern(); break;
1173 default: HandleTopLevelExpression(); break;
1178 //===----------------------------------------------------------------------===//
1179 // Main driver code.
1180 //===----------------------------------------------------------------------===//
1183 // Install standard binary operators.
1184 // 1 is lowest precedence.
1185 BinopPrecedence['<'] = 10;
1186 BinopPrecedence['+'] = 20;
1187 BinopPrecedence['-'] = 20;
1188 BinopPrecedence['*'] = 40; // highest.
1190 // Prime the first token.
1191 fprintf(stderr, "ready> ");
1201 <!-- *********************************************************************** -->
1204 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1205 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1206 <a href="http://validator.w3.org/check/referer"><img
1207 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1209 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1210 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
1211 Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $