You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
8.4 KiB
Go

2 months ago
package main
import (
"bufio"
2 months ago
"fmt"
"os"
2 months ago
"slices"
"github.com/fatih/color"
2 months ago
)
const CONCAT rune = '~'
func isOperator(c rune) bool {
if c == '+' || c == '?' || c == '*' || c == '|' || c == CONCAT {
2 months ago
return true
}
return false
}
/* priority returns the priority of the given operator */
func priority(op rune) int {
precedence := []rune{'|', CONCAT, '+', '*', '?'}
2 months ago
return slices.Index(precedence, op)
}
/*
2 months ago
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.
2 months ago
See: https://blog.cernera.me/converting-regular-expressions-to-postfix-notation-with-the-shunting-yard-algorithm/
*/
func shuntingYard(re string) []postfixNode {
2 months ago
re_postfix := make([]rune, 0)
2 months ago
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_.
*/
i := 0
for i < len(re_runes) {
2 months ago
re_postfix = append(re_postfix, re_runes[i])
if re_runes[i] == '[' && (i == 0 || re_runes[i-1] != '\\') { // We do not touch things inside brackets, unless they are escaped
for re_runes[i] != ']' {
i++ // Skip all characters inside brackets
// TODO: Check for escaped characters
// Check ahead for character range
if i < len(re_runes)-2 && re_runes[i+1] == '-' {
rangeStart := re_runes[i]
rangeEnd := re_runes[i+2]
if int(rangeEnd) < int(rangeStart) {
panic("Range is out of order.")
}
for i := rangeStart; i <= rangeEnd; i++ {
re_postfix = append(re_postfix, i)
}
i += 2 // Skip start and hyphen (end will automatically be skipped on next iteration of loop)
continue
}
re_postfix = append(re_postfix, re_runes[i])
}
continue
}
2 months ago
if re_runes[i] != '(' && re_runes[i] != '|' {
if i < len(re_runes)-1 {
if re_runes[i+1] != '|' && re_runes[i+1] != '*' && re_runes[i+1] != '+' && re_runes[i+1] != '?' && re_runes[i+1] != ')' {
2 months ago
re_postfix = append(re_postfix, CONCAT)
}
}
}
i++
2 months ago
}
opStack := make([]rune, 0) // Operator stack
outQueue := make([]postfixNode, 0) // Output queue
2 months ago
// Actual algorithm
for i := 0; i < len(re_postfix); i++ {
2 months ago
/* Two cases:
1. Current character is alphanumeric - send to output queue
2. Current character is operator - do the following:
a. If current character has greater priority than top of opStack, push to opStack.
b. If not, keep popping from opStack (and appending to outQueue) until:
i. opStack is empty, OR
ii. current character has greater priority than top of opStack
3. If current character is '(', push to opStack
4. If current character is ')', pop from opStack (and append to outQueue) until '(' is found. Discard parantheses.
5. If current character is '[', find all the characters until ']', then create a postfixNode containing all these contents. Add this node to outQueue.
2 months ago
*/
c := re_postfix[i]
2 months ago
if isAlphaNum(c) {
outQueue = append(outQueue, newPostfixNode(c))
continue
2 months ago
}
// Escape character - NOT IMPLEMENTED YET - DO NOT USE
// if c == '\\' { // Escape character - next character is treated as alphanum
// if i == len(re_postfix)-1 { // End of string - panic, because backslash is an escape character (something needs to come after it)
// panic("ERROR: Backslash with no escape character.")
// }
// outQueue = append(outQueue, re_postfix[i+1])
// }
if c == '.' { // Dot metacharacter - represents 'any' character, but I am only adding Unicode 0020-007E
outQueue = append(outQueue, newPostfixNode(dotCharacters()...))
continue
}
2 months ago
if isOperator(c) {
if len(opStack) == 0 {
opStack = append(opStack, c)
} else {
topStack, err := peek(opStack)
if err != nil {
panic("ERROR: Operator without operand.")
}
if priority(c) > priority(topStack) { // 2a
2 months ago
opStack = append(opStack, c)
} else {
for priority(c) <= priority(topStack) { // 2b
to_append := mustPop(&opStack)
outQueue = append(outQueue, newPostfixNode(to_append))
topStack, _ = peek(opStack)
2 months ago
}
opStack = append(opStack, c)
}
}
}
if c == '[' { // Used for character classes
i++ // Step forward so we can look at the character class
chars := make([]rune, 0) // List of characters - used only for character classes
for i < len(re_postfix) {
if re_postfix[i] == ']' {
break
}
chars = append(chars, re_postfix[i])
i++
}
if i == len(re_postfix) { // We have reached the end of the string, so we didn't encounter a closing brakcet. Panic.
panic("ERROR: Opening bracket without closing bracket.")
}
outQueue = append(outQueue, newPostfixNode(chars...))
i++ // Step forward to skip closing bracket
continue
}
2 months ago
if c == '(' {
opStack = append(opStack, c)
}
if c == ')' {
// Keep popping from opStack until we encounter an opening parantheses. Panic if we reach the end of the stack.
for val, err := peek(opStack); val != '('; val, err = peek(opStack) {
if err != nil {
panic("ERROR: Imbalanced parantheses.")
}
to_append := mustPop(&opStack)
outQueue = append(outQueue, newPostfixNode(to_append))
2 months ago
}
_ = mustPop(&opStack) // Get rid of opening parantheses
2 months ago
}
}
// Pop all remaining operators (and append to outQueue)
for len(opStack) > 0 {
to_append := mustPop(&opStack)
outQueue = append(outQueue, newPostfixNode(to_append))
2 months ago
}
return outQueue
2 months ago
}
// Thompson's algorithm. Constructs Finite-State Automaton from given string.
// Returns start state.
func thompson(re []postfixNode) *State {
nfa := make([]*State, 0) // Stack of states
2 months ago
for _, c := range re {
if c.nodetype == CHARACTER {
2 months ago
state := State{}
state.transitions = make(map[int][]*State)
state.content = rune2Contents(c.contents)
2 months ago
state.output = make([]*State, 0)
state.output = append(state.output, &state)
state.isEmpty = false
nfa = append(nfa, &state)
2 months ago
}
// Must be an operator if it isn't a character
switch c.nodetype {
case CONCATENATE:
s2 := mustPop(&nfa)
s1 := mustPop(&nfa)
s1 = concatenate(s1, s2)
2 months ago
nfa = append(nfa, s1)
case KLEENE: // Create a 0-state, concat the popped state after it, concat the 0-state after the popped state
s1 := mustPop(&nfa)
stateToAdd := kleene(*s1)
nfa = append(nfa, stateToAdd)
case PLUS: // a+ is equivalent to aa*
s1 := mustPop(&nfa)
s2 := kleene(*s1)
s1 = concatenate(s1, s2)
nfa = append(nfa, s1)
case QUESTION: // ab? is equivalent to a(b|)
s1 := mustPop(&nfa)
s2 := &State{}
s2.transitions = make(map[int][]*State)
s2.content = newContents(EPSILON)
s2.output = append(s2.output, s2)
s2.isEmpty = true
s3 := alternate(s1, s2)
nfa = append(nfa, s3)
case PIPE:
s1 := mustPop(&nfa)
s2 := mustPop(&nfa)
s3 := alternate(s1, s2)
nfa = append(nfa, s3)
2 months ago
}
}
if len(nfa) != 1 {
panic("ERROR: Invalid Regex.")
}
verifyLastStates(nfa)
return nfa[0]
}
func main() {
// Process:
// 1. Convert regex into postfix notation (Shunting-Yard algorithm)
// a. Add explicit concatenation operators to facilitate this
// 2. Build NFA from postfix representation (Thompson's algorithm)
// 3. Run the string against the NFA
if len(os.Args) != 2 {
fmt.Println("ERROR: Missing cmdline args")
os.Exit(22)
}
2 months ago
var re string
re = os.Args[1]
var test_str string
// Read test string from stdin
reader := bufio.NewReader(os.Stdin)
test_str, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
fmt.Scanln(&test_str)
2 months ago
re_postfix := shuntingYard(re)
// fmt.Println(re_postfix)
startState := thompson(re_postfix)
matchIndices := findAllMatches(startState, test_str)
inColor := false
if len(matchIndices) > 0 {
for i, c := range test_str {
for _, indices := range matchIndices {
if i >= indices.startIdx && i < indices.endIdx {
color.New(color.FgRed).Printf("%c", c)
inColor = true
break
}
}
if inColor == false {
fmt.Printf("%c", c)
}
inColor = false
}
} else {
fmt.Print(test_str)
}
2 months ago
}