tags:

views:

100

answers:

1

I have an ANTLR grammar file as part of a C# project file and followed the steps outlined in the User Manual.

<Project ...>
    <PropertyGroup>
        <Antlr3ToolPath>$(ProjectDir)tools\antlr-3.1.3\lib</Antlr3ToolPath>
        <AntlrCleanupPath>$(ProjectDir)AntlrCleanup\$(OutputPath)</AntlrCleanupPath>
    </PropertyGroup>
    <ItemGroup>
        <Antlr3 Include="Grammar\Foo.g">
            <OutputFiles>FooLexer.cs;FooParser.cs</OutputFiles>
        </Antlr3>
        <Antlr3 Include="Grammar\Bar.g">
            <OutputFiles>BarLexer.cs;BarParser.cs</OutputFiles>
        </Antlr3>
    </ItemGroup>
    <Target Name="GenerateAntlrCode"
            Inputs="@(Antlr3)"
            Outputs="%(Antlr3.OutputFiles)">
        <Exec Command="java -cp %22$(Antlr3ToolPath)\antlr-3.1.3.jar%22 org.antlr.Tool -message-format vs2005 @(Antlr3Input)" Outputs="%(Antlr3Input.OutputFiles)" />
        <Exec Command="%22$(AntlrCleanupPath)\AntlrCleanup.exe%22 @(Antlr3Input) %(Antlr3Input.OutputFiles)" />
    </Target>
    <ItemGroup>
         <!-- ...other files here... -->
         <Compile Include="Grammar\FooLexer.cs">
             <AutoGen>True</AutoGen>
             <DesignTime>True</DesignTime>
             <DependentUpon>Foo.g</DependentUpon>
          </Compile>
          <Compile Include="Grammar\FooParser.cs">
              <AutoGen>True</AutoGen>
              <DesignTime>True</DesignTime>
              <DependentUpon>Foo.g</DependentUpon>
          </Compile>
          <!-- ... -->
    </ItemGroup>
</Project>

For whatever reason, the Compile steps only use old versions of the code, no amount of tweaking appears to help.

By "old versions," I mean that if I clean the solution, build the project, Foo.g will make FooLexer.cs and FooParser.cs. Should I then make an update to Foo.g and recompile, the new versions of the lexer and parser C# files are ignored and the old versions are used. I have to compile a second time...

+1  A: 

There seems to be a bug in the IDE: Visual Studio only monitors changes in C# files that it modifies itself (e.g. designer generated code). For code modified/generated outside of the IDE (e.g. external tool like ANTLR) it will use the in-memory version of the file, without refreshing it from the disk.

The workaround is to not use the "hosted" cache, and instead spawn an external CSC process to compile the project. You do this by setting the "UseHostCompilerIfAvailable" project property to false like so in your .csproj:

<UseHostCompilerIfAvailable>FALSE</UseHostCompilerIfAvailable>

For more info, see this entry in the MS Connect website.

I had the exact same problem as you with ANTLR in Visual Studio, and this fixed it for me. However, some people report problems with project-to-project dependencies after setting that option to 'false' so watch out for side effects...

Ludovic