Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example mistakenly refers to VisitorInterp #4796

Open
jirislaby opened this issue Mar 13, 2025 · 1 comment
Open

Example mistakenly refers to VisitorInterp #4796

jirislaby opened this issue Mar 13, 2025 · 1 comment

Comments

@jirislaby
Copy link

from VisitorInterp import VisitorInterp

This first example should not refer to visitor.

@kaby76
Copy link
Contributor

kaby76 commented Mar 13, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants