首页 > 解决方案 > 允许任意规则顺序的语法

问题描述

我正在(尝试)设计一种特定领域的语言(我称之为“华氏”)来设计引用风格。

用华氏温度编写的程序:

这是一个简化但有效的示例:

macro m1
  "Hello World!"
end

macro m2
  "Hello World!"
end

citation
  "Hello World!"
end

该语法将把上面的代码识别为语法正确:

style = macro* citation

(*  example of macro definition

    macro hw
        "Hello World!"
    end

    *)

macro = <'macro'> #'[a-z0-9]+' statement+ end

citation = <'citation'> statement+ end

statement = #'".*?"'

<end> = <'end'>

然而,“块”的顺序(例如macroor citation)应该无关紧要。

问题:我应该如何更改我的语法,以便它识别以下程序在语法上是正确的?

macro m1
  "Hello World!"
end

citation
  "Hello World!"
end

macro m2
  "Hello World!"
end

PS:我打算添加其他顺序也无关紧要的可选块。

标签: clojuredslcontext-free-grammarinstaparse

解决方案


对于 0..n 规则,您可以将它们放在citation. 例如

style = tools* citation tools*
tools = macro | foo | bar
...

推荐阅读