Use FsLex and FsYacc to make a parser in F#

Use FsLex and FsYacc to make a parser in F#

  • Comments 8

Update: The template I posted yesterday was for PowerPack 1.9.9.9 which is not the most recent version available. I have since updated the template to target PowerPack 2.0.0.0. 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).

LexYacc

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.

Leave a Comment
  • Please add 2 and 6 and type the answer here:
  • Post
  • 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 1.9.9.9 of the PowerPack, so if you download the latest Powerpack as I did its probably not going to work...

  • Hi,

    The VS2010 template does not work for F# PowerPack 2.0.0.0.

    Is there a way to fix it?

    Thanks in advance.

  • Hi,

    The VS2010 template does not work for F# PowerPack 2.0.0.0.

    Is there a way to fix it?

    Thanks in advance.

  • Hi,

    The VS2010 template does not work for F# PowerPack 2.0.0.0.

    Is there a way to fix it?

    Thanks in advance.

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

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

    C:\Users\<yourname>\AppData\Local\Microsoft\VisualStudio\10.0\Extensions\Jomo Fisher\F# Parsed Language Starter\1.2\Language\Language.zip

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

         <HintPath>$(MSBuildExtensionsPath32)\..\FSharpPowerPack-2.0.0.0\bin\FSharp.PowerPack.dll</HintPath>

    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:

       <PromptForSaveOnCreation>true</PromptForSaveOnCreation>

  • 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.

  • 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:

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

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

       | Term { $1 }

    Term:

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

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

       | Factor { $1 }

    Factor:

       | 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)

Page 1 of 1 (8 items)