title: Creating Simple Parser in F#
body: <p>I am currently attempting to create an extremely simple parser in F# using FsLex and FsYacc. At first, the only functionality I am trying to achieve is allowing the program to take in a string that represents addition of integers and output a result. For example, I would want the parser to be able to take in "5 + 2" and output the string "7". I am only interested in string arguments and outputs because I would like to import the parser into Excel using Excel DNA once I extend the functionality to support more operations. However, I am currently struggling to get even this simple integer addition to work correctly. </p>
  
  <p>My lexer.fsl file looks like:</p>
  
  <pre><code>{
  module lexer
  open System
  open Microsoft.FSharp.Text.Lexing
  open Parser
  
  let lexeme = LexBuffer&lt;_&gt;.LexemeString
  
  let ops = ["+", PLUS;] |&gt; Map.ofList
  }
  
  let digit = ['0'-'9']
  let operator = "+"
  let integ = digit+
  
  rule lang = parse
      | integ 
      {INT(Int32.Parse(lexeme lexbuf))}
      | operator 
      {ops.[lexeme lexbuf]}
  </code></pre>
  
  <p>My parser.fsy file looks like:</p>
  
  <pre><code>%{
  open Program
  %}
  
  %token &lt;int&gt;INT
  %token PLUS
  
  %start input
  %type &lt;int&gt; input
  
  %%
  
  input: 
      exp {$1}
  ;
  
  exp: 
      | INT { $1 }
      | exp exp PLUS { $1 + $2 }
  ;
  </code></pre>
  
  <p>Additionally, I have a Program.fs file that acts like an (extremely small) AST:</p>
  
  <pre><code>module Program
  
  type value = 
      | Int of int
  
  type op = Plus
  </code></pre>
  
  <p>Finally, I have the file Main.fs that is supposed to test out the functionality of the interpreter (as well as import the function into Excel). </p>
  
  <pre><code>module Main
  
  open ExcelDna.Integration
  open System
  open Program
  open Parser
  
  [&lt;ExcelFunction(Description = "")&gt;]
      let main () =
      let x = "5 + 2"
      let lexbuf = Microsoft.FSharp.Text.Lexing.LexBuffer&lt;_&gt;.FromString x
      let y = input lexer.lang lexbuf
      y
  </code></pre>
  
  <p>However, when I run this function, the parser doesn't work at all. When I build the project, the parser.fs and lexer.fs files are created correctly. I feel that there is something simple I am missing, but I have no idea how to make this function correctly.</p>