This repository was archived by the owner on Sep 6, 2024. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ BRACKET { } ( ) [ ]
2+ BOOLEAN true false
Original file line number Diff line number Diff line change 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
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]
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
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
Original file line number Diff line number Diff line change 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" ))
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 ))
You can’t perform that action at this time.
0 commit comments