-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Review right recursion rules in our parser #2884
Comments
I had a quick poke at the first example above, and it's pretty easy to avoid the right recursion there at least. It seems to be used to create a linked list of the parameters, and we can achieve that with a left recursive rule which tracks both the list so far and the current last entry, e.g. like so:
|
Testcase:
Current SWIG exits with code 0 but doesn't generate any output files. With the patch above it works. Nobody is realistically going to work on 10,000 typemaps at once like this, and with e.g. 1,000 it seems to work, but I'm inclined towards applying fixes like the above anyway as they don't really seem to add extra complications and eliminate a use of a bison anti-pattern. For this case at least, we could also perhaps build the list in reverse of the current order - I suspect nothing actually cares about the order, but it's hard to easily see all the places that consume the resulting linked list. |
Another artificial testcase. this one for the
SWIG quietly exits with status 0 and nothing generated. With "only" 9990 |
Incidentally, for this case both GCC and clang warn (clang by default, GCC with e.g.
I've also worked out why SWIG fails to report an error - we ought to check if |
#2876 made me look to see if there were other cases of right recursion and it is more widespread (and this is just the cases where it happens in the first rule):
This is something you're meant to avoid doing because "Right recursion uses up space on the Bison stack in proportion to the number of elements in the sequence, because all the elements must be shifted onto the stack before the rule can be applied even once."
Probably in some of the cases it's OK in practice because there are never going to be more than a handful of items which have to be stored on the stack before they can be reduced, e.g. it's unlikely we will have enough type qualifiers to cause a problem with stack depth:
type_qualifier
[...]| type_qualifier_raw type_qualifier {
[...]We should review all of these instances and eliminate the ones where there could be deep recursion in real world cases, and ideally eliminate as many others as we easily can to. Unhelpfully I can't see a trivial way to find them all - bison doesn't seem to provide a way to warn about them, and they're not trivial to grep for since (like the above case) the rule can be split over multiple lines. At least some can be grepped for though, and if need be a scan by eye through the file should identify the rest fairly quickly.
The text was updated successfully, but these errors were encountered: