ParseKit 贪婪匹配模式 [英] ParseKit greedy matching mode

查看:56
本文介绍了ParseKit 贪婪匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作类似公式验证器的东西,我正在使用 ParseKit 框架来完成它.我的方法是创建正确的语法,当在示例字符串上调用 didMatchFormula 回调方法时,我假设已找到公式,因此它是有效的.

I am making something like formula validator and I am using ParseKit framework to accomplish that. My approach is to create proper grammar and when didMatchFormula callback method is called on sample string I assume formula has been found and therefore it is valid.

然而,有一个困难 - 即使公式部分后面还包含其他字符,也会从示例字符串中检测到公式.我需要类似贪婪模式的匹配 - 整个字符串将与公式语法匹配,以便仅当字符串包含公式且没有其他字符时才调用 didMatchFormula.

There is one difficulty however - formula is detected from sample string even if it contains also other characters following formula part. I would need something like greedy mode for matching - an entire string would be matched against formula grammar so that didMatchFormula would be called only if string contains formula and no other characters.

你能给我一些提示,如何使用 PaseKit 或其他方式完成它.我不能使用正则表达式,因为我的公式会使用递归,而 regexp 不是处理这种情况的好工具.

Can you give me some hints how to accomplish that with PaseKit or in other way. I cannot use regular expressions since my formulas would use recursion and regexp is not a good tool for handling that.

推荐答案

ParseKit 的开发者在这里.

使用 ParseKit(或任何解析工具包)执行此操作的最简单和最优雅的方法可能是设计您的公式语言在每个语句后都有一个终止符字符.这与大多数类似 C 的编程语言中的 ; 终止语句的概念相同.

Probably the simplest and most elegant way to do this with ParseKit (or any parsing toolkit) is to design your formula language have a terminator char after every statement. This would be the same concept as ; terminating statements in most C-like programming languages.

以下是使用 . 作为语句终止符的示例玩具公式语言:

Here's an example toy formula language which uses . as the statement terminator:

@start = lang;
lang = statment+;
statment = Word+ terminator;
terminator = '.';

请注意我是如何设计该语言的,以便您的贪婪"需求成为该语言的固有特征.想一想——如果输入字符串以任何垃圾内容结尾,这些内容不是以 . 结尾的有效语句,我的 lang 生成将找不到匹配项,解析将失败.

Notice how I have designed the language so that your "greedy" requirement is an inherent feature of the language. Think about it – if the input string ends with any junk content which is not a valid statement ending in a ., my lang production will not find a match and the parse will fail.

使用这种类型的设计,您将不需要使用的停车工具包中的任何贪婪"功能.相反,您的需求会自然地通过您的语言设计来满足.

With this type of design, you won't need any "greedy" features in the parsking toolkit you use. Rather, your requirement will be naturally met by your language design.

这篇关于ParseKit 贪婪匹配模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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