ParseKit greedy matching mode

南楼画角 提交于 2019-12-11 12:56:23

问题


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.

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.

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.


回答1:


Developer of ParseKit here.

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 = '.';

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.



来源:https://stackoverflow.com/questions/13939642/parsekit-greedy-matching-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!