|
| 1 | +module Happy.Tabular.Main ( |
| 2 | + TabularArgs(..), TabularResult, runTabular |
| 3 | + ) where |
| 4 | + |
| 5 | +import Happy.Grammar |
| 6 | +import Happy.Tabular.Tables |
| 7 | +import qualified Happy.Tabular.LALR as LALR |
| 8 | +import Happy.Tabular.FindRedundancies |
| 9 | +import Happy.Tabular.Info |
| 10 | +import Happy.Tabular |
| 11 | +import Data.Array (Array) |
| 12 | +import System.IO |
| 13 | +import System.Exit (exitWith, ExitCode(..)) |
| 14 | +import Control.Monad |
| 15 | +import Data.Version (Version) |
| 16 | + |
| 17 | +-------- Main entry point (runTabular) -------- |
| 18 | + |
| 19 | +data TabularArgs = TabularArgs { |
| 20 | + inFile :: String, -- printed to the info file, not used otherwise |
| 21 | + infoFile :: Maybe String, |
| 22 | + |
| 23 | + dumpLR0 :: Bool, |
| 24 | + dumpLA :: Bool, |
| 25 | + dumpAction :: Bool, |
| 26 | + dumpGoto :: Bool |
| 27 | +} |
| 28 | + |
| 29 | +type TabularResult = (ActionTable, GotoTable, [LALR.Lr1State], [Int]) |
| 30 | + |
| 31 | +runTabular :: Bool -> TabularArgs -> Grammar -> Version -> IO TabularResult |
| 32 | +runTabular glr args g version = do |
| 33 | + let select_reductions = |
| 34 | + if glr |
| 35 | + then select_all_reductions |
| 36 | + else select_first_reduction |
| 37 | + |
| 38 | + let tables = genTables select_reductions g |
| 39 | + sets = lr0items tables |
| 40 | + la = lookaheads tables |
| 41 | + items2 = lr1items tables |
| 42 | + goto = gotoTable tables |
| 43 | + action = actionTable tables |
| 44 | + (conflictArray, (sr,rr)) = conflicts tables |
| 45 | + optPrint (dumpLR0 args) (print sets) |
| 46 | + optPrint (dumpLA args) (print la) |
| 47 | + optPrint (dumpAction args) (print action) |
| 48 | + optPrint (dumpGoto args) (print goto) |
| 49 | + reportUnusedRules tables |
| 50 | + let (unused_rules, unused_terminals) = redundancies tables |
| 51 | + writeInfoFile sets g action goto conflictArray (inFile args) (infoFile args) unused_rules unused_terminals version |
| 52 | + reportConflicts g sr rr |
| 53 | + return (action, goto, items2, unused_rules) |
| 54 | + where |
| 55 | + optPrint b io = when b (putStr "\n---------------------\n" >> io) |
| 56 | + |
| 57 | +-------- Helpers -------- |
| 58 | + |
| 59 | +reportUnusedRules :: Tables -> IO () |
| 60 | +reportUnusedRules tables = do |
| 61 | + let (unused_rules, unused_terminals) = redundancies tables |
| 62 | + when (not (null unused_rules)) $ |
| 63 | + hPutStrLn stderr ("unused rules: " ++ show (length unused_rules)) |
| 64 | + when (not (null unused_terminals)) $ |
| 65 | + hPutStrLn stderr ("unused terminals: " ++ show (length unused_terminals)) |
| 66 | + |
| 67 | +reportConflicts :: Grammar -> Int -> Int -> IO () |
| 68 | +reportConflicts g sr rr = case expect g of |
| 69 | + Just n | n == sr && rr == 0 -> return () |
| 70 | + Just _ | rr > 0 -> |
| 71 | + die $ "The grammar has reduce/reduce conflicts.\n" ++ |
| 72 | + "This is not allowed when an expect directive is given\n" |
| 73 | + Just _ -> |
| 74 | + die $ "The grammar has " ++ show sr ++ " shift/reduce conflicts.\n" ++ |
| 75 | + "This is different from the number given in the expect directive\n" |
| 76 | + _ -> do |
| 77 | + if sr /= 0 |
| 78 | + then hPutStrLn stderr ("shift/reduce conflicts: " ++ show sr) |
| 79 | + else return () |
| 80 | + |
| 81 | + if rr /= 0 |
| 82 | + then hPutStrLn stderr ("reduce/reduce conflicts: " ++ show rr) |
| 83 | + else return () |
| 84 | + |
| 85 | +die :: String -> IO a |
| 86 | +die s = hPutStr stderr s >> exitWith (ExitFailure 1) |
| 87 | + |
| 88 | +writeInfoFile |
| 89 | + :: [LALR.ItemSetWithGotos] |
| 90 | + -> Grammar |
| 91 | + -> ActionTable |
| 92 | + -> GotoTable |
| 93 | + -> Array Int (Int,Int) |
| 94 | + -> String |
| 95 | + -> Maybe String |
| 96 | + -> [Int] |
| 97 | + -> [String] |
| 98 | + -> Version |
| 99 | + -> IO () |
| 100 | +writeInfoFile sets g action goto conflictArray file info_file unused_rules unused_terminals version = |
| 101 | + let info = genInfoFile |
| 102 | + (map fst sets) |
| 103 | + g |
| 104 | + action |
| 105 | + goto |
| 106 | + (token_specs g) |
| 107 | + conflictArray |
| 108 | + file |
| 109 | + unused_rules |
| 110 | + unused_terminals |
| 111 | + version |
| 112 | + in case info_file of |
| 113 | + Just s -> writeFile s info >> hPutStrLn stderr ("Grammar info written to: " ++ s) |
| 114 | + Nothing -> return () |
0 commit comments