Skip to content

Commit

Permalink
parsing time
Browse files Browse the repository at this point in the history
  • Loading branch information
kwanghoon committed Apr 16, 2022
1 parent 8cbfb2f commit 86f91f3
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 29 deletions.
7 changes: 6 additions & 1 deletion app/ambiguous/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CommonParserUtil
import Token
import Expr

import ParserTime

-- | Utility
rule prodRule action = (prodRule, action, Nothing )
Expand Down Expand Up @@ -61,7 +62,11 @@ parserSpec = ParserSpec
parserSpecFile = "mygrammar.grm",
genparserexe = "yapb-exe",

synCompSpec = Nothing
synCompSpec = Nothing,
parserTime = ParserTime {
pa_startTime=startTime,
pa_finishTime=finishTime
}
}


8 changes: 7 additions & 1 deletion app/error/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import CommonParserUtil
import Token
import Expr

import ParserTime

import Control.Monad.Trans (lift)
import qualified Control.Monad.Trans.State.Lazy as ST

Expand Down Expand Up @@ -70,7 +72,11 @@ parserSpec = ParserSpec
parserSpecFile = "mygrammar.grm",
genparserexe = "yapb-exe",

synCompSpec = Nothing
synCompSpec = Nothing,
parserTime = ParserTime {
pa_startTime=startTime,
pa_finishTime=finishTime
}
}


8 changes: 7 additions & 1 deletion app/parser/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import CommonParserUtil
import Token
import Expr

import ParserTime

-- | Utility
rule prodRule action = (prodRule, action, Nothing )
ruleWithPrec prodRule action prec = (prodRule, action, Just prec)
Expand Down Expand Up @@ -66,7 +68,11 @@ parserSpec = ParserSpec
parserSpecFile = "mygrammar.grm",
genparserexe = "yapb-exe",

synCompSpec = Nothing
synCompSpec = Nothing,
parserTime = ParserTime {
pa_startTime=startTime,
pa_finishTime=finishTime
}
}


8 changes: 7 additions & 1 deletion app/syntaxcompletion/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import CommonParserUtil
import Token
import Expr

import ParserTime

-- | Utility
rule prodRule action = (prodRule, action, Nothing )
ruleWithPrec prodRule action prec = (prodRule, action, Just prec)
Expand Down Expand Up @@ -48,5 +50,9 @@ parserSpec = ParserSpec
parserSpecFile = "mygrammar.grm",
genparserexe = "yapb-exe",

synCompSpec = Nothing
synCompSpec = Nothing,
parserTime = ParserTime {
pa_startTime=startTime,
pa_finishTime=finishTime
}
}
5 changes: 4 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: yapb
version: 0.2.4
version: 0.2.5
github: "kwanghoon/yapb"
license: BSD3
author: "Kwanghoon Choi"
Expand Down Expand Up @@ -65,6 +65,7 @@ library:
- SynCompAlgoUtil
- SynCompAlgorithm
- AVL
- ParserTime

dependencies:
- regex-tdfa >= 1.3.1 && < 1.4
Expand All @@ -86,6 +87,7 @@ executables:
- -threaded
- -rtsopts
- -with-rtsopts=-N
- -O2
dependencies:
- yapb

Expand Down Expand Up @@ -181,6 +183,7 @@ tests:
- -threaded
- -rtsopts
- -with-rtsopts=-N
- -O2
dependencies:
- yapb
- process
71 changes: 51 additions & 20 deletions src/parserlib/CommonParserUtil.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE GADTs #-}
module CommonParserUtil
( LexerSpec(..), ParserSpec(..), AutomatonSpec(..)
( LexerSpec(..), ParserSpec(..), AutomatonSpec(..),AutomatonTime(..)
, LexerParserState, Line, Column
, ProdRuleStr, ParseAction, ProdRulePrec
, Stack, StkElem(..), push, pop, prStack
Expand All @@ -16,7 +16,9 @@ module CommonParserUtil
, get, getText
, LexError(..), ParseError(..), lpStateFrom
, successfullyParsed, handleLexError, handleParseError
, SynCompSpec(..))
, SynCompSpec(..)
, ParserTime(..)
)
where

