antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.1k stars 3.69k forks source link

line 1:0 mismatched input 'package' expecting {IntegerLiteral, FloatingPointLiteral, BooleanLiteral, Character... #2921

Open Sidnio opened 1 year ago

Sidnio commented 1 year ago

image

kaby76 commented 1 year ago

Missing essential details:

Kenne@DESKTOP-DL44R7B MINGW64 ~/big-index/grammars-v4/java/java8
$ trgen
C:/msys64/home/Kenne/big-index/grammars-v4/java/java8/
Rendering template file from CSharp/Encodings.cs to Generated/Encodings.cs
Rendering template file from CSharp/ErrorListener.cs to Generated/ErrorListener.cs
Rendering template file from CSharp/makefile to Generated/makefile
Rendering template file from CSharp/Test.cs to Generated/Test.cs
Rendering template file from CSharp/Test.csproj to Generated/Test.csproj
Rendering template file from CSharp/test.sh to Generated/test.sh
Rendering template file from CSharp/tester.psm1 to Generated/tester.psm1
Copying source file from C:/msys64/home/Kenne/big-index/grammars-v4/java/java8/Test.java to Generated/Test.java
Copying source file from C:/msys64/home/Kenne/big-index/grammars-v4/java/java8/pom.xml to Generated/pom.xml
Copying source file from C:/msys64/home/Kenne/big-index/grammars-v4/java/java8/Java8Parser.g4 to Generated/Java8Parser.g4
Copying source file from C:/msys64/home/Kenne/big-index/grammars-v4/java/java8/Java8Lexer.g4 to Generated/Java8Lexer.g4
Copying source file from C:/msys64/home/Kenne/big-index/grammars-v4/java/java8/input.txt to Generated/input.txt
Copying source file from C:/msys64/home/Kenne/big-index/grammars-v4/java/java8/CodepointRangeScanner.java to Generated/CodepointRangeScanner.java

Kenne@DESKTOP-DL44R7B MINGW64 ~/big-index/grammars-v4/java/java8
$ cd Generated/

Kenne@DESKTOP-DL44R7B MINGW64 ~/big-index/grammars-v4/java/java8/Generated
$ ls
CodepointRangeScanner.java  ErrorListener.cs  Java8Lexer.g4   makefile  Test.cs      Test.java  tester.psm1
Encodings.cs                input.txt         Java8Parser.g4  pom.xml   Test.csproj  test.sh

Kenne@DESKTOP-DL44R7B MINGW64 ~/big-index/grammars-v4/java/java8/Generated
$ make
dotnet restore
  Determining projects to restore...
  Restored C:\msys64\home\Kenne\big-index\grammars-v4\java\java8\Generated\Test.csproj (in 377 ms).
dotnet build
MSBuild version 17.4.0+18d5aef85 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  Test -> C:\msys64\home\Kenne\big-index\grammars-v4\java\java8\Generated\bin\Debug\net6.0\Test.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:11.39

Kenne@DESKTOP-DL44R7B MINGW64 ~/big-index/grammars-v4/java/java8/Generated
$ make test
bash test.sh
../examples/helloworld.java
CSharp 0 stdin success 0.5105612
../examples/instanceof.java
CSharp 0 stdin success 0.2999091
../examples/Receiver.java
CSharp 0 stdin success 0.0591327
../examples/Unicode.java
CSharp 0 stdin success 0.515561

Kenne@DESKTOP-DL44R7B MINGW64 ~/big-index/grammars-v4/java/java8/Generated
$ ./bin/Debug/net6.0/Test.exe ../input.txt
CSharp 0 ../input.txt success 0.5104357
Total Time: 0.5751392

Kenne@DESKTOP-DL44R7B MINGW64 ~/big-index/grammars-v4/java/java8/Generated
$ cat input.txt
package com.example.customviewedit;
public class tt {
  public static void main(String[] args) {
    System.out.println("dddddd");
  }
}

Kenne@DESKTOP-DL44R7B MINGW64 ~/big-index/grammars-v4/java/java8/Generated
$

The grammar and generated parser work fine.

lab.antlr.org also works just fine. Screenshot (22)

kaby76 commented 1 year ago

And, it also works in Intellij. Screenshot (23)

The doc for the Antlr Intellij plugin is terrible though. It took me an hour to figure out how to use the extension/plugin years ago, and the doc still does not have clear, precise, enumerated steps to test a rule in a grammar. Follow these steps. 1) Clone the grammars-v4 repo. 2) Start Intellij and open .../grammars-v4/java/java8. The Project pane will be on the left and show all the files in the directory. 3) Double-click on "Java8Parser.g4" in the Project pane. This opens the file in a text pane on the right. 4) In the Project pane, right-click on "Java8Parser.g4", and in the pop-up menu, select "Generate ANTLR Recognizer". 5) In the text pane of Java8Parser.g4, scoll down in the text and locate the "compilationUnit" rule. 6) Right-click on the "compilationUnit" text in the text pane, and select "Test rule compilationUnit" in the pop-up menu. 7) In the Antlr Preview pane, enter the input. The result will be displayed.

Sidnio commented 1 year ago

而且,它也适用于Intellij。 截图 (23)

Antlr Intellij插件的文档很糟糕。几年前,我花了一个小时才弄清楚如何使用扩展/插件,但该文档仍然没有清晰、精确、枚举的步骤来测试语法中的规则。请按照以下步骤操作。

  1. 克隆语法 v4 存储库。
  2. 启动 Intellij 并打开 .../grammars-v4/java/java8。“工程”窗格将位于左侧,并显示目录中的所有文件。
  3. 双击“项目”窗格中的“Java8Parser.g4”。这将在右侧的文本窗格中打开文件。
  4. 在“项目”窗格中,右键单击“Java8Parser.g4”,然后在弹出菜单中选择“生成 ANTLR 识别器”。
  5. 在 Java8Parser.g4 的文本窗格中,向下搜索文本并找到“编译单元”规则。
  6. 右键单击文本窗格中的“编译单元”文本,然后在弹出菜单中选择“测试规则编译单元”。
  7. 在“Antlr 预览”窗格中,输入输入。将显示结果。

Java8Parser.g4

image

JavaParser.g4

JavaParser Ok

image