arithy / packcc

A parser generator for C
Other
346 stars 28 forks source link

parser reads more data than necessary #59

Closed andrewchambers closed 6 months ago

andrewchambers commented 2 years ago

If you have a grammar like:

foo <- "foobar\n" / "foo\n"

and input the string "foo\n..........", then packcc will read 8 bytes for this rule when it really only needed to read 4.

As an example of where this is a problem, consider an interactive parser where the user enters data line by line, if the user types "foo\n", the parser will request two or more lines of input from the user when only one was actually needed.

arithy commented 2 years ago

Can you work around this issue by modifying the PEG as shown below?

foo <- "foo\n" / "foobar\n"

The shorter string has to be put before the longer ones.

andrewchambers commented 2 years ago

That's a good suggestion but maybe does not work for more complicated grammars where such interactions are less obvious.

In the end I have rewritten my code to buffer lines before passing it to the parser after a known terminator, this issue no longer affects me, I don't mind closing it.

arithy commented 6 months ago

@andrewchambers , I have fixed this issue. I inform you, just in case.