Use FsLex and FsYacc to make a parser in F#

Update: The template I posted yesterday was for PowerPack which is not the most recent version available. I have since updated the template to target PowerPack If you installed the old template you may need to uninstall at Tools\Extension Manager…


Jomo Fisher—F# is excellent for parsing text and creating new languages. The F# PowerPack contains two tools—FsLex.exe and FsYacc.exe—to create powerful lexers and parsers. However, it isn’t so easy to configure a Visual Studio project correctly to use these tools. To help with this, I made a project template that you can use to get started quickly. Just follow these quick steps:

1)      Download the F# PowerPack from here.

2)      In VS2010, go to File\New Project. Select ‘Online Templates’ and search for ‘F# Parsed’ (or my name).


The starter project implements a simple calculator language that you can grow into a more sophisticated Domain Specific Language or text processing application.

Credit goes to Matt Valerio and Brian McNamara for figuring out the details and to Don Syme for making the calculator language that this is based on. Bugs and omissions are probably my fault. Please let me know what you think, and whether you have any issues or spot any bugs that I should address.

This posting is provided “AS IS” with no warranties, and confers no rights.

Comments (8)

  1. Phil says:

    I'd really like this, as I struggled to get Don's calculator sample from Chapter 16 of Expert F# running on VS2010. Unfortunately the template appears to be tied to version of the PowerPack, so if you download the latest Powerpack as I did its probably not going to work…

  2. jarana says:


    The VS2010 template does not work for F# PowerPack

    Is there a way to fix it?

    Thanks in advance.

  3. jarana says:


    The VS2010 template does not work for F# PowerPack

    Is there a way to fix it?

    Thanks in advance.

  4. jarana says:


    The VS2010 template does not work for F# PowerPack

    Is there a way to fix it?

    Thanks in advance.

  5. pblasucci says:

    Nice work. Although the extension seems to fail if one uses of the power pack; it complains it can not find the version.

  6. jhugard says:

    To update the template to work properly with PowerPack, find the following Zip file:

    C:Users<yourname>AppDataLocalMicrosoftVisualStudio10.0ExtensionsJomo FisherF# Parsed Language

    Unpack the zip, and edit "Language.fsproj" find the line with "FSharpPowerPack-" and replace with the following, then repack the zip.


    Also, if you have unchecked "Save new projects when created" via "Tools|Options", then also edit "Language.vstemplate" to add the following line at the end of the <TemplateData> section:


  7. jhugard says:

    Just noticed that you updated the template.  However, it appears that the version downloaded from within VS2010 is still the old version – even deleting the AppData directory did not help.

    When you fix this, would you mind also updating the .vstemplate to enable PromptForSaveOnCreation, so that the template works when "Save new projects on creation" is turned off?

    Finally, make sure you don't hard code the path to Program Files, since it won't work if the root drive is not C:, and won't work on an x64 install (since PowerPack is installed into "Program Files(x86)"… Relative off of $(MSBuildExtensionsPath32) worked just fine for me.

  8. Nathan Phillips says:

    Thanks for the starter – I'll be using this to quickly get started building my parser. The sample code is over-complicated for what it does though. Exactly the same effect (including operator precedence) is achieved with a single expression discriminator.

    From Ast.fs:

    type Expr =

       | Float of Double

       | Integer of Int32

       | Times of Expr * Expr

       | Divide of Expr * Expr

       | Plus of Expr * Expr

       | Minus of Expr * Expr

    From parser.fsy:


       | Expr PLUS Term { Plus($1, $3) }

       | Expr MINUS Term { Minus($1, $3) }

       | Term { $1 }


       | Term ASTER Factor { Times($1, $3) }

       | Term SLASH Factor { Divide($1, $3) }

       | Factor { $1 }


       | FLOAT { Float($1) }

       | INT32 { Integer($1) }

       | LPAREN Expr RPAREN { $2 }

    From Program.fs:

    let rec evalExpr expr =

       match expr with

       | Float x   -> x

       | Integer x -> float x

       | Times (term, fact)  -> (evalExpr term) * (evalExpr fact)

       | Divide (term, fact) -> (evalExpr term) / (evalExpr fact)

       | Plus (expr, term)  -> (evalExpr expr) + (evalExpr term)

       | Minus (expr, term) -> (evalExpr expr) – (evalExpr term)