2

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 find some? (and yes, I have been googling...)

Thx

Waypoint
  • 17,283
  • 39
  • 116
  • 170

3 Answers3

4

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

Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • As you speak of including AntlrJavaRuntime, does it mean whole dest of \java\org\antlr\runtime, which has tens of java files? – Waypoint Sep 29 '11 at 13:00
  • Yes, all the compiled `.java` source files need to be a part of your Android app/project. – Bart Kiers Sep 29 '11 at 13:03
  • ok, I have found some pre-prepared antlr-runtime-3.1.3 so hope it helps. Thank you for answer – Waypoint Sep 29 '11 at 13:11
1

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thanks, I definitely look into Amazon bookcase, I am looking for solution how to incporporate or use ANTLR portation for Android. – Waypoint Sep 29 '11 at 11:31
  • My point is that you will exercise ANTLR offline from Android. You'll package the Java .class files that it'll create for you into your app. By the time you get to Android, it's just Java classes. – duffymo Sep 29 '11 at 13:25
0

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'
Victor Paléologue
  • 2,025
  • 1
  • 17
  • 27