Is there a way to get current number of tokens parsed in stack in yacc

StackOverflow https://stackoverflow.com/questions/23208235

  •  07-07-2023
  •  | 
  •  

سؤال

I am running into parser stack overflow in yacc. I am not sure how is the current parser stack size determined. IS there a way to get current parser stack size, so that once the number of tokens reaches the maximum stack depth, an error can be reported? Is there a variable in yacc that holds this information?

هل كانت مفيدة؟

المحلول

There is no standard way to get the parser stack size, although obviously it is internally available since the parser is capable of producing a stack overflow error (without segfaulting or otherwise invoking undefined behaviour). You don't need to check this yourself; you simply need to print the error message provided to yyerror; if the stack overflows, the error message will mention that fact.

There are a few ways you can end up with a version of yàcc which doesn't resize the stack. One is the use of the public domain Berkeley yacc, often called byacc; the version I have kicking around (from 1993) sets the default stack size to 500.

Another possibility is to use Gnu bison, compiling the result with a C++ compiler; by default, this will make the stack non-relocatable since bison doesn't know whether the semantic value union is trivially copyable. (Newer versions of bison might not have this restriction.) By default, the initial bison stack size is 200.

A common way to blow up stacks is to use right recursion for long lists. A particularly bad one is some variant on the following:

program: /* empty */
       | statement program
       ;

which will cause the parser stack overflow if a "program" is too long. It's usually sufficient to just change that to left recursion:

program: /* empty */
       | program statement
       ;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top