xref: /aosp_15_r20/external/llvm/docs/tutorial/LangImpl01.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker=================================================
2*9880d681SAndroid Build Coastguard WorkerKaleidoscope: Tutorial Introduction and the Lexer
3*9880d681SAndroid Build Coastguard Worker=================================================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. contents::
6*9880d681SAndroid Build Coastguard Worker   :local:
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard WorkerTutorial Introduction
9*9880d681SAndroid Build Coastguard Worker=====================
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard WorkerWelcome to the "Implementing a language with LLVM" tutorial. This
12*9880d681SAndroid Build Coastguard Workertutorial runs through the implementation of a simple language, showing
13*9880d681SAndroid Build Coastguard Workerhow fun and easy it can be. This tutorial will get you up and started as
14*9880d681SAndroid Build Coastguard Workerwell as help to build a framework you can extend to other languages. The
15*9880d681SAndroid Build Coastguard Workercode in this tutorial can also be used as a playground to hack on other
16*9880d681SAndroid Build Coastguard WorkerLLVM specific things.
17*9880d681SAndroid Build Coastguard Worker
18*9880d681SAndroid Build Coastguard WorkerThe goal of this tutorial is to progressively unveil our language,
19*9880d681SAndroid Build Coastguard Workerdescribing how it is built up over time. This will let us cover a fairly
20*9880d681SAndroid Build Coastguard Workerbroad range of language design and LLVM-specific usage issues, showing
21*9880d681SAndroid Build Coastguard Workerand explaining the code for it all along the way, without overwhelming
22*9880d681SAndroid Build Coastguard Workeryou with tons of details up front.
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard WorkerIt is useful to point out ahead of time that this tutorial is really
25*9880d681SAndroid Build Coastguard Workerabout teaching compiler techniques and LLVM specifically, *not* about
26*9880d681SAndroid Build Coastguard Workerteaching modern and sane software engineering principles. In practice,
27*9880d681SAndroid Build Coastguard Workerthis means that we'll take a number of shortcuts to simplify the
28*9880d681SAndroid Build Coastguard Workerexposition. For example, the code uses global variables
29*9880d681SAndroid Build Coastguard Workerall over the place, doesn't use nice design patterns like
30*9880d681SAndroid Build Coastguard Worker`visitors <http://en.wikipedia.org/wiki/Visitor_pattern>`_, etc... but
31*9880d681SAndroid Build Coastguard Workerit is very simple. If you dig in and use the code as a basis for future
32*9880d681SAndroid Build Coastguard Workerprojects, fixing these deficiencies shouldn't be hard.
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard WorkerI've tried to put this tutorial together in a way that makes chapters
35*9880d681SAndroid Build Coastguard Workereasy to skip over if you are already familiar with or are uninterested
36*9880d681SAndroid Build Coastguard Workerin the various pieces. The structure of the tutorial is:
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker-  `Chapter #1 <#language>`_: Introduction to the Kaleidoscope
39*9880d681SAndroid Build Coastguard Worker   language, and the definition of its Lexer - This shows where we are
40*9880d681SAndroid Build Coastguard Worker   going and the basic functionality that we want it to do. In order to
41*9880d681SAndroid Build Coastguard Worker   make this tutorial maximally understandable and hackable, we choose
42*9880d681SAndroid Build Coastguard Worker   to implement everything in C++ instead of using lexer and parser
43*9880d681SAndroid Build Coastguard Worker   generators. LLVM obviously works just fine with such tools, feel free
44*9880d681SAndroid Build Coastguard Worker   to use one if you prefer.
45*9880d681SAndroid Build Coastguard Worker-  `Chapter #2 <LangImpl02.html>`_: Implementing a Parser and AST -
46*9880d681SAndroid Build Coastguard Worker   With the lexer in place, we can talk about parsing techniques and
47*9880d681SAndroid Build Coastguard Worker   basic AST construction. This tutorial describes recursive descent
48*9880d681SAndroid Build Coastguard Worker   parsing and operator precedence parsing. Nothing in Chapters 1 or 2
49*9880d681SAndroid Build Coastguard Worker   is LLVM-specific, the code doesn't even link in LLVM at this point.
50*9880d681SAndroid Build Coastguard Worker   :)
51*9880d681SAndroid Build Coastguard Worker-  `Chapter #3 <LangImpl03.html>`_: Code generation to LLVM IR - With
52*9880d681SAndroid Build Coastguard Worker   the AST ready, we can show off how easy generation of LLVM IR really
53*9880d681SAndroid Build Coastguard Worker   is.
54*9880d681SAndroid Build Coastguard Worker-  `Chapter #4 <LangImpl04.html>`_: Adding JIT and Optimizer Support
55*9880d681SAndroid Build Coastguard Worker   - Because a lot of people are interested in using LLVM as a JIT,
56*9880d681SAndroid Build Coastguard Worker   we'll dive right into it and show you the 3 lines it takes to add JIT
57*9880d681SAndroid Build Coastguard Worker   support. LLVM is also useful in many other ways, but this is one
58*9880d681SAndroid Build Coastguard Worker   simple and "sexy" way to show off its power. :)
59*9880d681SAndroid Build Coastguard Worker-  `Chapter #5 <LangImpl05.html>`_: Extending the Language: Control
60*9880d681SAndroid Build Coastguard Worker   Flow - With the language up and running, we show how to extend it
61*9880d681SAndroid Build Coastguard Worker   with control flow operations (if/then/else and a 'for' loop). This
62*9880d681SAndroid Build Coastguard Worker   gives us a chance to talk about simple SSA construction and control
63*9880d681SAndroid Build Coastguard Worker   flow.
64*9880d681SAndroid Build Coastguard Worker-  `Chapter #6 <LangImpl06.html>`_: Extending the Language:
65*9880d681SAndroid Build Coastguard Worker   User-defined Operators - This is a silly but fun chapter that talks
66*9880d681SAndroid Build Coastguard Worker   about extending the language to let the user program define their own
67*9880d681SAndroid Build Coastguard Worker   arbitrary unary and binary operators (with assignable precedence!).
68*9880d681SAndroid Build Coastguard Worker   This lets us build a significant piece of the "language" as library
69*9880d681SAndroid Build Coastguard Worker   routines.
70*9880d681SAndroid Build Coastguard Worker-  `Chapter #7 <LangImpl07.html>`_: Extending the Language: Mutable
71*9880d681SAndroid Build Coastguard Worker   Variables - This chapter talks about adding user-defined local
72*9880d681SAndroid Build Coastguard Worker   variables along with an assignment operator. The interesting part
73*9880d681SAndroid Build Coastguard Worker   about this is how easy and trivial it is to construct SSA form in
74*9880d681SAndroid Build Coastguard Worker   LLVM: no, LLVM does *not* require your front-end to construct SSA
75*9880d681SAndroid Build Coastguard Worker   form!
76*9880d681SAndroid Build Coastguard Worker-  `Chapter #8 <LangImpl08.html>`_: Compiling to Object Files - This
77*9880d681SAndroid Build Coastguard Worker   chapter explains how to take LLVM IR and compile it down to object
78*9880d681SAndroid Build Coastguard Worker   files.
79*9880d681SAndroid Build Coastguard Worker-  `Chapter #9 <LangImpl09.html>`_: Extending the Language: Debug
80*9880d681SAndroid Build Coastguard Worker   Information - Having built a decent little programming language with
81*9880d681SAndroid Build Coastguard Worker   control flow, functions and mutable variables, we consider what it
82*9880d681SAndroid Build Coastguard Worker   takes to add debug information to standalone executables. This debug
83*9880d681SAndroid Build Coastguard Worker   information will allow you to set breakpoints in Kaleidoscope
84*9880d681SAndroid Build Coastguard Worker   functions, print out argument variables, and call functions - all
85*9880d681SAndroid Build Coastguard Worker   from within the debugger!
86*9880d681SAndroid Build Coastguard Worker-  `Chapter #10 <LangImpl10.html>`_: Conclusion and other useful LLVM
87*9880d681SAndroid Build Coastguard Worker   tidbits - This chapter wraps up the series by talking about
88*9880d681SAndroid Build Coastguard Worker   potential ways to extend the language, but also includes a bunch of
89*9880d681SAndroid Build Coastguard Worker   pointers to info about "special topics" like adding garbage
90*9880d681SAndroid Build Coastguard Worker   collection support, exceptions, debugging, support for "spaghetti
91*9880d681SAndroid Build Coastguard Worker   stacks", and a bunch of other tips and tricks.
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard WorkerBy the end of the tutorial, we'll have written a bit less than 1000 lines
94*9880d681SAndroid Build Coastguard Workerof non-comment, non-blank, lines of code. With this small amount of
95*9880d681SAndroid Build Coastguard Workercode, we'll have built up a very reasonable compiler for a non-trivial
96*9880d681SAndroid Build Coastguard Workerlanguage including a hand-written lexer, parser, AST, as well as code
97*9880d681SAndroid Build Coastguard Workergeneration support with a JIT compiler. While other systems may have
98*9880d681SAndroid Build Coastguard Workerinteresting "hello world" tutorials, I think the breadth of this
99*9880d681SAndroid Build Coastguard Workertutorial is a great testament to the strengths of LLVM and why you
100*9880d681SAndroid Build Coastguard Workershould consider it if you're interested in language or compiler design.
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard WorkerA note about this tutorial: we expect you to extend the language and
103*9880d681SAndroid Build Coastguard Workerplay with it on your own. Take the code and go crazy hacking away at it,
104*9880d681SAndroid Build Coastguard Workercompilers don't need to be scary creatures - it can be a lot of fun to
105*9880d681SAndroid Build Coastguard Workerplay with languages!
106*9880d681SAndroid Build Coastguard Worker
107*9880d681SAndroid Build Coastguard WorkerThe Basic Language
108*9880d681SAndroid Build Coastguard Worker==================
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard WorkerThis tutorial will be illustrated with a toy language that we'll call
111*9880d681SAndroid Build Coastguard Worker"`Kaleidoscope <http://en.wikipedia.org/wiki/Kaleidoscope>`_" (derived
112*9880d681SAndroid Build Coastguard Workerfrom "meaning beautiful, form, and view"). Kaleidoscope is a procedural
113*9880d681SAndroid Build Coastguard Workerlanguage that allows you to define functions, use conditionals, math,
114*9880d681SAndroid Build Coastguard Workeretc. Over the course of the tutorial, we'll extend Kaleidoscope to
115*9880d681SAndroid Build Coastguard Workersupport the if/then/else construct, a for loop, user defined operators,
116*9880d681SAndroid Build Coastguard WorkerJIT compilation with a simple command line interface, etc.
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard WorkerBecause we want to keep things simple, the only datatype in Kaleidoscope
119*9880d681SAndroid Build Coastguard Workeris a 64-bit floating point type (aka 'double' in C parlance). As such,
120*9880d681SAndroid Build Coastguard Workerall values are implicitly double precision and the language doesn't
121*9880d681SAndroid Build Coastguard Workerrequire type declarations. This gives the language a very nice and
122*9880d681SAndroid Build Coastguard Workersimple syntax. For example, the following simple example computes
123*9880d681SAndroid Build Coastguard Worker`Fibonacci numbers: <http://en.wikipedia.org/wiki/Fibonacci_number>`_
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard Worker::
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker    # Compute the x'th fibonacci number.
128*9880d681SAndroid Build Coastguard Worker    def fib(x)
129*9880d681SAndroid Build Coastguard Worker      if x < 3 then
130*9880d681SAndroid Build Coastguard Worker        1
131*9880d681SAndroid Build Coastguard Worker      else
132*9880d681SAndroid Build Coastguard Worker        fib(x-1)+fib(x-2)
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard Worker    # This expression will compute the 40th number.
135*9880d681SAndroid Build Coastguard Worker    fib(40)
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard WorkerWe also allow Kaleidoscope to call into standard library functions (the
138*9880d681SAndroid Build Coastguard WorkerLLVM JIT makes this completely trivial). This means that you can use the
139*9880d681SAndroid Build Coastguard Worker'extern' keyword to define a function before you use it (this is also
140*9880d681SAndroid Build Coastguard Workeruseful for mutually recursive functions). For example:
141*9880d681SAndroid Build Coastguard Worker
142*9880d681SAndroid Build Coastguard Worker::
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker    extern sin(arg);
145*9880d681SAndroid Build Coastguard Worker    extern cos(arg);
146*9880d681SAndroid Build Coastguard Worker    extern atan2(arg1 arg2);
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker    atan2(sin(.4), cos(42))
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard WorkerA more interesting example is included in Chapter 6 where we write a
151*9880d681SAndroid Build Coastguard Workerlittle Kaleidoscope application that `displays a Mandelbrot
152*9880d681SAndroid Build Coastguard WorkerSet <LangImpl06.html#kicking-the-tires>`_ at various levels of magnification.
153*9880d681SAndroid Build Coastguard Worker
154*9880d681SAndroid Build Coastguard WorkerLets dive into the implementation of this language!
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard WorkerThe Lexer
157*9880d681SAndroid Build Coastguard Worker=========
158*9880d681SAndroid Build Coastguard Worker
159*9880d681SAndroid Build Coastguard WorkerWhen it comes to implementing a language, the first thing needed is the
160*9880d681SAndroid Build Coastguard Workerability to process a text file and recognize what it says. The
161*9880d681SAndroid Build Coastguard Workertraditional way to do this is to use a
162*9880d681SAndroid Build Coastguard Worker"`lexer <http://en.wikipedia.org/wiki/Lexical_analysis>`_" (aka
163*9880d681SAndroid Build Coastguard Worker'scanner') to break the input up into "tokens". Each token returned by
164*9880d681SAndroid Build Coastguard Workerthe lexer includes a token code and potentially some metadata (e.g. the
165*9880d681SAndroid Build Coastguard Workernumeric value of a number). First, we define the possibilities:
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard Worker    // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
170*9880d681SAndroid Build Coastguard Worker    // of these for known things.
171*9880d681SAndroid Build Coastguard Worker    enum Token {
172*9880d681SAndroid Build Coastguard Worker      tok_eof = -1,
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard Worker      // commands
175*9880d681SAndroid Build Coastguard Worker      tok_def = -2,
176*9880d681SAndroid Build Coastguard Worker      tok_extern = -3,
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker      // primary
179*9880d681SAndroid Build Coastguard Worker      tok_identifier = -4,
180*9880d681SAndroid Build Coastguard Worker      tok_number = -5,
181*9880d681SAndroid Build Coastguard Worker    };
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard Worker    static std::string IdentifierStr; // Filled in if tok_identifier
184*9880d681SAndroid Build Coastguard Worker    static double NumVal;             // Filled in if tok_number
185*9880d681SAndroid Build Coastguard Worker
186*9880d681SAndroid Build Coastguard WorkerEach token returned by our lexer will either be one of the Token enum
187*9880d681SAndroid Build Coastguard Workervalues or it will be an 'unknown' character like '+', which is returned
188*9880d681SAndroid Build Coastguard Workeras its ASCII value. If the current token is an identifier, the
189*9880d681SAndroid Build Coastguard Worker``IdentifierStr`` global variable holds the name of the identifier. If
190*9880d681SAndroid Build Coastguard Workerthe current token is a numeric literal (like 1.0), ``NumVal`` holds its
191*9880d681SAndroid Build Coastguard Workervalue. Note that we use global variables for simplicity, this is not the
192*9880d681SAndroid Build Coastguard Workerbest choice for a real language implementation :).
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard WorkerThe actual implementation of the lexer is a single function named
195*9880d681SAndroid Build Coastguard Worker``gettok``. The ``gettok`` function is called to return the next token
196*9880d681SAndroid Build Coastguard Workerfrom standard input. Its definition starts as:
197*9880d681SAndroid Build Coastguard Worker
198*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Worker    /// gettok - Return the next token from standard input.
201*9880d681SAndroid Build Coastguard Worker    static int gettok() {
202*9880d681SAndroid Build Coastguard Worker      static int LastChar = ' ';
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker      // Skip any whitespace.
205*9880d681SAndroid Build Coastguard Worker      while (isspace(LastChar))
206*9880d681SAndroid Build Coastguard Worker        LastChar = getchar();
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker``gettok`` works by calling the C ``getchar()`` function to read
209*9880d681SAndroid Build Coastguard Workercharacters one at a time from standard input. It eats them as it
210*9880d681SAndroid Build Coastguard Workerrecognizes them and stores the last character read, but not processed,
211*9880d681SAndroid Build Coastguard Workerin LastChar. The first thing that it has to do is ignore whitespace
212*9880d681SAndroid Build Coastguard Workerbetween tokens. This is accomplished with the loop above.
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard WorkerThe next thing ``gettok`` needs to do is recognize identifiers and
215*9880d681SAndroid Build Coastguard Workerspecific keywords like "def". Kaleidoscope does this with this simple
216*9880d681SAndroid Build Coastguard Workerloop:
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
219*9880d681SAndroid Build Coastguard Worker
220*9880d681SAndroid Build Coastguard Worker      if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
221*9880d681SAndroid Build Coastguard Worker        IdentifierStr = LastChar;
222*9880d681SAndroid Build Coastguard Worker        while (isalnum((LastChar = getchar())))
223*9880d681SAndroid Build Coastguard Worker          IdentifierStr += LastChar;
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard Worker        if (IdentifierStr == "def")
226*9880d681SAndroid Build Coastguard Worker          return tok_def;
227*9880d681SAndroid Build Coastguard Worker        if (IdentifierStr == "extern")
228*9880d681SAndroid Build Coastguard Worker          return tok_extern;
229*9880d681SAndroid Build Coastguard Worker        return tok_identifier;
230*9880d681SAndroid Build Coastguard Worker      }
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard WorkerNote that this code sets the '``IdentifierStr``' global whenever it
233*9880d681SAndroid Build Coastguard Workerlexes an identifier. Also, since language keywords are matched by the
234*9880d681SAndroid Build Coastguard Workersame loop, we handle them here inline. Numeric values are similar:
235*9880d681SAndroid Build Coastguard Worker
236*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard Worker      if (isdigit(LastChar) || LastChar == '.') {   // Number: [0-9.]+
239*9880d681SAndroid Build Coastguard Worker        std::string NumStr;
240*9880d681SAndroid Build Coastguard Worker        do {
241*9880d681SAndroid Build Coastguard Worker          NumStr += LastChar;
242*9880d681SAndroid Build Coastguard Worker          LastChar = getchar();
243*9880d681SAndroid Build Coastguard Worker        } while (isdigit(LastChar) || LastChar == '.');
244*9880d681SAndroid Build Coastguard Worker
245*9880d681SAndroid Build Coastguard Worker        NumVal = strtod(NumStr.c_str(), 0);
246*9880d681SAndroid Build Coastguard Worker        return tok_number;
247*9880d681SAndroid Build Coastguard Worker      }
248*9880d681SAndroid Build Coastguard Worker
249*9880d681SAndroid Build Coastguard WorkerThis is all pretty straight-forward code for processing input. When
250*9880d681SAndroid Build Coastguard Workerreading a numeric value from input, we use the C ``strtod`` function to
251*9880d681SAndroid Build Coastguard Workerconvert it to a numeric value that we store in ``NumVal``. Note that
252*9880d681SAndroid Build Coastguard Workerthis isn't doing sufficient error checking: it will incorrectly read
253*9880d681SAndroid Build Coastguard Worker"1.23.45.67" and handle it as if you typed in "1.23". Feel free to
254*9880d681SAndroid Build Coastguard Workerextend it :). Next we handle comments:
255*9880d681SAndroid Build Coastguard Worker
256*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Worker      if (LastChar == '#') {
259*9880d681SAndroid Build Coastguard Worker        // Comment until end of line.
260*9880d681SAndroid Build Coastguard Worker        do
261*9880d681SAndroid Build Coastguard Worker          LastChar = getchar();
262*9880d681SAndroid Build Coastguard Worker        while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
263*9880d681SAndroid Build Coastguard Worker
264*9880d681SAndroid Build Coastguard Worker        if (LastChar != EOF)
265*9880d681SAndroid Build Coastguard Worker          return gettok();
266*9880d681SAndroid Build Coastguard Worker      }
267*9880d681SAndroid Build Coastguard Worker
268*9880d681SAndroid Build Coastguard WorkerWe handle comments by skipping to the end of the line and then return
269*9880d681SAndroid Build Coastguard Workerthe next token. Finally, if the input doesn't match one of the above
270*9880d681SAndroid Build Coastguard Workercases, it is either an operator character like '+' or the end of the
271*9880d681SAndroid Build Coastguard Workerfile. These are handled with this code:
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
274*9880d681SAndroid Build Coastguard Worker
275*9880d681SAndroid Build Coastguard Worker      // Check for end of file.  Don't eat the EOF.
276*9880d681SAndroid Build Coastguard Worker      if (LastChar == EOF)
277*9880d681SAndroid Build Coastguard Worker        return tok_eof;
278*9880d681SAndroid Build Coastguard Worker
279*9880d681SAndroid Build Coastguard Worker      // Otherwise, just return the character as its ascii value.
280*9880d681SAndroid Build Coastguard Worker      int ThisChar = LastChar;
281*9880d681SAndroid Build Coastguard Worker      LastChar = getchar();
282*9880d681SAndroid Build Coastguard Worker      return ThisChar;
283*9880d681SAndroid Build Coastguard Worker    }
284*9880d681SAndroid Build Coastguard Worker
285*9880d681SAndroid Build Coastguard WorkerWith this, we have the complete lexer for the basic Kaleidoscope
286*9880d681SAndroid Build Coastguard Workerlanguage (the `full code listing <LangImpl02.html#full-code-listing>`_ for the Lexer
287*9880d681SAndroid Build Coastguard Workeris available in the `next chapter <LangImpl02.html>`_ of the tutorial).
288*9880d681SAndroid Build Coastguard WorkerNext we'll `build a simple parser that uses this to build an Abstract
289*9880d681SAndroid Build Coastguard WorkerSyntax Tree <LangImpl02.html>`_. When we have that, we'll include a
290*9880d681SAndroid Build Coastguard Workerdriver so that you can use the lexer and parser together.
291*9880d681SAndroid Build Coastguard Worker
292*9880d681SAndroid Build Coastguard Worker`Next: Implementing a Parser and AST <LangImpl02.html>`_
293*9880d681SAndroid Build Coastguard Worker
294