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?

Dhanuka777
  • 8,331
  • 7
  • 70
  • 126

2 Answers2

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.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • 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 ` True ` – Panagiotis Kanavos Oct 16 '17 at 14:14
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>
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236