antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

Example mistakenly refers to VisitorInterp

Open jirislaby opened this issue 10 months ago • 1 comments

https://github.com/antlr/antlr4/blob/811b7fda58bd14d7f0abc496b6fd651dfa01ed97/doc/python-target.md?plain=1#L29

This first example should not refer to visitor.

jirislaby avatar Mar 13 '25 07:03 jirislaby

Looks like I never added the file with example for calling a visitor. See the code below. VisitorInterp should in the example because the audience is one who wants to have a working parser program--all the way through a visitor, as most people want to do something with the parse tree. At the time I wrote this, there were quite a few on StackOverflow and here who had problems writing a parser driver example in order to check their environment. So, this is why the example is a copy/paste program and not a "template."

Please feel free to correct the example.

$ cat VisitorInterp.py
import sys
from antlr4 import *
from ExprParser import ExprParser
from ExprVisitor import ExprVisitor

class VisitorInterp(ExprVisitor):
    def enterEveryRule(self, ctx:ParserRuleContext):
        pass

    def exitEveryRule(self, ctx:ParserRuleContext):
        pass

    def visitAtom(self, ctx:ExprParser.AtomContext):
        return int(ctx.getText())

    def visitExpr(self, ctx:ExprParser.ExprContext):
        if ctx.getChildCount() == 3:
            if ctx.getChild(0).getText() == "(":
                return self.visit(ctx.getChild(1))
            op = ctx.getChild(1).getText()
            v1 = self.visit(ctx.getChild(0))
            v2 = self.visit(ctx.getChild(2))
            if op == "+":
                return v1 + v2
            if op == "-":
                return v1 - v2
            if op == "*":
                return v1 * v2
            if op == "/":
                return v1 / v2
            return 0
        if ctx.getChildCount() == 2:
            opc = ctx.getChild(0).getText()
            if opc == "+":
                return self.visit(ctx.getChild(1))
            if opc == "-":
                return - self.visit(ctx.getChild(1))
            return 0
        if ctx.getChildCount() == 1:
            return self.visit(ctx.getChild(0))
        return 0

    def visitStart_(self, ctx:ExprParser.Start_Context):
        print("here")
        for i in range(0, ctx.getChildCount(), 2):
            print("result")
            print(self.visit(ctx.getChild(i)))
        return 0
03/13-07:16:59 ~/expr/Generated-Python3
$ cat Driver2.py
import sys
from antlr4 import *
from ExprLexer import ExprLexer
from ExprParser import ExprParser
from ListenerInterp import ListenerInterp

def main(argv):
    input_stream = FileStream(argv[1])
    lexer = ExprLexer(input_stream)
    stream = CommonTokenStream(lexer)
    parser = ExprParser(stream)
    tree = parser.start_()
    if parser.getNumberOfSyntaxErrors() > 0:
        print("syntax errors")
    else:
        linterp = ListenerInterp()
        walker = ParseTreeWalker()
        walker.walk(linterp, tree)

if __name__ == '__main__':
    main(sys.argv)

03/13-07:17:47 ~/expr/Generated-Python3
$ python Driver2.py in.txt
-1.0
1
5
72
03/13-07:18:10 ~/expr/Generated-Python3

kaby76 avatar Mar 13 '25 11:03 kaby76