Antlr does not work with VS2017

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

Following https://github.com/sharwell/antlr4cs , a .g4 file has been added to the project. The project file is as follows:

<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 the existing XML element according to the MSBuild property of the column in the table above, or add one if it does not already exist. For example, to generate interfaces and base classes for your analyzer for both the parse tree and the visitor tree, update the project element to look like this.

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

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

+6
source share
2 answers

This is just all I need in my .NET Standard 1.3 class library project to store 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> 

Please note that you can use the new version of Antlr4, as version 4.6.1 was the only version available when creating this answer.

+4
source

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

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

 <Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator> 

Files are not generated when the .g4 file is .g4 . They will be generated when dotnet build called.

The globbing file works as expected. But changing settings, such as <Visitor>false</Visitor> , requires calling dotnet clean before dotnet build .

The default Antlr task parameters 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> 

The globe works, so if I want to collect all g4 files and turn off visitors, all I need to write is:

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

My entire csproj file is as follows:

 <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> 
+1
source

Source: https://habr.com/ru/post/1015424/


All Articles