语法中的Antlr4-JS动作:如何访问令牌和定义函数? (直接在Java中,而不在JS中) [英] Antlr4-JS actions in grammar: how to access tokens and define funcs ? (direct in java, not in JS)

查看:196
本文介绍了语法中的Antlr4-JS动作:如何访问令牌和定义函数? (直接在Java中,而不在JS中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试适应JS目标 Expr.g4 . 在此示例中,动作直接在语法中.它们包括@parser :: members中定义的实用程序函数,这些函数在规则中被调用.

I'm trying to adapt to JS target the Expr.g4 of the book. In this example, actions are directly in the grammar. They include utility functions defined in @parser::members , that are called in the rules.

该示例在Java中运行良好,但是在其JS翻译中,我有2个问题: -使动作功能在动作规则中可见 -获取功能中识别的令牌.

The example works well in Java, but in its JS translation I have 2 problem: - getting the action function visible by the action rule - getting the tokens recognized in the function.

我终于设法得到了这个可行的公式:

I finally manage to get this working formulation:

@parser::members {

  myeval =  function(left, op, right) {
        switch ( op ) {
            case Expr_jsParser.MUL : return left * right;
            case Expr_jsParser.DIV : return left / right;
            case Expr_jsParser.ADD : return left + right;
            case Expr_jsParser.SUB : return left - right;
        }
        return 0;
    };
}

(myeval是动作函数.MUL,DIV等是语法标记.Expr_jsParser是Antlr生成的类) 我很确定有一种更干净的方法可以做到这一点,不是吗?

( myeval is the action function. MUL, DIV, etc are the grammar tokens. Expr_jsParser is a class generated by Antlr) I'm pretty sure there is a cleaner way to do this, isn't it ?

推荐答案

Eric Vergnaud(Antlr的JS家伙)告诉我,这是正常行为.简而言之,Java接受跳过前缀,而JS不接受.

Eric Vergnaud (the JS guy of Antlr) tells me that it's the regular behavior. Simply, java accept skipping the prefix while JS does not.

这篇关于语法中的Antlr4-JS动作:如何访问令牌和定义函数? (直接在Java中,而不在JS中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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