<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Random coding knowledge</title>
  <subtitle></subtitle>
  <link href="https://thanos.codes/feed.xml" rel="self"/>
  <link href="https://thanos.codes"/>
  <updated>2020-06-08T01:00:00+01:00</updated>
  <id>https://thanos.codes</id>
  <author>
    <name>Thanos Papathanasiou</name>
    <email>thanospapathanasiou@hotmail.com</email>
  </author>
  
  <entry>
    <title>Implementing conditional resolution of dependencies with Castle.Windsor</title>
    <link href="https://thanos.codes/blog/implementing-conditional-resolution-of-dependencies-with-castle-windsor/"/>
    <updated>2020-05-27T01:00:00+01:00</updated>
    <id>https://thanos.codes/blog/implementing-conditional-resolution-of-dependencies-with-castle-windsor/</id>
    <content type="html">&lt;p&gt;Let’s say you are given the task of creating a console program to calculate the given element of the Fibonacci sequence.&lt;/p&gt;
&lt;p&gt;Great, that’s easy enough. You can just use your favorite tools: VS Code and dotnet core.&lt;/p&gt;
&lt;p&gt;You just open up your terminal, cd into the directory of your projects and type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;
mkdir DependencyInjectionPlayground
cd DependencyInjectionPlayground
dotnet new console
code .
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After a little thinking you write the code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;
using System;

