Skip to content

Commit

Permalink
[ #163 ] Set Bison max stack size to 10.000.000 (for right recursion)
Browse files Browse the repository at this point in the history
Default is only 10.000, which seems an anachronism in 2019.

Parsing right-recursive categories needs O(n) stack size, thus,
YYMAXDEPTH is a hard limit on the depth of right recursion.

BNFC attempts to rewrite right recursion to left recursion, but only
when it can be done easily.  Thus, we might still have right recursion
left in the generated Bison grammar.
  • Loading branch information
andreasabel committed Nov 23, 2019
1 parent b2e8ef1 commit 4aff753
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
45 changes: 24 additions & 21 deletions source/src/BNFC/Backend/C/CFtoBisonC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,30 @@ cf2Bison rp name cf env

header :: String -> CF -> String
header name cf = unlines
[ "/* This Bison file was machine-generated by BNFC */"
, "%locations"
, "%{"
, "#include <stdlib.h>"
, "#include <stdio.h>"
, "#include <string.h>"
, "#include \"Absyn.h\""
, "typedef struct " ++ name ++ "_buffer_state *YY_BUFFER_STATE;"
, "YY_BUFFER_STATE " ++ name ++ "_scan_string(const char *str);"
, "void " ++ name ++ "_delete_buffer(YY_BUFFER_STATE buf);"
, "extern int yyparse(void);"
, "extern int yylex(void);"
, "extern int " ++ name ++ "_init_lexer(FILE * inp);"
-- this must be deferred until yylloc is defined
, "extern void yyerror(const char *str);"
, ""
, unlines $ map parseResult $ nub $ map normCat eps
, unlines $ map (parseMethod name) eps
, concatMap reverseList $ filter isList $ allCatsNorm cf
, "%}"
]
[ "/* This Bison file was machine-generated by BNFC */"
, "%locations"
, "%{"
, "#include <stdlib.h>"
, "#include <stdio.h>"
, "#include <string.h>"
, "#include \"Absyn.h\""
, ""
, "#define YYMAXDEPTH 10000000" -- default maximum stack size is 10000, but right-recursion needs O(n) stack
, ""
, "typedef struct " ++ name ++ "_buffer_state *YY_BUFFER_STATE;"
, "YY_BUFFER_STATE " ++ name ++ "_scan_string(const char *str);"
, "void " ++ name ++ "_delete_buffer(YY_BUFFER_STATE buf);"
, "extern int yyparse(void);"
, "extern int yylex(void);"
, "extern int " ++ name ++ "_init_lexer(FILE * inp);"
-- this must be deferred until yylloc is defined
, "extern void yyerror(const char *str);"
, ""
, unlines $ map parseResult $ nub $ map normCat eps
, unlines $ map (parseMethod name) eps
, concatMap reverseList $ filter isList $ allCatsNorm cf
, "%}"
]
where
eps = allEntryPoints cf
-- Andreas, 2019-04-29, #210: Generate also parsers for CoercCat.
Expand Down
3 changes: 3 additions & 0 deletions source/src/BNFC/Backend/CPP/NoSTL/CFtoBison.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ header name cf = unlines
, "#include <stdio.h>"
, "#include <string.h>"
, "#include \"Absyn.H\""
, ""
, "#define YYMAXDEPTH 10000000" -- default maximum stack size is 10000, but right-recursion needs O(n) stack
, ""
, "int yyparse(void);"
, "int yylex(void);"
, "int yy_mylinenumber;" --- hack to get line number. AR 2006
Expand Down
3 changes: 3 additions & 0 deletions source/src/BNFC/Backend/CPP/STL/CFtoBisonSTL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ header inPackage name cf = unlines
, "#include <string.h>"
, "#include <algorithm>"
, "#include \"Absyn.H\""
, ""
, "#define YYMAXDEPTH 10000000" -- default maximum stack size is 10000, but right-recursion needs O(n) stack
, ""
, "typedef struct yy_buffer_state *YY_BUFFER_STATE;"
, "int yyparse(void);"
, "int yylex(void);"
Expand Down

0 comments on commit 4aff753

Please sign in to comment.