创建用于创建网页的脚本语言

发布于 2024-08-24 15:01:12 字数 1069 浏览 6 评论 0原文

我正在创建一种用于创建网页的脚本语言,但不知道到底从哪里开始。

我有一个如下所示的文件:


mylanguagename(main) {
       OnLoad(protected) {
              Display(img, text, link);
       }

       Canvas(public) {
              Image img: "Images\my_image.png";
                    img.Name: "img";
                    img.Border: "None";
                    img.BackgroundColor: "Transparent";
                    img.Position: 10, 10;

              Text text: "This is a multiline str#ning. The #n creates a new line.";
                   text.Name: text;
                   text.Position: 10, 25;

              Link link: "Click here to enlarge img.";
                   link.Name: "link";
                   link.Position: 10, 60;

                   link.Event: link.Clicked;
       }

       link.Clicked(sender, link, protected) {
              Image img: from Canvas.FindElement(img);
                    img.Size: 300, 300;
       }
}

...并且我需要能够使上面的文本指向 Windows 脚本主机。我知道这是可以做到的,因为不久前网上曾经有很多文档,但我现在似乎找不到它们。

有人可以帮忙,或者让我朝着正确的方向开始吗?

谢谢

I am creating a scripting language to be used to create web pages, but don't know exactly where to begin.

I have a file that looks like this:


mylanguagename(main) {
       OnLoad(protected) {
              Display(img, text, link);
       }

       Canvas(public) {
              Image img: "Images\my_image.png";
                    img.Name: "img";
                    img.Border: "None";
                    img.BackgroundColor: "Transparent";
                    img.Position: 10, 10;

              Text text: "This is a multiline str#ning. The #n creates a new line.";
                   text.Name: text;
                   text.Position: 10, 25;

              Link link: "Click here to enlarge img.";
                   link.Name: "link";
                   link.Position: 10, 60;

                   link.Event: link.Clicked;
       }

       link.Clicked(sender, link, protected) {
              Image img: from Canvas.FindElement(img);
                    img.Size: 300, 300;
       }
}

... and I need to be able to make that text above target the Windows Scripting Host. I know this can be done, because there used to be a lot of Docs on it around the net a while back, but I cannot seem to find them now.

Can somebody please help, or get me started in the right direction?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

○闲身 2024-08-31 15:01:12

您正在创建一种不存在的特定于领域的语言。您想翻译成另一种语言。您将需要一个合适的扫描器和解析器。您可能被告知要查看 antlr。 yacc/bison,或黄金。到底出了什么问题?

仅供参考,创建新语言是一个有趣的练习,但在你做这样的事情之前,你可能会问一个很好的问题:“为什么?我的新语言提供了哪些我无法得到任何其他(合理)方式的东西?” ?”

You're making a domain-specific language which does not exist. You want to translate to another language. You will need a proper scanner and parser. You've probably been told to look at antlr. yacc/bison, or gold. What went wrong with that?

And as an FYI, it's a fun exercise to make new languages, but before you do for something like this, you might ask a good solid "why? What does my new language provide that I couldn't get any other (reasonable) way?"

对风讲故事 2024-08-31 15:01:12

关于解析和语言创建需要理解的是,编写编译器/解释器主要是对输入文本进行一组数据转换。

通常,您首先会将输入文本翻译为一系列标记,每个标记代表您语言中的一个概念或文字值。

然后,您通常会根据令牌流创建一个中间结构,通常是描述所编写代码的某种树结构。

然后可以出于各种原因(包括优化)验证或修改该树结构。

完成后,您通常会将树写成其他形式 - 汇编指令,甚至是另一种语言的程序 - 事实上,C++ 的最早版本写出直接的 C 代码,然后由常规 C 编译器进行编译根本没有 C++ 知识。因此,虽然跳过程序集生成步骤可能看起来像作弊,但它背后有一个悠久而自豪的传统:)

我故意没有对特定库提出任何建议,因为了解整个过程可能比选择特定库重要得多例如,解析器技术。从长远来看,无论你使用 lex/yacc 还是 ANTLR 或者其他东西,这并不重要。它们(基本上)都能工作,并且都已在各种项目中成功使用。

