我不知道如何获得有关这方面的文档。我刚刚发现,大多数编译器都使用Backus–Naur形式来描述一种语言。
从Marpa::R2
Perl包中,可以得到这个解析算术字符串(如42 * 1 + 7
:)的简单示例
:default ::= action => [name,values]
lexeme default = latm => 1
Calculator ::= Expression action => ::first
Factor ::= Number action => ::first
Term ::=
Term '*' Factor action => do_multiply
| Factor action => ::first
Expression ::=
Expression '+' Term action => do_add
| Term action => ::first
Number ~ digits
digits ~ [d]+
:discard ~ whitespace
whitespace ~ [s]+
我想修改它,以便递归地解析类似XML的示例,例如:
<foo>
Some content here
<bar>
I am nested into foo
</bar>
A nested block was before me.
</foo>
并将其表达为:
>(Some content here)
>>(I am nested into foo)
>(A nested block was before me)
我可以在哪里使用此功能:
sub block($content, $level) {
for each $content line
$line = (">" x $level).$content
return $content
}
这对我来说是个好的开始吗?
有一个开源的Marpa驱动的XML解析器。