import Attrs
Expand Down Expand Up @@ -136,9 +138,16 @@ data ParserSpec token ast m a =
grammarFile :: String, -- ex) grammar.txt
parserSpecFile :: String, -- ex) mygrammar.grm
genparserexe :: String, -- ex) genlrparse-exe
synCompSpec :: Maybe SynCompSpec
synCompSpec :: Maybe SynCompSpec,
parserTime :: ParserTime m a
}

data ParserTime m a =
ParserTime {
pa_startTime :: ST.StateT (LexerParserState a) m Integer,
pa_finishTime :: Integer -> ST.StateT (LexerParserState a) m ()
}

data SynCompSpec =
SynCompSpec { isAbleToSearch :: String -> Bool -- terminasls or non-terminals
}
Expand Down Expand Up @@ -431,7 +440,15 @@ data AutomatonSpec token ast m a =
am_gotoTbl :: GotoTable,
am_prodRules :: ProdRules,
am_parseFuns :: ParseActionList token ast m a,
am_initState :: Int
am_initState :: Int,
am_time :: AutomatonTime m a
}

data AutomatonTime m a =
AutomatonTime {
am_startTime :: ST.StateT (LexerParserState a) m Integer,
am_finishTime :: Integer -> ST.StateT (LexerParserState a) m (),
am_cputime :: Integer
}

initState = 0
Expand Down Expand Up @@ -496,7 +513,11 @@ runAutomaton flag amSpec init_lp_state lexer =
Nothing -> return flag
Just config -> return $ config_DEBUG config

ST.evalStateT (runYapbAutomaton flag amSpec (lexer flag)) init_lp_state
ST.evalStateT runYA init_lp_state
where
runYA =
do start_cputime <- am_startTime (am_time amSpec)
runYapbAutomaton flag amSpec{am_time=(am_time amSpec){am_cputime=start_cputime}} (lexer flag)

