Skip to content

Commit

Permalink
[ re #349 ] C parser: code segment after %union, entrypoints in the end
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andreasabel committed Apr 1, 2021
1 parent 2bfc24d commit b94bd95
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions source/src/BNFC/Backend/C/CFtoBisonC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,7 +60,7 @@ cf2Bison rp name cf env = unlines
, prRules (rulesForBison rp cf env)
, "%%"
, ""
, errorHandler name
, entryCode name cf
]

header :: String -> CF -> String
Expand All @@ -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 */"
, "%}"
]
Expand All @@ -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)
Expand All @@ -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 $
Expand Down

0 comments on commit b94bd95

Please sign in to comment.