ANTLR and Android

后端 未结 3 1782
抹茶落季
抹茶落季 2020-12-18 13:12

is there any guide how to use ANTLR on Android? I have found some ANTLR portation for Android but it looks like being without any tutorial or manual. Do you know where to fi

相关标签:
3条回答
  • 2020-12-18 13:40

    I'm not sure what "using ANTLR" means to you. Here's what it means to me:

    I'm assuming that you will create a grammar, generate the parser/lexer Java classes, compile them, deploy them in your Android app, and then let them parse whatever your app sends into an AST.

    If you want to know how to do that, there's no better place than the ANTLR documentation or the book you can buy from Amazon.

    0 讨论(0)
  • 2020-12-18 13:47

    There is an Android Studio (actually IntelliJ) plug-in for this, called "ANTLR v4 grammar plugin".

    This tutorial worked for me.

    In short, simply copy grammar (a .g4 file) into your project, right-click on it > Configure ANTLR...

    From there you can select to generate Java or Kotlin files, which will compile and run once you added the runtime in your dependencies:

    implementation 'org.antlr:antlr4-runtime:4.7'
    
    0 讨论(0)
  • 2020-12-18 13:56

    After reading the README from this ANTLR port:

    AntlrJavaRuntime - Earlence Fernandes, The CRePE Project, VU Amsterdam ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The Runtime is available as an external library against which apps can link. It provides the necessary mechanisms to execute Lexer/Parser code generated by the ANTLR tool. The model is offline, in the sense that the parser/lexer is generated off the mobile phone on a desktop computer. The resulting files are transferred to an Android project which then uses this library.

    Building
    ~~~~~~~~

    1. lunch the appropriate target
    2. make AntlrJavaRuntime
    3. verify that AntlrJavaRuntime.xml was placed in /system/etc/permissions and AntlrJavaRuntime.jar was placed in /system/framework
    4. after this, you can run a normal make

    It seems to me that the only difference is when you want to run your parser on an Android device (or -emulator) you must include the AntlrJavaRuntime in your Android project/app.

    So, writing the grammar, generating a parser and lexer from said grammar would be the same as on a "normal" machine. Here's a previous Q&A that shows how to write a simple expression parser: ANTLR: Is there a simple example?

    EDIT

    Also see this Q&A: android ANTLR make not working properly

    0 讨论(0)
提交回复
热议问题