Parsecite auto-complete

Given the simple grammar of Parsekit.

@start = sentence+; sentence = 'beer' container; container = 'bottle' | 'cup'; 

If I have a partial analysis of beer , can Parsekit return possible additions to the proposal?

+6
source share
1 answer

ParseKit developer is here.

Unfortunately, in ParseKit there is no β€œfree” mechanism that will return this data itself.

However, this is exactly the task with which ParseKit is developed / is well suited. But you must write the code yourself. Something like this will work:

First, slightly modify the grammar to include the named production for 'beer' . This will simplify the callback:

 @start = sentence+; sentence = beer container; beer = 'beer'; container = 'bottle' | 'cup'; 

Then do the assembler callback for beer production.

 - (void)parser:(PKParser *)p didMatchBeer:(PKAssembly *)a { // here you could find some clever way to look up the completions for 'beer' prefix // or you can hardcode the completions yourself NSArray *completions = [NSArray arrayWithObjects:@"bottle", @"cup", nil]; // store the completions as the current assembly target a.target = completions; } 

Your client or driver code should look like this:

 PKParser *p = [PKParserFactory parserFromGrammar:g assembler:a]; PKAssembly *input = [PKAssembly assemblyWithString:@"beer"]; // get this string from the user in reality PKAssembly *result = [p bestMatchFor:input]; NSArray *completions = result.target; 

This should give you an idea of ​​how these types of things can be implemented with ParseKit. For such a small example, this solution may look like an overkill (and probably this). But for a great example in the real world, this would be a very powerful and elegant autocomplete solution.

+4
source

Source: https://habr.com/ru/post/906426/


All Articles