From b94bd95e0be2706effc116a4f885cee18cd6a367 Mon Sep 17 00:00:00 2001 From: Andreas Abel Date: Wed, 24 Mar 2021 18:22:53 +0100 Subject: [PATCH] [ re #349 ] C parser: code segment after %union, entrypoints in the end Reorganized the input for bison a bit, to prepare for removal of global YY_RESULT variables. Got rid of the forward declaration for yyerror. Entrypoints can be at the end of the file, since nothing depends on them. --- source/src/BNFC/Backend/C/CFtoBisonC.hs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/source/src/BNFC/Backend/C/CFtoBisonC.hs b/source/src/BNFC/Backend/C/CFtoBisonC.hs index 7ca0af875..360ac1f2e 100644 --- a/source/src/BNFC/Backend/C/CFtoBisonC.hs +++ b/source/src/BNFC/Backend/C/CFtoBisonC.hs @@ -48,6 +48,7 @@ cf2Bison :: RecordPositions -> String -> CF -> SymMap -> String cf2Bison rp name cf env = unlines [ header name cf , union (allParserCatsNorm cf) + , unionDependentCode name , "%token _ERROR_" , tokens (map fst $ tokenPragmas cf) env , declarations cf @@ -59,7 +60,7 @@ cf2Bison rp name cf env = unlines , prRules (rulesForBison rp cf env) , "%%" , "" - , errorHandler name + , entryCode name cf ] header :: String -> CF -> String @@ -82,16 +83,12 @@ header name cf = unlines , "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);" , "" , concatMap reverseList $ filter isList $ allParserCatsNorm cf , "/* Global variables holding parse results for entrypoints. */" , unlines $ map parseResult $ nub $ map normCat eps - , unlines $ map (parseMethod cf name) eps , "/* End C preamble code */" , "%}" ] @@ -102,6 +99,16 @@ header name cf = unlines -- Found old comment: -- -- M.F. 2004-09-17 changed allEntryPoints to allCatsIdNorm. Seems to fix the [Ty2] bug. +-- | Code that needs the @YYSTYPE@ defined by the @%union@ pragma. +-- +unionDependentCode :: String -> String +unionDependentCode name = unlines + [ "%{" + , errorHandler name + , "int yyparse(void);" + , "%}" + ] + -- | Generates declaration and initialization of the @YY_RESULT@ for a parser. -- -- Different parsers (for different precedences of the same category) @@ -124,6 +131,13 @@ errorHandler name = unlines , "}" ] +-- | Parser entry point code. +-- +entryCode :: String -> CF -> String +entryCode name cf = unlines $ map (parseMethod cf name) eps + where + eps = toList (allEntryPoints cf) + --This generates a parser method for each entry point. parseMethod :: CF -> String -> Cat -> String parseMethod cf name cat = unlines $