ANTLR:有一个简单的例子吗? [英] ANTLR: Is there a simple example?

查看:186
本文介绍了ANTLR:有一个简单的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始使用ANTLR,但是花了几个小时在 antlr.org <上查看这些示例后/ a> site,我仍然无法清楚地理解Java进程的语法。

I'd like to get started with ANTLR, but after spending a few hours reviewing the examples at the antlr.org site, I still can't get a clear understanding of the grammar to Java process.

是否有一些简单的例子,类似于四操作计算器实现的使用ANTLR进行解析器定义并一直到Java源代码?

Is there some simple example, something like a four-operations calculator implemented with ANTLR going through the parser definition and all the way to the Java source code?

推荐答案

首先创建一个语法。下面是一个小语法,您可以使用它来评估使用4个基本数学运算符构建的表达式:+, - ,*和/。你也可以用括号对表达式进行分组。

You first create a grammar. Below is a small grammar that you can use to evaluate expressions that are built using the 4 basic math operators: +, -, * and /. You can also group expressions using parenthesis.

注意这个语法只是一个非常基本的语法:它不处理一元运算符(减号:-1 + 9)或小数,如.99(没有前导数字),仅列举两个缺点。这只是一个你可以自己处理的例子。

Note that this grammar is just a very basic one: it does not handle unary operators (the minus in: -1+9) or decimals like .99 (without a leading number), to name just two shortcomings. This is just an example you can work on yourself.

这是语法文件的内容 Exp.g

grammar Exp;

/* This will be the entry point of our parser. */
eval
    :    additionExp
    ;

/* Addition and subtraction have the lowest precedence. */
additionExp
    :    multiplyExp 
         ( '+' multiplyExp 
         | '-' multiplyExp
         )* 
    ;

/* Multiplication and division have a higher precedence. */
multiplyExp
    :    atomExp
         ( '*' atomExp 
         | '/' atomExp
         )* 
    ;

/* An expression atom is the smallest part of an expression: a number. Or 
   when we encounter parenthesis, we're making a recursive call back to the
   rule 'additionExp'. As you can see, an 'atomExp' has the highest precedence. */
atomExp
    :    Number
    |    '(' additionExp ')'
    ;

/* A number: can be an integer value, or a decimal value */
Number
    :    ('0'..'9')+ ('.' ('0'..'9')+)?
    ;

/* We're going to ignore all white space characters */
WS  
    :   (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;}
    ;

(解析器规则以小写字母开头,词法分析器规则以大写字母开头)

创建语法后,您将需要从中生成解析器和词法分析器。下载 ANTLR jar 并将其存储在与语法文件相同的目录中。

After creating the grammar, you'll want to generate a parser and lexer from it. Download the ANTLR jar and store it in the same directory as your grammar file.

在shell /命令提示符下执行以下命令:

Execute the following command on your shell/command prompt:

java -cp antlr-3.2.jar org.antlr.Tool Exp.g

它不应该产生任何错误消息,现在应生成 ExpLexer.java ExpParser.java Exp.tokens 文件。

It should not produce any error message, and the files ExpLexer.java, ExpParser.java and Exp.tokens should now be generated.

要查看一切是否正常,请创建此测试类:

To see if it all works properly, create this test class:

import org.antlr.runtime.*;

public class ANTLRDemo {
    public static void main(String[] args) throws Exception {
        ANTLRStringStream in = new ANTLRStringStream("12*(5-6)");
        ExpLexer lexer = new ExpLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ExpParser parser = new ExpParser(tokens);
        parser.eval();
    }
}

并编译它:

// *nix/MacOS
javac -cp .:antlr-3.2.jar ANTLRDemo.java

// Windows
javac -cp .;antlr-3.2.jar ANTLRDemo.java

然后运行它:

// *nix/MacOS
java -cp .:antlr-3.2.jar ANTLRDemo

// Windows
java -cp .;antlr-3.2.jar ANTLRDemo

如果一切顺利,控制台上不会打印任何内容。这意味着解析器没有发现任何错误。当您将12 *(5-6)更改为12 *(5-6然后重新编译)时并运行它,应打印以下内容:

If all goes well, nothing is being printed to the console. This means the parser did not find any error. When you change "12*(5-6)" into "12*(5-6" and then recompile and run it, there should be printed the following:

line 0:-1 mismatched input '<EOF>' expecting ')'

