使用ANTLR语法规则来解析Go中的表达式

huangapple go评论88阅读模式
英文:

Using antlr grammar rules to parse expression in Go

问题

基本上是从https://stackoverflow.com/questions/64842665/复制的所有内容。

我有以下语法文件:

grammar Expr;

@parser::header {
import (
    "os"
)
}

@parser::members {

func eval(left int, op antlr.Token, right int) int {
    if   (op.GetText() == "*") {
        return left * right
    } else if (op.GetText() == "+") {
        return left + right
    } else if (op.GetText() == "-") {
        return left - right
    } else {
        return 0
    }
}
}

stat:   e NEWLINE
    |   NEWLINE                   
    ;

e returns [int v]
    : a=e op=('+'|'-') b=e  {
                $v = eval($a.v, $op, $b.v)
                fmt.Fprintf(os.Stdout, "got args=%d %d\n", $a.v, $b.v)
                }
    | INT                   {
                $v = $INT.int
                fmt.Fprintf(os.Stdout, "got number=%d\n", $v)
                }    
    ; 

MUL : '*';
DIV : '/';
ADD : '+';
SUB : '-';

ID  :   [a-zA-Z]+ ;      // match identifiers
INT :   [0-9]+ ;         // match integers
NEWLINE:'\r'? '\n' ;     // return newlines to parser (is end-statement signal)
WS  :   [ \t]+ -> skip ; // toss out whitespace

这是测试代码:

package main

import (
	"os"
	"./parser"
	"github.com/antlr/antlr4/runtime/Go/antlr"
)

func calc(inputfile string) {
    input, _ := antlr.NewFileStream(inputfile)// Setup the input
	lexer := parser.NewExprLexer(input)// Create the Lexer
	stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
	p := parser.NewExprParser(stream)// Create the Parser
        p.Stat()
}

func main() {
	calc(os.Args[1])
}

这些是我运行的命令:

java org.antlr.v4.Tool -Dlanguage=Go -o parser -no-listener Expr.g4
go build expr_t.go

上述两个程序都可以正常工作。然而,如何打印最终输出?我尝试了以下代码,但它不起作用:

stat:   e NEWLINE {
	$v = $e.int
	fmt.Printf("=%d\n", $v)
                }
    |   NEWLINE                   
    ;

我阅读了整个ANTLR Mega教程的部分,但没有找到有关此方面的详细信息。

英文:

Basically duplicating everything from https://stackoverflow.com/questions/64842665/

I have the following grammar file:

grammar Expr;

@parser::header {
import (
    "os"
)
}

@parser::members {

func eval(left int, op antlr.Token, right int) int {
    if   (op.GetText() == "*") {
        return left * right
    } else if (op.GetText() == "+") {
        return left + right
    } else if (op.GetText() == "-") {
        return left - right
    } else {
        return 0
    }
}
}

stat:   e NEWLINE
    |   NEWLINE                   
    ;

e returns [int v]
    : a=e op=('+'|'-') b=e  {
                $v = eval($a.v, $op, $b.v)
                fmt.Fprintf(os.Stdout, "got args=%d %d\n", $a.v, $b.v)
                }
    | INT                   {
                $v = $INT.int
                fmt.Fprintf(os.Stdout, "got number=%d\n", $v)
                }    
    ; 

MUL : '*' ;
DIV : '/' ;
ADD : '+' ;
SUB : '-' ;

ID  :   [a-zA-Z]+ ;      // match identifiers
INT :   [0-9]+ ;         // match integers
NEWLINE:'\r'? '\n' ;     // return newlines to parser (is end-statement signal)
WS  :   [ \t]+ -> skip ; // toss out whitespace

And this is the test code:

package main

import (
	"os"
	"./parser"
	"github.com/antlr/antlr4/runtime/Go/antlr"
)

func calc(inputfile string) {
    input, _ := antlr.NewFileStream(inputfile)// Setup the input
	lexer := parser.NewExprLexer(input)// Create the Lexer
	stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
	p := parser.NewExprParser(stream)// Create the Parser
        p.Stat()
}

func main() {
	calc(os.Args[1])
}

These are the commands that I have run:

java org.antlr.v4.Tool -Dlanguage=Go -o parser -no-listener Expr.g4
go build expr_t.go

The above 2 programs work correctly. However, how to print the final output? I tried with this and it is not working:

stat:   e NEWLINE {
	$v = $e.int
	fmt.Printf("=%d\n", $v)
                }
    |   NEWLINE                   
    ;

I went through the whole section of The ANTLR Mega Tutorial, but wasn't able to find the details on the aspect.

答案1

得分: 1

e规则返回一个名为v的值:

e returns [int v]
 : ...
 | ...
 ;

所以,不要使用$e.int,而应该使用$e.v

stat
 : e NEWLINE { fmt.Printf("=%d\n", $e.v) }
 | NEWLINE                   
 ;

但是,你也可以让入口规则stat返回一个值:

stat returns [int v]
 : a=e NEWLINE {$v = $a.v}
 | NEWLINE     {$v = 0}
 ;

然后在你的代码中,可以这样做:

func calc(inputfile string) {
    input, _ := antlr.NewFileStream(inputfile)
    lexer := parser.NewExprLexer(input)
    stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
    p := parser.NewExprParser(stream)
    result := p.Stat()
    fmt.Fprintf(os.Stderr, "%s", result.GetText())
    fmt.Printf("%d\n", result.GetV())
}

代码测试通过。

英文:

The e rule returns a value called v:

e returns [int v]
 : ...
 | ...
 ;

So, instead of doing $e.int you should do $e.v:

stat
 : e NEWLINE { fmt.Printf("=%d\n", $e.v) }
 | NEWLINE                   
 ;

But, you could also let the entry rule stat return a value:

stat returns [int v]
 : a=e NEWLINE {$v = $a.v}
 | NEWLINE     {$v = 0}
 ;

and then in your code, do something like this:

func calc(inputfile string) {
    input, _ := antlr.NewFileStream(inputfile)
    lexer := parser.NewExprLexer(input)
    stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
    p := parser.NewExprParser(stream)
	result := p.Stat()
	fmt.Fprintf(os.Stderr, "%s", result.GetText())
	fmt.Printf("%d\n", result.GetV())
}

Code tested fine.

huangapple
  • 本文由 发表于 2021年9月19日 04:33:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/69238230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定