You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ANTLR4 PostgreSQL语法在Visual Studio构建中报错问题求助

PostgreSQL ANTLR4语法文件构建问题排查与解决

初始问题场景

从GitHub的grammars-v4仓库下载PostgreSQL的PostgreSQLLexer.g4和PostgreSQLParser.g4语法文件,在Visual Studio 2022中新建.NET 8类库项目,引入Antlr4 4.6.6和Antlr4.Runtime.Standard 4.13.1 NuGet包后,构建时出现以下语法解析错误:

mismatched input 'options {' expecting COLON while matching a lexer rule    PostgresSqlClasses  ...\PostgreSQLLexer.g4  1421

mismatched input '=' expecting COLON while matching a rule  PostgresSqlClasses  ...\PostgreSQLLexer.g4  1422    

mismatched input ';' expecting COLON while matching a rule  PostgresSqlClasses  ...\PostgreSQLLexer.g4  1422

mismatched input 'options {' expecting COLON while matching a lexer rule    PostgresSqlClasses  ...\PostgreSQLLexer.g4  1616

同时伴随构建脚本错误:

mismatched character '<EOF>' expecting ']'  PostgresSqlClasses  ...\antlr4.codegenerator\4.6.6\build\Antlr4.CodeGenerator.targets   132 

初步排查

经ANTLR专家Bart Kiers确认,最新更新的语法文件本身无问题,错误大概率源于Visual Studio 2022环境或Java配置异常。

切换构建方案:使用Antlr4BuildTasks

按照Kaby76的建议,改用Antlr4BuildTasks NuGet包,项目配置如下:

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="PostgreSQLLexer.g4" />
    <None Remove="PostgreSQLParser.g4" />
  </ItemGroup>

  <ItemGroup>
    <Antlr4 Include="PostgreSQLLexer.g4">
        <Package>AntlrPostgresClasses</Package>
        <AntOutDir>GeneratedOutput</AntOutDir>
      </Antlr4>
    <Antlr4 Include="PostgreSQLParser.g4">
        <Package>AntlrPostgresClasses</Package>
        <AntOutDir>GeneratedOutput</AntOutDir>
      </Antlr4>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Antlr4.Runtime.Standard" Version="4.13.1" />
    <PackageReference Include="Antlr4BuildTasks" Version="12.7.0" PrivateAssets="All" />
  </ItemGroup>

</Project>

新错误:CS0246类型未找到

生成部分C#文件后,出现编译错误:

CS0246 The type or namespace name 'PostgreSQLLexerBase' could not be found (are you missing a using directive or an assembly reference?) AntlrPostgresClasses C:\Repos\PostgresParsers\AntlrPostgresClasses\GeneratedOutput\PostgreSQLLexer.cs 35

CS0246 The type or namespace name 'PostgreSQLParserBase' could not be found (are you missing a using directive or an assembly reference?) AntlrPostgresClasses C:\Repos\PostgresParsers\AntlrPostgresClasses\GeneratedOutput\PostgreSQLParser.cs 38

错误出现在自动生成的代码文件中。

最终解决

按照Kaby76的建议,添加指定的4个基础类文件后,类库可正常构建(首次构建未成功,后续构建恢复正常)。


内容的提问来源于stack exchange,提问作者Hugh Jones

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 16:33:30