Guest User

Untitled

a guest
Jan 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. open import Data.List
  2. open import Data.Nat
  3. open import Data.Nat.Properties
  4. open import Data.Product
  5.  
  6. module Earley where
  7.  
  8. data Terminality : Set where
  9. T : Terminality
  10. NT : Terminality
  11.  
  12. module Grammar
  13. (Symbol : (Terminality → Set))
  14. where
  15.  
  16. Terminal = Symbol
  17.  
  18. infixr 5 _~∷_
  19. infixr 5 _|∷_
  20.  
  21. -- infix 5 _~_
  22. -- infix 5 _ǁ_
  23.  
  24. infix 4 _∷=_
  25.  
  26. infix 5 _∷⟨×⟩_
  27. infix 6 _∷|+|_
  28.  
  29. -- a parse forest is a grammar where each nonterminal is a forest.
  30.  
  31. data SymbolicString : Set where
  32. ε : SymbolicString
  33. _~∷_ : {t : Terminality} (a : Symbol t) → (b : SymbolicString) → SymbolicString
  34. _|∷_ : {t : Terminality} (a : Symbol t) → (b : SymbolicString) → SymbolicString
  35.  
  36. -- _~_ : (x : SymbolicString) → (y : SymbolicString) → SymbolicString
  37. -- _|_ : (x : SymbolicString) → (y : SymbolicString) → SymbolicString
  38.  
  39. data Rule : Set where
  40. _∷=_ : (α : Symbol NT) → (β : SymbolicString) → Rule
  41.  
  42. Grammar = List Rule
  43.  
  44. data ParseTree : Set where
  45. ∅ : ParseTree
  46. λ' : ParseTree
  47. Leaf : Symbol T → ParseTree
  48. _⟨×⟩_ : (left right : ParseTree) → ParseTree
  49.  
  50. data Level : Set where
  51. sum-level : Level
  52. product-level : Level
  53.  
  54. data ParseForest : Level → Set
  55.  
  56. data ParseForest where
  57. ∅ : ParseForest sum-level
  58. λ' : ParseForest product-level
  59. Leaf : {l : Level} → (Symbol T) → ParseForest l
  60. _∷⟨×⟩_ : (x : ParseForest product-level) → (xs : ParseForest sum-level) → ParseForest product-level
  61. _∷|+|_ : (x : ParseForest sum-level) → (xs : ParseForest product-level) → ParseForest sum-level
  62.  
  63. flip-level : Level → Level
  64. flip-level sum-level = product-level
  65. flip-level product-level = sum-level
  66.  
  67. -- flip-level : {l : Level} (forest : ParseForest l) → ParseForest (flip l)
  68. -- flip-level {sum-level} s = λ' ∷⟨×⟩ s
  69. -- flip-level {product-level} p = ∅ ∷|+| p
  70.  
  71. -- Leaf' : Symbol T → ParseForest product-level
  72. -- Leaf' l = flip-level (Leaf l)
  73.  
  74. data MySymbol : Terminality → Set
  75.  
  76. data MySymbol where
  77. MyT : ℕ → MySymbol T
  78. MyNT : ℕ → MySymbol NT
  79.  
  80. open Grammar MySymbol
  81.  
  82. The = MyT 0
  83. A = MyT 1
  84. Man = MyT 2
  85. Dog = MyT 3
  86. Bites = MyT 4
  87. Pets = MyT 5
  88.  
  89. sentence = MyNT 0
  90. subject = MyNT 1
  91. predicate = MyNT 2
  92. article = MyNT 3
  93. noun = MyNT 4
  94. verb = MyNT 5
  95. direct_object = MyNT 6
  96.  
  97. forest = (λ' ∷⟨×⟩ Leaf The) ∷⟨×⟩ Leaf Man
  98.  
  99. grammar = (sentence ∷= subject ~∷ predicate ~∷ ε) ∷
  100. (subject ∷= article ~∷ noun ~∷ ε) ∷
  101. (predicate ∷= verb ~∷ direct_object ~∷ ε) ∷
  102. (direct_object ∷= article ~∷ noun ~∷ ε) ∷
  103. (article ∷= The |∷ A ~∷ ε) ∷
  104. (noun ∷= Man |∷ Dog ~∷ ε) ∷
  105. (verb ∷= Bites |∷ Pets ~∷ ε) ∷
  106. []
  107.  
  108. -- rule ∷=
Add Comment
Please, Sign In to add comment