From ca9b97f14e4b55902ddac6f909cc2ad732ae16ec Mon Sep 17 00:00:00 2001 From: Nikolay Yakimov Date: Wed, 3 Jun 2020 18:28:10 +0300 Subject: [PATCH] Make lexer FA graph output optional --- Main.hs | 9 ++++++--- README.md | 2 ++ lib/Lexer/Build.hs | 8 +++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Main.hs b/Main.hs index 6c7ed66..5bc71eb 100644 --- a/Main.hs +++ b/Main.hs @@ -21,11 +21,11 @@ import qualified Options.Applicative as OA import Data.Version import Paths_alpaca_parser_generator -type MainProgram = Text -> FilePath -> FilePath -> IO () +type MainProgram = Bool -> Text -> FilePath -> FilePath -> IO () runProgram :: (LexerWriter lang, ParserWriter parser lang) => Proxy lang -> Proxy parser -> MainProgram -runProgram lang parserMethod parserName baseFileName inputFile = do +runProgram lang parserMethod debugLexer parserName baseFileName inputFile = do input <- T.readFile inputFile let (lexicRaw, grammarLines) = second (drop 1) . break (=="%%") $ T.lines input rootdir = takeDirectory inputFile @@ -33,7 +33,7 @@ runProgram lang parserMethod parserName baseFileName inputFile = do lexic = filter (not . T.null) lexicRaw setCurrentDirectory rootdir runInIO $ do - writeFiles =<< makeLexer lang lexic + writeFiles =<< makeLexer lang debugLexer lexic wrap parserName $ makeParser lang parserMethod ParserOptions{ parserOptionsName = parserName , parserOptionsBaseFileName = baseFileName @@ -88,6 +88,9 @@ parser = (option (enumReader langTbl) <> help "Parser method, default lalr" <> metavar (intercalate "|" (concatMap fst parsTbl)) <> value (ParserProxy lalrParser))) + <*> switch + ( long "debug-lexer" + <> help "Output lexer finite automata graphs in GraphViz format") <*> strOption ( short 'n' <> long "name" diff --git a/README.md b/README.md index 72296b2..0ec776d 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ Available options: Target language, default cpp -p,--parser recursive|rec|ll1|lr0|lr1|slr|lalr Parser method, default lalr + --debug-lexer Output lexer finite automata graphs in GraphViz + format -n,--name NAME Parser class name, default "Parser" -b,--basename FILENAME Parser output file base name, default "parser" GRAMMARFILE Grammar input file diff --git a/lib/Lexer/Build.hs b/lib/Lexer/Build.hs index 38e6d9c..5a02df2 100644 --- a/lib/Lexer/Build.hs +++ b/lib/Lexer/Build.hs @@ -24,12 +24,14 @@ import Lexer.Types import Data.Proxy makeLexer :: (LexerWriter lang, Monad m) => Proxy lang - -> [Text] -> MyMonadT m [(FilePath,Text)] -makeLexer lang input = do + -> Bool -> [Text] -> MyMonadT m [(FilePath,Text)] +makeLexer lang outputDebug input = do defs <- liftEither . left T.lines $ mapM (fmap regex . scanLine) input let nfa = evalState (buildNFA defs) 0 dfa = simplifyDFA . nfaToDFA $ nfa - debug = (("nfa.gv", nfaToGraphviz nfa) :) . (("dfa.gv", dfaToGraphviz dfa) :) + debug = if outputDebug + then (("nfa.gv", nfaToGraphviz nfa) :) . (("dfa.gv", dfaToGraphviz dfa) :) + else id stList = map (second (second M.toList)) $ IM.toList dfa accSt <- catMaybes <$> mapM (\(f, (s, _)) -> fmap (f,) <$> isSingle f s) stList