开发者

ANTLR and Android

开发者 https://www.devze.com 2023-04-09 01:49 出处:网络
is there any guide how to use ANTLR on Android? I have found some ANTLR porta开发者_C百科tion for Android but it looks like being without any tutorial or manual. Do you know where to find some? (and y

is there any guide how to use ANTLR on Android? I have found some ANTLR porta开发者_C百科tion for Android but it looks like being without any tutorial or manual. Do you know where to find some? (and yes, I have been googling...)

Thx


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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消