我如何在javascript中为此编写解释器? [英] How would I write an interpreter for this in javascript?

查看:62
本文介绍了我如何在javascript中为此编写解释器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在javascript中为脚本语言编写一个解释器。可以运行此脚本的东西:

I want to write an interpreter for a scripting language in javascript. Something that could run this script:

set myVariable to "Hello World"
repeat 5 times with x
begin
    set myVariable to myVariable plus " " plus x
end
popup "myVariable is: " plus myVariable

以上的等价javascript将是:

The equivalent javascript of the above would be:

var myVariable = "Hello World";
for (var x=1; x<=5; x++) {
    myVariable += " " + x;
}
alert("myVariable is: " + myVariable);

我不想从一个翻译到另一个,我想写一个javascript程序到直接解释并执行脚本。怎么办呢?

I don't want to translate from one to the other, I want to write a javascript program to interprete and execute the script directly. How can this be done?

更新:

我正在寻找一个教程(最好用javascript但是C会这样做,这将引导我完成这个。我想我正在寻找一个不使用任何外部工具的工具,因为工具似乎是我的问题。我不想使用调用库和一堆预构建代码的东西。我希望看到整件事,从头开始。

I'm looking for a tutorial (preferably in javascript but C would do) that will walk me through this. I guess I'm looking for one that does not use any external tools as the tools seem to be my issue. I don't want to use something that calls libraries and a bunch of pre-built code. I want to see the whole thing, done from scratch.

推荐答案

好的,我实际上会尝试解决这个问题。 ..虽然我无法将所有你需要知道的东西提炼成几句甚至段落。

OK, I'll actually try to tackle this question somewhat... although there is no way I could possibly distill everything you need to know into a few sentences or even paragraphs.

首先,您应该了解/熟悉构建编译器所涉及的内容。你说你想解释代码 - 但是,我认为你真正想要的是将代码编译为Javascript(以及Javascript中)。

First, you should gain an understanding / familiarity with what's involved in building a compiler. You say you want to "interpret" the code - but, I think what you really want is to compile the code to Javascript (and in Javascript as well).

维基百科有一个关于这个主题的精彩页面: http ://en.wikipedia.org/wiki/Compiler

Wikipedia has a great page on the topic: http://en.wikipedia.org/wiki/Compiler

事情的要点是:

1。)将文本(源代码)转换为某种内存数据结构(抽象语法树 - AST )实际上让你可以推断出你所获得的程序的结构。

1.) Convert the text (source code) into some sort of in-memory data structure (abstract syntax tree - AST) that actually lets you reason about the structure of the program you've been given.

2。)给定该结构,产生输出(在这种情况下为Javascript)。

2.) Given that structure, produce your output (Javascript, in this case).

进一步细分步骤1 - 定义 语法 例如;这种新语言的有效语法是什么,什么不是?通常情况下,最好用纸上的 BNF 来推断这类事情。 (或者您使用的工具所使用的任何语法 - 尽管(E)BNF是标准)。关于这一步骤的挑战性部分不仅是完成解析源代码的繁琐工作 - 还要确保你已经提出了明确的易于解析的语法。这两个要求实际上比你想象的要难得多。

To break down step 1 a bit further - Define your grammar e.g.; what is valid syntax in this new language of yours, and what is not? Typically, it's best to reason about this sort of thing with BNF on paper (or whatever syntax the tools you use prefer - although (E)BNF is the standard). The challenging part about this step is not only doing the grunt work of parsing the source code - but also making sure you've come up with a grammar that is unambiguous and readily parsable. Those two requirements are actually somewhat more difficult to nail down than you might think.

我在C#中构建了一个LALR解析器生成器 - 而且,我可以告诉你,除非你之前已经构建了一个,否则这不是一项微不足道的任务。除此之外,有很多好的,除非你真的想知道它是如何为它的乐趣而工作的,或者因为你是这样的事情,使用解析器更有意义 - 别人写的发电机。关于解析器生成器的好处在于它将采用您已经想到的语法定义将其转换为将在另一端吐出AST的程序。这是为您完成的大量工作。事实上,Javascript有一些:

I've built an LALR parser generator in C# - and, I can tell you, unless you've built one before, it's not a trivial task. Beyond that, there are so many good ones, that, unless you are really wanting to know how it works for the fun of it or because you're into that kind of thing, it makes a whole lot more sense to use a parser-generator someone else wrote. The great thing about a parser generator is that it will take that syntax definition you've come up with convert it into a program that will spit out an AST the other end. That's a HUGE amount of work that was just done for you. And, in fact, there are a few for Javascript:

http://www.google.com/search?q=javascript+parser+generator

PEG.js - 用于JavaScript的解析器生成器

JS / CC Parser Generator项目主页

开到第2步。这一步对于像中缀表达式这样的东西来说可能是非常基础的 - 或者它可能变得非常复杂。但是,给定AST的想法是将其转换为输出格式(Javascript)。通常,您需要检查解析器中发生的简单语法检查未检查的内容。例如,即使在您的示例代码中,也有许多可能出错的事情。在您说加x 的部分,如果开发人员从未定义 x ,会发生什么?这应该是一个错误吗?应该 x 默认为某个值?这是您的语言真正发挥作用的地方。并且,要回溯一分钟 - 您需要花费在此步骤上的时间 - 而不是解析器。使用工具 - 认真。你正在谈论开始一个大型且具有挑战性的项目 - 不要让自己变得更难。要添加所有这些 - 通常需要通过AST进行多次传递。例如,第一遍可以查找并设置模块定义,第二遍可以查找并设置命名空间,另一遍可以设置类等。最终应用程序的结构的这些进一步改进在稍后使用确定对特定类/变量/模块/等的引用是否有效的步骤(实际存在或可以引用)。

On to step 2. This step can be very basic for something like infix expressions - or it can get very complex. But, the idea is, given the AST, "convert" it into your output format (Javascript). Typically you need to check for things that aren't checked for by the "simple" syntax checking that occurs in the parser. For example, even in your sample code there is a whole number of things that could possibly go wrong. In the part where you say plus x what would happen if the developer never defined x? Should this be an error? Should x default to some value? This is where your language really comes to life. And, to back-track for a minute - your time needs to be spent on this step - not on the parser. Use a tool for that - seriously. You're talking about starting a large and challenging project - don't make it even harder for yourself. To add to all this - there is often a need to make multiple "passes" through the AST. For example, the first pass may look for and setup "module" definitions, the second pass may look for and setup "namespaces", another pass may setup classes, etc. These further refinements of the structure of the final application are used in later steps to determine if a reference to a particular class/variable/module/etc is valid (it actually exists or can be referenced).

关于编译器,有一些非常棒的书。臭名昭着的龙书就是其中之一。

There are a few really great books on compilers. The infamous "dragon book" is one.

这篇关于我如何在javascript中为此编写解释器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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