namespace DependencyInjectionPlayground
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = Convert.ToInt32(args[0]);
            var fibonacciCalculator = new FibonacciCalculator();
            var output = fibonacciCalculator.Calculate(n);
            Console.WriteLine($&amp;quot;The {n}-th element of the fibonacci sequence is {output}&amp;quot;);
        }
    }
 
    public class FibonacciCalculator
    {
        //Calculates the n-th element of the fibonacci sequence
        public long Calculate(int n)
        {
            if (n &amp;lt;= 2) return 1;
            return Calculate(n-1) + Calculate(n-2);
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then click save and go back to the console.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;
dotnet build
dotnet run 5

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And you get the output:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;
&amp;quot;The 5-th element of the fibonacci sequence is 5&amp;quot;

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Job well done.&lt;/p&gt;
&lt;p&gt;That is until you realize that for large numbers it gets exponentially slower because of the nature of this recursive algorithm.&lt;/p&gt;
&lt;p&gt;The damn thing has a time complexity of O(2^n)&lt;/p&gt;
&lt;p&gt;As Rocky would say:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://thanos.codes/blog/implementing-conditional-resolution-of-dependencies-with-castle-windsor/images/rocky.png&quot; alt=&quot;Motivational Rocky!&quot;&gt;&lt;/p&gt;
&lt;p&gt;So, after you get your inspirational speech you go online and after some googling and Wikipedia reading you come across the article on Memoization.&lt;/p&gt;
&lt;p&gt;You update your algorithm to take advantage of your newfound knowledge:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;
public class MemoizedFibonacciCalculator
{
    private long[] memoized;
    private int current;

    public MemoizedFibonacciCalculator()
    {
        memoized = new long[32];
        memoized[0] = 0;
        memoized[1] = 1;
        memoized[2] = 1;
        current = 2;
    }

    //Calculates the n-th element of the fibonacci sequence
    public long Calculate(int n)
    {
        //if we have calculated the n-th element already, return it
        if (n &amp;lt;= current) return memoized[n];

        //if we haven&#39;t then resize the array to hold the new values.
        Array.Resize(ref memoized, n + 1);

        //Calculate all the elements between the currently calculated element
        //and the n-th element and then save them.
        for (int i = current + 1; i &amp;lt;= n; i++)
        {
            memoized[i] = memoized[i - 2] + memoized[i - 1];
        }

        current = n;
        return memoized[current];
    }
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It seems that using our original recursive algorithm sacrifices cpu speed versus the updated algorithm using memoization which sacrifices ram.&lt;/p&gt;
&lt;p&gt;We don’t want to limit our users to one or the other as we don’t know where they are going to run this code.&lt;/p&gt;
&lt;p&gt;It seems we’ll have to provide them the option to choose the algorithm on runtime.&lt;/p&gt;
&lt;p&gt;This is where dependency injection comes into play. Switch to your trusty console and type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;
dotnet add package Castle.Windsor
dotnet restore
dotnet build

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After some reading of the Castle Windsor documentation you come across this interface: IHandlerSelector&lt;/p&gt;
&lt;p&gt;A “go to definition” returns this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
    Summary:

    Implementors of this interface allow to extend the way the container perform component resolution based on some application specific business logic.

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Perfect.&lt;/p&gt;
&lt;p&gt;Let’s write a generic handler selector class that would allow us to apply basic selection criteria to the requested implementations:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;
public class GenericHandlerSelector : IHandlerSelector
{
    private readonly Func&amp;lt;Type, bool&amp;gt; filter;
    private readonly Func&amp;lt;IHandler, bool&amp;gt; predicate;

    public GenericHandlerSelector(Func&amp;lt;Type, bool&amp;gt; filter, Func&amp;lt;IHandler, bool&amp;gt; predicate)
    {
        this.filter = filter;
        this.predicate = predicate;
    }

    public bool HasOpinionAbout(string key, Type service)
    {
        return filter(service);
    }

    public IHandler SelectHandler(string key, Type service, IHandler[] handlers)
    {
        return handlers.First(predicate);
    }
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This basically allows the bootstrap container code to apply logic to what implementation of an interface the container is going to use.&lt;/p&gt;
&lt;p&gt;The code finally looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csharp&quot;&gt;
using System;
using System.Linq;
using Castle.Windsor;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;

namespace DependencyInjectionPlayground
{
    class Program
    {
        static void Main(string[] args)
        {
            bool useMemoization = Convert.ToBoolean(args[1]);
            var container = BootstrapContainer(useMemoization);

            var n = Convert.ToInt32(args[0]);
            var fibonacciCalculator = container.Resolve&amp;lt;IFibonacciCalculator&amp;gt;();

            var output = fibonacciCalculator.Calculate(n);
            Console.WriteLine($&amp;quot;The {n}-th element of the fibonacci sequence is {output}&amp;quot;);

        }

        static IWindsorContainer BootstrapContainer(bool useMemoization)
        {
            IWindsorContainer container = new WindsorContainer();

            container.Register(Component.For&amp;lt;IFibonacciCalculator&amp;gt;().ImplementedBy&amp;lt;FibonacciCalculator&amp;gt;());
            container.Register(Component.For&amp;lt;IFibonacciCalculator&amp;gt;().ImplementedBy&amp;lt;MemoizedFibonacciCalculator&amp;gt;());

            // filter -&amp;gt; this handler only affects implementations of IFibonacciCalculator
            // handler -&amp;gt; depending on input, choose the implementation we want
            var memoizationHandler = new GenericHandlerSelector(
                (filter) =&amp;gt; filter == typeof(IFibonacciCalculator),
                (handler) =&amp;gt; useMemoization
                    ? handler.ComponentModel.Implementation == typeof(MemoizedFibonacciCalculator)
                    : handler.ComponentModel.Implementation == typeof(FibonacciCalculator)
                );

            container.Kernel.AddHandlerSelector(memoizationHandler);

            return container;
        }

    }

    public class GenericHandlerSelector : IHandlerSelector
    {
        private readonly Func&amp;lt;Type, bool&amp;gt; filter;
        private readonly Func&amp;lt;IHandler, bool&amp;gt; predicate;

        public GenericHandlerSelector(Func&amp;lt;Type, bool&amp;gt; filter, Func&amp;lt;IHandler, bool&amp;gt; predicate)
        {
            this.filter = filter;
            this.predicate = predicate;
        }

        public bool HasOpinionAbout(string key, Type service)
        {
            return filter(service);
        }

        public IHandler SelectHandler(string key, Type service, IHandler[] handlers)
        {
            return handlers.First(predicate);
        }
    }

    public interface IFibonacciCalculator
    {
        //Calculates the n-th element of the fibonacci sequence
        long Calculate(int n);
    }

    public class FibonacciCalculator : IFibonacciCalculator
    {
        //Calculates the n-th element of the fibonacci sequence
        public long Calculate(int n)
        {
            if (n &amp;lt;= 2) return 1;
            return Calculate(n - 1) + Calculate(n - 2);
        }

    }

    public class MemoizedFibonacciCalculator : IFibonacciCalculator
    {
        private long[] memoized;
        private int current;

        public MemoizedFibonacciCalculator()
        {
            memoized = new long[32];
            memoized[0] = 0;
            memoized[1] = 1;
            memoized[2] = 1;
            current = 2;
        }

        //Calculates the n-th element of the fibonacci sequence
        public long Calculate(int n)
        {
            //if we have calculated the n-th element already, return it
            if (n &amp;lt;= current) return memoized[n];

            Array.Resize(ref memoized, n + 1);

            for (int i = current + 1; i &amp;lt;= n; i++)
            {
                memoized[i] = memoized[i - 2] + memoized[i - 1];
            }
            current = n;
            return memoized[current];
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we switch back to our trusty console and do a:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;
dotnet build
dotnet run 40 false
dotnet run 40 true

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The false/true is user input for the use or not of Memoization.&lt;/p&gt;
&lt;p&gt;Unless you are running this on a supercomputer, you’ll notice the difference in speed between the two algorithms.&lt;/p&gt;
&lt;p&gt;If you want to do a little bit of digging I’d suggest measuring the exact difference in the time it took to run both of these using System.Diagnostics&lt;/p&gt;
&lt;p&gt;Happy coding!&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Setting up eleventy to work with github pages</title>
    <link href="https://thanos.codes/blog/setting-up-eleventy-to-work-with-github-pages/"/>
    <updated>2020-05-31T01:00:00+01:00</updated>
    <id>https://thanos.codes/blog/setting-up-eleventy-to-work-with-github-pages/</id>
    <content type="html">&lt;p&gt;Ever since github started offering their GitHub Pages to use as a personal website for its users, I&#39;ve been interested in using them. I&#39;ve even tried setting mine up a couple of times but I always found that configuring jekyll and finding a proper theme that would play well with whatever versions of rubygems I already had was a pain.&lt;/p&gt;
&lt;p&gt;Furthermore, there was no reason in my mind to have yet another development environment (Ruby&#39;s) installed on my local machine and kept up to date so that I could write a blog post once in a blue moon.&lt;/p&gt;
&lt;p&gt;So, in the end, I never bothered with actually using my github page for quite some time.&lt;/p&gt;
&lt;p&gt;I do tend prioritize things lower if there is significant friction when doing something with them and blogging with the github pages had quite a bit of friction involved.&lt;/p&gt;
&lt;p&gt;It had been going like that for a while until a few days ago when I came across a youtube video about &lt;a href=&quot;https://www.youtube.com/watch?v=j8mJrhhdHWc&quot;&gt;setting up a static site with eleventy.&lt;/a&gt; The tool seemed to get out of your way as much as possible and allowed you to simply write your markdown files to be processed into pages.&lt;/p&gt;
&lt;p&gt;Within a few minutes I was convinced. I started yet again the process of setting up my github page and working with eleventy to create a decent static page as output.&lt;/p&gt;
&lt;p&gt;Everything was working just fine and dandy until the time came to actually have github host the pages that eleventy outputs. There were alot of websites documenting how to do this process but I found them to be either too complicated (setup github actions, CI servers, etc) or the result would not be clean enough (have the eleventy static site output commited to the same repository as the code).&lt;/p&gt;
&lt;p&gt;In the end, there was this &lt;a href=&quot;https://tomhiskey.co.uk/posts/deploying-eleventy-to-github-pages-one-way/&quot;&gt;blogpost by Tom Hiskey&lt;/a&gt; that mentioned in the TLDR:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;If you&#39;re trying to deploy an Eleventy site to GitHub Pages, one option is to build it locally and fiddle about with settings.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;But, as I already mentioned, I don&#39;t want to mix my static site (the output) with the code that is responsible for that (the input)&lt;/p&gt;
&lt;p&gt;So I did the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a base directory, let&#39;s call it &amp;quot;blog&amp;quot;&lt;/li&gt;
&lt;li&gt;Inside that directory we&#39;ll clone the github page, &lt;a href=&quot;https://github.com/ThanosPapathanasiou/thanospapathanasiou.github.io&quot;&gt;thanospapathanasiou.github.io&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;And we will also initialize another git folder called &lt;a href=&quot;https://github.com/ThanosPapathanasiou/personal-blog&quot;&gt;personal-blog&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Our folder structure will end up being like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;./blog/
./blog/thanospapathanasiou.github.io (git repository 1)
./blog/personal-blog/ (git repository 2)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this folder structure, you can start writing your blogposts in the personal-blog folder and have eleventy running and serving them locally for any tweeks you might need to do before you commit them.&lt;/p&gt;
&lt;p&gt;Be careful though. If you want to keep things clean and separated like I do then you don&#39;t want to commit the static html output in the personal-blog folder, you want to commit them to your github page.&lt;/p&gt;
&lt;p&gt;The problem is that copying and pasting the changes from codingblog/_site/* to thanospapathanasiou.github.io/* can be such a pain.&lt;/p&gt;
&lt;p&gt;Thankfully the good ol&#39; Makefile comes to the resque! This is the &lt;a href=&quot;https://github.com/ThanosPapathanasiou/personal-blog/blob/master/Makefile&quot;&gt;one&lt;/a&gt; I wrote for my purposes.&lt;/p&gt;
&lt;p&gt;You basically need three things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Clean the codingblog/_site/* so you start from an empty base.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;rm -rf _site/*
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Build your eleventy static site into codingblog/_site/*&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;npx @11ty/eleventy eleventy --input=site --output=_site
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;
&lt;p&gt;finally, we need to copy that static file into our github pages folder.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;First we need to clean up the github pages folder&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code&gt;find ../thanospapathanasiou.github.io/ -mindepth 1 ! &#39;(&#39; -path &#39;../thanospapathanasiou.github.io/.git/*&#39; -or -name &#39;.git&#39; -or -name &#39;CNAME&#39; &#39;)&#39; -delete
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Copy the output to the github page folder&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code&gt;cp -a ./_site/. ../thanospapathanasiou.github.io/
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Step 3.1 is a bit complicated so let&#39;s break it down a bit.&lt;/p&gt;
&lt;p&gt;Basically what we need to do is find all the items in the github page directory that are &lt;strong&gt;NOT&lt;/strong&gt; in its .git folder, are not the actual .git folder itself, or the CNAME file (that&#39;s the file that has the custom domain name your blog will point to)&lt;/p&gt;
&lt;p&gt;Now my proccess from creating a new blogpost is quite straightforward and with a lot less friction!&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a folder with the blogpost&#39;s permalink in the &lt;code&gt;/personal-blog/site/blog&lt;/code&gt; folder.
Let&#39;s say that the name of that folder is &lt;code&gt;setting-up-eleventy-to-work-with-github-pages&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;index.md&lt;/code&gt; file and start writing&lt;/li&gt;
&lt;li&gt;Open a terminal in the &lt;code&gt;/personal-blog/&lt;/code&gt; folder and run &lt;code&gt;make run&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Have your browser navigate to &lt;code&gt;http://localhost:8080&lt;/code&gt; and watch it for anything not displaying correctly&lt;/li&gt;
&lt;li&gt;Once you are done with editing, run &lt;code&gt;make publish&lt;/code&gt; and the changes will appear in the github directory&lt;/li&gt;
&lt;li&gt;Commit and push the changes from both repositories&lt;/li&gt;
&lt;li&gt;Profit?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I&#39;ve found that the above proccess works really well for me, if you want to have a look at the repository for yourself then the link is &lt;a href=&quot;https://github.com/ThanosPapathanasiou/personal-blog&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Happy blogging!&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Using FSLexYacc, the F# lexer and parser</title>
    <link href="https://thanos.codes/blog/using-fslexyacc-the-fsharp-lexer-and-parser/"/>
    <updated>2020-06-08T01:00:00+01:00</updated>
    <id>https://thanos.codes/blog/using-fslexyacc-the-fsharp-lexer-and-parser/</id>
    <content type="html">&lt;p&gt;Lately I&#39;ve been playing around with F# and specifically with using it for lexing and parsing. I&#39;ve had to dig around a bit to get all the information needed to understand how to get a simple project up and running so I thought compiling that information would be a good idea.&lt;/p&gt;
&lt;p&gt;This is going to be a tutorial that will help you setup a simple F# console application that will read the user&#39;s input and will try to evaluate it based on the basic math syntax that we will declare.&lt;/p&gt;
&lt;p&gt;It should be able to evaluate input like:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;1 + 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(1 + -2) - (6 / 3)&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Pretty advanced stuff, I know :D&lt;/p&gt;
&lt;p&gt;If you don&#39;t feel like typing everything and just want to download the &lt;a href=&quot;https://github.com/ThanosPapathanasiou/personal-blog/tree/master/code-examples/lexing-and-parsing&quot;&gt;code&lt;/a&gt; and start tinkering then try the following:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;npm install -g github-files-fetcher
github-files-fetcher -url=&amp;quot;https://github.com/ThanosPapathanasiou/personal-blog/tree/master/code-examples/lexing-and-parsing&amp;quot;
code lexing-and-parsing
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Otherwise, here&#39;s a step by step instruction to help you get up and running.&lt;/p&gt;
&lt;h4&gt;Setting up a project to work with FsLexYacc&lt;/h4&gt;
&lt;hr&gt;
&lt;p&gt;Let&#39;s start with a brand new F# console app:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mkdir lexing-and-parsing
cd lexing-and-parsing
dotnet new console -lang=F#
dotnet run
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That should give us the default console output:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Hello world from F#
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In order to use the F# lexer and parser we&#39;ll need to add the &lt;code&gt;FsLexYacc&lt;/code&gt; nuget package as a reference. So our next step is to run:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;dotnet add package FsLexYacc
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The way lexing and parsing works in F# is by generating F# code from two files, a .fsl one that contains the rules for the lexer and a .fsy one that contains the rules for the parser.&lt;/p&gt;
&lt;p&gt;Lets create them:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;echo &#39;&#39; &amp;gt; Lexer.fsl
echo &#39;&#39; &amp;gt; Parser.fsy
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next we&#39;ll need to edit the .fsproj file and add these two lines in the &lt;code&gt;&amp;lt;PropertyGroup&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-xml&quot;&gt;    &amp;lt;FsLexToolExe&amp;gt;fslex.dll&amp;lt;/FsLexToolExe&amp;gt;
    &amp;lt;FsYaccToolExe&amp;gt;fsyacc.dll&amp;lt;/FsYaccToolExe&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and these ones in the &lt;code&gt;&amp;lt;ItemGroup&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-xml&quot;&gt;    &amp;lt;FsYacc Include=&amp;quot;Parser.fsy&amp;quot;&amp;gt;
      &amp;lt;OtherFlags&amp;gt;--module Parser&amp;lt;/OtherFlags&amp;gt;
    &amp;lt;/FsYacc&amp;gt;

    &amp;lt;FsLex Include=&amp;quot;Lexer.fsl&amp;quot;&amp;gt;
      &amp;lt;OtherFlags&amp;gt;--module Lexer --unicode&amp;lt;/OtherFlags&amp;gt;
    &amp;lt;/FsLex&amp;gt;

    &amp;lt;Compile Include=&amp;quot;Parser.fsi&amp;quot; /&amp;gt;
    &amp;lt;Compile Include=&amp;quot;Parser.fs&amp;quot; /&amp;gt;
    &amp;lt;Compile Include=&amp;quot;Lexer.fs&amp;quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What these changes do is basically tell the F# compiler that it has the fslex and fsyacc tools at it&#39;s disposal and it will have to run the transformation of the  &lt;code&gt;Lexer.fsl&lt;/code&gt; and &lt;code&gt;Parser.fsy&lt;/code&gt; files &lt;em&gt;&lt;strong&gt;before&lt;/strong&gt;&lt;/em&gt; the compilation starts. We can even specify the module name that the generated code is part of. I chose to keep it simple with Lexer and Parser.&lt;/p&gt;
&lt;p&gt;You will notice also that we are including three files (&lt;code&gt;Parser.fsi&lt;/code&gt;, &lt;code&gt;Parser.fs&lt;/code&gt; and &lt;code&gt;Lexer.fs&lt;/code&gt;) that don&#39;t exist as references in our project. These files will be generated from our .fsl and .fsy files by the lexer and parser tools as the output of the transformation I mention above.&lt;/p&gt;
&lt;p&gt;At this point our project is setup to use the lexer and parser however since we don&#39;t have anything in the &lt;code&gt;Lexer.fsl&lt;/code&gt; and &lt;code&gt;Parser.fsy&lt;/code&gt; files the compilation will fail.&lt;/p&gt;
&lt;p&gt;So lets add something!&lt;/p&gt;
&lt;h4&gt;Basic lexing&lt;/h4&gt;
&lt;hr&gt;
&lt;p&gt;Lexing is the process of taking raw text and converting it into a sequense of tokens. A token is a string with an assigned meaning.&lt;/p&gt;
&lt;p&gt;In our case we&#39;ll try to create a lexer and parser that will do something basic, evaluate simple mathematic operations. i.e. 1 + 1 should return 2, -1 + 2 = 1, (1 + 2) - (4 + 1) = -2 etc&lt;/p&gt;
&lt;p&gt;So, our lexer should identify these tokens:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Number&lt;/li&gt;
&lt;li&gt;Operator&lt;/li&gt;
&lt;li&gt;Left Parenthesis&lt;/li&gt;
&lt;li&gt;Right Parenthesis&lt;/li&gt;
&lt;li&gt;End of input&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is the finished Lexer.fsl that tokenizes according to the above rules.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-fslex&quot;&gt;{
open FSharp.Text.Lexing
open Parser

let lexeme lexbuf = LexBuffer&amp;lt;_&amp;gt;.LexemeString lexbuf
}

let digit = [&#39;0&#39;-&#39;9&#39;]
let operator = [&#39;+&#39; &#39;-&#39; &#39;*&#39; &#39;/&#39;]
let whitespace = [&#39; &#39; &#39;\t&#39;]

rule tokenize = parse
| whitespace        { tokenize lexbuf }
| [&#39;-&#39;]?digit+      { Number ( System.Int32.Parse( lexeme lexbuf ) ) }
| operator          { OP ( lexeme lexbuf ) }
| &#39;(&#39;               { LPAREN }
| &#39;)&#39;               { RPAREN }
| eof               { EOF }
| _                 { lexeme lexbuf |&amp;gt; sprintf &amp;quot;Parsing error: %s&amp;quot; |&amp;gt; failwith }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s go through it and I&#39;ll explain it bit by bit.&lt;/p&gt;
&lt;p&gt;First bit:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{
open FSharp.Text.Lexing
open Parser

let lexeme lexbuf = LexBuffer&amp;lt;_&amp;gt;.LexemeString lexbuf
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I mentioned earlier that Lexer.fsl isn&#39;t written in F# but a custom language. That is true for the rest of this file but not for the bits between the { and }&lt;/p&gt;
&lt;p&gt;Here we using F# to open the &lt;a href=&quot;http://fsprojects.github.io/FsLexYacc/reference/index.html&quot;&gt;FSharp.Text.Lexing&lt;/a&gt; namespace that we need to access the input buffer &lt;code&gt;LexBuffer&lt;/code&gt; that allows us have a look into what part is being tokenized.&lt;/p&gt;
&lt;p&gt;We are also opening our own Parser module (the one that will be autogenerated by Parser.fsy). We need that because the definition of the tokens that we want to output is there.&lt;/p&gt;
&lt;p&gt;Finally, we are declaring a small helping function that returns the matching characters of a token as a string. i.e. the characters 1, 2, 3 would be returned as &amp;quot;123&amp;quot;&lt;/p&gt;
&lt;p&gt;Second bit:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;let digit = [&#39;0&#39;-&#39;9&#39;]
let operator = [&#39;+&#39; &#39;-&#39; &#39;*&#39; &#39;/&#39;]
let whitespace = [&#39; &#39; &#39;\t&#39;]

rule tokenize = parse
| whitespace        { tokenize lexbuf }
| [&#39;-&#39;]?digit+      { lexeme lexbuf |&amp;gt; System.Int32.Parse |&amp;gt; Number  }
| operator          { lexeme lexbuf |&amp;gt; OP }
| &#39;(&#39;               { LPAREN }
| &#39;)&#39;               { RPAREN }
| eof               { EOF }
| _                 { lexeme lexbuf |&amp;gt; sprintf &amp;quot;Parsing error: %s&amp;quot; |&amp;gt; failwith }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this part the actual tokenization process happens.&lt;/p&gt;
&lt;p&gt;First, for readability, we define what a digit, operator and whitespace are.
Then we have the actual tokenization function &lt;code&gt;rule tokenize = parse&lt;/code&gt; followed by all the cases we support (with the final one &lt;code&gt;_&lt;/code&gt; handling all the undesired states by just returning an error). These cases are matching against a kind of regular expression.&lt;/p&gt;
&lt;p&gt;This is what it does&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;In case of whitespace, ignore it. (get the next token)&lt;/li&gt;
&lt;li&gt;In case of multiple digits even with a - in front then get that string ( &lt;code&gt;lexeme lexbuf&lt;/code&gt; ) then parse it into a System.Int32 and then return that int32 as a Number token. (this Number token will be defined in our Parser.fsy file)&lt;/li&gt;
&lt;li&gt;In case of a string matching any of our operators then get that string and return the OP symbol&lt;/li&gt;
&lt;li&gt;In case of a left parenthesis then return the LPAREN symbol&lt;/li&gt;
&lt;li&gt;In case of a right parenthesis then return the RPAREN symbol&lt;/li&gt;
&lt;li&gt;When we reach the end of the input given then return the EOF symbol&lt;/li&gt;
&lt;li&gt;In case we get a character that doesn&#39;t match any of the above rules then we raise an exception.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Remember: Everything between { and } is valid F# code, that&#39;s why we are able to use &lt;code&gt;|&amp;gt;&lt;/code&gt; to cast to the Number and OP symbols.&lt;/p&gt;
&lt;p&gt;As an example, if we gave
&lt;code&gt;(1 + 2) + -3&lt;/code&gt;
the symbols should be
&lt;code&gt;LPAREN NUMBER(1) OP(&amp;quot;+&amp;quot;) NUMBER(2) RPAREN OP(&amp;quot;+&amp;quot;) NUMBER(-3) EOF&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;That looks good, let&#39;s see how to do the parsing now!&lt;/p&gt;
&lt;h4&gt;Basic Parsing&lt;/h4&gt;
&lt;hr&gt;
&lt;p&gt;Parsing is the process of reading a stream of tokens and attempting to match them to a set of rules with the end result being an abstract syntax tree.&lt;/p&gt;
&lt;p&gt;In layman&#39;s terms, lexing is the process of defining the alphabet (the actual characters allowed) of your language while parsing is the process of defining the syntax (i.e. subject - verb - object)&lt;/p&gt;
&lt;p&gt;The syntax that we want to be able to understand is based upon basic math rules.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;perform an operation between two numbers: &lt;code&gt;NUMBER OP NUMBER&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;perform an operation between a number and another operation that uses parentheses: &lt;code&gt;NUMBER OP (NUMBER OP NUMBER)&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here&#39;s the end result:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-fsyacc&quot;&gt;%{
let getOp op = 
    match op with 
    | &amp;quot;+&amp;quot; -&amp;gt; ( + )
    | &amp;quot;-&amp;quot; -&amp;gt; ( - )
    | &amp;quot;/&amp;quot; -&amp;gt; ( / )
    | &amp;quot;*&amp;quot; -&amp;gt; ( * )
    | _ -&amp;gt; failwith &amp;quot;unknown operator&amp;quot;
%}

%token &amp;lt;int&amp;gt; Number
%token &amp;lt;string&amp;gt; OP
%token EOF LPAREN RPAREN
%start parse
%type &amp;lt;int&amp;gt; parse
%%

parse: expr EOF        	     { $1 }

expr: Number                 { $1 }
    | expr OP expr           { $1 |&amp;gt; (getOp $2) &amp;lt;| $3 }
    | LPAREN expr RPAREN     { $2 }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Like with the lexing above, let&#39;s go through it bit by bit.&lt;/p&gt;
&lt;p&gt;The first part consists of the following F# code.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-fsharp&quot;&gt;%{
let getOp op = 
    match op with 
    | &amp;quot;+&amp;quot; -&amp;gt; ( + )
    | &amp;quot;-&amp;quot; -&amp;gt; ( - )
    | &amp;quot;/&amp;quot; -&amp;gt; ( / )
    | &amp;quot;*&amp;quot; -&amp;gt; ( * )
    | _ -&amp;gt; failwith &amp;quot;unknown operator&amp;quot;
%}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, we are declaring an F# function that matches the operator symbols to actual F# mathematical functions and returns them. So it takes a &lt;code&gt;string&lt;/code&gt; argument as input and returns a function of type: &lt;code&gt;Int-&amp;gt;Int-&amp;gt;Int&lt;/code&gt; (i.e. it takes two &lt;code&gt;Int&lt;/code&gt; arguments and returns a result of type &lt;code&gt;Int&lt;/code&gt;)&lt;/p&gt;
&lt;p&gt;Next, we have the token definitions. Notice that the Tokens &lt;code&gt;Number&lt;/code&gt; and &lt;code&gt;OP&lt;/code&gt; have .net types.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;%token &amp;lt;int&amp;gt; Number
%token &amp;lt;string&amp;gt; OP
%token EOF LPAREN RPAREN
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Right after that we specify to the parser what rule it should use as a start and what .net type the parsing operation will return:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;%start parse
%type &amp;lt;int&amp;gt; parse
%%
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we have the actual rules. These rules declare what syntax is valid in our &lt;em&gt;language&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Let&#39;s go through them one at a time.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;parse: expr EOF        	     { $1 }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We are parsing a single expression and we expect the EOF symbol right after. The part between the { and } is a sort of substitution. The &lt;code&gt;parse&lt;/code&gt; rule expects an expression and an &lt;code&gt;EOF&lt;/code&gt; symbol and we only care about the first argument, hence $1 (i.e. try to parse the &lt;code&gt;expr&lt;/code&gt;)&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;expr: Number                 { $1 }
    | expr OP expr           { $1 |&amp;gt; (getOp $2) &amp;lt;| $3 }
    | LPAREN expr RPAREN     { $2 }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a bit more complicated. You can see that an &lt;code&gt;expr&lt;/code&gt; can be different things. The rule is also recursive.&lt;/p&gt;
&lt;p&gt;It basically states that an expression can be&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a number&lt;/li&gt;
&lt;li&gt;an expression followed by an operator symbol and another expression&lt;/li&gt;
&lt;li&gt;a left parenthesis symbol followed by an expression followed by a right parenthesis symbol.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can also notice that in the second case, we are calling the &lt;code&gt;getOp&lt;/code&gt; function we declared earlier instead of just propagating the operator symbol. So in the generated code the &lt;code&gt;+&lt;/code&gt; operator will not be a simple &lt;code&gt;string&lt;/code&gt; type variable but an &lt;code&gt;Int-&amp;gt;Int-&amp;gt;Int&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Let&#39;s try and use the input from the lexing example.&lt;/p&gt;
&lt;p&gt;The initial input was&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(1 + 2) + -3
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and the symbols we got were:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;LPAREN NUMBER(1) OP(&amp;quot;+&amp;quot;) NUMBER(2) RPAREN OP(&amp;quot;+&amp;quot;) NUMBER(-3) EOF
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The symbols are matched against the &lt;code&gt;parse&lt;/code&gt; rule and they give us this abstract syntax tree.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;                   expr op expr
                  /     |      \
                 /    OP(&amp;quot;+&amp;quot;)   NUMBER(-3)
                /            
       LPAREN expr RPAREN    
               |                
          expr op expr         
         /     |      \         
NUMBER(1)    OP(&amp;quot;+&amp;quot;)   NUMBER(2) 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That would basically be translated and executed like this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;NUMBER(1) OP(&amp;quot;+&amp;quot;) NUMBER(2)&lt;/code&gt; translates to &lt;code&gt;1 |&amp;gt; (+) &amp;lt;| 2&lt;/code&gt; and results in 3&lt;/li&gt;
&lt;li&gt;the evaluation of that expression becomes the first argument for the next expression&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NUMBER(3) OP(&amp;quot;+&amp;quot;) NUMBER(-3)&lt;/code&gt; translates to &lt;code&gt;3 |&amp;gt; (+) &amp;lt;| -3&lt;/code&gt; and results in 0&lt;/li&gt;
&lt;li&gt;the evaluation of that expression becomes the value that &lt;code&gt;parse&lt;/code&gt; returns.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Tying it all together&lt;/h4&gt;
&lt;hr&gt;
&lt;p&gt;We have the lexer and parser all ready. If&#39;s finally the time to use them!&lt;/p&gt;
&lt;p&gt;Open &lt;code&gt;Program.fs&lt;/code&gt; and replace the code with this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-fsharp&quot;&gt;open System
open FSharp.Text.Lexing
open Lexer
open Parser

let evaluate (input:string) =
  let lexbuf = LexBuffer&amp;lt;char&amp;gt;.FromString input
  let output = Parser.parse Lexer.tokenize lexbuf
  string output

[&amp;lt;EntryPoint&amp;gt;]
let main argv =

    printfn &amp;quot;Press Ctrl+c to Exit&amp;quot;

    while true do
        printf &amp;quot;Evaluate &amp;gt; &amp;quot;
        let input = Console.ReadLine()
        try
            let result = evaluate input
            printfn &amp;quot;%s&amp;quot; result
        with ex -&amp;gt; printfn &amp;quot;%s&amp;quot; (ex.ToString())

    0 // return an integer exit code
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a very simple &lt;code&gt;main&lt;/code&gt; function that will continue to loop until the user closes the program. It just takes the user&#39;s input and calls the &lt;code&gt;evaluate&lt;/code&gt; function declared in the beginning of the program.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;evaluate&lt;/code&gt; function does a couple interesting things.&lt;/p&gt;
&lt;p&gt;First, it creates a lex buffer from the input string given by the user: &lt;code&gt;let lexbuf = LexBuffer&amp;lt;char&amp;gt;.FromString input&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Next, it uses that buffer and the &lt;code&gt;Lexer.tokenize&lt;/code&gt; function we declared in the lexer as input to the &lt;code&gt;Parser.Parse&lt;/code&gt; function we declared in our parser : &lt;code&gt;let output = Parser.parse Lexer.tokenize lexbuf&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Finally, it converts the output from &lt;code&gt;int&lt;/code&gt; to &lt;code&gt;string&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;That&#39;s it. Now we can open our terminal and type &lt;code&gt;dotnet run&lt;/code&gt; and we should be greeted with:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;Press Ctrl+c to Exit
Evaluate &amp;gt; 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Try entering: &lt;code&gt;(1 + 2) + -3&lt;/code&gt; and see if it will evaluate to &lt;code&gt;0&lt;/code&gt;. Try any number of complicated expressions and see if the result matches what you expect. Have a look at what happens if you use illegal characters.&lt;/p&gt;
&lt;p&gt;Try replacing the &lt;code&gt;expr&lt;/code&gt; with this equivalent:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;expr: Number                 { $1 }
    | OP expr expr           { (getOp $1) $2 $3 }
    | LPAREN expr RPAREN     { $2 }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After we recompile, suddenly our parser will be expecting that we use &lt;a href=&quot;https://en.wikipedia.org/wiki/Polish_notation&quot;&gt;polish notation!&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you want to understand this a bit better I would encourage you to start tinkering with the parser. Try coming up with your own rules!&lt;/p&gt;
&lt;p&gt;As always,&lt;/p&gt;
&lt;p&gt;Happy coding!&lt;/p&gt;
&lt;p&gt;&lt;code&gt; &lt;/code&gt;&lt;br&gt;
&lt;code&gt; &lt;/code&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h6&gt;Some interesting sites to have a look&lt;/h6&gt;
&lt;p&gt;If you want to have a look at the final project, instead of typing it you can get it &lt;a href=&quot;https://github.com/ThanosPapathanasiou/personal-blog/tree/master/code-examples/lexing-and-parsing&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you want to get into parsing and lexing and expand your understanding a bit more you can have a look at these sites:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikibooks.org/wiki/F_Sharp_Programming/Lexing_and_Parsing&quot;&gt;F# programming wikibooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html#s:lexyacc-example&quot;&gt;OCaml&#39;s lexyacc examples&lt;/a&gt; (F#&#39;s lexyacc is basically identical to the OCaml one)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you want to understand a bit more about the abstract syntax trees then you can read wikipedia&#39;s entry on &lt;a href=&quot;https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form&quot;&gt;Backus-Naur form&lt;/a&gt;&lt;/p&gt;
</content>
  </entry>
</feed>