Example #1
0
 def parse_rule(self, node):
     name = node.children[0].symbol.name
     alternatives = self.parse_alternatives(node.children[4])
     symbol = Nonterminal(name)
     if self.start_symbol is None:
         self.start_symbol = symbol
     if self.change_startrule and symbol.name == self.change_startrule:
         self.start_symbol = symbol
     r = Rule(symbol)
     for a in alternatives:
         r.add_alternative(a[0], a[1], a[2])
     # add additional alternatives to the grammar (grammar extension feature, e.g. languageboxes)
     if self.extra_alternatives.has_key(symbol.name):
         for n in self.extra_alternatives[symbol.name]:
             r.add_alternative([MagicTerminal(n), Nonterminal("WS")], None)
     self.rules[symbol] = r
Example #2
0
 def parse_rule(self, node):
     name = node.children[0].symbol.name
     self.current_rulename = name
     alternatives = self.parse_alternatives(node.children[4])
     symbol = Nonterminal(name)
     if self.start_symbol is None:
         self.start_symbol = symbol
     if self.change_startrule and symbol.name == self.change_startrule:
         self.start_symbol = symbol
     r = Rule(symbol)
     for a in alternatives:
         r.add_alternative(a[0], a[1], a[2])
     # add additional alternatives to the grammar (grammar extension feature, e.g. languageboxes)
     if self.extra_alternatives.has_key(symbol.name):
         for n in self.extra_alternatives[symbol.name]:
             r.add_alternative([MagicTerminal(n), Nonterminal("WS")], None)
     self.rules[symbol] = r
Example #3
0
    def create_parser(self, pickle_id=None):
        self.all_terminals.update(self.terminals)

        for fname, terminals, parentrule in self.functions:
            if fname.startswith("*match_until"):
                if Nonterminal(fname) not in self.rules:
                    r = Rule(Nonterminal(fname))
                    for t in self.all_terminals:
                        if t not in terminals:
                            r.add_alternative(
                                [Nonterminal(fname),
                                 Terminal(t)], None, t)
                    r.add_alternative([])
                    self.rules[r.symbol] = r
                # remove whitespace before special rule from parent rule, e.g.
                # multistring ::= "MLS" WS *match_until "MLS" WS
                #                       ^ this WS causes shift/reduce conflicts
                prule = self.rules[Nonterminal(parentrule)]
                for a in prule.alternatives:
                    for i in range(len(a)):
                        sym = a[i]
                        if sym.name == "WS":
                            if len(a) > i + 1 and a[i + 1].name.startswith(
                                    "*match_until"):
                                a.pop(i)
                                break

        if self.implicit_ws():
            ws_rule = Rule()
            ws_rule.symbol = Nonterminal("WS")
            ws_rule.add_alternative([Nonterminal("WS"), Terminal("<ws>")])
            # get comment rule
            if self.options.has_key('comment_rule'):
                cmt_rules = self.options['comment_rule']
                for cmt_rule in cmt_rules:
                    if Nonterminal(cmt_rule) in self.rules:
                        ws_rule.add_alternative(
                            [Nonterminal("WS"),
                             Nonterminal("comment")])
            if self.implicit_newlines():
                ws_rule.add_alternative(
                    [Nonterminal("WS"),
                     Terminal("<return>")])
                ws_rule.add_alternative([
                    Nonterminal("WS"),
                    Terminal("<backslash>"),
                    Terminal("<return>")
                ])
            ws_rule.add_alternative([])  # or empty
            self.rules[ws_rule.symbol] = ws_rule
            for a in ws_rule.alternatives:
                self.prod_ids[Production(ws_rule.symbol,
                                         a)] = len(self.prod_ids)

            # allow whitespace/comments at beginning of file
            start_rule = Rule()
            start_rule.symbol = Nonterminal("Startrule")
            start_rule.add_alternative([Nonterminal("WS"), self.start_symbol])
            self.rules[start_rule.symbol] = start_rule
            self.prod_ids[Production(start_rule.symbol,
                                     start_rule.alternatives[0])] = len(
                                         self.prod_ids)
            self.start_symbol = start_rule.symbol

        incparser = IncParser()
        incparser.from_dict(self.rules, self.start_symbol, self.lr_type,
                            self.implicit_ws(), pickle_id, self.precedences,
                            self.prod_ids)
        incparser.init_ast()
        self.incparser = incparser
Example #4
0
    def create_parser(self, pickle_id = None):
        startrule = self.ast.children[1] # startrule
        grammar = startrule.children[1]
        parser = grammar.children[0]
        assert parser.symbol.name == "parser"
        self.parse_rules(parser)

        if self.implicit_ws():
            ws_rule = Rule()
            ws_rule.symbol = Nonterminal("WS")
            ws_rule.add_alternative([Terminal("<ws>"), Nonterminal("WS")])
            ws_rule.add_alternative([Terminal("<return>"), Nonterminal("WS")])
            ws_rule.add_alternative([Terminal("<backslash>"), Terminal("<return>"), Nonterminal("WS")])
            ws_rule.add_alternative([]) # or empty
            self.rules[ws_rule.symbol] = ws_rule

            # allow whitespace/comments at beginning of file
            start_rule = Rule()
            start_rule.symbol = Nonterminal("Startrule")
            start_rule.add_alternative([Nonterminal("WS"), self.start_symbol])
            self.rules[start_rule.symbol] = start_rule
            self.start_symbol = start_rule.symbol

        incparser = IncParser()
        incparser.from_dict(self.rules, self.start_symbol, self.lr_type, self.implicit_ws(), pickle_id, self.precedences)
        incparser.init_ast()
        self.incparser = incparser