Skip to content

Commit 16b23f4

Browse files
committed
Remove code changes
1 parent 65e4a80 commit 16b23f4

File tree

8 files changed

+150
-94
lines changed

8 files changed

+150
-94
lines changed

packages/grammar/src/Happy/Grammar/Grammar.lhs

+21-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ The Grammar data type.
2828

2929
> data Production
3030
> = Production Name [Name] (String,[Int]) Priority
31-
> deriving (Show)
31+
32+
#ifdef DEBUG
33+
34+
> deriving Show
35+
36+
#endif
3237

3338
> data Grammar
3439
> = Grammar {
@@ -53,11 +58,11 @@ The Grammar data type.
5358
> attributetype :: String,
5459
> lexer :: Maybe (String,String),
5560
> error_handler :: Maybe String,
56-
> error_sig :: ErrorHandlerType,
57-
> hd :: Maybe String,
58-
> tl :: Maybe String
61+
> error_sig :: ErrorHandlerType
5962
> }
6063

64+
#ifdef DEBUG
65+
6166
> instance Show Grammar where
6267
> showsPrec _ (Grammar
6368
> { productions = p
@@ -83,12 +88,24 @@ The Grammar data type.
8388
> . showString "\neof = " . shows eof
8489
> . showString "\n"
8590

91+
#endif
92+
8693
> data Assoc = LeftAssoc | RightAssoc | None
94+
95+
#ifdef DEBUG
96+
8797
> deriving Show
8898

99+
#endif
100+
89101
> data Priority = No | Prio Assoc Int | PrioLowest
102+
103+
#ifdef DEBUG
104+
90105
> deriving Show
91106

107+
#endif
108+
92109
> instance Eq Priority where
93110
> No == No = True
94111
> Prio _ i == Prio _ j = i == j

packages/tabular/src/Happy/Tabular.hs

+60-56
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,84 @@
1-
module Happy.Tabular(
2-
mkFirst, genLR0Items, genLookaheads, genLR1States, genActionTable, genGotoTable, countConflicts,
3-
Lr0Item(..), Lr1Item(..), Lr0State, Lr1State, LookaheadInfo,
4-
TabularArgs(..), TabularResult, runTabular
5-
) where
1+
module Happy.Tabular (TabularArgs(..), Lr1State, TabularResult, runTabular) where
62

73
import Happy.Grammar.Grammar
8-
import Happy.Tabular.NameSet (NameSet)
94
import Happy.Tabular.Tables
105
import Happy.Tabular.First
11-
import qualified Happy.Tabular.LALR as LALR
12-
import Happy.Tabular.LALR (Lr0Item, Lr1Item, precalcClosure0, propLookaheads, calcLookaheads, mergeLookaheadInfo)
6+
import Happy.Tabular.LALR
137
import Happy.Tabular.FindRedundancies
148
import Happy.Tabular.Info
159
import Data.Set (Set)
1610
import Data.Array (Array)
1711
import System.IO
18-
import System.Exit
12+
import System.Exit hiding (die)
1913
import Control.Monad
2014

21-
-------- Pure tabular functions, may be called without creating TabularArgs --------
22-
23-
type Lr0State = (Set Lr0Item, [(Name, Int)])
24-
type Lr1State = ([Lr1Item], [(Name, Int)])
25-
type LookaheadInfo = Array Int [(Lr0Item, NameSet)]
26-
27-
genLR0Items :: Grammar -> [Lr0State]
28-
genLR0Items g = LALR.genLR0items g (precalcClosure0 g)
29-
30-
genLookaheads :: Grammar -> [Lr0State] -> ([Name] -> NameSet) -> LookaheadInfo
31-
genLookaheads g sets first =
32-
let (spont, prop) = propLookaheads g sets first in
33-
calcLookaheads (length sets) spont prop
34-
35-
genLR1States :: LookaheadInfo -> [Lr0State] -> [Lr1State]
36-
genLR1States = mergeLookaheadInfo
37-
38-
genActionTable :: Grammar -> ([Name] -> NameSet) -> [Lr1State] -> ActionTable
39-
genActionTable = LALR.genActionTable
40-
41-
genGotoTable :: Grammar -> [Lr0State] -> GotoTable
42-
genGotoTable = LALR.genGotoTable
43-
44-
countConflicts :: ActionTable -> (Array Int (Int, Int), (Int, Int))
45-
countConflicts = LALR.countConflicts
46-
4715
-------- Main entry point (runTabular) --------
4816

4917
data TabularArgs = TabularArgs {
5018
inFile :: String, -- printed to the info file, not used otherwise
51-
infoFile :: Maybe String,
19+
infoFile :: Maybe String
20+
21+
#ifdef DEBUG
5222

23+
,
5324
dumpLR0 :: Bool,
5425
dumpLA :: Bool,
5526
dumpAction :: Bool,
5627
dumpGoto :: Bool
28+
29+
#endif
30+
5731
}
5832

33+
type Lr1State = ([Lr1Item], [(Name, Int)])
5934
type TabularResult = (ActionTable, GotoTable, [Lr1State], [Int])
6035

61-
runTabular :: TabularArgs -> Grammar -> IO TabularResult
62-
runTabular args g =
63-
let first = mkFirst g
64-
sets = genLR0Items g
65-
la = genLookaheads g sets first
66-
items2 = genLR1States la sets
67-
goto = genGotoTable g sets
68-
action = genActionTable g first items2
69-
(conflictArray, (sr,rr)) = (countConflicts action)
36+
runTabular :: TabularArgs -> Bool -> Grammar -> IO TabularResult
37+
runTabular args glr g =
38+
let first = {-# SCC "First" #-} (mkFirst g)
39+
closures = {-# SCC "Closures" #-} (precalcClosure0 g)
40+
sets = {-# SCC "LR0_Sets" #-} (genLR0items g closures)
41+
_lainfo@(spont,prop) = {-# SCC "Prop" #-} (propLookaheads g sets first)
42+
la = {-# SCC "Calc" #-} (calcLookaheads (length sets) spont prop)
43+
items2 = {-# SCC "Merge" #-} (mergeLookaheadInfo la sets)
44+
goto = {-# SCC "Goto" #-} (genGotoTable g sets)
45+
action = {-# SCC "Action" #-} (genActionTable g first items2)
46+
(conflictArray,(sr,rr)) = {-# SCC "Conflict" #-} (countConflicts action)
7047
in do
48+
49+
#ifdef DEBUG
50+
7151
optPrint (dumpLR0 args) (print sets)
7252
optPrint (dumpLA args) (print la)
7353
optPrint (dumpAction args) (print action)
7454
optPrint (dumpGoto args) (print goto)
75-
(unused_rules, unused_terminals) <- reportUnusedRules g action
55+
56+
#endif
57+
58+
(unused_rules, unused_terminals) <- reportUnusedRules glr g action
7659
writeInfoFile sets g action goto conflictArray (inFile args) (infoFile args) unused_rules unused_terminals
7760
reportConflicts g sr rr
7861
return (action, goto, items2, unused_rules)
62+
63+
#ifdef DEBUG
64+
7965
where
8066
optPrint b io = when b (putStr "\n---------------------\n" >> io)
8167

68+
#endif
69+
8270

8371
-------- Helpers --------
8472

85-
reportUnusedRules :: Grammar -> ActionTable -> IO ([Int], [String])
86-
reportUnusedRules g action =
87-
let result@(unused_rules, unused_terminals) = find_redundancies first_reduction g action in do
88-
when (not (null unused_rules)) $ hPutStrLn stderr ("unused rules: " ++ show (length unused_rules))
89-
when (not (null unused_terminals)) $ hPutStrLn stderr ("unused terminals: " ++ show (length unused_terminals))
90-
return result
73+
reportUnusedRules :: Bool -> Grammar -> ActionTable -> IO ([Int], [String])
74+
reportUnusedRules glr g action =
75+
let reduction_filter = if glr then any_reduction else first_reduction
76+
(unused_rules, unused_terminals) = find_redundancies reduction_filter g action in do
77+
when (not (null unused_rules))
78+
(hPutStrLn stderr ("unused rules: " ++ show (length unused_rules)))
79+
when (not (null unused_terminals))
80+
(hPutStrLn stderr ("unused terminals: " ++ show (length unused_terminals)))
81+
return (unused_rules, unused_terminals)
9182

9283
reportConflicts :: Grammar -> Int -> Int -> IO ()
9384
reportConflicts g sr rr = case expect g of
@@ -106,14 +97,27 @@ reportConflicts g sr rr = case expect g of
10697
if rr /= 0
10798
then hPutStrLn stderr ("reduce/reduce conflicts: " ++ show rr)
10899
else return ()
109-
#if !MIN_VERSION_base(4,8,0)
110-
where die s = hPutStr stderr s >> exitWith (ExitFailure 1)
111-
#endif
100+
101+
die :: String -> IO a
102+
die s = hPutStr stderr s >> exitWith (ExitFailure 1)
112103

113104
type ItemSetWithGotos = (Set Lr0Item, [(Name,Int)])
114105
writeInfoFile :: [ItemSetWithGotos] -> Grammar -> ActionTable -> GotoTable -> Array Int (Int,Int) -> String -> Maybe String -> [Int] -> [String] -> IO ()
115106
writeInfoFile sets g action goto conflictArray file info_file unused_rules unused_terminals =
116107
let info = genInfoFile (map fst sets) g action goto (token_specs g) conflictArray file unused_rules unused_terminals in
117108
case info_file of
118109
Just s -> writeFile s info >> hPutStrLn stderr ("Grammar info written to: " ++ s)
119-
Nothing -> return ()
110+
Nothing -> return ()
111+
112+
---
113+
--- selects what counts as a reduction when calculating used/unused
114+
115+
any_reduction :: LRAction -> [Int]
116+
any_reduction (LR'Reduce r _) = [r]
117+
any_reduction (LR'Multiple as a) = concatMap any_reduction (a : as)
118+
any_reduction _ = []
119+
120+
first_reduction :: LRAction -> [Int]
121+
first_reduction (LR'Reduce r _) = [r]
122+
first_reduction (LR'Multiple _ a) = first_reduction a -- eg R/R conflict
123+
first_reduction _ = []

packages/tabular/src/Happy/Tabular/FindRedundancies.lhs

+1-14
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,4 @@ Find unused rules and tokens
3232
> is_shift :: LRAction -> Bool
3333
> is_shift (LR'Shift _ _) = True
3434
> is_shift (LR'Multiple _ LR'Shift{}) = True
35-
> is_shift _ = False
36-
37-
---
38-
selects what counts as a reduction when calculating used/unused
39-
40-
> any_reduction :: LRAction -> [Int]
41-
> any_reduction (LR'Reduce r _) = [r]
42-
> any_reduction (LR'Multiple as a) = concatMap any_reduction (a : as)
43-
> any_reduction _ = []
44-
45-
> first_reduction :: LRAction -> [Int]
46-
> first_reduction (LR'Reduce r _) = [r]
47-
> first_reduction (LR'Multiple _ a) = first_reduction a -- eg R/R conflict
48-
> first_reduction _ = []
35+
> is_shift _ = False

packages/tabular/src/Happy/Tabular/LALR.lhs

+14-1
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,24 @@ Generation of LALR parsing tables.
3636
This means rule $a$, with dot at $b$ (all starting at 0)
3737

3838
> data Lr0Item = Lr0 {-#UNPACK#-}!Int {-#UNPACK#-}!Int -- (rule, dot)
39-
> deriving (Eq,Ord,Show)
39+
> deriving (Eq,Ord
40+
41+
#ifdef DEBUG
42+
43+
> ,Show
44+
45+
#endif
46+
47+
> )
4048

4149
> data Lr1Item = Lr1 {-#UNPACK#-}!Int {-#UNPACK#-}!Int NameSet -- (rule, dot, lookahead)
50+
51+
#ifdef DEBUG
52+
4253
> deriving (Show)
4354

55+
#endif
56+
4457
> type RuleList = [Lr0Item]
4558

4659
-----------------------------------------------------------------------------

packages/tabular/src/Happy/Tabular/Tables.lhs

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,27 @@ Datatypes for goto and action tables which are consumed by happy-backend.
1414
> | LR'Fail -- :-(
1515
> | LR'MustFail -- :-(
1616
> | LR'Multiple [LRAction] LRAction -- conflict
17-
> deriving (Eq, Show)
17+
> deriving(Eq
18+
19+
#ifdef DEBUG
20+
21+
> ,Show
22+
23+
#endif
24+
25+
> )
1826

1927
> type ActionTable = Array Int{-state-} (Array Int{-terminal#-} LRAction)
2028

2129
> data Goto = Goto Int | NoGoto
22-
> deriving(Eq, Show)
30+
> deriving(Eq
31+
32+
#ifdef DEBUG
33+
34+
> ,Show
35+
36+
#endif
37+
38+
> )
2339

2440
> type GotoTable = Array Int{-state-} (Array Int{-nonterminal #-} Goto)

src/AbsSyn.lhs

+5
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ Here is the abstract syntax of the language we parse.
4747
> = PrecNone -- no user-specified precedence
4848
> | PrecShift -- %shift
4949
> | PrecId String -- %prec ID
50+
51+
#ifdef DEBUG
52+
5053
> deriving Show
5154

55+
#endif
56+
5257
%-----------------------------------------------------------------------------
5358
Parser Generator Directives.
5459

0 commit comments

Comments
 (0)