Skip to content

Instantly share code, notes, and snippets.

@alonWang
Last active May 20, 2022 20:06
Show Gist options
  • Save alonWang/39ff7f8de6f48f78e9030203c831abf4 to your computer and use it in GitHub Desktop.
Save alonWang/39ff7f8de6f48f78e9030203c831abf4 to your computer and use it in GitHub Desktop.
easy lexer
package main
/*
简单的词法分析器演示
number: [0-9]+
identifier: [a-zA-Z_]
*/
/*
0 start
1 identifer
2 number
*/
var state = 0
var cache []rune
func main() {
var input = "123 abc 13 ag"
cache = make([]rune, 0)
for _, c := range input {
if state == 0 {
if c == ' ' {
continue
} else if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_' {
state = 1
cache = append(cache, c)
continue
} else if '0' <= c && c <= '9'{
state =2
cache = append(cache, c)
continue
}else{
println("Error,unrecognized character "+ string(c))
break
}
} else if state == 1 {
if c == ' '{
state = 0
println("token: " + string(cache))
cache=make([]rune,0)
continue
}else if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_'{
cache = append(cache, c)
continue
}else if '0' <= c && c <= '9'{
println("Error,unrecognized character "+ string(c))
break;
}
}else if state ==2 {
if c == ' '{
state = 0
println("token: " + string(cache))
cache=make([]rune,0)
continue
}else if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_'{
println("Error,unrecognized character "+ string(c))
break;
}else if '0' <= c && c <= '9'{
cache = append(cache, c)
continue
}
}
}
if len(cache) > 0 {
println("token: " + string(cache))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment