-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTypes.hs
53 lines (36 loc) · 1.28 KB
/
Types.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
51
52
53
{-
Project: VUT FIT FLP 1. Project (Functional) - SIMPLIFY-BKG
Author: Dominik Harmim <xharmi00@stud.fit.vutbr.cz>
Year: 2020
Module: Types
Description: Definitions of data types. The internal representation of the
context-free grammar.
-}
{-# LANGUAGE RecordWildCards #-}
module Types (CFG(..), Err, Rule, Rules, Symbol, Symbols) where
import Data.List (intercalate)
-- A nonterminal or terminal symbol.
type Symbol = Char
-- A list of nonterminal and terminal symbols.
type Symbols = [Symbol]
-- A production rule is a pair of a symbol and a list of symbols.
type Rule = (Symbol, Symbols)
-- A list of production rules.
type Rules = [Rule]
-- The context-free grammar (CFG) is represented by a list of nonterminal
-- symbols, a list of terminal symbols, the starting nonterminal symbol,
-- and a list of production rules.
data CFG = CFG
{ nonterminals :: Symbols
, terminals :: Symbols
, startingSymbol :: Symbol
, rules :: Rules }
-- The definition of showing the CFG.
instance Show CFG where
show CFG{..} = unlines $
[intercalate "," $ map (: []) nonterminals] ++
[intercalate "," $ map (: []) terminals] ++
[[startingSymbol]] ++
map (\(l, r) -> [l] ++ "->" ++ r) rules
-- A type of a result or an error message.
type Err = Either String