Antlr4 Javascript 访问者 [英] Antlr4 Javascript Visitor

查看:27
本文介绍了Antlr4 Javascript 访问者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在 Antlr4 访问者的帮助下开发 JavaScript 编译器.我已经用 Java 实现了这一点,但无法弄清楚如何在 JavaScript 中做到这一点.也许有人可以回答我几个问题?

I'm currently trying to develope a JavaScript Compiler with the help of an Antlr4 Visitor. I've got this already implemented with Java but cannot figure out how to do this in JavaScript. Probably somebody can answer me a few questions?

1:在 Java 中有一个 Visitor.visit 函数.如果我是对的,这在 Javascript 中是不可能的.有没有办法解决这个问题?

1: In Java there is a Visitor.visit function. If im right this isn't possibile with Javascript. Is there a work around for this?

2:我的 Javascript 访问者获得了所有生成的访问函数,但是当我使用 console.log(ctx) 时,上下文是未定义的.知道为什么吗?

2: My Javascript Visitor got all the generated visiting functions but when I use console.log(ctx) the context is undefined. Any idea why?

从 SimpleVisitor.js 中提取:

Extract from the SimpleVisitor.js:

// Visit a parse tree produced by SimpleParser#parse.
SimpleVisitor.prototype.visitParse = function(ctx) {
        console.log(ctx);
};

主js文件:

var antlr4 = require('lib/antlr4/index');
var SimpleLexer = require('antlr4/SimpleLexer');
var SimpleParser = require('antlr4/SimpleParser');
var SimpleVisitor = require('antlr4/SimpleVisitor');    

var input = "double hallo = 1;";
var chars = new antlr4.InputStream(input);
var lexer = new SimpleLexer.SimpleLexer(chars);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new SimpleParser.SimpleParser(tokens);
var visitor = new SimpleVisitor.SimpleVisitor();
parser.buildParseTrees = true;
var tree = parser.parse();

visitor.visitParse();

这可能足以开始......

This is probably enough to start with ...

布鲁诺

可能上下文未定义,因为我不带参数调用函数,但是我从哪里获得开始"-上下文?

Probably the context is undefined because I call the function without arguments but where do I get the "starting"-context?

编辑 2:

所以我想我知道这应该如何解决.剩下的一个问题我如何确定在每个访问者函数中接下来调用哪个规则?

So I think I get the idea how this should work out. One Question remaining how do I determine which rule to call next inside each visitor function?

推荐答案

访问者背后的基本思想是您必须自己处理所有逻辑.为此,我使用 antlr 生成了访问者.我自己的访问者覆盖了我实现逻辑所需的所有功能.

The basic idea behind the visitor is that you have to handle all the logic by yourself. To do this I generated the visitor using antlr. My own visitor overrides all functions that I need to implement my logic.

  1. 创建词法分析器、标记、...

  1. create lexer, tokens, ...

var antlr4 = require('antlr4/index');
var SimpleJavaLexer = require('generated/GrammarLexer');
var SimpleJavaParser = require('generated/GrammarParser');
var SimpleJavaVisitor = require('generated/GrammarVisitor');
var Visitor = require('./Visitor');

var input = "TestInput";
var chars = new antlr4.InputStream(input);
var lexer = new GrammarLexer.GrammarLexer(chars);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new GrammarParser.GrammarParser(tokens);
var visitor = new Visitor.Visitor();
parser.buildParseTrees = true;
var tree = parser.parse();

  • 并调用您的入口函数

  • and call your entry function

    visitor.visitTest(tree);
    

  • 在新访问者中,您需要实现新逻辑以确定接下来要调用哪个函数(正确的上下文作为参数很重要)

  • inside your new visitor you need to implement your new logic to determine which function to call next (the right context as argument is important)

    var GrammarVisitor =     require('generated/GrammarVisitor').GrammarVisitor;
    
    function Visitor () {
      SimpleJavaVisitor.call(this);
      return this;
    };
    
    Visitor.prototype = Object.create(GrammarVisitor.prototype);
    Visitor.prototype.constructor = Visitor;
    Visitor.prototype.visitTest = function(ctx) {
        // implement logic to determine which function to visit
        // then call next function and with the right context
        this.visitBlock(ctx.block());
    };
    

  • 我希望你能理解我的基本想法.如果有人有任何问题,请发表评论.

    I hope you can understand my basic idea. If anybody got any questions just comment.

    这篇关于Antlr4 Javascript 访问者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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