{-# LANGUAGE NoImplicitPrelude #-} {- BNF Converter: C Main file Copyright (C) 2004 Author: Michael Pellauer Copyright (C) 2020 Andreas Abel -} module BNFC.Backend.C (makeC, bufferC, bufferH) where import Prelude hiding ((<>)) import Data.Foldable (toList) import qualified Data.Map as Map import BNFC.Utils import BNFC.CF import BNFC.Options import BNFC.Backend.Base import BNFC.Backend.C.CFtoCAbs import BNFC.Backend.C.CFtoFlexC import BNFC.Backend.C.CFtoBisonC import BNFC.Backend.C.CFtoCSkel import BNFC.Backend.C.CFtoCPrinter import BNFC.PrettyPrint import qualified BNFC.Backend.Common.Makefile as Makefile makeC :: SharedOptions -> CF -> MkFiles () makeC opts cf = do let (hfile, cfile) = cf2CAbs (linenumbers opts) prefix cf mkfile "Absyn.h" hfile mkfile "Absyn.c" cfile mkfile "Buffer.h" bufferH mkfile "Buffer.c" $ bufferC "Buffer.h" let (flex, env) = cf2flex prefix cf mkfile (name ++ ".l") flex let bison = cf2Bison (linenumbers opts) prefix cf env mkfile (name ++ ".y") bison let header = mkHeaderFile (linenumbers opts) cf (Map.elems env) mkfile "Parser.h" header let (skelH, skelC) = cf2CSkel cf mkfile "Skeleton.h" skelH mkfile "Skeleton.c" skelC let (prinH, prinC) = cf2CPrinter cf mkfile "Printer.h" prinH mkfile "Printer.c" prinC mkfile "Test.c" (ctest cf) Makefile.mkMakefile opts (makefile name prefix) where name :: String name = lang opts -- The prefix is a string used by flex and bison -- that is prepended to generated function names. -- It should be a valid C identifier. prefix :: String prefix = snakeCase_ name ++ "_" makefile :: String -> String -> String -> Doc makefile name prefix basename = vcat [ "CC = gcc -g" , "CCFLAGS = --ansi -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unneeded-internal-declaration ${CC_OPTS}" -- The @#define _POSIX_C_SOURCE 200809L@ is now placed locally in -- the generated lexer. -- , "CCFLAGS = --ansi -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unneeded-internal-declaration -D_POSIX_C_SOURCE=200809L ${CC_OPTS}" -- , "# Setting _POSIX_C_SOURCE to 200809L activates strdup in string.h." -- , "# strdup was not in the ISO C standard before 6/2019 (C2x), yet in POSIX 1003.1." -- , "# See https://en.cppreference.com/w/c/experimental/dynamic/strdup" , "" , "FLEX = flex" , "FLEX_OPTS = -P" <> text prefix , "" , "BISON = bison" , "BISON_OPTS = -t -p" <> text prefix , "" , "OBJS = Absyn.o Buffer.o Lexer.o Parser.o Printer.o" , "" , Makefile.mkRule ".PHONY" ["clean", "distclean"] [] , Makefile.mkRule "all" [testName] [] , Makefile.mkRule "clean" [] -- peteg: don't nuke what we generated - move that to the "vclean" target. [ "rm -f *.o " ++ testName ++ " " ++ unwords [ name ++ e | e <- [".aux", ".log", ".pdf",".dvi", ".ps", ""]] ] , Makefile.mkRule "distclean" ["clean"] [ "rm -f " ++ unwords [ "Absyn.h", "Absyn.c" , "Buffer.h", "Buffer.c" , name ++ ".l", "Lexer.c" , name ++ ".y", "Parser.h", "Parser.c" , "Printer.c", "Printer.h" , "Skeleton.c", "Skeleton.h" , "Test.c" , basename, name ++ ".tex" ] ] , Makefile.mkRule testName ["${OBJS}", "Test.o"] [ "@echo \"Linking " ++ testName ++ "...\"" , "${CC} ${OBJS} Test.o -o " ++ testName ] , Makefile.mkRule "Absyn.o" [ "Absyn.c", "Absyn.h"] [ "${CC} ${CCFLAGS} -c Absyn.c" ] , Makefile.mkRule "Buffer.o" [ "Buffer.c", "Buffer.h"] [ "${CC} ${CCFLAGS} -c Buffer.c" ] , Makefile.mkRule "Lexer.c" [ name ++ ".l" ] [ "${FLEX} ${FLEX_OPTS} -oLexer.c " ++ name ++ ".l" ] , Makefile.mkRule "Parser.c" [ name ++ ".y" ] [ "${BISON} ${BISON_OPTS} " ++ name ++ ".y -o Parser.c" ] , Makefile.mkRule "Lexer.o" [ "Lexer.c", "Parser.h" ] [ "${CC} ${CCFLAGS} -c Lexer.c " ] , Makefile.mkRule "Parser.o" ["Parser.c", "Absyn.h" ] [ "${CC} ${CCFLAGS} -c Parser.c" ] , Makefile.mkRule "Printer.o" [ "Printer.c", "Printer.h", "Absyn.h" ] [ "${CC} ${CCFLAGS} -c Printer.c" ] , Makefile.mkRule "Test.o" [ "Test.c", "Parser.h", "Printer.h", "Absyn.h" ] [ "${CC} ${CCFLAGS} -c Test.c" ] ] where testName = "Test" ++ name -- | Generate a test program that parses stdin and prints the AST and it's -- linearization ctest :: CF -> String ctest cf = unlines [ "/*** Compiler Front-End Test automatically generated by the BNF Converter ***/", "/* */", "/* This test will parse a file, print the abstract syntax tree, and then */", "/* pretty-print the result. */", "/* */", "/****************************************************************************/", "", "#include ", "#include ", "#include ", "", "#include \"Parser.h\"", "#include \"Printer.h\"", "#include \"Absyn.h\"", "", "void usage(void) {", " printf(\"usage: Call with one of the following argument " ++ "combinations:\\n\");", " printf(\"\\t--help\\t\\tDisplay this help message.\\n\");", " printf(\"\\t(no arguments)\\tParse stdin verbosely.\\n\");", " printf(\"\\t(files)\\t\\tParse content of files verbosely.\\n\");", " printf(\"\\t-s (files)\\tSilent mode. Parse content of files " ++ "silently.\\n\");", "}", "", "int main(int argc, char ** argv)", "{", " FILE *input;", " " ++ dat ++ " parse_tree;", " int quiet = 0;", " char *filename = NULL;", "", " if (argc > 1) {", " if (strcmp(argv[1], \"-s\") == 0) {", " quiet = 1;", " if (argc > 2) {", " filename = argv[2];", " } else {", " input = stdin;", " }", " } else {", " filename = argv[1];", " }", " }", "", " if (filename) {", " input = fopen(filename, \"r\");", " if (!input) {", " usage();", " exit(1);", " }", " }", " else input = stdin;", " /* The default entry point is used. For other options see Parser.h */", " parse_tree = p" ++ def ++ "(input);", " if (parse_tree)", " {", " printf(\"\\nParse Successful!\\n\");", " if (!quiet) {", " printf(\"\\n[Abstract Syntax]\\n\");", " printf(\"%s\\n\\n\", show" ++ dat ++ "(parse_tree));", " printf(\"[Linearized Tree]\\n\");", " printf(\"%s\\n\\n\", print" ++ dat ++ "(parse_tree));", " }", " return 0;", " }", " return 1;", "}", "" ] where cat :: Cat cat = firstEntry cf def :: String def = identCat cat dat :: String dat = identCat . normCat $ cat mkHeaderFile :: RecordPositions -> CF -> [String] -> String mkHeaderFile _ cf env = unlines $ concat [ [ "#ifndef PARSER_HEADER_FILE" , "#define PARSER_HEADER_FILE" , "" , "#include \"Absyn.h\"" , "" , "typedef union" , "{" ] , map (" " ++) unionBuiltinTokens , concatMap mkPointer $ allParserCatsNorm cf , [ "} YYSTYPE;" , "" -- https://www.gnu.org/software/bison/manual/html_node/Location-Type.html#Location-Type , "typedef struct YYLTYPE" , "{" , " int first_line;" , " int first_column;" , " int last_line;" , " int last_column;" , "} YYLTYPE;" , "" , "#define _ERROR_ 258" , mkDefines (259::Int) env , "" , "extern YYLTYPE yylloc;" , "extern YYSTYPE yylval;" , "" ] , concatMap mkFunc $ toList $ allEntryPoints cf , [ "" , "#endif" ] ] where mkDefines n [] = mkString n mkDefines n (s:ss) = ("#define " ++ s +++ (show n) ++ "\n") ++ (mkDefines (n+1) ss) mkString n = if isUsedCat cf (TokenCat catString) then ("#define _STRING_ " ++ show n ++ "\n") ++ mkChar (n+1) else mkChar n mkChar n = if isUsedCat cf (TokenCat catChar) then ("#define _CHAR_ " ++ show n ++ "\n") ++ mkInteger (n+1) else mkInteger n mkInteger n = if isUsedCat cf (TokenCat catInteger) then ("#define _INTEGER_ " ++ show n ++ "\n") ++ mkDouble (n+1) else mkDouble n mkDouble n = if isUsedCat cf (TokenCat catDouble) then ("#define _DOUBLE_ " ++ show n ++ "\n") ++ mkIdent(n+1) else mkIdent n mkIdent n = if isUsedCat cf (TokenCat catIdent) then ("#define _IDENT_ " ++ show n ++ "\n") else "" -- Andreas, 2019-04-29, issue #210: generate parsers also for coercions mkFunc c = [ identCat (normCat c) ++ " p" ++ identCat c ++ "(FILE *inp);" , identCat (normCat c) ++ " ps" ++ identCat c ++ "(const char *str);" ] -- | A tiny buffer library for string buffers in the lexer. bufferH :: String bufferH = unlines [ "/* This utility file was automatically generated by BNFC. */" , "" , "/* A dynamically allocated character buffer that grows as it is appended. */" , "" , "#ifndef BUFFER_HEADER" , "#define BUFFER_HEADER" , "" , "typedef struct buffer {" , " char* chars; /* Pointer to start of the buffer. */" , " unsigned int size; /* Buffer size (>= 1). */" , " unsigned int current; /* Next free character position (< size). */" , "} * Buffer;" , "" , "/* External interface. */" , "/************************************************************************/" , "" , "/* Create a new buffer of the given size. */" , "Buffer newBuffer (const unsigned int size);" , "" , "/* Deallocate the buffer. */" , "void freeBuffer (Buffer buffer);" , "" , "/* Deallocate the buffer, but return its content as string. */" , "char* releaseBuffer (Buffer buffer);" , "" , "/* Clear contents of buffer. */" , "void resetBuffer (Buffer buffer);" , "" , "/* Append string at the end of the buffer. */" , "void bufferAppendString (Buffer buffer, const char *s);" , "" , "/* Append single character at the end of the buffer. */" , "void bufferAppendChar (Buffer buffer, const char c);" , "" , "/* Give read-only access to the buffer content. */" , "const char* bufferContent (Buffer buffer);" , "" , "#endif" ] -- | A tiny buffer library for string buffers in the lexer. bufferC :: String -> String bufferC bufferH = unlines [ "/* This utility file was automatically generated by BNFC. */" , "" , "/* A dynamically allocated character buffer that grows as it is appended. */" , "" , "#include /* assert */" , "#include /* free, malloc */" , "#include /* fprintf */" , "#include /* size_t, strncpy */" , "#include \"" ++ bufferH ++ "\"" , "" , "/* Internal functions. */" , "/************************************************************************/" , "" , "/* Make sure the buffer can hold `n` more characters. */" , "static void bufferAllocateChars (Buffer buffer, const unsigned int n);" , "" , "/* Increase the buffer size to the new `buffer->size`. */" , "static void resizeBuffer(Buffer buffer);" , "" , "/* External interface. */" , "/************************************************************************/" , "" , "/* Create a new buffer of the given size. */" , "" , "Buffer newBuffer (const unsigned int size) {" , "" , " /* The buffer cannot be of size 0. */" , " assert (size >= 1);" , "" , " /* Allocate and initialize a new Buffer structure. */" , " Buffer buffer = (Buffer) malloc(sizeof(struct buffer));" , " buffer->size = size;" , " buffer->current = 0;" , " buffer->chars = NULL;" , " resizeBuffer(buffer);" , " buffer->chars[0] = 0;" , " return buffer;" , "}" , "" , "/* Deallocate the buffer and its content. */" , "" , "void freeBuffer (Buffer buffer) {" , " free(buffer->chars);" , " free(buffer);" , "}" , "" , "/* Deallocate the buffer, but return its content as string. */" , "" , "char* releaseBuffer (Buffer buffer) {" , " char* content = (char*) realloc (buffer->chars, buffer->current + 1);" , " free(buffer);" , " return content;" , "}" , "" , "/* Clear contents of buffer. */" , "" , "void resetBuffer (Buffer buffer) {" , " buffer->current = 0;" , " buffer->chars[buffer->current] = 0;" , "}" , "" , "/* Append string at the end of the buffer. */" , "" , "void bufferAppendString (Buffer buffer, const char *s)" , "{" , " /* Nothing to do if s is the empty string. */" , " size_t len = strlen(s);" , " if (len) {" , "" , " /* Make sure the buffer can hold all of s. */" , " bufferAllocateChars(buffer, len);" , "" , " /* Append s at the end of the buffer. */" , " strncpy(buffer->chars + buffer->current, s, len);" , "" , " /* Terminate with 0. */" , " buffer->current += len;" , " buffer->chars[buffer->current] = 0;" , " }" , "}" , "" , "/* Append single character at the end of the buffer. */" , "" , "void bufferAppendChar (Buffer buffer, const char c)" , "{" , " /* Make sure the buffer can hold one more character and append it. */" , " bufferAllocateChars(buffer, 1);" , " buffer->chars[buffer->current] = c;" , "" , " /* Terminate with 0. */" , " buffer->current++;" , " buffer->chars[buffer->current] = 0;" , "}" , "" , "/* Give read-only access to the buffer content." , " Does not survive the destruction of the buffer object. */" , "" , "const char* bufferContent (Buffer buffer) {" , " return buffer->chars;" , "}" , "" , "/* Internal functions. */" , "/************************************************************************/" , "" , "/* Make sure the buffer can hold `n` more characters. */" , "" , "static void bufferAllocateChars (Buffer buffer, const unsigned int n) {" , " /* 1 extra char for terminating 0. */" , " unsigned int requiredSize = buffer->current + 1 + n;" , " if (buffer->size < requiredSize)" , " {" , " do buffer->size *= 2; /* Double the buffer size */" , " while (buffer->size < requiredSize);" , " resizeBuffer(buffer);" , " }" , "}" , "" , "/* Increase the buffer size to the new `size`. */" , "" , "static void resizeBuffer(Buffer buffer)" , "{" , " /* The new size needs to be strictly greater than the currently" , " * used part, otherwise writing to position buffer->current will" , " * be out of bounds." , " */" , " assert(buffer->size > buffer->current);" , "" , " /* Resize (or, the first time allocate) the buffer. */" , " buffer->chars = (char*) realloc(buffer->chars, buffer->size);" , "" , " /* Crash if out-of-memory. */" , " if (! buffer->chars)" , " {" , " fprintf(stderr, \"Buffer.c: Error: Out of memory while attempting to grow buffer!\\n\");" , " exit(1); /* This seems to be the right exit code for out-of-memory. 137 is only when the OS kills us. */" , " }" , "}" ]