Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Antlr not working with VS2017

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?

like image 959
Dhanuka777 Avatar asked Mar 03 '17 00:03

Dhanuka777


2 Answers

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.

like image 160
Lex Li Avatar answered Nov 11 '22 13:11

Lex Li


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>
like image 36
Panagiotis Kanavos Avatar answered Nov 11 '22 13:11

Panagiotis Kanavos