-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f793fc
commit 6d2e42a
Showing
7 changed files
with
1,549 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,132 @@ | ||
|
||
Lexical analize following function defition. | ||
```go | ||
func foo000() int { | ||
x := 1 * 10 + 123 - 1000 / 5432 | ||
|
||
return x | ||
} | ||
``` | ||
|
||
|
||
```bash | ||
$ go install github.com/goropikari/[email protected] | ||
$ golex sample.l | ||
$ go run golex.yy.go | ||
|
||
2 abb | ||
3 ab | ||
1 a | ||
1 a | ||
1 a | ||
Keyword | ||
"func" | ||
Identifier | ||
"foo000" | ||
LParen | ||
"(" | ||
RParen | ||
")" | ||
Identifier | ||
"int" | ||
LBracket | ||
"{" | ||
Identifier | ||
"x" | ||
Operator | ||
":=" | ||
Digit | ||
"1" | ||
Operator | ||
"*" | ||
Digit | ||
"10" | ||
Operator | ||
"+" | ||
Digit | ||
"123" | ||
Operator | ||
"-" | ||
Digit | ||
"1000" | ||
Operator | ||
"/" | ||
Digit | ||
"5432" | ||
Keyword | ||
"return" | ||
Identifier | ||
"x" | ||
RBracket | ||
"}" | ||
2022/08/29 01:39:20 EOF | ||
exit status 1 | ||
``` | ||
|
||
|
||
`golex.yy.go` | ||
`sample.l` | ||
```go | ||
// "a" { return State1, nil } | ||
// "abb" { return State2, nil } | ||
// "a*bb*" { return State3, nil } | ||
%{ | ||
import ( | ||
"fmt" | ||
"log" | ||
) | ||
|
||
type Type = int | ||
const ( | ||
Keyword Type = iota + 1 | ||
Identifier | ||
Digit | ||
Whitespace | ||
LParen | ||
RParen | ||
LBracket | ||
RBracket | ||
Operator | ||
) | ||
|
||
%} | ||
|
||
%% | ||
"if|for|while|func" { return Keyword, nil } | ||
"[a-zA-Z][a-zA-Z0-9]*" { return Identifier, nil } | ||
"[1-9][0-9]*" { return Digit, nil } | ||
"[ \t\n\r]*" { return Whitespace, nil } | ||
"\\(" { return LParen, nil } | ||
"\\)" { return RParen, nil } | ||
"{" { return LBracket, nil } | ||
"}" { return RBracket, nil } | ||
"[\\+|\\-|\\*|/|:=|==|!=]" { return Operator, nil } | ||
"." {} | ||
%% | ||
|
||
func main() { | ||
lex := New("abbabaaa") | ||
lex := New(` | ||
func foo000() { | ||
x := 1 * 10 + 123 - 1000 / 5432 | ||
} | ||
`) | ||
for { | ||
n, err := lex.Next() | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
switch n { | ||
case State1: | ||
fmt.Println(State1, YYtext) | ||
case State2: | ||
fmt.Println(State2, YYtext) | ||
case State3: | ||
fmt.Println(State3, YYtext) | ||
case Keyword: | ||
fmt.Println("Keyword") | ||
case Identifier: | ||
fmt.Println("Identifier") | ||
case Digit: | ||
fmt.Println("Digit") | ||
case Whitespace: | ||
fmt.Println("Whitespace") | ||
case LParen: | ||
fmt.Println("LParen") | ||
case RParen: | ||
fmt.Println("RParen") | ||
case LBracket: | ||
fmt.Println("LBracket") | ||
case RBracket: | ||
fmt.Println("RBracket") | ||
case Operator: | ||
fmt.Println("Operator") | ||
} | ||
fmt.Printf("\t %#v\n",YYtext) | ||
} | ||
} | ||
``` |
Oops, something went wrong.