Skip to content

Commit

Permalink
Make lexer FA graph output optional
Browse files Browse the repository at this point in the history
  • Loading branch information
lierdakil committed Jun 3, 2020
1 parent 7c5d712 commit ca9b97f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
9 changes: 6 additions & 3 deletions Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ 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
grammar = T.unlines grammarLines
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
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions lib/Lexer/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca9b97f

Please sign in to comment.