基于XML的编程语言 [英] XML based programming language

查看:71
本文介绍了基于XML的编程语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我最近不得不处理基于XML的

编程语言。 (程序是以编程方式生成的
。)


XML导致两级解析问题:首先

将xml解析成令牌,然后解析令牌

来掌握它们的含义(基于语义<​​br />
的编程语言)。


基本上,我使用elementtree作为复杂的词法分析器。并写了一个

递归下降解析器来执行语义分析和

解释。

(效果很好。)


但是我一直想知道:解析器生成器工具是否存在我可以用来代替写b / b
手动递归下降解析器吗?


祝你好运,

Stefaan。

Hello,

I have recently had to deal with an XML-based
programming language. (The programs are
generated programmatically.)

XML leads to a "two-level" parsing problem: first
parse the xml into tokens, then parse the tokens
to grasp their meaning (based on the semantics
of the programming language).

Basically, I used elementtree as a sophisticated "lexer" and wrote a
recursive descent parser to perform the semantic analysis and
interpretation.
(It works great.)

But I keep wondering: do parser generator tools
exist that I could have used instead of writing
the recursive descent parser manually ?

Best regards,
Stefaan.

推荐答案

stefaan schrieb:
stefaan schrieb:

您好,


我最近不得不处理基于XML的

编程语言。 (程序是以编程方式生成的
。)


XML导致两级解析问题:首先

将xml解析成令牌,然后解析令牌

来掌握它们的含义(基于语义<​​br />
的编程语言)。


基本上,我使用elementtree作为复杂的词法分析器。并写了一个

递归下降解析器来执行语义分析和

解释。

(效果很好。)


但我一直想知道:解析器生成器工具是否存在我可以使用的
而不是手动编写递归下降解析器?b

$ b
Hello,

I have recently had to deal with an XML-based
programming language. (The programs are
generated programmatically.)

XML leads to a "two-level" parsing problem: first
parse the xml into tokens, then parse the tokens
to grasp their meaning (based on the semantics
of the programming language).

Basically, I used elementtree as a sophisticated "lexer" and wrote a
recursive descent parser to perform the semantic analysis and
interpretation.
(It works great.)

But I keep wondering: do parser generator tools
exist that I could have used instead of writing
the recursive descent parser manually ?



你还没有编写递归下降解析器。至少不在

的意义上。


解析器(递归下降或其他)将采用
$ b中写的字符串$ b它接受的语言,在编程语言领域

通常返回一个抽象语法树。在哪一个工作 -

代码生成,解释,优化。


你写的东西通常被称为减速器,遍及
树,重写它,将其转换为解释和诸如此类的东西。


我一直在使用使用XML-Schema或DTD的工具并生成

来自它的类型对象,它们能够从

XML流中反序列化。这些工具中更好的生成访问者和/或匹配器,

,它们基本上是通过类型化方法在

文档顺序中遍历生成的对象树的对象。像这样的东西(java伪代码):


类访问者{


公众访问(对象o){

if(o instanceof Expr){

visit((Expr)o);

else if(o instanceof SubExpr){

visit(( SubExpr)o);

}


公众访问(Expr e){

for(SubExpr se:e.subExpressions) {

访问(se);

}

}


公众访问(SubExpr e) {

//什么都不做

}


}

这个访客你可以继承例如,创建一个翻译。


所有这些在理论上也可以在python中使用。使用多方法

可以创建调度,等等。


我不太相信它真的值得付出努力。一个简单的

基于标签名称的调度方案,以及非常好的

ElementTree-api在我眼中就足够了。然后你可以这样做:

class访客(ojbect):


def访问(自我,节点):

descent = True

if getattr(self," visit_%s"%node.tag):

descent = getattr(self," visit_%s"%node .tag)(节点)

如果下降:

节点中的孩子:

self.visit(孩子)

然后是元素expr你可以定义


类Foo(访问者):

def visit_expr(self,node):

...

HTH,


Diez

You haven''t written a recursive descent parser. At least not in the
sense of the word.

A parser (recursive descent or otherwise) will take a string written in
the language it accepts, and in the field of programming languages
usually returns an abstract syntax tree. On which one works - for
code-generation, interpretation, optimization.

What you wrote is usually called a reducer, the part that traverses the
tree, rewriting it, transforming it for interpretation and whatnot.

I''ve been working with tools that use a XML-Schema or DTD and generate
typed objects from it, that are capable of being deserialized from a
XML-stream. The better of these tools generate visitors and/or matchers,
which basically are objects that traverse the generated object tree in
document order, via typed methods. Something like this (java pseudocode):

class Visitor {

public visit(Object o) {
if(o instanceof Expr) {
visit((Expr)o);
else if(o instanceof SubExpr) {
visit((SubExpr)o);
}

public visit(Expr e) {
for(SubExpr se : e.subExpressions) {
visit(se);
}
}

public visit(SubExpr e) {
// not doing anything
}

}
This visitor you can then subclass, for example to create an interpreter.

All of this is theoretically possible in python, too. Using multimethods
one can create the dispatching, and so forth.

I''m just not too convinced that it really is worth the effort. A simple
tag-name-based dispatching scheme, together with the really nice
ElementTree-api suffices in my eyes. Then you could do something like this:
class Visitor(ojbect):

def visit(self, node):
descent = True
if getattr(self, "visit_%s" % node.tag):
descent = getattr(self, "visit_%s" % node.tag)(node)
if descent:
for child in node:
self.visit(child)
Then for an element "expr" you could define

class Foo(Visitor):
def visit_expr(self, node):
...
HTH,

Diez


谢谢Diez回答。

据我所知,它或多或少与我所拥有的相对应。


但我的问题可能更多:


" ;如果elementtree是lex,则什么是yacc。 ?
Thank you Diez for answering.
As far as I can see, it more or less corresponds to what I have.

But my question was perhaps more this:

"If elementtree is "lex", what is "yacc" ? "


stefaan写道:
stefaan wrote:

谢谢Diez回答。

据我所知,它或多或少与我所拥有的相对应。


但我的问题可能更多:


" ;如果elementtree是lex,则什么是yacc。 ? "
Thank you Diez for answering.
As far as I can see, it more or less corresponds to what I have.

But my question was perhaps more this:

"If elementtree is "lex", what is "yacc" ? "



Elementtree不是lex。你在这里比较苹果和橘子。 Lex

tokenizes,yacc创建树。两者都包含在XML本身中 - 它是

定义了内置于elementtree中的标记化和解析。所以,

elemnttree是XML的lex _and_ yacc。如果您的语言是用

XML编写的,那就是它的全部内容。


Diez

Elementtree isn''t lex. You are comparing apples and oranges here. Lex
tokenizes, yacc creates trees. Both of is covered in XML itself - it''s
defined the tokenization and parsing, built into elementtree. So,
elemnttree is lex _and_ yacc for XML. And if your language is written in
XML, that''s all there is to it.

Diez


这篇关于基于XML的编程语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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