antlr - ANTLR4 and the Python target -
i'm having issues getting going python target in antlr4. there seems few examples available , going corresponding java code doesn't seem relevant.
i'm using standard hello.g4 grammar:
// define grammar called hello grammar hello; r : 'hello' id ; // match keyword hello followed identifier id : [a-z]+ ; // match lower-case identifiers ws : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
the example (built standard hello.g4 example):
input_ = antlr4.filestream(_filename) lexer = hellolexer.hellolexer(input_) stream = antlr4.commontokenstream(lexer) parser = helloparser.helloparser(stream) rule_name = 'r' tree = getattr(parser, rule_name)()
i wrote listener. assert/verify correct, i'll repeat here:
class hellolistener(antlr4.parsetreelistener): def enterr(self, ctx): print("enterr") def exitr(self, ctx): print("exitr") def enterid(self, ctx): print("enterid") def exitid(self, ctx): print("exitid")
so, first, can't guarantee string i'm giving valid because i'm not getting screen output. how tell tree object if matched? how extract matching rules/tokens?
a python example great, if possible.
i hear you, having same issues right now. python documentation v4 useless , v3 differs usable. i'm thinking switching java implement stuff.
regarding code: think own custom listener has inherit generated hellolistener. can printing there.
also try parsing invalid input see if parser starts @ all. i'm not sure line getattr(parser, rule_name)() though. followed steps in (unfortunately short) documentation antlr4 python target: https://theantlrguy.atlassian.net/wiki/display/antlr4/python+target
you can find documentation listener stuff there. hope helps.
Comments
Post a Comment