Antlr4-JS actions in grammar: how to access tokens and define funcs ? (direct in java, not in JS)

痞子三分冷 提交于 2019-12-07 23:08:16

问题


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.

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


回答1:


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.



来源:https://stackoverflow.com/questions/31296258/antlr4-js-actions-in-grammar-how-to-access-tokens-and-define-funcs-direct-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!