Android ANTLR not working properly

I am trying to use ANTLR on Android and I found this: ANTLR and Android

After loading AntlrJavaRuntime, I'm not sure what to do, I have to do the following:

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

First of all, what does step 1 mean?

Secondly, when I try to do: make AntlrJavaRuntime, I get the following error:

 ~/AntlrJavaRuntime$ make AntlrJavaRuntime make: Nothing to be done for `AntlrJavaRuntime'. 

It is very difficult to find documentation about this, so any help is really appreciated (or a clear example of how to get ANTLR for an Android project).

+8
android parsing antlr makefile
source share
2 answers

Well, I just worked a little with the β€œsimple” version of ANTLR, and everything went fine.

Here is what I did:

1 new Android project

Create a new project called AndAbac (Android-Abacus) with a package named bk.andabac and AndAbac activity.

2 create a grammar

Create a grammar file called Exp.g (explanation here ) anywhere in your system and paste the following into it:

 grammar Exp; @parser::header { package bk.andabac; } @lexer::header { package bk.andabac; } eval returns [double value] : exp=additionExp {$value = $exp.value;} ; additionExp returns [double value] : m1=multiplyExp {$value = $m1.value;} ( '+' m2=multiplyExp {$value += $m2.value;} | '-' m2=multiplyExp {$value -= $m2.value;} )* ; multiplyExp returns [double value] : a1=atomExp {$value = $a1.value;} ( '*' a2=atomExp {$value *= $a2.value;} | '/' a2=atomExp {$value /= $a2.value;} )* ; atomExp returns [double value] : n=Number {$value = Double.parseDouble($n.text);} | '(' exp=additionExp ')' {$value = $exp.value;} ; Number : ('0'..'9')+ ('.' ('0'..'9')+)? ; WS : (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;} ; 

3 download ANTLR and generate lexer / parser

Download ANTLR here: http://www.antlr3.org/download/antlr-3.3-complete.jar and place it in the same directory as your Exp.g file. Create a lexer and parser (explanation here ) and copy the generated .java files to the following folder in your Android project: src/bk/andabac . Also put this ANTLR banner in the classpath of your Android project.

4 change some project files

Paste the following into res/layout/main.xml :

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/input_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="5 * (8 + 2)" /> <Button android:id="@+id/parse_button" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="eval" /> <TextView android:id="@+id/output_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout> 

and the following in src/bk/andabac/AndAbac.java :

 package bk.andabac; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import org.antlr.runtime.ANTLRStringStream; import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.RecognitionException; public class AndAbac extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button button = (Button)findViewById(R.id.parse_button); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { EditText in = (EditText)findViewById(R.id.input_text); TextView out = (TextView)findViewById(R.id.output_text); String source = in.getText().toString(); ExpLexer lexer = new ExpLexer(new ANTLRStringStream(source)); ExpParser parser = new ExpParser(new CommonTokenStream(lexer)); try { out.setText(source + " = " + parser.eval()); } catch (RecognitionException e) { out.setText("Oops: " + e.getMessage()); } } }); } } 

5 check application

Either run the project in an emulator, or create an APK file and install it on an Android device (I tested both, and both worked). After clicking the eval button, you will see the following:

enter image description here

+20
source share

This answer may be a bit late, but anyway ...

I am the author of the port of Antlr. These sources are intended for use in the Android source code. however, simply including sources in your andriod project should solve your problem.

I should have mentioned that building instructions only when adding Antlr to the Android OS source code.

-Earlence

+2
source share

All Articles