即使手动进行自己的解析也不是一个坏主意,因为它将帮助您学习如何完成解析的模式,因此使用解析器生成器往往会更有意义,而不是成为巫毒的黑匣子。

The thing to understand about parsing and language creation is that writing a compiler/interpreter is primarily about a set of data transformations done to an input text.

Generally, from an input text you will first translate it into a series of tokens, each token representing a concept in your language or a literal value.

From the token stream, you will generally then create an intermediate structure, typically some kind of tree structure describing the code that was written.

This tree structure can then be validated or modified for various reasons, including optimization.

Once that's done, you'll typically write the tree out to some other form - assembly instructions or even a program in another language - in fact, the earliest versions of C++ wrote out straight C code, which were then compiled by a regular C compiler that had no knowledge of C++ at all. So while skipping the assembly generation step might seem like cheating, it has a long and proud tradition behind it :)

I deliberately haven't gotten into any suggestions for specific libraries, as understanding the overall process is probably much more important than choosing a specific parser technology, for instance. Whether you use lex/yacc or ANTLR or something else is pretty unimportant in the long run. They'll all (basically) work, and have all been used successfully in various projects.

Even doing your own parsing by hand isn't a bad idea, as it will help you to learn the patterns of how parsing is done, and so then using a parser generator will tend to make more sense rather than being a black box of voodoo.

孤蝉 2024-08-31 15:01:12

类似于 C# 的语言不容易解析 - 有一些自然的左递归规则。所以你必须使用一个可以正确处理它们的解析器生成器。 ANTLR 很适合。

如果 PEG 更适合,请尝试以下操作:http://www.meta-alternative.net/mbase。 html

Languages similar to C# are not easy to parse - there are some naturally left-recursive rules. So you have to use a parser generator that can deal with them properly. ANTLR fits well.

If PEG fits better, try this: http://www.meta-alternative.net/mbase.html

懒猫 2024-08-31 15:01:12

您想将 C# 程序转换为 JavaScript 吗? Script# 可以为您做到这一点。

So you want to translate C# programs to JavaScript? Script# can do this for you.

乖不如嘢 2024-08-31 15:01:12

与其编写自己的语言,然后运行翻译器将其转换为 Javascript,为什么不扩展 Javascript 来完成您想要它做的事情呢?

看看 jQuery - 它以非常自然和流畅的语法以许多强大的方式扩展了 Javascript。这几乎和拥有自己的语言一样好。看看人们为它创建的许多扩展,尤其是 jQuery UI。

Rather than write your own language and then run a translator to convert it into Javascript, why not extend Javascript to do what you want it to do?

Take a look at jQuery - it extends Javascript in many powerful ways with a very natural and fluent syntax. It's almost as good as having your own language. Take a look at the many extensions people have created for it too, especially jQuery UI.

初熏 2024-08-31 15:01:12

假设你真的致力于做到这一点,那么这就是你要走的路。这通常是你应该做的:source ->扫描仪->代币 ->解析器 ->语法树

1) 创建一个扫描器/解析器来解析您的语言。您需要编写一个语法来生成一个解析器,该解析器可以扫描/解析您的语法,以标记/验证它们。

我认为这里最简单的方法是使用 Irony,这将使创建解析器变得快速而容易。这是一个很好的起点

http://www.codeproject.com/KB/recipes/ Irony.aspx

2) 构建语法树 - 在这种情况下,我建议您构建一个简单的 XML 表示而不是实际的语法树,以便稍后可以遍历 DOM 的 XML 表示来吐出 VB /Java 脚本。如果您的要求很复杂(比如您想要编译它等),您可以创建 DLR 表达式树或使用代码 DOM - 但在这里我想我们正在谈论翻译器,而不是编译器。

但是嘿等等 - 如果不是为了教育目的,请考虑从一开始就将您的“脚本”表示为 xml,这样您就可以避免中间使用扫描仪/解析器,然后再吐出一些 VB/Java 脚本/Html那。

