Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top