Antlr not working with VS2017
Asked Answered
T

2

6

I am trying to set up a simple project with Antlr in .net core 1.0 project using VS2017.

Following https://github.com/sharwell/antlr4cs, added .g4 file to the project. The project file looks like this,

<Project Sdk="Microsoft.NET.Sdk">    
  <PropertyGroup>
     <TargetFramework>netcoreapp1.0</TargetFramework>   </PropertyGroup>

   <ItemGroup>
     <None Remove="Calculator.g4" />   </ItemGroup>

   <ItemGroup>
    <PackageReference Include="Antlr4" Version="4.5.4-beta001" />   </ItemGroup>  

   <ItemGroup>
     <AdditionalFiles Include="Calculator.g4" />   
   </ItemGroup>
</Project>

But the document says,

Locate an existing XML element according to the MSBuild Property column in the table above, or add one if it does not already exist. For example, to generate both the parse tree listener and visitor interfaces and base classes for your parser, update the project item to resemble the following.

<Antlr4 Include="CustomLanguage.g4">  
<Generator>MSBuild:Compile</Generator> 
<CustomToolNamespace>MyProject.Folder</CustomToolNamespace>  
<Listener>True</Listener>   <Visitor>True</Visitor> </Antlr4>

There is no any Antlr4 tag in this proj file. Is Antlr not supported in VS2017? is tehre a good example I can follow to use ANtlr with .net core?

Tosha answered 3/3, 2017 at 0:17 Comment(8)
I just tested 4.6.1-beta001 in several scenarios and it works quite well.Venerate
@LexLi with VS2017?Tosha
Definitely in VS2017 RTM.Venerate
Hi @LexLi, how did you generate "<Antlr4" tag in proj file? Were you able to set the BuildType to "Antlr" for .g4 file?Tosha
VS2017 does not have Antlr Language Support Extension which used to do the trick automatically. (marketplace.visualstudio.com/…).Tosha
there is something called "manually".Venerate
@LexLi, I tried manual approach, then the project wont build.Tosha
Appreciate if you can put the proj file in the answer.Tosha
V
4

This is simply all I need in my .NET Standard 1.3 class library project to hold the grammar file.

<ItemGroup>
    <Antlr4 Include="Something.g4">
      <Generator>MSBuild:Compile</Generator>
      <Listener>False</Listener>
      <Visitor>False</Visitor>
    </Antlr4>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Antlr4" Version="4.6.1-beta001" />
  </ItemGroup>

Note that you might use a newer Antlr4 package version as 4.6.1 was the only version available when this answer was created.

Venerate answered 9/3, 2017 at 16:51 Comment(1)
Just landed on this, looking for the (non-existent) documentation. 4.6.5-beta001 is the latest version, which removes the dependency on Java as an experimental feature. You need to add <PropertyGroup> <Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator> </PropertyGroup>Endymion
E
4

I was just looking for the same thing, for .NET Core 2.0.

The current ANTLR4 package version is 4.6.5 Beta 1. In this version the C# generator was ported to C# so the dependency to Java was removed. This is still experimental and has to be enabled manually with :

<Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator>

Files aren't generated when a .g4 file is modified. They will be generated when dotnet build is called.

File globbing works as expected BUT changing settings like <Visitor>false</Visitor> requires a call dotnet clean before dotnet build.

The default Antlr task options can be found in the source :

<Antlr4>
  <Generator>MSBuild:Compile</Generator>
  <CustomToolNamespace Condition="'$(Antlr4IsSdkProject)' != 'True'">$(RootNamespace)</CustomToolNamespace>
  <CopyToOutputDirectory>Never</CopyToOutputDirectory>
  <Encoding>UTF-8</Encoding>
  <TargetLanguage>CSharp</TargetLanguage>
  <Listener>true</Listener>
  <Visitor>true</Visitor>
  <Abstract>false</Abstract>
  <ForceAtn>false</ForceAtn>
</Antlr4>

Globbing works, so if I want to build all g4 files and disable visitors, all I have to write is :

<ItemGroup>
  <Antlr4 Include="**/*.g4" >
    <Visitor>false</Visitor>
  </Antlr4>
</ItemGroup>

My entire csproj file looks like this :

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="Antlr4">
      <Version>4.6.5-beta001</Version>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
      <Antlr4 Include="**/*.g4" >
        <Visitor>false</Visitor>
      </Antlr4>
  </ItemGroup>

</Project>
Endymion answered 16/10, 2017 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.