X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2Ftutorial%2FLangImpl2.html;h=018d0be76032fcac56a4e1cfd7a84d0d13f6faa2;hb=04eeefb32a3ae7de4cde1908d30bff61e0d6b86e;hp=6dba2607714daf0f66b15d60b77bec4a0cfd2c5f;hpb=bd779a3a1f405e208be2d2a9d6c1960b12b1ec99;p=oota-llvm.git diff --git a/docs/tutorial/LangImpl2.html b/docs/tutorial/LangImpl2.html index 6dba2607714..018d0be7603 100644 --- a/docs/tutorial/LangImpl2.html +++ b/docs/tutorial/LangImpl2.html @@ -98,7 +98,7 @@ know what the stored numeric value is.

Right now we only create the AST, so there are no useful accessor methods on them. It would be very easy to add a virtual method to pretty print the code, for example. Here are the other expression AST node definitions that we'll use -in the basic form of the Kaleidoscope language. +in the basic form of the Kaleidoscope language:

@@ -130,7 +130,7 @@ public:
-

This is all (intentially) rather straight-forward: variables capture the +

This is all (intentionally) rather straight-forward: variables capture the variable name, binary operators capture their opcode (e.g. '+'), and calls capture a function name as well as a list of any argument expressions. One thing that is nice about our AST is that it captures the language features without @@ -201,7 +201,7 @@ calls like this:

 /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current
