forked from Attempto/ACE-in-GF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.hs
50 lines (43 loc) · 1.33 KB
/
Parser.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Main where
-- Copies every line of STDIN to STDOUT, adding
-- * "|OK", if line can be parsed with the given PGF,
-- * "|FAIL" otherwise.
-- The number of parse trees is added to OK.
-- The successfully consumed line prefix (plus the failing token)
-- is added to "FAIL".
--
-- Usage example:
--
-- $ echo -e "John asks Mary .\nJohn aasks Mary ." | ./Parser ACE-0_0_2.pgf TestAttemptoAce
-- John asks Mary .|OK (1)
-- John aasks Mary .|FAIL John aasks
-- Parser: <stdin>: hGetLine: end of file
-- TODO: maybe the simple tokenizer `words` is not the best choice
-- TODO: how to suppress the "end of file" error
-- TODO: what does "Just 4" mean?
import PGF
import Data.Maybe
import System.Environment (getArgs)
main :: IO ()
main = do
file:lang:_ <- getArgs
pgf <- readPGF file
loop (parseResult pgf (fromJust (readLanguage lang)))
loop :: (String -> String) -> IO ()
loop trans = do
s <- getLine
putStr s
putStrLn $ trans s
loop trans
parseResult :: PGF -> Language -> String -> String
parseResult pgf lang s =
case parse_ pgf lang (startCat pgf) (Just 4) s of
(ParseFailed num, _) -> "|FAIL " ++ unwords (take num (words s))
(ParseOk trees, _) -> "|OK (" ++ show (length trees) ++ ")"
_ -> "|FAIL"
-- (Unused)
-- Returns the first language in the PGF
getEng :: PGF -> Language
getEng pgf =
case languages pgf of
lang:_ -> lang