好的,现在我们要在语法中添加一些Java代码,以便解析器实际上有用的东西。添加代码可以通过在语法中放置 {} 并在其中包含一些普通的Java代码来完成。

Okay, now we want to add a bit of Java code to the grammar so that the parser actually does something useful. Adding code can be done by placing { and } inside your grammar with some plain Java code inside it.

但首先:语法文件中的所有解析器规则都应返回原始double值。你可以通过在每条规则后添加返回[double value] 来做到这一点:

But first: all parser rules in the grammar file should return a primitive double value. You can do that by adding returns [double value] after each rule:

grammar Exp;

eval returns [double value]
    :    additionExp
    ;

additionExp returns [double value]
    :    multiplyExp 
         ( '+' multiplyExp 
         | '-' multiplyExp
         )* 
    ;

// ...

这几乎无需解释:每条规则都是预计将返回双倍价值。现在要与返回值 double value 交互(这不在普通Java代码块 {...} )从代码块内部,您需要在值前面添加一个美元符号

which needs little explanation: every rule is expected to return a double value. Now to "interact" with the return value double value (which is NOT inside a plain Java code block {...}) from inside a code block, you'll need to add a dollar sign in front of value:

grammar Exp;

/* This will be the entry point of our parser. */
eval returns [double value]                                                  
    :    additionExp { /* plain code block! */ System.out.println("value equals: "+$value); }
    ;

// ...

这是语法,但现在使用Java已添加的代码:

Here's the grammar but now with the Java code added:

grammar Exp;

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;}
    ;

由于我们的 eval 规则现在返回double,将您的ANTLRDemo.java更改为:

and since our eval rule now returns a double, change your ANTLRDemo.java into this:

import org.antlr.runtime.*;

public class ANTLRDemo {
    public static void main(String[] args) throws Exception {
        ANTLRStringStream in = new ANTLRStringStream("12*(5-6)");
        ExpLexer lexer = new ExpLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ExpParser parser = new ExpParser(tokens);
        System.out.println(parser.eval()); // print the value
    }
}

再次(重新)生成语法中的新词法分析器和解析器(1),编译所有类(2)并运行ANTLRDemo(3):

Again (re) generate a fresh lexer and parser from your grammar (1), compile all classes (2) and run ANTLRDemo (3):

// *nix/MacOS
java -cp antlr-3.2.jar org.antlr.Tool Exp.g   // 1
javac -cp .:antlr-3.2.jar ANTLRDemo.java      // 2
java -cp .:antlr-3.2.jar ANTLRDemo            // 3

// Windows
java -cp antlr-3.2.jar org.antlr.Tool Exp.g   // 1
javac -cp .;antlr-3.2.jar ANTLRDemo.java      // 2
java -cp .;antlr-3.2.jar ANTLRDemo            // 3

您现在可以看到表达式 12 *(5-6)的结果打印到您的控制台!

and you'll now see the outcome of the expression 12*(5-6) printed to your console!

再次:这是一个非常简短的解释。我鼓励您浏览 ANTLR wiki 并阅读一些内容。教程和/或玩我刚刚发布的内容。

Again: this is a very brief explanation. I encourage you to browse the ANTLR wiki and read some tutorials and/or play a bit with what I just posted.

祝你好运!

编辑:

这篇文章显示了如何扩展上面的示例,以便可以提供 Map< String,Double> 来保存提供的表达式中的变量。

This post shows how to extend the example above so that a Map<String, Double> can be provided that holds variables in the provided expression.

Q& A 演示如何使用 ANTLR4 创建简单的表达式解析器和赋值器。

And this Q&A demonstrates how to create a simple expression parser, and evaluator using ANTLR4.

使此代码与当前版本的Antlr一起使用(2014年6月)我需要做一些改变。 ANTLRStringStream 需要成为 ANTLRInputStream ,从更改所需的返回值parser.eval() parser.eval()。value ,我需要删除 WS 子句最后,因为不再允许在词法分析器操作中显示诸如 $ channel 之类的属性值。

To get this code working with a current version of Antlr (June 2014) I needed to make a few changes. ANTLRStringStream needed to become ANTLRInputStream, the returned value needed to change from parser.eval() to parser.eval().value, and I needed to remove the WS clause at the end, because attribute values such as $channel are no longer allowed to appear in lexer actions.

这篇关于ANTLR:有一个简单的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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