Antlr4 项目目录结构问题

huangapple go评论80阅读模式
英文:

Antlr4 Project Directory Structure Issue

问题

我正在尝试使用Java的包目录结构设置一个使用Antlr4的项目。

顶层目录包含 PondParser.g4PondLexer.g4build_script

build_script 预期用于构建和运行antlr4测试工具

{ 
    echo 'clean project' 
    find ./com/sevan/pond/ -type f -delete
} && { 
    echo 'launch ANTLR parser generator' 
    java -jar /usr/local/lib/antlr-4.8-complete.jar -package com.sevan.pond  ./PondLexer.g4 -o com/sevan/pond
    java -jar /usr/local/lib/antlr-4.8-complete.jar -package com.sevan.pond  ./PondParser.g4 -o com/sevan/pond
} && { 
    echo 'compile .java files'
    javac ./com/sevan/pond/*.java 
} && {
    echo 'run test rig with gui'
    java org.antlr.v4.gui.TestRig  ./com/sevan/pond/PondParser prog -gui 
}

相关的 PondParser.g4 头部和选项:

parser grammar PondParser;

@header {   
    package com.sevan.pond;
}

options {
    tokenVocab = PondLexer;
}

相关的 PondLexer.g4 头部和选项:

lexer grammar PondLexer ;

@header {   
    package com.sevan.pond;
}

在编译构建脚本时出现错误 (javac ./com/sevan/pond/*.java )。

具体的错误如下:

./com/sevan/pond/LexerRules.java:4: error: class, interface, or enum expected
    package com.sevan.pond;
    ^
./com/sevan/pond/Pond.java:4: error: class, interface, or enum expected
    package com.sevan.pond;
    ^
./com/sevan/pond/PondBaseListener.java:4: error: class, interface, or enum expected
    package com.sevan.pond;
    ^
./com/sevan/pond/PondListener.java:4: error: class, interface, or enum expected
    package com.sevan.pond;
    ^
4 errors

这里有什么遗漏/错误?任何建议、链接和答案将不胜感激。


更新

上述的四个错误是由于生成的java文件中存在重复的包声明。这是因为在使用 -package@header 来指定包时造成的。然而,在修复了这个问题之后,测试工具仍然出现 Can't load PondParser as lexer or parser 错误。

英文:

I'm trying to set up an Antlr4 project using Java's package directory structure.

The top level directory contains PondParser.g4, PondLexer.g4, and build_script.

build_script is expected to build and run the antlr4 test rig

{ 
    echo 'clean project' 
    find ./com/sevan/pond/ -type f -delete
} && { 
    echo 'launch ANTLR parser generator' 
    java -jar /usr/local/lib/antlr-4.8-complete.jar -package com.sevan.pond  ./PondLexer.g4 -o com/sevan/pond
    java -jar /usr/local/lib/antlr-4.8-complete.jar -package com.sevan.pond  ./PondParser.g4 -o com/sevan/pond
} && { 
    echo 'compile .java files'
    javac ./com/sevan/pond/*.java 
} && {
    echo 'run test rig with gui'
    java org.antlr.v4.gui.TestRig  ./com/sevan/pond/PondParser prog -gui 
}

Relevant PondParser.g4 header and options:

parser grammar PondParser;

@header {   
    package com.sevan.pond;
}

options {
    tokenVocab = PondLexer;
}

Relevant PondLexer.g4 header and options:

lexer grammar PondLexer ;

@header {   
    package com.sevan.pond;
}

The build script fails while compiling (javac ./com/sevan/pond/*.java ).

The specific errors are as follows:

./com/sevan/pond/LexerRules.java:4: error: class, interface, or enum expected
    package com.sevan.pond;
    ^
./com/sevan/pond/Pond.java:4: error: class, interface, or enum expected
    package com.sevan.pond;
    ^
./com/sevan/pond/PondBaseListener.java:4: error: class, interface, or enum expected
    package com.sevan.pond;
    ^
./com/sevan/pond/PondListener.java:4: error: class, interface, or enum expected
    package com.sevan.pond;
    ^
4 errors

What's missing/wrong here? Any suggestions, links, and answers would be greatly appreciated.


Update

The four errors above were due to a double package declaration in the generated java files. This was caused by specifying the package using -package and @header. After fixing this, however, the test rig still fails with Can't load PondParser as lexer or parser.

答案1

得分: 1

这样是不是更合适:

# *nix
javac -cp .:/usr/local/lib/antlr-4.8-complete.jar ./com/sevan/pond/*.java 

# Windows
javac -cp .;path\to\antlr-4.8-complete.jar .\com\sevan\pond\*.java 

也就是说,您希望将当前工作目录添加到类路径(.),以及添加 ANTLR 库。

编辑

这里有一个可工作的演示(我刚刚测试过)。

文件/文件夹结构:

-+
 |
 +-- antlr-4.8-complete.jar
 |
 +-- com/
      |
      +-- sevan/
           |
           +-- pond/
                |
                +-- PondParser.g4
                |
                +-- PondLexer.g4

导航到 antlr-4.8-complete.jar 所在的位置。

生成词法分析器和语法分析器类:

java -jar antlr-4.8-complete.jar -package com.sevan.pond com/sevan/pond/*.g4

然后编译所有生成的 .java 文件:

javac -cp .:antlr-4.8-complete.jar com/sevan/pond/*.java

最后,按照以下方式运行 TestRig:

java -cp .:antlr-4.8-complete.jar org.antlr.v4.gui.TestRig com.sevan.pond.Pond prog -gui

现在您可以输入一些文本,并且可以通过按下 CTRL+D 终止。我输入了 abc\n 并得到了这个结果:

Antlr4 项目目录结构问题

供参考,这是语法的外观:

PondLexer.g4

lexer grammar PondLexer;

ANY
 : .
 ;

PondParser.g4

parser grammar PondParser;

options {
  tokenVocab=PondLexer;
}

prog
 : ANY* EOF
 ;
英文:

Shouldn't that be:

# *nix
javac -cp .:/usr/local/lib/antlr-4.8-complete.jar ./com/sevan/pond/*.java 

# Windows
javac -cp .;path\to\antlr-4.8-complete.jar .\com\sevan\pond\*.java 

?

I.e.: you want to add the current working dir to the classpath (.), as well as the ANTLR lib.

EDIT

Here's a working demo (just tested it myself).

Files/folder structure:

-+
 |
 +-- antlr-4.8-complete.jar
 |
 +-- com/
      |
      +-- sevan/
           |
           +-- pond/
                |
                +-- PondParser.g4
                |
                +-- PondLexer.g4

Navigate to where antlr-4.8-complete.jar resides.

Generate lexer and parser classes:

java -jar antlr-4.8-complete.jar -package com.sevan.pond com/sevan/pond/*.g4

And compile all generated .java files:

javac -cp .:antlr-4.8-complete.jar com/sevan/pond/*.java

Fianlly, run TestRig as follows:

java -cp .:antlr-4.8-complete.jar org.antlr.v4.gui.TestRig com.sevan.pond.Pond prog -gui

You can now enter some text and can terminate by pressing <kbd>CTRL</kbd>+<kbd>D</kbd>. I typed abc\n and got this:

Antlr4 项目目录结构问题

For reference, heres what the grammars look like:

PondLexer.g4

lexer grammar PondLexer;

ANY
 : .
 ;

PondParser.g4

parser grammar PondParser;

options {
  tokenVocab=PondLexer;
}

prog
 : ANY* EOF
 ;

huangapple
  • 本文由 发表于 2020年9月14日 23:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63887658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定