MSBuild tasks for Lex and Yacc

While I am thrilled about all the new features we've put into the F# CTP, perhaps the thing I'm most excited about are the MSBuild tasks for Lex and Yacc. You heard that right. If you want to use fslex.exe of fsyacc.exe as part of your project, you can now integrate them into your project via MSBuild. And the thing that makes this feature even better is the fact that I wrote the build tasks. (That’s right, testers do write features at Microsoft HUZZAH!)

To wire in fslex and fsyacc files, simply add the following snippet to your .FSPROJ file:

  <!-- Generate the lexer and parser -->
FsYacc Include="Parser.fsy" />
FsLex Include="Lexer.fsl" />


And import the F# PowerPack targets file.

<Import Project="$(MSBuildExtensionsPath)\FSharp\1.0\FSharp.PowerPack.Targets" />

And auto-magically Lex and Yacc will be kicked off as part of your build. Even better, is that since they are integrated into MSBuild the files won’t be regenerated unless the lexer or parser has changed. Which for sufficiently advanced parsers is a dramatic time savings.

To demonstrate this I’ve updated my MegaCalc project to the new F# Project System. Which if you recall is a simple four-function calculator written in F#.


Disclaimer: All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. So in other words, if the attached source files cause you any grief whatsoever you can’t blame me or Microsoft.

Comments (5)

  1. Brennan says:

    Awesome.  So helpful.  Thanks. 🙂

  2. Andrew says:

    Love the build task.

    You should note that fsc.exe needs the Lexer.fs before Program.fs, with the Sept CTP.  At least as coming from C#, this was totally confusing.

    When does the VS Item Template go in?  😀

  3. Roland says:

    Is it also possible to set the –light-off flag of fsyacc and fslex via the build targets?


  4. Ike Butler says:

    Hello. I have a bit of a problem and was wondering if anyone could offer a possible resolution and/or lead towards finding a solution to my problem.

    I’m working with project templates via VS2008 Professional Team.

    I’m able to easily add the neccessary references to my templated project, but where I run into problems is trying to rename the default class.cs file associated with making a new project.

    Could anyone tell me how to take my formatted (templated Class1.cs) file and rename it to something else?  

    ie. Class.cs -> MyClassName.cs within the template.

    I used this template file:

    <VSTemplate Version="2.0.0" xmlns="; Type="Project">


      <Name>DMIBusiness Objects</Name>

      <Description>Extended DMI Business Object Assembly</Description>






      <DefaultName>DMIBusiness Objects</DefaultName>







      <Project TargetFileName="DMIBusiness Objects.csproj"

               File="DMIBusiness Objects.csproj"


        <Folder Name="Properties" TargetFolderName="Properties">

          <ProjectItem ReplaceParameters="true"






    I even tried changing it within the Project.TargetFileName to:



Skip to main content