goyacc text/scanner 示例

在編譯的原理的世界中,lex and yacc 的 example 很經典。在 python 中,有第三方工具包ply實現了類似於 lex and yacc的工具。跟傳統的lex 和yacc 一樣,lex 提供 token 供 yacc 進行語法分析(移進–規約),在go語言的世界中,可使用golang 自帶的 text/scanner 近似的替代lex(它提供golang語言的詞法分析),使用goyacc完成語法分析的過程。下面就將給出一個使用上述環境的example。

// filename : main.y
// usage: goyacc -o "main.go" main.y && go run main.go

%{

package main

import (
    "bufio"
    "os"
    "strings"
    "text/scanner"
    "strconv"
    "log"
    "io"
    "strings"
    "fmt"
)

%}

%union {
    num int
}

%type <num> expr term

%token '+' '-' '*' '/'

%token <num> NUM

%%

top:    expr
        {  fmt.Println($1) }
expr:   term
        { $$ = $1 }
    |   expr '+' term
        { $$ = $1 + $3 }
    |   expr '-' term
        { $$ = $1 - $3}
    ;

term:   NUM
        { $$ = $1 }
    |   term '*' NUM
        { $$ = $1 * $3 }
    |   term '/' NUM
        { $$ = $1 / $3 }
    ;

%%

const eof=0

type MainLex struct {
    scanner.Scanner
}

func (l *MainLex) Lex(lval *yySymType) int {
    token, lit := l.Scan(), l.TokenText()
    tok := int(token)

    if tok == scanner.Int {
        lval.num, _ = strconv.Atoi(lit)
        return NUM
    }
    return tok
}
func (x *MainLex) Error(s string) {
    log.Printf("parse error: %s", s)
}

func main() {
    in := bufio.NewReader(os.Stdin)
    for {
        if _, err := os.Stdout.WriteString("> "); err != nil {
            log.Fatalf("WriteString: %s", err)
        }
        line, err := in.ReadString('\n')
        if err == io.EOF {
            return
        }
        if err != nil {
            log.Fatalf("ReadBytes: %s", err)
        }
        s := new(MainLex)
        s.Init(strings.NewReader(line))
        yyParse(s)
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章