Try   HackMD

ANTLR 4開發環境設置

ANTLR (ANother Tool for Language Recognition)是用來做語法解析的工具,可產生的解析器語言支援Java、C#、Python、JavaScript、Go、C++、Swift、PHP,本篇介紹的是C#的部分。

Java安裝

ANTLR是用Java開發的,所以必須先安裝Java,我使用的是AdoptOpenJDK,OpenJDK 8 (LTS) - HotSpot的版本。

  1. 將下載的檔案解壓縮到C槽。

  2. 在環境變數path加入C:\jdk8u242-b08\bin

完成以上步驟可在終端機輸入java -version確認是否設置成功。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

ANTLR 4安裝

  1. ANTLR官網下載antlr-4.8-complete.jar並儲存到C:\jdk8u242-b08\lib

  2. 在環境變數CLASSPATH加入
    .;C:\jdk8u242-b08\lib\antlr-4.8-complete.jar;%CLASSPATH%

  3. C:\jdk8u242-b08\bin建立一個antlr4.bat檔案,內容為
    java org.antlr.v4.Tool %*

  4. C:\jdk8u242-b08\bin建立一個grun.bat檔案,內容為
    java org.antlr.v4.gui.TestRig %*

完成以上步驟可在終端機輸入antlr4確認是否設置成功。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Visual Studio Code設置

環境設置

  1. 安裝ANTLR4 grammar syntax support擴充套件。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  2. settings.json加入以下設定。

    ​​​"antlr4.generation": {
    ​​​    "mode": "external",
    ​​​    "outputDir": "antlr4-runtime",   //產生的解析器檔案存放的資料夾
    ​​​    "language": "CSharp",            //產生的解析器語言
    ​​​    "listeners": false,
    ​​​    "visitors": true
    ​​​}
    

ANTLR專案設置

  1. launch.jsonconfigurations加入以下設定。

    ​​​{
    ​​​    "name": "Debug ANTLR4",
    ​​​    "type": "antlr-debug",
    ​​​    "request": "launch",
    ​​​    "input": "input.txt",    //要解析的檔案
    ​​​    "startRule": "expr",     //起始規則
    ​​​    "printParseTree": true,  //編譯後顯示文字樹狀圖
    ​​​    "visualParseTree": false //編譯後顯示圖形樹狀圖
    ​​​}
    
  2. 建立ANTLR的解析規則檔,副檔名為.g4,並在第一行加入grammar 檔名;
    例如:檔名為Hello.g4,則此檔案第一行為grammar Hello;

ANTLR基本語法:規則名稱 : 規則內容1 | 規則內容2 ;
規則名稱第一個字母小寫為parser,大寫為lexer。

Visual Studio C#專案設置

  1. 從NuGet安裝Antlr4.Runtime.Standard套件。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  2. 將ANTLR產生的四支.cs檔複製到C#專案裡。

  3. 加入using命名空間指示詞。

    ​​​using Antlr4.Runtime;
    ​​​using Antlr4.Runtime.Tree;
    

加入以下程式碼可開始解析。

ICharStream stream = CharStreams.fromPath(openFileDialog1.FileName);
ITokenSource lexer = new HelloLexer(stream);
ITokenStream tokens = new CommonTokenStream(lexer);
HelloParser parser = new HelloParser(tokens);
IParseTree tree = parser.expr();
HelloVisitor visitor = new HelloVisitor();
visitor.Visit(tree);

如果要在某些解析規則中做一些處理,需先加入以下class,然後才在這個class裡加入要處理的function。

public class HelloVisitor : HelloBaseVisitor<object>
{
   function1() {...}
   function2() {...}
}

要處理的function是從HelloVisitor.cs找出和ANTLR規則名稱相對應的function後修改而成。
例如:Result VisitExpr([NotNull] HelloParser.ExprContext context);

function長相:

public override object VisitExpr(HelloParser.ExprContext context)
{
   do things...
   return VisitChildren(context);
}

參考資料
ANTLR官網
作者GitHub
The ANTLR Mega Tutorial