-/// token the parser it looking at.  getNextToken reads another token from the
+/// token the parser is looking at.  getNextToken reads another token from the
 /// lexer and updates CurTok with its results.
 static int CurTok;
 static int getNextToken() {
@@ -263,11 +263,11 @@ static ExprAST *ParseNumberExpr() {
 
 

This routine is very simple: it expects to be called when the current token is a tok_number token. It takes the current number value, creates -a NumberExprAST node, advances the lexer to the next token and finally +a NumberExprAST node, advances the lexer to the next token, and finally returns.

There are some interesting aspects to this. The most important one is that -this routine eats all of the tokens that correspond to the production, and +this routine eats all of the tokens that correspond to the production and returns the lexer buffer with the next token (which is not part of the grammar production) ready to go. This is a fairly standard way to go for recursive descent parsers. For a better example, the parenthesis operator is defined like @@ -293,7 +293,7 @@ static ExprAST *ParseParenExpr() { parser:

-1) it shows how we use the Error routines. When called, this function expects +1) It shows how we use the Error routines. When called, this function expects that the current token is a '(' token, but after parsing the subexpression, it is possible that there is no ')' waiting. For example, if the user types in "(4 x" instead of "(4)", the parser should emit an error. Because errors can @@ -305,8 +305,8 @@ calling ParseExpression (we will soon see that ParseExpression ParseParenExpr). This is powerful because it allows us to handle recursive grammars, and keeps each production very simple. Note that parentheses do not cause construction of AST nodes themselves. While we could -do it this way, the most important role of parens are to guide the parser and -provide grouping. Once the parser constructs the AST, parens are not +do it this way, the most important role of parentheses are to guide the parser +and provide grouping. Once the parser constructs the AST, parentheses are not needed.

The next simple production is for handling variable references and function @@ -337,7 +337,7 @@ static ExprAST *ParseIdentifierExpr() { if (CurTok == ')') break; if (CurTok != ',') - return Error("Expected ')'"); + return Error("Expected ')' or ',' in argument list"); getNextToken(); } } @@ -350,21 +350,21 @@ static ExprAST *ParseIdentifierExpr() {

-

This routine follows the same style as the other routines (it expects to be +

This routine follows the same style as the other routines. (It expects to be called if the current token is a tok_identifier token). It also has recursion and error handling. One interesting aspect of this is that it uses look-ahead to determine if the current identifier is a stand alone variable reference or if it is a function call expression. It handles this by -checking to see if the token after the identifier is a '(' token, and constructs +checking to see if the token after the identifier is a '(' token, constructing either a VariableExprAST or CallExprAST node as appropriate.

-

Now that we have all of our simple expression parsing logic in place, we can -define a helper function to wrap it together into one entry-point. We call this +

Now that we have all of our simple expression-parsing logic in place, we can +define a helper function to wrap it together into one entry point. We call this class of expressions "primary" expressions, for reasons that will become more clear later in the tutorial. In order to parse an arbitrary primary expression, we need to determine what sort of -specific expression it is:

+expression it is:

@@ -383,13 +383,13 @@ static ExprAST *ParsePrimary() {
 
-

Now that you see the definition of this function, it makes it more obvious -why we can assume the state of CurTok in the various functions. This uses -look-ahead to determine which sort of expression is being inspected, and parses -it with a function call.

+

Now that you see the definition of this function, it is more obvious why we +can assume the state of CurTok in the various functions. This uses look-ahead +to determine which sort of expression is being inspected, and then parses it +with a function call.

-

Now that basic expressions are handled, we need to handle binary expressions, -which are a bit more complex.

+

Now that basic expressions are handled, we need to handle binary expressions. +They are a bit more complex.

@@ -447,12 +447,12 @@ int main() { or -1 if the token is not a binary operator. Having a map makes it easy to add new operators and makes it clear that the algorithm doesn't depend on the specific operators involved, but it would be easy enough to eliminate the map -and do the comparisons in the GetTokPrecedence function (or just use +and do the comparisons in the GetTokPrecedence function. (Or just use a fixed-size array).

With the helper above defined, we can now start parsing binary expressions. The basic idea of operator precedence parsing is to break down an expression -with potentially ambiguous binary operators into pieces. Consider for example +with potentially ambiguous binary operators into pieces. Consider ,for example, the expression "a+b+(c+d)*e*f+g". Operator precedence parsing considers this as a stream of primary expressions separated by binary operators. As such, it will first parse the leading primary expression "a", then it will see the @@ -708,7 +708,7 @@ static FunctionAST *ParseTopLevelExpr() { -

Now that we have all the pieces, lets build a little driver that will let us +

Now that we have all the pieces, let's build a little driver that will let us actually execute this code we've built!

@@ -732,7 +732,7 @@ static void MainLoop() { fprintf(stderr, "ready> "); switch (CurTok) { case tok_eof: return; - case ';': getNextToken(); break; // ignore top level semicolons. + case ';': getNextToken(); break; // ignore top-level semicolons. case tok_def: HandleDefinition(); break; case tok_extern: HandleExtern(); break; default: HandleTopLevelExpression(); break; @@ -742,13 +742,13 @@ static void MainLoop() { -

The most interesting part of this is that we ignore top-level semi colons. +

The most interesting part of this is that we ignore top-level semicolons. Why is this, you ask? The basic reason is that if you type "4 + 5" at the command line, the parser doesn't know whether that is the end of what you will type or not. For example, on the next line you could type "def foo..." in which case 4+5 is the end of a top-level expression. Alternatively you could type "* 6", which would continue the expression. Having top-level semicolons allows you to -type "4+5;" and the parser will know you are done.

+type "4+5;", and the parser will know you are done.

@@ -760,8 +760,8 @@ type "4+5;" and the parser will know you are done.

With just under 400 lines of commented code (240 lines of non-comment, non-blank code), we fully defined our minimal language, including a lexer, -parser and AST builder. With this done, the executable will validate -Kaleidoscope code and tell us if it is gramatically invalid. For +parser, and AST builder. With this done, the executable will validate +Kaleidoscope code and tell us if it is grammatically invalid. For example, here is a sample interaction:

@@ -798,8 +798,8 @@ Representation (IR) from the AST.

Here is the complete code listing for this and the previous chapter. Note that it is fully self-contained: you don't need LLVM or any external -libraries at all for this (other than the C and C++ standard libraries of -course). To build this, just compile with:

+libraries at all for this. (Besides the C and C++ standard libraries, of +course.) To build this, just compile with:

@@ -870,7 +870,7 @@ static int gettok() {
   if (LastChar == '#') {
     // Comment until end of line.
     do LastChar = getchar();
-    while (LastChar != EOF && LastChar != '\n' & LastChar != '\r');
+    while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
     
     if (LastChar != EOF)
       return gettok();
@@ -933,7 +933,7 @@ public:
 /// of arguments the function takes).
 class PrototypeAST {
   std::string Name;
-  std::vector< Args;
+  std::vector<std::string> Args;
 public:
   PrototypeAST(const std::string &name, const std::vector<std::string> &args)
     : Name(name), Args(args) {}
@@ -955,7 +955,7 @@ public:
 //===----------------------------------------------------------------------===//
 
 /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current
-/// token the parser it looking at.  getNextToken reads another token from the
+/// token the parser is looking at.  getNextToken reads another token from the
 /// lexer and updates CurTok with its results.
 static int CurTok;
 static int getNextToken() {
@@ -1007,7 +1007,7 @@ static ExprAST *ParseIdentifierExpr() {
       if (CurTok == ')') break;
     
       if (CurTok != ',')
-        return Error("Expected ')'");
+        return Error("Expected ')' or ',' in argument list");
       getNextToken();
     }
   }
@@ -1132,7 +1132,7 @@ static FunctionAST *ParseDefinition() {
 static FunctionAST *ParseTopLevelExpr() {
   if (ExprAST *E = ParseExpression()) {
     // Make an anonymous proto.
-    PrototypeAST *Proto = new PrototypeAST("", std::vector<());
+    PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
     return new FunctionAST(Proto, E);
   }
   return 0;
@@ -1167,7 +1167,7 @@ static void HandleExtern() {
 }
 
 static void HandleTopLevelExpression() {
-  // Evaluate a top level expression into an anonymous function.
+  // Evaluate a top-level expression into an anonymous function.
   if (FunctionAST *F = ParseTopLevelExpr()) {
     fprintf(stderr, "Parsed a top-level expr\n");
   } else {
@@ -1182,7 +1182,7 @@ static void MainLoop() {
     fprintf(stderr, "ready> ");
     switch (CurTok) {
     case tok_eof:    return;
-    case ';':        getNextToken(); break;  // ignore top level semicolons.
+    case ';':        getNextToken(); break;  // ignore top-level semicolons.
     case tok_def:    HandleDefinition(); break;
     case tok_extern: HandleExtern(); break;
     default:         HandleTopLevelExpression(); break;
@@ -1211,6 +1211,7 @@ int main() {
 }
 
+Next: Implementing Code Generation to LLVM IR