FSLex example solution?

2k views Asked by At

I've been using C/lex for a long time and would like to use F#/fslex now. I'm comparably well off in C# and in the process of learning F#. The only thing is that I can't see any project example or template where fslex is properly included in the Visual Studio build process. Does anyone know where I can find one?

Lots of Greetings! Volker

3

There are 3 answers

1
YotaXP On

I'm using the free VS Shell until I finally threaten myself enough to finally buy the full VS, and unless I'm doing something wrong, (and I probably am) none of the normal solutions seem to work for me. What I do is add the following pre-build events to the project properties:

"C:\Program Files (x86)\FSharpPowerPack-2.0.0.0\bin\fslex.exe"  "$(ProjectDir)\Lexer.fsl"
"C:\Program Files (x86)\FSharpPowerPack-2.0.0.0\bin\fsyacc.exe" "$(ProjectDir)\Parser.fsy" --module Parser

That seems to work well enough for me. Errors all go to the output window. If you're on a 32-bit machine, you would remove the (x86) from those paths.

1
Chad Boyer On

Doing a little research it looks like they need to be separate. I found the following at this blog post ( http://blogs.msdn.com/chrsmith/archive/2008/01/18/fslex-Sample.aspx ) by Chris Smith:

Although you can add an F# Lex Specification file from the Visual Studio Add-In, to run fslex you will need to break to the command line. Fslex.exe is located in your F# distribution’s ‘bin’ directory.

Hope that helps.

0
Tomas Petricek On

Unfortunately, there is no built-in item template for .fsl files, because FsLex is a part of PowerPack (so Visual Studio cannot expect that it will be installed). It would be definitely useful to have some template that could be installed with PowerPack!

Anyway, if you're looking for a sample project that uses FsLex (and has .fsl files as part of the project), then you can take a look at the F# source code (distribtued with the Visual Studio 2008 MSI/ZIP package). The project that contains .fsl is FSharp.Compiler.fsproj and on my installation, it can be found in C:\Programs Files\FSharp-1.9.9.9\source\fsharp\FSharp.Compiler. Surprisingly, it includes a lexer for the F# language itself :-).

To add FsLex item to the MSBUILD project (which is also Visual Studio project), it uses the following:

<FsLex Include="..\lex.fsl">
  <OtherFlags>--lexlib Internal.Utilities.Text.Lexing</OtherFlags>
  <Link>lex.fsl</Link>
</FsLex>
<Compile Include="lex.fs" />

In case you also needed FsYacc, here is an example (also from FSharp.Compiler.fsproj):

<FsYacc Include="..\pars.fsy">
  <Module>Microsoft.FSharp.Compiler.Parser</Module>
  <Open>Microsoft.FSharp.Compiler</Open>
  <OtherFlags>--internal --lexlib Internal.Utilities.Text.Lexing 
              --parslib Internal.Utilities.Text.Parsing</OtherFlags>
  <Link>pars.fsy</Link>
</FsYacc>
<Compile Include="pars.fs" />

Note that you need the FsYacc/FsLex command to invoke the custom tool, but also the Compile command which tells the compiler to include the produced fs file when building the project.