blob: 1d505bea17e2b430c596d6bc128038628607ac1c (
plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
|
{-
**************************************************************
GF Module
Description : This module prints a CFG as a Nuance GSL 2.0
grammar.
Author : Björn Bringert (bringert@cs.chalmers.se)
License : GPL (GNU General Public License)
Created : September 13, 2004
Modified : October 1, 2004
**************************************************************
-}
-- FIXME: remove / warn / fail if there are int / string literal
-- categories in the grammar
module PrGSL (gslPrinter) where
import SRG
import Ident
import CFGrammar
import Parser (Symbol(..))
import GrammarTypes
import PrintParser
import Option
gslPrinter :: Ident -- ^ Grammar name
-> Options -> CFGrammar -> String
gslPrinter name opts cfg = prGSL srg ""
where srg = makeSRG name opts cfg
prGSL :: SRG -> ShowS
prGSL (SRG{grammarName=name,startCat=start,origStartCat=origStart,rules=rs})
= header . mainCat . unlinesS (map prRule rs)
where
header = showString ";GSL2.0" . nl
. comments ["Nuance speech recognition grammar for " ++ name,
"Generated by GF"] . nl . nl
mainCat = showString ("; Start category: " ++ origStart) . nl
. showString ".MAIN " . prCat start . nl . nl
prRule (SRGRule cat origCat rhs) =
showString "; " . prtS origCat . nl
. prCat cat . sp . wrap "[" (unwordsS (map prAlt rhs)) "]" . nl
prAlt rhs = wrap "(" (unwordsS (map prSymbol rhs')) ")"
where rhs' = rmPunct rhs
prSymbol (Cat c) = prCat c
prSymbol (Tok t) = wrap "\"" (prtS t) "\""
prCat c = showString c
rmPunct :: [Symbol String Token] -> [Symbol String Token]
rmPunct [] = []
rmPunct (Tok t:ss) | all isPunct (prt t) = rmPunct ss
rmPunct (s:ss) = s : rmPunct ss
isPunct :: Char -> Bool
isPunct c = c `elem` "-_.;.,?!()[]{}"
comments :: [String] -> ShowS
comments = unlinesS . map (showString . ("; " ++))
|