android ANTLR 使无法正常工作 [英] android ANTLR make not working properly

查看:49
本文介绍了android ANTLR 使无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android 上使用 ANTLR,我发现了这个:ANTLR 和 Android

下载 AntlrJavaRuntime 后,我不知道该怎么做,我应该这样做:

<代码>1.午餐适当的目标2. 制作 AntlrJavaRuntime3. 验证 AntlrJavaRuntime.xml 是否放置在/system/etc/permissions 和4. AntlrJavaRuntime.jar 放在/system/framework5.在此之后,您可以运行正常的make

首先,第 1 步到底意味着什么?

其次,当我尝试执行以下操作时:make AntlrJavaRuntime 我收到以下错误:

~/AntlrJavaRuntime$ make AntlrJavaRuntimemake: 对 `AntlrJavaRuntime' 不需要做任何事情.

很难找到有关此的文档,因此非常感谢您的帮助(或如何为 Android 项目准备 ANTLR 的清晰示例).

解决方案

嗯,我只是用一个普通"的 ANTLR 版本做了一个小测试,一切都很顺利.

这是我所做的:

1 个新的 Android 项目

使用名为 bk.andabac 的包和名为 AndAbac 的活动创建一个名为 AndAbac (Android-Abacus) 的新项目.

2 创建语法

创建一个名为Exp.g的语法文件(说明此处) 系统上的任何位置,然后将以下内容粘贴到其中:

grammar Exp;@parser::header {包 bk.andabac;}@lexer::header {包 bk.andabac;}eval 返回 [双倍值]: exp=additionExp {$value = $exp.value;};addExp 返回 [double value]: m1=multiplyExp {$value = $m1.value;}( '+' m2=multiplyExp {$value += $m2.value;}|'-' m2=multiplyExp {$value -= $m2.value;})*;乘法返回 [双倍值]: a1=atomExp {$value = $a1.value;}( '*' a2=atomExp {$value *= $a2.value;}|'/' a2=atomExp {$value/= $a2.value;})*;atomExp 返回 [double value]: n=Number {$value = Double.parseDouble($n.text);}|'(' exp=additionExp ')' {$value = $exp.value;};数字: ('0'..'9')+ ('.' ('0'..'9')+)?;WS: (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;};

3 下载 ANTLR &生成词法分析器/解析器

在此处下载 ANTLR:http://www.antlr3.org/download/antlr-3.3-complete.jar 并将其放在与 Exp.g 文件相同的目录中.生成词法分析器和解析器(解释此处)并复制生成的.java 文件到您的 Android 项目中的以下文件夹:src/bk/andabac.还要将此 ANTLR jar 放到您的 Android 项目的类路径中.

4 更改一些项目文件

res/layout/main.xml中粘贴以下内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"机器人:方向=垂直"android:layout_width="fill_parent"android:layout_height="fill_parent"><编辑文本android:id="@+id/input_text"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="5 * (8 + 2)"/><按钮android:id="@+id/parse_button"android:layout_height="wrap_content"android:layout_width="fill_parent"机器人:文本=评估"/><文本视图android:id="@+id/output_text"android:layout_width="fill_parent"android:layout_height="wrap_content"机器人:文本="/></LinearLayout>

以及src/bk/andabac/AndAbac.java中的以下内容:

package bk.andabac;导入 android.app.Activity;导入 android.os.Bundle;导入 android.view.View;导入 android.widget.Button;导入 android.widget.EditText;导入 android.widget.TextView;导入 org.antlr.runtime.ANTLRStringStream;导入 org.antlr.runtime.CommonTokenStream;导入 org.antlr.runtime.RecognitionException;公共类 AndAbac 扩展 Activity {@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);最终按钮按钮 = (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);字符串源 = in.getText().toString();ExpLexer 词法分析器 = new ExpLexer(new ANTLRStringStream(source));ExpParser parser = new ExpParser(new CommonTokenStream(lexer));尝试 {out.setText(source + " = " + parser.eval());}捕获(识别异常 e){out.setText("糟糕:" + e.getMessage());}}});}}

5 测试应用

要么在模拟器中运行该项目,要么创建一个 APK 文件并将其安装在 Android 设备上(我测试了两者,两者都有效).按下 eval 按钮后,您将看到以下内容:

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

After downloading the AntlrJavaRuntime I am not sure of what to do, I am supposed to make this:

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 even means?

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

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

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

解决方案

Well, I just did a little test with a "plain" ANTLR version, and everything went just fine.

Here's what I did:

1 new Android project

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

2 create a grammar

Create a grammar file called Exp.g (explanation here) anywhere on your system and paste the following in 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 & generate lexer/parser

Download ANTLR here: http://www.antlr3.org/download/antlr-3.3-complete.jar and put it in the same directory as your Exp.g file. Generate 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 jar to the classpath of your Android project.

4 change some project files

Paste the following in 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 test the app

Either run the project in an emulator, or create an APK file and install this on an Android device (I tested both, and both worked). You will see the following after pressing the eval button:

这篇关于android ANTLR 使无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