Assuming you are really dedicated to do this, here is the way to go. This is normally what you should do: source -> SCANNER -> tokens -> PARSER -> syntax tree

1) Create a scanner/ parser to parse your language. You need to write a grammar to generate a parser that can scan/parse your syntax, to tokenize/validate them.

I think the easiest way here is to go with Irony, that'll make creating a parser quick and easy. Here is a good starting point

http://www.codeproject.com/KB/recipes/Irony.aspx

2) Build a syntax tree - In this case, I suggest you to build a simple XML representation instead of an actual syntax tree, so that you can later walk the XML representation of your DOM to spit out VB/Java Script. If your requirements are complex (like you want to compile it or so), you can create a DLR Expression Tree or use the Code DOM - but here I guess we are talking about a translator, and not about a compiler.

But hey wait - if it is not for educational purposes, consider representing your 'script' as an xml right from the beginning, so that you can avoid a scanner/parser in between, before spitting out some VB/Java script/Html out of that.

卷耳 2024-08-31 15:01:12

我不想无礼……但你为什么要这样做?

为常规语言创建解析器是一项艰巨的任务。只是不要这样做。

为什么你不直接使用 html、javascript 和 css(以及上面有人建议的 jquery)

如果你不知道从哪里开始,那么你可能没有任何此类经验,也可能没有很好的理由,为什么要这样做。

我想为你解除痛苦。忘了它。这可能是个坏主意!

M。

I don't wan to be rude... but why are you doing this?

Creating a parser for a regular language is a non-trivial task. Just don't do it.

Why don't you just use html, javascript and css (and jquery as someone above suggested)

If you don't know where to begin, then you probably don't have any experience of this kind and probably you don't have a good reason, why to do this.

I want to save you the pain. Forget it. It's probably a BAD IDEA!

M.

命硬 2024-08-31 15:01:12
  1. 查看为小语言构建语言处理器。我相信这是一个非常好的介绍。事实上,两天前,当我的模板语言解析器遇到问题时,我刚刚查阅了我的副本。

  2. 如果可能的话,使用 XML。如果你想在生产中使用这个东西,你不想手动摆弄词法分析器和解析器。我已经犯过几次这样的错误了。你最终会支持你不应该支持的代码。看来你的语言主要是一种模板语言。 XML 在那里会很好地工作。正如 ASPX 文件是 XML 一样。您的服务器端块可以用 Javascript 编写,并根据需要进行修改。如果这是一个学习练习,那么一定要全部手工完成。

我认为编写自己的语言是一个很好的练习。参加大学水平的编译器写作课程也是如此。祝你好运。

  1. Check out Constructing Language Processors for Little Languages. It's a very good intro I believe. In fact I just consulted my copy 2 days ago when I was having trouble with my template language parser.

  2. Use XML if at all possible. You don't want to fiddle with a lexer and parser by hand if you want this thing in production. I've made this mistake a few times. You end up supporting code that you really shouldn't be. It seems that your language is mainly a templating language. XML would work great there. Just as ASPX files are XML. Your server side blocks can be written in Javascript, modified if necessary. If this is a learning exercise then do it all by hand, by all means.

I think writing your own language is a great exercise. So is taking a college level compiler writing class. Good luck.

橘虞初梦 2024-08-31 15:01:12

显然,您需要设计用于翻译语言的机制:解析、树构建、模式匹配、目标语言树构建、目标语言漂亮打印。
您可以尝试使用 YACC(或等效工具)完成所有这些操作,但您会发现解析
只是完整翻译器的一小部分。这意味着还有很多工作要做
要做的不仅仅是解析,这需要时间和精力。

我们的 DMS 软件重组工具包是一个商业解决方案,用于为相对温和的用户构建完整的翻译器成本。

如果您想从头开始自己做这件事作为练习,那没问题。只是要为真正需要付出的努力做好准备。

最后一点:如果你想得到一个好的结果,设计一门完整的语言是很困难的。

