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 -->
<ItemGroup>
<FsYacc Include="Parser.fsy" />
<FsLex Include="Lexer.fsl" />
</ItemGroup>
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
Anonymous
September 09, 2008
PingBack from http://www.easycoded.com/msbuild-tasks-for-lex-and-yacc/Anonymous
September 09, 2008
Awesome. So helpful. Thanks. :)Anonymous
September 21, 2008
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? :DAnonymous
October 15, 2008
Is it also possible to set the --light-off flag of fsyacc and fslex via the build targets? thxAnonymous
February 18, 2009
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="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project"> <TemplateData> <Name>DMIBusiness Objects</Name> <Description>Extended DMI Business Object Assembly</Description> <ProjectType>CSharp</ProjectType> <ProjectSubType> </ProjectSubType> <SortOrder>1000</SortOrder> <CreateNewFolder>true</CreateNewFolder> <DefaultName>DMIBusiness Objects</DefaultName> <ProvideDefaultName>true</ProvideDefaultName> <LocationField>Enabled</LocationField> <EnableLocationBrowseButton>true</EnableLocationBrowseButton> <Icon>__TemplateIcon.ico</Icon> </TemplateData> <TemplateContent> <Project TargetFileName="DMIBusiness Objects.csproj" File="DMIBusiness Objects.csproj" ReplaceParameters="true"> <Folder Name="Properties" TargetFolderName="Properties"> <ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem> </Folder> </Project> </TemplateContent> </VSTemplate> I even tried changing it within the Project.TargetFileName to: TargetFileName="$safeprojectname$.cs"??? Help