[antlr-interest] VS2008 Integration Example
Gavin Lambert
antlr at mirality.co.nz
Tue Oct 14 02:09:06 PDT 2008
At 21:35 14/10/2008, André van der Merwe wrote:
>Does anyone have a simple example VS2008
>solution that calls ANTLR as part of the build
>(i.e. not a pre-build event).
>
>Ive followed the instructions on the web site
>but Im doing something wrong....
For which target language?
This is the method I use for C# projects (will
probably also work with other .NET languages, but *not* C/C++):
1. Right-click, Unload Project, Right-click, Edit
project. (If you're using Express, you don't
have this option, but you can edit the project
file outside of VS instead.)
2. Somewhere inside the first <PropertyGroup> section, add these:
<AntlrFolder>D:\path\to\antlr\lib</AntlrFolder>
<AntlrClassPath>$(AntlrFolder)\antlr-3.jar;$(AntlrFolder)\stringtemplate-3.2.jar;$(AntlrFolder)\antlr-2.7.7.jar</AntlrClassPath>
3. Immediately below the
<ItemGroup>...</ItemGroup> block containing your
normal source files, add this:
<ItemGroup>
<Antlr3 Include="Your.g">
<OutputFiles>YourLexer.g;YourParser.g;Your.tokens</OutputFiles>
</Antlr3>
<Antlr3 Include="YourTree.g">
<InputFiles>Your.tokens</InputFiles>
<OutputFiles>YourTree.cs</OutputFiles>
</Antlr3>
<Compile Include="YourLexer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Your.g</DependentUpon>
</Compile>
<Compile Include="YourParser.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Your.g</DependentUpon>
</Compile>
<Compile Include="YourTree.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>YourTree.g</DependentUpon>
</Compile>
</ItemGroup>
4. Immediately above the closing </Project> tag, add this:
<Target Name="GenerateAntlrCode"
Inputs="@(Antlr3);%(Antlr3.InputFiles)"
Outputs="%(Antlr3.OutputFiles)">
<Exec Command="java -cp
"$(AntlrClassPath)" org.antlr.Tool
-message-format vs2005 @(Antlr3)"
Outputs="%(Antlr3.OutputFiles)" />
</Target>
<PropertyGroup>
<BuildDependsOn>GenerateAntlrCode;$(BuildDependsOn)</BuildDependsOn>
</PropertyGroup>
5. Edit the above to suit your real files.
There's probably better ways to do this (I'm not
really all that experienced with MSBuild), but
the above works for me. It's a little bit clunky
in that you have to manually edit the project
whenever you add new grammar files (or outputs),
but that's usually not something that happens very often.
One caveat: Visual Studio does weird things if
you have the generated .cs files open when you
build, especially if you've modified the
grammar. It's best to close the .cs files before
you build. (It sometimes seems to compile the
version of the code that's shown in the editor --
ie. the old code -- instead of the newly
generated code. This is especially true if it
thinks the code has been modified -- which
includes the line-ending adjustment it likes
making when you open them.)
More information about the antlr-interest
mailing list