> Well, in some grammars, the base case of a left recursion may not > consume any input. From my C grammar for Rats! > > variant generic DirectAbstractDeclarator = > <FixedArray> DirectAbstractDeclarator "[":Symbol > AssignmentExpression? void:"]":Symbol > / <VariableArray> DirectAbstractDeclarator "[":Symbol > VariableLength void:"]":Symbol > / <Function> DirectAbstractDeclarator "(":Symbol > ParameterTypeList? void:")":Symbol > / <Parenthesized> void:"(":Symbol AttributedAbstractDeclarator > void:")":Symbol > / <Empty> null > ; > > The last alternative does not touch the input and just returns a null > value, which is the desired behavior for direct abstract declarators. > So, Bryan, your condition is too restrictive. Any suggestions? > > Robert >
The loop in the example code I gave is a do-while loop, so the first iteration through doesn't invoke the length test. Thus I believe a null match will work, it is just that any future left recursive matches for the same rule at the same point must match with additional length. Although looking at the code again, I notice that I forgot the return after the loop, so perhaps that is the source of the confusion, so with Bryan's change and the fixed return it now look like: // Checks for memorized version, and if not found, does the actual parse // Returns the length of the match, or -1 if no match. int check_parse(rule, position) key = tuple(rule, postion); if find(memory[key]) return memory[key]; else cur = -1; do memory[key] = cur; prev = cur; cur = do_pase(rule, position); while(cur != -1 && cur > prev) return cur; end end _______________________________________________ PEG mailing list PEG@lists.csail.mit.edu https://lists.csail.mit.edu/mailman/listinfo/peg