Question

Hi I am creating grammar for some formula which is using recursion. The formula is much more complicated but for now I test only some part of it. After calling parse method it crashes on allMatchesFor method. The stack trace is full of allMatchesFor invocations so it looks like it is in infinite loop Where is the problem. Is it in my grammar construction logic or else ? How can I accomplish similar thing to avoid this crash

My grammar is like:   

@start = formula;
formula = valueTypeResult;
valueTypeResult = (value | concept | arithmeticStatement);
arithmeticStatement = valueTypeResult (arithmeticOperator valueTypeResult)+;
value = 'V';
concept = 'C';
arithmeticOperator = 'A';

Can you please tell me how to create grammars which use recursion ?

Thanks

Was it helpful?

Solution

Developer of ParseKit here.

ParseKit has no problem with recursion, but your grammar is incorrect. This is more of a BNF grammar problem than a ParseKit problem.

Here's the correct version of what I believe you are attempting with your grammar above:

@start = formula;
formula = arithmeticStatement;
arithmeticStatement = valueTypeResult (arithmeticOperator valueTypeResult)+;
valueTypeResult = (value | concept);
value = 'V';
concept = 'C';
arithmeticOperator = 'A';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top