简体   繁体   English

使用琶音编写 PEG 递归表达式语法有困难

[英]Difficulty writing PEG recursive expression grammar with Arpeggio

My input text might have a simple statement like this:我的输入文本可能有这样一个简单的语句:

aircraft

In my language I call this a name which represents a set of instances with various properties.在我的语言中,我称其为代表一组具有各种属性的实例的名称 It yields an instance_set of all aircraft instances in this example.它产生在这个例子中所有的飞机实例的instance_set。

I can apply a filter in parenthesis to any instance_set:我可以将括号中的过滤器应用于任何 instance_set:

aircraft(Altitude < ceiling)

It yields another, possibly reduced instance_set.它产生另一个可能减少的 instance_set。 And since it is an instance set, I can filter it yet again:由于它是一个实例集,我可以再次过滤它:

aircraft(Altitude < ceiling)(Speed > min_speed)

I foolishly thought I could do something like this in my grammar:我愚蠢地认为我可以在语法中做这样的事情:

instance_set = expr
expr = source / instance_set
source = name filter?

It parses my first two cases correctly, but chokes on the last one:它正确解析了我的前两个案例,但在最后一个案例中窒息:

 aircraft(Altitude < ceiling)(Speed > min_speed)

The error reported being just before the second open paren.错误报告就在第二个打开的括号之前。

Why doesn't Arpeggio see that there is just a filtered instance_set which is itself a filtered instance set?为什么 Arpeggio 看不到只有一个过滤的 instance_set,它本身就是一个过滤的实例集?

I humbly submit my appeal to the peg parsing gods and await insight...我虚心向钉钉解析神提交我的呼吁,等待洞察力......

Your first two cases both match source .您的前两个案例都匹配source Once source is matched, it's matched;一旦source匹配,它就匹配; that's the PEG contract.这就是 PEG 合同。 So the parser isn't going to explore the alternative.所以解析器不会探索替代方案。

But suppose it did.但假设确实如此。 How could that help?这有什么帮助? The rule says that if an expr is not a source , then it's an instance_set .规则说,如果expr不是source ,那么它就是instance_set But an instance_set is just an expr .但是instance_set只是一个expr In other words, an expr is either a source or it's an expr .换句话说, expr要么是source要么是expr Clearly the alternative doesn't get us anywhere.显然,替代方案并没有让我们到任何地方。

I'm pretty sure Arpeggio has repetitions, which is what you really want here:我很确定琶音有重复,这就是你在这里真正想要的:

source = name filter*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM