aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-03-23 00:19:56 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-03-23 00:19:56 +0100
commit2da427d49c7c04b83f8ad81534db471d3037a41a (patch)
treec7d8332178dd4bf26be4d4d62622889ead7c9a3d
parent50d8fcfe69fce34fed41b006189fd827a124bf97 (diff)
Parser stub
-rw-r--r--lexer.go9
-rw-r--r--lexer_test.go5
-rw-r--r--main.go20
-rw-r--r--parser.y13
4 files changed, 27 insertions, 20 deletions
diff --git a/lexer.go b/lexer.go
index fb247f7..8082e35 100644
--- a/lexer.go
+++ b/lexer.go
@@ -22,18 +22,11 @@ func (y *yyLex) Error(s string) {
log.Println(s)
}
-/*
-type yySymType struct{}
func (y *yyLex) Lex(lval *yySymType) (ret int) {
item := <-y.items
+ lval.sval = item.val
return item.typ
}
-*/
-
-func (y *yyLex) Lex() (int, string) {
- item := <-y.items
- return item.typ, item.val
-}
func lex(input string) *yyLex {
l := &yyLex{
diff --git a/lexer_test.go b/lexer_test.go
index 785a810..27cdfed 100644
--- a/lexer_test.go
+++ b/lexer_test.go
@@ -4,12 +4,13 @@ import "testing"
func testInput(t *testing.T, ty int, s ...string) {
for _, input := range s {
+ yy := new(yySymType)
l := lex(input + "\n")
- typ, s := l.Lex()
+ typ := l.Lex(yy)
if input[0] == '\'' {
input = input[1 : len(input)-1]
}
- if typ != ty || s != input {
+ if typ != ty || yy.sval != input {
t.Error("expeced", input, "got", s)
}
}
diff --git a/main.go b/main.go
index 2d615b1..6cc9a4b 100644
--- a/main.go
+++ b/main.go
@@ -1,28 +1,28 @@
package main
+//go:generate -command yacc to tool yacc
+//go:generate yacc -o parser.go parser.y
+
import (
"bufio"
+ "flag"
"io"
"os"
- "fmt"
)
+func init() {
+ flag.IntVar(&yyDebug, "debug", 1, "debug level")
+ flag.Parse()
+}
+
func main() {
in := bufio.NewReader(os.Stdin)
-
for {
os.Stdout.WriteString("\t")
line, err := in.ReadString('\n')
if err == io.EOF {
return
}
- l := lex(line)
- for {
- i, s := l.Lex()
- if i == EOF {
- break
- }
- fmt.Println(i, s)
- }
+ yyParse(lex(line))
}
}
diff --git a/parser.y b/parser.y
new file mode 100644
index 0000000..4a4a5a3
--- /dev/null
+++ b/parser.y
@@ -0,0 +1,13 @@
+%{
+package main
+%}
+
+%union {
+ sval string
+}
+
+%%
+
+line:
+
+%%