-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
139 lines (116 loc) · 3.73 KB
/
main.go
File metadata and controls
139 lines (116 loc) · 3.73 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/dekuu5/FiniteStateMachine/dfa"
"github.com/dekuu5/FiniteStateMachine/nfa"
"github.com/dekuu5/FiniteStateMachine/utils"
)
func main() {
// Define command-line flags for the JSON file and type (DFA or NFA)
filePath := flag.String("file", "", "Path to the JSON file containing the automaton")
automatonType := flag.String("type", "dfa", "Type of the automaton (dfa or nfa)")
flag.Parse()
// Check if the file path is provided
if *filePath == "" {
log.Fatal("Please provide the path to the JSON file using the -file flag")
}
// Read the automaton from the provided JSON file
// Validate and process based on the automaton type
switch strings.ToLower(*automatonType) {
case "dfa":
automatonJson := utils.ReadJson(*filePath)
if valid := dfa.ValidateDfa(automatonJson); !valid {
log.Fatalf("Error validating the DFA")
os.Exit(-1)
}
// printDfaJson(automatonJson)
processDfa(automatonJson)
case "nfa":
fmt.Println("NFA")
automatonJson := utils.ReadJsonNfa(*filePath)
if valid := nfa.ValidateNfa(automatonJson); !valid {
log.Fatalf("Error validating the NFA")
os.Exit(-1)
}
// nfaTree := nfa.Constructor(automatonJson)
// nfaTree := nfa.Constructor(automatonJson)
// printNfa(*nfaTree)
// printNfa(*nfaTree)
processNfa(automatonJson)
default:
log.Fatalf("Unknown automaton type: %s", *automatonType)
os.Exit(-1)
}
}
func processDfa(dfaJson dfa.FiniteAutomata) {
// Loop to get the input string
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter a string to validate using the DFA: ")
// Read input until newline and trim any extra whitespace
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading input:", err)
return
}
// Remove the newline character from the end of the input
symbols := []rune(strings.TrimSpace(input))
fmt.Println(symbols)
dfaTree := dfa.Constructor(dfaJson)
if valid := dfaTree.ValidateString(symbols); valid {
fmt.Printf("String %s is accepted\n", input)
} else {
fmt.Printf("String %s is rejected\n", input)
}
}
func processNfa(nfaJson nfa.NFiniteAutomata) {
// Loop to get the input string
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter a string to validate using the NFA: ")
// Read input until newline and trim any extra whitespace
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading input:", err)
return
}
// Remove the newline character from the end of the input
symbols := []rune(strings.TrimSpace(input))
fmt.Println(symbols)
nfaTree := nfa.Constructor(nfaJson)
printNfa(*nfaTree)
if valid := nfaTree.ValidateStringDac(symbols); valid {
fmt.Printf("String %s is accepted\n", input)
} else {
fmt.Printf("String %s is rejected\n", input)
}
if valid := nfaTree.ValidateStringDac(symbols); valid {
fmt.Printf("String %s is accepted\n", input)
} else {
fmt.Printf("String %s is rejected\n", input)
}
}
func printDfa(dfaJson dfa.DFA) {
fmt.Printf("States: %v\n", dfaJson.States)
fmt.Printf("Symbols: %v\n", dfaJson.Symbols)
fmt.Printf("Start State: %v\n", dfaJson.StartState)
fmt.Printf("Accept States: %v\n", dfaJson.AcceptStates)
fmt.Println("Transitions:")
for state, transitions := range dfaJson.Transitions {
fmt.Printf(" %s: %v\n", state, transitions)
}
}
// a problem with this function is that it doesn't print the format correctly
func printNfa(nfaJson nfa.NFA) {
fmt.Printf("States: %v\n", nfaJson.States)
fmt.Printf("Symbols: %v\n", nfaJson.Symbols)
fmt.Printf("Start State: %v\n", nfaJson.StartState)
fmt.Printf("Accept States: %v\n", nfaJson.AcceptStates)
fmt.Println("Transitions:")
for _, state := range nfaJson.States {
fmt.Printf(" %s: %v\n", state, nfaJson.Transitions[state])
}
}