def main(argv): input = FileStream("code.c") lexer = CymbolLexer(input) stream = CommonTokenStream(lexer) parser = CymbolParser(stream) tree = parser.fileF() visitor = MyVisitor() visitor.visit(tree)
def exitVarDecl(self, ctx:CymbolParser.VarDeclContext): startIndex = ctx.start.tokenIndex stopIndex = ctx.stop.tokenIndex cmtChannel = self.tokens.getHiddenTokensToRight(stopIndex, CymbolLexer.COMMENTS) if cmtChannel != None: tok = cmtChannel[0] if tok != None: token_array = self.tokens.tokens token_array.insert(startIndex, tok.clone()) tok.text = "\n" if __name__ == '__main__': if len(sys.argv) > 1: input_stream = FileStream(sys.argv[1]) else: input_stream = InputStream(sys.stdin.read()) lexer = CymbolLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = CymbolParser(token_stream) tree = parser.startrule() lisp_tree_str = tree.toStringTree(recog=parser) print(lisp_tree_str) walker = ParseTreeWalker() collector = CommentShifter(token_stream) walker.walk(collector, tree) print(token_stream.getText())
def enterFunctionDecl(self, ctx: CymbolParser.FunctionDeclContext): name = ctx.ID().getText() self.current_function_name = name self.graph.add_node(name) def exitCall(self, ctx): name = ctx.ID().getText() self.graph.add_edge(self.current_function_name, name) if __name__ == '__main__': if len(sys.argv) > 1: input_stream = FileStream(sys.argv[1]) else: input_stream = InputStream(sys.stdin.read()) lexer = CymbolLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = CymbolParser(token_stream) tree = parser.top() # lisp_tree_str = tree.toStringTree(recog=parser) # print(lisp_tree_str) walker = ParseTreeWalker() collector = FunctionListener() walker.walk(collector, tree) # print(collector.graph) print(collector.graph.toDOT())
startIndex = ctx.start.tokenIndex stopIndex = ctx.stop.tokenIndex cmtChannel = self.tokens.getHiddenTokensToRight( stopIndex, CymbolLexer.COMMENTS) if cmtChannel != None: tok = cmtChannel[0] if tok != None: token_array = self.tokens.tokens token_array.insert(startIndex, tok.clone()) tok.text = "\n" if __name__ == '__main__': if len(sys.argv) > 1: input_stream = FileStream(sys.argv[1]) else: input_stream = InputStream(sys.stdin.read()) lexer = CymbolLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = CymbolParser(token_stream) tree = parser.startrule() lisp_tree_str = tree.toStringTree(recog=parser) print(lisp_tree_str) walker = ParseTreeWalker() collector = CommentShifter(token_stream) walker.walk(collector, tree) print(token_stream.getText())