Using antlr grammar rules to parse expression in Go

Viewed 348

Basically duplicating everything from how to access grammar components in antlr golang target

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 Answers

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.

Related