Antlr4 Javascript访问者 [英] Antlr4 Javascript Visitor

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

问题描述

我目前正在尝试在Antlr4 Visitor的帮助下开发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?

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

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天全站免登陆