2

我一直在努力使这个问题的答案适用于 Android 上的 ANTLR4。我已确定添加正确的 Java 文件并将 jar 添加到项目中,但它没有向我显示输入的结果。一旦我点击我设备上的“评估”按钮,输出就是这个

5 * (8 + 2) 等于 []

我所做的唯一更改是语法和导入方法,以使它们在 ANTLR4 上工作。

这是语法(Exp.g4)代码:

grammar Exp;

@parser::header {
  package bk.andabac2;
}

@lexer::header {
  package bk.andabac2;
}

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') {skip();}
 ;

还有我在 Eclipse 中的 MainActivity.java 代码:

package bk.andabac2;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_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 ANTLRInputStream(source));
                ExpParser parser = new ExpParser(new CommonTokenStream(lexer));

                try {
                    out.setText(source + " evalúa a " + parser.eval());
                }
                catch (RecognitionException e) {
                    out.setText("Oops: " + e.getMessage());
                }
            }
        });
    }

}

我在日志中得到的是关于 TreeViewer 类的以下消息:

Unable to resolve superclass of Lorg/antlr/v4/runtime/tree/gui/TreeViewer; (763)
Link of class 'Lorg/antlr/v4/runtime/tree/gui/TreeViewer;' failed
Could not find class 'org.antlr.v4.runtime.tree.gui.TreeViewer', referenced from method org.antlr.v4.runtime.RuleContext.inspect
VFY: unable to resolve new-instance 1351 (Lorg/antlr/v4/runtime/tree/gui/TreeViewer;) in Lorg/antlr/v4/runtime/RuleContext;
VFY: replacing opcode 0x22 at 0x0000
Unable to resolve superclass of Lorg/antlr/v4/runtime/tree/gui/TreeViewer; (763)
Link of class 'Lorg/antlr/v4/runtime/tree/gui/TreeViewer;' failed
DexOpt: unable to opt direct call 0x21be at 0x02 in Lorg/antlr/v4/runtime/RuleContext;.inspect
4

2 回答 2

0

以下是创建 Android 兼容的 ANTLR4 运行时库的方法:

1.首先克隆ANTLR4:

git clone https://github.com/antlr/antlr4.git
cd antlr4

2. Checkout tag 4.3(或其他版本):

git checkout 4.3

3. 拆下整个gui包装:

rm -rf runtime/Java/src/org/antlr/v4/runtime/tree/gui

4.修复包含gui类的类中的编译器错误:

  • runtime/Java/src/org/antlr/v4/runtime/RuleContext.java
  • runtime/Java/src/org/antlr/v4/runtime/tree/Trees.java

5. 通过执行以下操作构建项目:

mvn clean install -DskipTests=true

6.将运行时库复制到Android项目中:

cp runtime/Java/target/antlr4-runtime-4.3.jar /your/android/project/app/libs

我还创建了一个使用这个库的小型 Android 应用程序。在这个 GitHub 存储库中找到它:https ://github.com/bkiers/antlr4android

于 2014-10-03T10:32:39.817 回答
0

我遇到了同样类型的问题。看来,antlr 运行时通常是为用作可访问 javax.swing 库的 java 应用程序而构建的。这些在android上不存在。
所以我的策略是重建 antlr 4 运行时,并删除任何与 javax.swing 相关的库引用。

我最近在 github 上为 antlr4 创建了一个分支来支持 android:https ://github.com/jkalkhof/antlr4/tree/4.0-android

于 2014-10-02T18:06:55.663 回答