Ejemplo n.º 1
0
 def visitWhileStat(self, ctx):
     if self._debug:
         print("while statement, condition is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
         print("and block is:")
         print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
     raise NotImplementedError()
Ejemplo n.º 2
0
def ast_to_actions_seq(node, rule_names, file_id, parent_id: int, action_id):
    global rules_dict, actions_dict, action_counter, rules_in_files, actions_in_files
    rule_name = Trees.getNodeText(node, rule_names)
    if rule_name not in rules_dict:
        rules_dict[rule_name] = (len(rules_dict), 1, 1)
        rules_in_files[rule_name] = {file_id}
    else:
        rules_in_files[rule_name].add(file_id)
        rules_dict[rule_name] = (rules_dict[rule_name][0],
                                 len(rules_in_files[rule_name]),
                                 rules_dict[rule_name][2] + 1)
    action_str = rule_name
    rule_seq = [[
        action_id, file_id, rules_dict[rule_name][0], parent_id, action_counter
    ]]
    action_counter += 1
    if not isinstance(node, antlr4.tree.Tree.TerminalNodeImpl):
        next_action_id = action_id
        for child in node.getChildren():
            child_subtree_seq = ast_to_actions_seq(child, rule_names, file_id,
                                                   action_id,
                                                   next_action_id + 1)
            rule_seq.extend(child_subtree_seq)
            action_str += '##' + Trees.getNodeText(child, rule_names)
            next_action_id = rule_seq[-1][0]
    if action_str not in actions_dict:
        actions_dict[action_str] = (len(actions_dict), 1, 1)
        actions_in_files[action_str] = {file_id}
    else:
        actions_in_files[action_str].add(file_id)
        actions_dict[action_str] = (actions_dict[action_str][0],
                                    len(actions_in_files[action_str]),
                                    actions_dict[action_str][2] + 1)
    rule_seq[0].append(actions_dict[action_str][0])
    return rule_seq
Ejemplo n.º 3
0
 def visitCondBlock(self, ctx):
     if self._debug:
         print("condblockstatement, condition is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
         print("and block is:")
         print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
     pass # TODO
Ejemplo n.º 4
0
 def visitWhileStat(self, ctx):
     if self._debug:
         print("while statement, condition is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
         print("and block is:")
         print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
         print("je suis là")
     pass # TODO
Ejemplo n.º 5
0
 def visitCondBlock(self, ctx):
     if self._debug:
         print("condblockstatement, condition is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
         print("and block is:")
         print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
     end_if = self.ctx_stack[-1]  # get the label for the end!
     raise NotImplementedError()
Ejemplo n.º 6
0
    def generate_tree(cls, code_line):
        tree, parser = get_grammar_tree(code_text=code_line)
        test_tree = Trees.getChildren(tree)[0]

        string_tree = Trees.toStringTree(test_tree, recog=parser)
        string_tree = string_tree.replace('( (', '[ (').replace(') )', ') ]')

        return Tree.fromstring(string_tree)
Ejemplo n.º 7
0
 def traverse(self, tree):
     parent_label = Trees.getNodeText(tree, self.parser.ruleNames)
     self.nodes.append((self.stack[-1], parent_label))
     for child in Trees.getChildren(tree):
         self.cnt += 1
         self.edges.append((self.stack[-1], self.cnt))
         self.stack.append(self.cnt)
         self.traverse(child)
         self.stack.pop()
Ejemplo n.º 8
0
def traverse(tree: Tree, file, dot, node_name):
    children = Trees.getChildren(tree)
    for child in children:
        node = Trees.getNodeText(tree, DbQlGrammarParser.ruleNames)
        child_node = Trees.getNodeText(child, DbQlGrammarParser.ruleNames)
        child_name = get_node_name()

        dot.node(node_name, node)
        dot.node(child_name, child_node)

        dot.edge(node_name, child_name)
        traverse(child, file, dot, child_name)
 def visitWhileStat(self, ctx):
     if self._debug:
         print("while statement, condition is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
         print("and block is:")
         print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
     labelbegin, labelend = self._current_function.new_label_while()
     self._current_function.addLabel(labelbegin)
     reg = self.visit(ctx.expr())
     self._current_function.addInstructionCondJUMP(labelend, reg,
                                                   Condition("beq"), 0)
     self.visit(ctx.stat_block())
     self._current_function.addInstructionJUMP(labelbegin)
     self._current_function.addLabel(labelend)
 def visitCondBlock(self, ctx):
     if self._debug:
         print("condblockstatement, condition is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
         print("and block is:")
         print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
     end_if = self.ctx_stack[-1]  # get the label for the end!
     next_cond = self._current_function.new_label('end_cond')
     cond = self.visit(ctx.expr())
     self._current_function.addInstructionCondJUMP(next_cond, cond,
                                                   Condition("beq"), 0)
     self.visit(ctx.stat_block())
     self._current_function.addInstructionJUMP(end_if)
     self._current_function.addLabel(next_cond)
Ejemplo n.º 11
0
def process(t, ruleNames):
    global level
    if t.getChildCount() == 0:
        return escapeWhitespace(Trees.getNodeText(t, ruleNames), False)
    sb = ""
    sb += lead(level)
    level += 1
    s = escapeWhitespace(Trees.getNodeText(t, ruleNames), False)
    sb += (s + ' ')
    for i in range(t.getChildCount()):
        sb += process(t.getChild(i), ruleNames)
    level -= 1
    # sb += lead(level)
    return sb
 def visitWhileStat(self, ctx):
     if self._debug:
         print("while statement, condition is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
         print("and block is:")
         print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
     ltest1 = self._current_function.new_label("ltest1")
     lendwhile1 = self._current_function.new_label("lendwhile")
     self._current_function.addLabel(ltest1)
     tmp4=self.visit(ctx.expr())
     self._current_function.addInstructionCondJUMP(lendwhile1,tmp4,Condition(MiniCParser.EQ),0)
     self.visit(ctx.stat_block())
     self._current_function.addInstructionJUMP(ltest1)
     self._current_function.addLabel(lendwhile1)
 def visitCondBlock(self, ctx):
     if self._debug:
         print("condblockstatement, condition is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
         print("and block is:")
         print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
     tmp4=self.visit(ctx.expr())
     lendif1 = self._current_function.new_label("lendif")
     self._current_function.addInstructionCondJUMP(lendif1,tmp4,Condition(MiniCParser.EQ),0)
     self.visit(ctx.stat_block())
     end_if=self.ctx_stack[-1]
     self._current_function.addInstructionJUMP(end_if)
     self._current_function.addLabel(lendif1)
     return tmp4
Ejemplo n.º 14
0
def parse(
    text: str,
    start: str,
    strict=False,
    transform: Union[str, Callable] = None,
    error_listener: ErrorListener = None,
) -> ParseTree:

    lexer = LexerGo(antlr4.InputStream(text))
    lexer.removeErrorListeners()
    lexer.addErrorListener(LexerErrorListener())

    stream = CommonTokenStream(lexer)
    parser = ParserGo(stream)

    tree = parser.sourceFile()
    printer = ParserGoListener()
    walker = ParseTreeWalker()
    walker.walk(printer, tree)
    visitor = ParserGoVisitor()

    parser.buildParseTrees = True  # default

    if strict:
        error_listener = StrictErrorListener()

    if error_listener is not None and error_listener is not True:
        parser.removeErrorListeners()
        if error_listener:
            parser.addErrorListener(error_listener)

    print(Trees.toStringTree(tree, None, parser))

    return tree
Ejemplo n.º 15
0
 def evaluate(self, t: ParseTree):
     # return all children of t that match nodeName
     return [
         c for c in Trees.getChildren(t)
         if isinstance(c, TerminalNode) and (
             c.symbol.type == self.tokenType) == (not self.invert)
     ]
Ejemplo n.º 16
0
def parse(argv):
    if len(sys.argv) > 1:
        input = FileStream(argv[1])  #read the first argument as a filestream
        lexer = DecafLexer(input)  #call your lexer
        stream = CommonTokenStream(lexer)
        #stream.fill()
        #print(stream.tokens)
        #tokencitos = ([token.text for token in stream.tokens])
        #print(tokencitos)
        parser = DecafParser(stream)
        tree = parser.program(
        )  #start from the parser rule, however should be changed to your entry rule for your specific grammar
        #pydot__tree_to_png(tree, "./tree.png")

        traverse(tree, parser.ruleNames)
        traverse_intermediate_code(tree, parser.ruleNames)

        f = open('treegen.txt', 'w')
        f.write((Trees.toStringTree(tree, None, parser)))

        f.close()
        #printer = DecafListener()
        printer = ImpListener()
        walker = ParseTreeWalker()
        walker.walk(printer, tree)

        #print(walker)
    else:
        print('Error : Expected a valid file')
Ejemplo n.º 17
0
 def visitCondBlock(self, ctx):
     if self._debug:
         print("condblockstatement, condition is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
         print("and block is:")
         print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
     end_if = self.ctx_stack[-1]
     next_cond = self._prog.new_label('end_cond')
     cond = self.visit(ctx.expr())
     src_cond = self._prog.addInstructionCondJUMP(next_cond, cond,
                                                  Condition('eq'),
                                                  Immediate(0))
     self.visit(ctx.stat_block())
     i = self._prog.addInstructionJUMP(end_if)
     self._prog.addLabel(next_cond)
     return src_cond
Ejemplo n.º 18
0
 def visitRelationalExpr(self, ctx):
     c = Condition(ctx.myop.type)
     if self._debug:
         print("relational expression:")
         print(Trees.toStringTree(ctx, None, self._parser))
         print("Condition:", c)
     raise NotImplementedError()
Ejemplo n.º 19
0
def get_pretty_tree(
    tree: "ParseTree", rule_names: list = None, parser: Parser = None, level: int = 0
) -> str:
    """Take antlr ``ParseTree`` and return indented tree format for test comparison.

    Adapted from ``antrl4.tree.Trees.toStringTree()`` method.

    Args:
        tree: The antlr parse tree.
        rule_names: Names of parser rules.
        parser: The parser used to generated the tree.
        level: Level of tree (used for indentation).

    Returns:
        Pretty tree format (indents of one space at each level).
    """
    indent_value = "  "  # indent using two spaces to match ``yaml`` reference files

    if parser is not None:
        rule_names = parser.ruleNames

    node_text = Trees.getNodeText(tree, rule_names)
    pretty_tree = level * indent_value + node_text + "\n"

    if tree.getChildCount() > 0:
        for i in range(0, tree.getChildCount()):
            pretty_tree += get_pretty_tree(tree.getChild(i), rule_names=rule_names, level=level + 1)

    return pretty_tree
    def __traverse(self, tree, counter):
        root = Trees.getNodeText(tree, DbQLGrammarParser.ruleNames)
        root_name = str(counter)

        for child in Trees.getChildren(tree):
            child_node = Trees.getNodeText(child, DbQLGrammarParser.ruleNames)
            new_counter = counter + 1
            child_name = str(new_counter)
            
            self.dot.node(root_name, root)
            self.dot.node(child_name, child_node)
            self.dot.edge(root_name, child_name)
            
            counter = self.__traverse(child, new_counter)
        
        return counter
Ejemplo n.º 21
0
 def evaluate(self, t: ParseTree):
     # return all children of t that match nodeName
     return [
         c for c in Trees.getChildren(t)
         if isinstance(c, ParserRuleContext) and (
             c.ruleIndex == self.ruleIndex) == (not self.invert)
     ]
Ejemplo n.º 22
0
 def evaluate(self, t):
     # return all children of t that match nodeName
     nodes = []
     for c in Trees.getChildren(t):
         if isinstance(c, TerminalNode):
             if (c.symbol.type == self.tokenType) == (not self.invert):
                 nodes.append(c)
     return nodes
Ejemplo n.º 23
0
 def visitWhileStat(self, ctx):
     if self._debug:
         print("while statement, condition is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
         print("and block is:")
         print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
     
     test = self._prog.new_label("test")
     end_while = self._prog.new_label("end_while")
     self._prog.addLabel(test)
     dr = self.visit(ctx.expr())
     #If expr is false, jump to end
     self._prog.addInstructionCondJUMP(end_while, dr, Condition(MuParser.EQ), 0)
     #else execute code
     self.visit(ctx.stat_block())
     self._prog.addInstructionJUMP(test)
     self._prog.addLabel(end_while)
Ejemplo n.º 24
0
    def visitCondBlock(self, ctx):
        if self._debug:
            print("condblockstatement, condition is:")
            print(Trees.toStringTree(ctx.expr(), None, self._parser))
            print("and block is:")
            print(Trees.toStringTree(ctx.stat_block(), None, self._parser))
        end_if = self.ctx_stack[-1]  # get the label for the end!

        end_cond = self._prog.new_label("end_cond")

        dr = self.visit(ctx.expr())
        #If condition (in dr) is false jump to endif
        self._prog.addInstructionCondJUMP(end_cond, dr, Condition(MuParser.EQ), 0)
        #Else condition is true
        self.visit(ctx.stat_block())
        self._prog.addInstructionJUMP(end_if)
        self._prog.addLabel(end_cond)
Ejemplo n.º 25
0
 def visitRelationalExpr(self, ctx):
     if self._debug:
         print("relational expression:")
         print(Trees.toStringTree(ctx, None, self._parser))
     tmpl = self.visit(ctx.expr(0))
     tmpr = self.visit(ctx.expr(1))
     c = Condition(ctx.myop.type)
     raise NotImplementedError()
Ejemplo n.º 26
0
 def evaluate(self, t: ParseTree):
     # return all children of t that match nodeName
     nodes = []
     for c in Trees.getChildren(t):
         if isinstance(c, TerminalNode):
             if (c.symbol.type == self.tokenType) == (not self.invert):
                 nodes.append(c)
     return nodes
Ejemplo n.º 27
0
 def evaluate(self, t: ParseTree):
     # return all children of t that match nodeName
     nodes = []
     for c in Trees.getChildren(t):
         if isinstance(c, ParserRuleContext):
             if (c.ruleIndex == self.ruleIndex) == (not self.invert):
                 nodes.append(c)
     return nodes
def main():
    ais = FileStream(sys.argv[1])
    lexer = myGrammarLexer(ais)
    tokens = CommonTokenStream(lexer)
    parser = myGrammarParser(tokens)
    tree = parser.rules()

    print Trees.toStringTree(tree, None, parser)

    # generating dot file
    walker = ParseTreeWalker()
    collector = FunctionListener()
    walker.walk(collector, tree)
    print collector.graph.toDOT()

    # using graphviz
    subprocess.check_call(["dot", "-Tpng", "test.dot", "-o", "dt.png"])
Ejemplo n.º 29
0
 def evaluate(self, t:ParseTree):
     # return all children of t that match nodeName
     nodes = []
     for c in Trees.getChildren(t):
         if isinstance(c, ParserRuleContext ):
             if (c.ruleIndex == self.ruleIndex ) == (not self.invert):
                 nodes.append(c)
     return nodes
Ejemplo n.º 30
0
def aster(path):
    dir_path = os.path.dirname(os.path.abspath(path))
    name = os.path.splitext(os.path.basename(path))[0]
    lexer = BasisLexer(FileStream(path))
    stream = CommonTokenStream(lexer)
    parser = BasisParser(stream)
    ast = parser.program()
    with open(dir_path + "\\" + name + ".rkt", 'w') as f:
        f.write(Trees.toStringTree(ast, None, parser))
        f.close()
Ejemplo n.º 31
0
def aster(path):
    dir_path = os.path.dirname(os.path.abspath(path))
    name = os.path.splitext(os.path.basename(path))[0]
    lexer = WenyanLexer(FileStream(path, encoding='utf8'))
    stream = CommonTokenStream(lexer)
    parser = WenyanParser(stream)
    ast = parser.program()
    with open('ast/' + name + ".txt", 'w') as f:
        f.write(Trees.toStringTree(ast, None, parser))
        f.close()
 def visitAssignStat(self, ctx):
     if self._debug:
         print("assign statement, rightexpression is:")
         print(Trees.toStringTree(ctx.expr(), None, self._parser))
     reg4expr = self.visit(ctx.expr())
     name = ctx.ID().getText()
     if name in self._memory:
         self._current_function.addInstructionMV(self._memory[name], reg4expr)
     else:
         raise Exception("Variable is not declared")
Ejemplo n.º 33
0
def main(argv):
    print "Parsing: " + argv[1] + "\n"

    input = FileStream(argv[1])
    lexer = SQLiteLexer(input)
    stream = CommonTokenStream(lexer)
    parser = SQLiteParser(stream)

    tree = parser.parse()

    print(Trees.toStringTree(tree, None, parser))
Ejemplo n.º 34
0
def main():
    '''Main entry point for the dfs CLI.'''
    args = docopt(__doc__, version=__version__)
    dslfile = args["FILE"]
    input = FileStream(dslfile)
    lexer = FinalStatePatternLexer(input)
    stream = CommonTokenStream(lexer)
    parser = FinalStatePatternParser(stream)
    tree = parser.top_level()
    traverser = FSPOntologyListener()
    walker = ParseTreeWalker()
    walker.walk(traverser, tree)
    print(Trees.toStringTree(tree, None, parser))
Ejemplo n.º 35
0
    def parse(self, sql):

        in_stream = InputStream(sql)
        lexer = SparksqlLexer(in_stream)
        token_stream = CommonTokenStream(lexer)

        try:

            parser = SparksqlParser(token_stream)
            parser._listeners = [CustomErrorListener()]
            self.tree = parser.select_statement()
            self.tree_str = Trees.toStringTree(self.tree, None, parser)
            self.is_valid = True

        except Exception as ex:
            self.is_valid = False
            self.error = ex
Ejemplo n.º 36
0
    def parse(formula: str):
        f = InputStream(formula)
        lexer = FODTLLexer(f)
        stream = CommonTokenStream(lexer)
        parser = FODTLParser(stream)
        parser.addParseListener(FodtlParser())
        parser.buildParseTrees = True

        # TODO : important prediction mode optimization (we gain x5 speed up with SLL)
        # use SLL for the first pass, if an error detected we probably have to make a second pass
        # using LL mode (to check, see the doc)
        # parser._interp.predictionMode = PredictionMode.LL  # ~2.5
        parser._interp.predictionMode = PredictionMode.SLL  # ~0.4
        # parser._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION  # ~2.5
        tr = parser.main()
        bt = Trees.toStringTree(tr, recog=parser)
        # print(bt)
        l = parser.getParseListeners().pop(0)
        return l.formula
Ejemplo n.º 37
0
 def toStringTree(self, ruleNames=None, recog=None):
     return Trees.toStringTree(self, ruleNames=ruleNames, recog=recog)
Ejemplo n.º 38
0
    def loop(self):
        while True:
            try:
                if self.readMore:
                    sys.stdout.write("... ")
                    sys.stdout.flush()
                    self.single_input += sys.stdin.readline()
                else:
                    sys.stdout.write(">>> ")
                    sys.stdout.flush()
                    self.single_input = sys.stdin.readline()

                input_stream = antlr4.InputStream(self.single_input)

                # Instantiate and run generated lexer
                self.lexer = CustomLexer(input_stream)
                self.tokens = antlr4.CommonTokenStream(self.lexer)

                # Setting up error handling stuff
                error_handler = CustomErrorStrategy()
                error_listener = CustomErrorListener()
                buffered_errors = BufferedErrorListener()
                error_listener.addDelegatee(buffered_errors)

                # Run parser and set error handler
                self.parser = TinyPyParser(self.tokens)
                self.parser._errHandler = error_handler

                # Remove default terminal error listener & and our own
                self.parser.removeErrorListeners()
                self.parser.addErrorListener(error_listener)

                # Parse input
                parse_tree = self.parser.single_input()

                # Determine what to do next
                if error_listener.eof_received:
                    print()
                    exit(0)
                elif error_listener.input_unfinished or self.lexer.opened > 0:
                    # User has not finished his input yet, read the next line and repeat
                    self.readMore = True
                    continue
                elif error_listener.errors_encountered > 0:
                    # Errors encountered, start over
                    print(buffered_errors.buffer)
                    self.readMore = False
                    continue
                else:
                    # Successfully parsed the input, next time start over
                    self.readMore = False

                # Build a flattened syntax tree
                cst = CST.CstFiltered(tree=parse_tree)

                # Print some stuff... (if needed)
                if self.args.cst:
                    print(cst)

                if self.args.parse_tree:
                    parseTreeString = Trees.toStringTree(parse_tree, recog=self.parser)
                    print(parseTreeString)

                # Evaluate it...
                visitor = CustomVisitor()
                ast = visitor.visitSingle_input(parse_tree)
                if ast == None:
                    continue

                if self.args.parse_only:
                    continue

                results = ast.eval()

                #
                # ast.eval() returns list of statements; loop through them and print
                #
                if results != None:
                    for statement in results:
                        if statement != None and not isinstance(statement, ControlFlowMark):
                            sys.displayhook(statement)

                #if results != None:
                #    sys.displayhook(results)

            except KeyboardInterrupt as e:
                print("")
                exit(0)
            except antlr4.RecognitionException as e:
                print("Caught" + str(e) )
            except runtime.Errors.BaseRuntimeException as e:
                print(e.__class__.__name__ + ": " + str(e))
            except SystemExit as e:
                sys.exit(e)
            except BaseException as e:
                print(e.__class__.__name__ + ": " + str(e))
Ejemplo n.º 39
0
 def evaluate(self, t:ParseTree):
     if self.invert:
         return list() # !* is weird but valid (empty)
     else:
         return Trees.getChildren(t)
Ejemplo n.º 40
0
 def evaluate(self, t:ParseTree):
     if self.invert:
         return list() # !* is weird but valid (empty)
     else:
         return Trees.descendants(t)
Ejemplo n.º 41
0
 def evaluate(self, t:ParseTree):
     return Trees.findAllTokenNodes(t, self.tokenType)
Ejemplo n.º 42
0
 def evaluate(self, t:ParseTree):
     # return all children of t that match nodeName
     return [c for c in Trees.getChildren(t) if isinstance(c, ParserRuleContext) and (c.ruleIndex == self.ruleIndex) == (not self.invert)]
Ejemplo n.º 43
0
 def evaluate(self, t:ParseTree):
     return Trees.findAllRuleNodes(t, self.ruleIndex)
Ejemplo n.º 44
0
 def toStringTree(self, ruleNames: list = None, recog: Parser = None):
     return Trees.toStringTree(self, ruleNames=ruleNames, recog=recog)
Ejemplo n.º 45
0
 def delete(self):
     if self._depth == 0:
         Trees.remove(self)
     else:
         self.parent.children.remove(self)
     del self
Ejemplo n.º 46
0
 def evaluate(self, t):
     # return all children of t that match nodeName
     return [c for c in Trees.getChildren(t) if isinstance(c, TerminalNode) and (c.symbol.type == self.tokenType) == (not self.invert)]