You obviously need machinery designed to translate langauges: parsing, tree building, pattern matching, target-language tree building, target-language prettyprinting.
You can try to do all of this with YACC (or equivalents), but you'll discover that parsing
is only a small part of a full translator. This means there's a lot more work
to do than just parsing, and that takes time and effort.

Our DMS Software Reengineering Toolkit is a commercial solution to building full translators for relatively modest costs.

If you want to do it on your own from the ground up as an exercise, that's fine. Just be prepared for the effort it really takes.

One last remark: designing a complete language is hard if you want to get a nice result.

过期情话 2024-08-31 15:01:12

我个人认为,每一次自我挑战都是好的。我确实同意其他观点,即如果您想要的是现实生活中问题的真正解决方案,那么最好坚持使用经过验证的解决方案。然而,如果正如您自己所说,您对解决这个问题有学术兴趣,那么我鼓励您继续下去。如果是这种情况,我可能会指出一些提示来帮助您走上正轨。

解析并不是一件容易的事,我们至少要花一个学期的时间。不过,它是可以学习的。我建议从 Terrence Parr 的关于语言实现模式的书开始。关于编译和解析的好书有很多,可能最让人又爱又恨的就是Dragon Book

这是相当沉重的东西,但如果你真的很喜欢这个,并且有时间,你绝对应该看一下。这就是鲁比逊漂流记的“我将靠自己实现一切”。我最近编写了一个 LR 解析器生成器,花了我不超过一个长周末的时间,但这是在阅读了大量内容并学习了完整的两个学期的编译器课程之后。

如果您没有时间或者只是不想学习“像男人一样”制作解析器,那么您可以随时尝试商业或学术解析器生成器。 ANTLR 就可以了,但是你必须学习它的元语言。我个人认为 Irony 是一个很棒的工具,特别是因为它位于 C# 内部,你可以看看源代码并自行学习。既然我们在这里,而且我根本不想做任何广告,我在 CodePlex 中发布了一个小工具,它可能对这项任务有用。亲自看看,它是开源且免费的。

最后一点提示,如果有人告诉您这是不可能的,请不要害怕。解析是一个困难的理论问题,但它并不是学不到的,而且它确实是你的投资组合中的一个很好的工具。我认为开发人员可以手动编写下降递归解析器,即使他从来不需要这样做,这对他来说非常好。如果您想彻底实现这一目标,请参加大学水平的编译器课程,一年后您就会感谢我。

Personally I think that every self-imposed challenge is good. I do agree with the other opinions that if what you want is a real solution to a real life problem, it's probably better to stick with proved solutions. However, if as you said yourself, you have an academic interest into solving this problem, then I encourage you to keep on. If this is the case, I might point a couple of tips to get you on the track.

Parsing is not really an easy task, that is way we take at least a semester of it. However, it can be learned. I would recommend starting with Terrence Parr's book on language implementation patterns. There are many great books about compiling and parsing, probably the most loved and hated been the Dragon Book.

This is pretty heavy stuff, but if you are really into this, and have the time, you should definitely take a look. This would be the Robisson Crusoe's "i'll make it all by myself approach". I have recently written an LR parser generator and it took me no more than a long weekend, but that after reading a lot and taking a full two-semesters course on compilers.

If you don't have the time or simply don't want to learn to make a parser "like men do", then you can always try a commercial or academic parser generator. ANTLR is just fine, but you have to learn its meta-language. Personally I think that Irony is a great tool, specially because it stays inside C# and you can take a look at the source code and learn for yourself. Since we are here, and I'm not trying to make any advertisement at all, I have posted a tiny tool in CodePlex that could be useful for this task. Take a look for yourself, it's open-source and free.

As a final tip, don't get scared if someone tells you it cannot be done. Parsing is a difficult theoretical problem but it's nothing that can't be learned, and it really is a great tool to have in your portfolio. I think it speaks very good of a developer that he can write an descent-recursive parser by hand, even if he never has to. If you want to pursuit this goal to its end, take a college-level compilers course, you'll thank me in a year.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文