Skip to content
This repository was archived by the owner on Sep 6, 2024. It is now read-only.

Commit 27e2b93

Browse files
committed
form nfas from file text
1 parent 04bb578 commit 27e2b93

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

src/watcompiler/Tokens.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BRACKET { } ( ) [ ]
2+
BOOLEAN true false

src/watcompiler/re.clj

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns watcompiler.re
22
(:require [clojure.set :refer :all]
33
[watcompiler.nfa :refer :all]
4-
[watcompiler.lang :refer :all])
4+
[watcompiler.lang :refer :all]
5+
[clojure.string :as str])
56
(:import [watcompiler.nfa NFA]))
67

78
;; Merging multiple nfas
@@ -62,7 +63,7 @@
6263
[& arguments]
6364
(let
6465
[stateS (gensym :s)
65-
class (first arguments)
66+
class (keyword (first arguments)) ;; Conver to a keyword since it reads strings from a file
6667
args (rest arguments)
6768
;; Key: string for keyword, Value: NFA for that keyword
6869
strings-nfas (into (sorted-map) (for [nfa-name args]
@@ -85,6 +86,23 @@
8586
all-transitions
8687
all-accept-priorities)))
8788

89+
(def readFile
90+
(with-open [rdr (clojure.java.io/reader "src/watcompiler/Tokens.txt")]
91+
(reduce conj [] (line-seq rdr))))
92+
93+
(def splitLines
94+
(into []
95+
(for [x readFile]
96+
(str/split x #" "))))
97+
98+
(def fileFormedNFA
99+
(let [nfas
100+
(into []
101+
(for [x splitLines]
102+
(apply form-multiple-nfas x)))]
103+
(apply merge-nfas nfas)))
104+
105+
88106
;; NFAs for types
89107

90108
;; Integer literal
@@ -106,6 +124,28 @@
106124
">=" "<=" "&" "&=" "=" "==" "!" "!=" "^=" "^" "+" "+="
107125
"++" "-" "-=" "--" "*" "*=" "/" "/=" "%" "%="))
108126

127+
;; read in the file, getting the parameters for non-regex nfas, merge them
128+
129+
130+
;; white space
131+
132+
133+
;; binary operators and assignment operators
134+
135+
;; assignment operators
136+
;; AssignmentOperator: one of
137+
;; = *= /= %= += -= <<= >>= >>>= &= ^= |=
138+
139+
;; unary operators
140+
;; binary operators on two elements
141+
142+
;; ; semi-colon , comma, quotation marks ""
143+
144+
;; String literal
145+
;; ".*"
146+
147+
148+
109149
;; Keywords nfa
110150
(def keywords-nfa
111151
(form-multiple-nfas :KEYWORD

test/watcompiler/re_test.clj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
[watcompiler.re :refer :all])
55
(:import [watcompiler.nfa NFA]))
66

7+
;; Form the NFAs from a file
8+
(deftest read-file
9+
(def lines readFile)
10+
(def separated splitLines)
11+
(def formed fileFormedNFA)
12+
13+
(is (= :BRACKET (run-NFA formed "]")))
14+
(is (= :BOOLEAN (run-NFA formed "true")))
15+
(is (= :BRACKET (run-NFA formed "{"))))
16+
717
;; Test forming multiple nfas from multiple strings
818
(deftest multiple-nfas-function-test
919
(def full-nfa (form-multiple-nfas :KEYWORD "int" "if"))
@@ -12,7 +22,6 @@
1222
(is (= false (run-NFA full-nfa "in")))
1323
(is (= false (run-NFA full-nfa "nt"))))
1424

15-
1625
;; Test function forming individual nfa
1726
(deftest function-test
1827
(def int-nfa-test (string-to-nfa "int" :INT))

0 commit comments

Comments
 (0)