<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LEXER &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/lexer/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Wed, 19 Jun 2019 17:50:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.5</generator>
	<item>
		<title>Compilers 101 &#8211; Overview and Lexer</title>
		<link>https://blog.xojo.com/2017/12/06/compilers-101-overview-and-lexer/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Wed, 06 Dec 2017 09:00:46 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[LEXER]]></category>
		<category><![CDATA[LLVM]]></category>
		<guid isPermaLink="false">http://blog.xojo.com/?p=2776</guid>

					<description><![CDATA[High-level posts with the goal giving you a basic understanding of the components of a compiler and how they all work together to create a native app.]]></description>
										<content:encoded><![CDATA[<p>At <a href="https://xojo.com/xdc/">XDC</a> 2016 there was a lot of interest in our <a href="https://www.xojo.com/store/#conference">Compiler session</a> and <a href="https://llvm.org">LLVM</a>. I’ve summarized a bit about LLVM in an <a href="https://blog.xojo.com/2017/12/04/llvm-everywhere/">earlier post</a>, but to take things further, we put together this series of blog posts on compilers.</p>
<p>These will all be at a high-level. None of these posts are going to teach you how to write a compiler. The goal of these posts is for you to have a basic understanding of the components of a compiler and how they all work together to create a native app.</p>
<p><span id="more-2776"></span></p>
<h2>Compiler Components</h2>
<p>A compiler is a complicated thing and consists of many components. In general the compiler is divided into two major parts: the front end and the back end. In turn, those two parts have their own components.</p>
<p>For the purposes of these posts, this is how we will be covering the components of the compiler:</p>
<h3>Front End</h3>
<p>The front end is responsible for taking the source code and converting it to a format that the back end can then use to generate binary code that can run on the target CPU architecture. The front end has these components:</p>
<ul>
<li>Lexer</li>
<li><a href="https://blog.xojo.com/2017/12/08/compilers-102-parser/">Parser</a></li>
<li><a href="https://blog.xojo.com/2018/01/22/compilers-103-semantic-analyzer/">Semantic Analyzer</a></li>
<li><a href="https://blog.xojo.com/2018/01/24/compilers-104-ir-generation/">IR (intermediate representation) Generator</a></li>
</ul>
<h3>Back End</h3>
<p>The back end takes the IR, optionally optimizes it and then generates a binary (machine code) file that can be run on the target CPU architecture. These are the components of the back end:</p>
<ul>
<li><a href="https://blog.xojo.com/2018/01/31/compilers-106-optimizer/">Optimizer</a></li>
<li><a href="https://blog.xojo.com/2018/03/26/compilers-108-code-generation/">Code Generation</a></li>
<li><a href="https://blog.xojo.com/2018/04/02/compilers-109-linking-and-wrap-up/">Linker</a></li>
</ul>
<p>Each of these steps processes things to get it a little further along for the next step to handle.</p>
<p>The linker is not technically part of the compiler but is often considered part of the compile process.</p>
<h2>Lexer</h2>
<p>The lexer turns source code into a stream of tokens. This term is actually a shortened version of “<a href="https://en.wikipedia.org/wiki/Lexical_analysis">lexical analysis</a>”. A token is essentially a representation of each item in the code at a simple level.</p>
<p>By way of example, here is a line of source code that does a simple calculation:</p>
<pre>sum = 3.14 + 2 * 4</pre>
<p>To see how a lexer works, let’s walk through how it would tokenize the above calculation, scanning it from left-to-right and tracking its type, value and position in the source code (which helps with more precise reporting of errors):</p>
<ol>
<li>The first token it finds is “sum”
<ol>
<li>type: identifier</li>
<li>value: sum</li>
<li>start: 0</li>
<li>length: 3</li>
</ol>
</li>
<li>Token: =
<ol>
<li>type: equals or assigns</li>
<li>value: n/a</li>
<li>start: 4</li>
<li>length: 1</li>
</ol>
</li>
<li>Token: 3.14
<ol>
<li>type: double</li>
<li> value: 3.14</li>
<li> start: 6</li>
<li>length: 4</li>
</ol>
</li>
<li>Token: +
<ol>
<li>type: plus</li>
<li>value: n/a</li>
<li>start: 11</li>
<li>length: 1</li>
</ol>
</li>
<li>Token: 2
<ol>
<li>type: integer</li>
<li>value: 2</li>
<li>start: 15</li>
<li>length: 1</li>
</ol>
</li>
<li>Token: *
<ol>
<li>type: multiply</li>
<li>value: n/a</li>
<li>start: 15</li>
<li>length: 1</li>
</ol>
</li>
<li>Token: 4
<ol>
<li>type: integer</li>
<li>value: 4</li>
<li>start: 17</li>
<li>length: 1</li>
</ol>
</li>
</ol>
<p>As you can see, white space and comments are ignored. So after processing that single line of code there are 7 tokens that are handed off to the next part of the compiler, which is the Parser. The <a href="https://blog.xojo.com/2017/12/08/compilers-102-parser/">Parser</a> is covered in the next post. Stay tuned!</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
