Added comments
This commit is contained in:
19
main.go
19
main.go
@@ -24,16 +24,21 @@ func priority(op rune) int {
|
||||
}
|
||||
|
||||
/*
|
||||
shuntingYard applies the Shunting-Yard algorithm
|
||||
|
||||
to convert the given infix expression to postfix. This makes
|
||||
it easier to parse the algorithm when doing Thompson.
|
||||
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.
|
||||
It also inserts explicit concatenation operators to make parsing easier in Thompson's algorithm.
|
||||
See: https://blog.cernera.me/converting-regular-expressions-to-postfix-notation-with-the-shunting-yard-algorithm/
|
||||
*/
|
||||
func shuntingYard(re string) string {
|
||||
re_postfix := make([]rune, 0)
|
||||
re_runes := []rune(re)
|
||||
/* Add concatenation operators */
|
||||
re_runes := []rune(re) // Convert the string to a slice of runes to allow iteration through it
|
||||
/* 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++ {
|
||||
re_postfix = append(re_postfix, 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
|
||||
outQueue := make([]rune, 0) // Output queue
|
||||
|
||||
|
Reference in New Issue
Block a user