runYapbAutomaton
:: (Monad m,
Expand All @@ -515,7 +536,7 @@ runYapbAutomaton
-- AST
ST.StateT (LexerParserState a) m ast

runYapbAutomaton flag (rm_spec@(AutomatonSpec {
runYapbAutomaton flag (am_spec@(AutomatonSpec {
am_initState=initState,
am_actionTbl=actionTbl,
am_gotoTbl=gotoTbl,
Expand Down Expand Up @@ -574,6 +595,7 @@ runYapbAutomaton flag (rm_spec@(AutomatonSpec {
Nothing ->
-- No more way to proceed now!
do lp_state <- ST.get
(am_finishTime (am_time am_spec)) (am_cputime (am_time am_spec))
throw (NotFoundAction terminal state stack actionTbl gotoTbl prodRules lp_state maybeStatus)

-- separated to support the haskell layout rule
Expand All @@ -588,7 +610,9 @@ runYapbAutomaton flag (rm_spec@(AutomatonSpec {
debug flag (terminalToString terminal) $ {- debug -}

case stack !! 1 of
StkNonterminal (Just ast) _ -> return ast
StkNonterminal (Just ast) _ ->
do (am_finishTime (am_time am_spec)) (am_cputime (am_time am_spec))
return ast
StkNonterminal Nothing _ -> error "Empty ast in the stack nonterminal"
_ -> error "Not Stknontermianl on Accept"

Expand Down Expand Up @@ -619,6 +643,7 @@ runYapbAutomaton flag (rm_spec@(AutomatonSpec {
case lookupGotoTable gotoTbl topState lhs of
Just state -> return state
Nothing -> do lp_state <- ST.get
(am_finishTime (am_time am_spec)) (am_cputime (am_time am_spec))
throw (NotFoundGoto topState lhs stack
actionTbl gotoTbl prodRules
lp_state maybeStatus)
Expand Down Expand Up @@ -667,7 +692,13 @@ parsing flag parserSpec init_lp_state lexer eot = do
am_actionTbl=actionTbl,
am_gotoTbl=gotoTbl,
am_prodRules=prodRules,
am_parseFuns=pFunList})
am_parseFuns=pFunList,
am_time=AutomatonTime {
am_startTime=pa_startTime (parserTime parserSpec),
am_finishTime=pa_finishTime (parserTime parserSpec),
am_cputime =0
}
})
init_lp_state lexer
-- putStrLn "done." -- for the interafce with Java-version RPC calculus interpreter.
return ast
Expand Down Expand Up @@ -928,24 +959,24 @@ _handleParseError
multiDbg (map (debug (flag || True)) (map show colorListList_symbols)) $

debug (flag || True) "" $
multiDbg (map (debug (flag || True)) (map show rawStrListList)) $
multiDbg (map (debug (flag || True)) (map show rawStrListList)) $

debug (flag || True) "" $
debug (flag || True) "" $

-- debug (flag || True) $ showConcat $ map (\x -> (show x ++ "\n")) colorListList_symbols
-- debug (flag || True) $ showConcat $ map (\x -> (show x ++ "\n")) rawStrListList -- mapM_ (putStrLn . show) rawStrListList

let formattedStrList =
case howtopresent of
0 -> strList
1 -> nub [ if null strList then "" else head strList | strList <- emacsColorListList ]
_ -> error ("Does not support prsentation method: " ++ show howtopresent)
let formattedStrList =
case howtopresent of
0 -> strList
1 -> nub [ if null strList then "" else head strList | strList <- emacsColorListList ]
_ -> error ("Does not support prsentation method: " ++ show howtopresent)

in
if emacsDisplay
then return (map Candidate formattedStrList)
else return []
in

if emacsDisplay
then return (map Candidate formattedStrList)
else return []

where
showConcat [] = ""
Expand Down
21 changes: 21 additions & 0 deletions src/util/ParserTime.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module ParserTime(startTime,finishTime,toSecond) where

import CommonParserUtil

import qualified Control.Monad.Trans.State.Lazy as ST
import Control.Monad.Trans(lift)
import System.CPUTime
import Text.Printf

startTime :: ST.StateT (LexerParserState a) IO Integer
startTime = lift getCPUTime

finishTime :: Integer -> ST.StateT (LexerParserState a) IO ()
finishTime sTime =
do fTime <- lift getCPUTime
-- lift ( putStrLn $ "parse time: start time: " ++ show sTime )
-- lift ( putStrLn $ "parse time: finish time: " ++ show fTime )
lift ( printf "parse time: %6.2fs\n" (toSecond (fTime - sTime)) )

toSecond :: Integer -> Float
toSecond cpuTime = fromIntegral cpuTime * 1e-12
9 changes: 5 additions & 4 deletions yapb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 7c59cf87fbeef808a2e3de7bb410ef94f49b773ab3251ef42e0b5c384972346a
-- hash: d2745442916343e66c3feacbdb504a906f8fec85056a6470ff874f28d5e642ae

name: yapb
version: 0.2.4
version: 0.2.5
synopsis: Yet Another Parser Builder (YAPB)
description: A programmable LALR(1) parser builder system. Please see the README on GitHub at <https://github.com/kwanghoon/yapb#readme>
category: parser builder
Expand Down Expand Up @@ -55,6 +55,7 @@ library
SynCompAlgoUtil
SynCompAlgorithm
AVL
ParserTime
other-modules:
Paths_yapb
hs-source-dirs:
Expand Down Expand Up @@ -202,7 +203,7 @@ executable yapb-exe
Paths_yapb
hs-source-dirs:
app/yapb
ghc-options: -threaded -rtsopts -with-rtsopts=-N
ghc-options: -threaded -rtsopts -with-rtsopts=-N -O2
build-depends:
base >=4.7 && <5
, deepseq >=1.4.4.0
Expand All @@ -220,7 +221,7 @@ test-suite yapb-test
Paths_yapb
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
ghc-options: -threaded -rtsopts -with-rtsopts=-N -O2
build-depends:
base >=4.7 && <5
, deepseq >=1.4.4.0
Expand Down

0 comments on commit 86f91f3

Please sign in to comment.