Added question mark operator - 0 or 1

master
Aadhavan Srinivasan 2 months ago
parent 13a57a4347
commit 7286e07858

@ -11,7 +11,7 @@ import (
const CONCAT rune = '~' const CONCAT rune = '~'
func isOperator(c rune) bool { func isOperator(c rune) bool {
if c == '+' || c == '*' || c == '|' || c == CONCAT { if c == '+' || c == '?' || c == '*' || c == '|' || c == CONCAT {
return true return true
} }
return false return false
@ -19,7 +19,7 @@ func isOperator(c rune) bool {
/* priority returns the priority of the given operator */ /* priority returns the priority of the given operator */
func priority(op rune) int { func priority(op rune) int {
precedence := []rune{'|', CONCAT, '+', '*'} precedence := []rune{'|', CONCAT, '+', '*', '?'}
return slices.Index(precedence, op) return slices.Index(precedence, op)
} }
@ -43,7 +43,7 @@ func shuntingYard(re string) string {
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] != '|' {
if i < len(re_runes)-1 { if i < len(re_runes)-1 {
if re_runes[i+1] != '|' && re_runes[i+1] != '*' && re_runes[i+1] != '+' && re_runes[i+1] != ')' { if re_runes[i+1] != '|' && re_runes[i+1] != '*' && re_runes[i+1] != '+' && re_runes[i+1] != '?' && re_runes[i+1] != ')' {
re_postfix = append(re_postfix, CONCAT) re_postfix = append(re_postfix, CONCAT)
} }
} }
@ -153,6 +153,15 @@ func thompson(re string) *State {
s2 := kleene(*s1) s2 := kleene(*s1)
s1 = concatenate(s1, s2) s1 = concatenate(s1, s2)
nfa = append(nfa, s1) nfa = append(nfa, s1)
case '?': // ab? is equivalent to a(b|)
s1 := mustPop(&nfa)
s2 := &State{}
s2.transitions = make(map[int][]*State)
s2.content = EPSILON
s2.output = make([]*State, 0)
s2.isEmpty = true
s3 := alternate(s1, s2)
nfa = append(nfa, s3)
case '|': case '|':
s1 := mustPop(&nfa) s1 := mustPop(&nfa)
s2 := mustPop(&nfa) s2 := mustPop(&nfa)

Loading…
Cancel
Save