2

我不知道如何获取有关此的文档。我刚刚发现大多数编译器都在使用 Backus-Naur 形式来描述一种语言。

Marpa::R2perl 包中,得到这个解析算术字符串的简单示例,例如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
}

对我来说是一个好的开始吗?

4

1 回答 1

3

有一个基于Marpa 的开源XML 解析器。

于 2015-06-30T15:23:45.487 回答