|
|
@ -24,16 +24,21 @@ func priority(op rune) int {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
shuntingYard applies the Shunting-Yard algorithm
|
|
|
|
The Shunting-Yard algorithm is used to convert the given infix (regeular) expression to postfix.
|
|
|
|
|
|
|
|
The primary benefit of this is getting rid of parentheses.
|
|
|
|
to convert the given infix expression to postfix. This makes
|
|
|
|
It also inserts explicit concatenation operators to make parsing easier in Thompson's algorithm.
|
|
|
|
it easier to parse the algorithm when doing Thompson.
|
|
|
|
|
|
|
|
See: https://blog.cernera.me/converting-regular-expressions-to-postfix-notation-with-the-shunting-yard-algorithm/
|
|
|
|
See: https://blog.cernera.me/converting-regular-expressions-to-postfix-notation-with-the-shunting-yard-algorithm/
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
func shuntingYard(re string) string {
|
|
|
|
func shuntingYard(re string) string {
|
|
|
|
re_postfix := make([]rune, 0)
|
|
|
|
re_postfix := make([]rune, 0)
|
|
|
|
re_runes := []rune(re)
|
|
|
|
re_runes := []rune(re) // Convert the string to a slice of runes to allow iteration through it
|
|
|
|
/* Add concatenation operators */
|
|
|
|
/* Add concatenation operators.
|
|
|
|
|
|
|
|
Only add a concatenation operator between two characters if both the following conditions are met:
|
|
|
|
|
|
|
|
1. The first character isn't an opening parantheses or alteration operator.
|
|
|
|
|
|
|
|
a. This makes sense, because these operators can't be _concatenated_ with anything else.
|
|
|
|
|
|
|
|
2. The second character isn't a 'closing operator' - one that applies to something before it
|
|
|
|
|
|
|
|
a. Again, these operators can'be concatenated _to_. They can, however, be concatenated _from_.
|
|
|
|
|
|
|
|
*/
|
|
|
|
for i := 0; i < len(re_runes); i++ {
|
|
|
|
for i := 0; i < len(re_runes); i++ {
|
|
|
|
re_postfix = append(re_postfix, re_runes[i])
|
|
|
|
re_postfix = append(re_postfix, re_runes[i])
|
|
|
|
if re_runes[i] != '(' && re_runes[i] != '|' {
|
|
|
|
if re_runes[i] != '(' && re_runes[i] != '|' {
|
|
|
@ -45,8 +50,6 @@ func shuntingYard(re string) string {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// fmt.Println(string(re_postfix))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
opStack := make([]rune, 0) // Operator stack
|
|
|
|
opStack := make([]rune, 0) // Operator stack
|
|
|
|
outQueue := make([]rune, 0) // Output queue
|
|
|
|
outQueue := make([]rune, 0) // Output queue
|
|
|
|
|
|
|
|
|
|
|
|