class RDP(object):

    def __init__(self, filename, grammar):
        self.attempted_matches = 0
        self.matched_then_discarded = 0
        self.correctly_matched = 0
        self.grammar = grammar
        self.token_stream = TokenStream(filename)

    def parse(self, nt, start_pos=0):
        result = ""
        if self.recursive_parse(nt, start_pos=0)[0]:
            result += "Success input is valid"
        else:
            result += "Fail input is not valid"
        result += self.display_metrics()
        return result

    def recursive_parse(self, nt, start_pos=0):
        sub_count = 0   #counter for number of succesfull matched rhs (used to count discarded matches)
        for production in self.grammar.grammar_productions:
            next_pos = start_pos
            if production.lhs == nt:
                valid = True
                for symbol in production.rhs:
                    valid = False
                    self.attempted_matches += 1
                    if symbol in self.grammar.get_terminals():
                        if self.token_stream.get_token(next_pos) == symbol:
                            next_pos += 1
                            valid = True
                        else:
                            break

                    else:
                        temp = self.recursive_parse(symbol, next_pos)
                        if temp[0]:
                            valid = True
                            next_pos = temp[1]
                            sub_count += temp[2] + 1
                        else:
                            break

                if valid:
                    self.correctly_matched += 1
                    #print production #uncomment to display correcly matched productions
                    return [True, next_pos, sub_count]

        self.matched_then_discarded += sub_count
        self.correctly_matched -= sub_count
        return [False, 0, sub_count]

    def display_metrics(self):
        metrics = "\nNumber of RHS matches attempted: " + str(self.attempted_matches) +\
                  "\nNumber of RHS matched then later discarded: " + str(self.matched_then_discarded)+\
                  "\nNumber of RHS matched in final result: " + str(self.correctly_matched)
        self.attempted_matches = 0
        self.matched_then_discarded = 0
        self.correctly_matched = 0
        return metrics
예제 #2
0
class NBTP(object):
    def __init__(self, filename, grammar):
        self.token_stream = TokenStream(filename)
        self.grammar = grammar
        temp = ParsingTable(grammar, sentence_symbol)
        self.parse_table = temp.get_table()

    def parse(self):
        start_symbol = ll1_grammar.grammar_productions[0].lhs
        output = ""
        position = 0
        sentinel = 99
        match_count = 0
        stack = list()

        stack.append(sentinel)
        stack.append(start_symbol)

        a = self.token_stream.get_token(position)
        t = stack[-1]

        while t != sentinel:
            if t == a:
                stack.pop()
                position += 1
                a = self.token_stream.get_token(position)
            elif t in self.grammar.get_terminals():
                return "Input invalid Error occured"
            elif self.parse_table[t][a] == []:
                return "Input invalid Error occured"
            else:
                match_count += 1
                output += "\n" + str(self.parse_table[t][a])
                stack.pop()
                for symbol in reversed((self.parse_table[t][a]).rhs):
                    stack.append(symbol)
            t = stack[-1]

        return "Input is valid.\nThere were " + str(match_count) +\
               " matched productions.(which are shown below)\n\n" + output
class NBTP(object):

    def __init__(self, filename, grammar):
        self.token_stream = TokenStream(filename)
        self.grammar = grammar
        temp = ParsingTable(grammar, sentence_symbol)
        self.parse_table = temp.get_table()

    def parse(self):
        start_symbol = ll1_grammar.grammar_productions[0].lhs
        output = ""
        position = 0
        sentinel = 99
        match_count = 0
        stack = list()

        stack.append(sentinel)
        stack.append(start_symbol)

        a = self.token_stream.get_token(position)
        t = stack[-1]

        while t != sentinel:
            if t == a:
                stack.pop()
                position +=1
                a = self.token_stream.get_token(position)
            elif t in self.grammar.get_terminals(): return "Input invalid Error occured"
            elif self.parse_table[t][a] == []: return "Input invalid Error occured"
            else:
                match_count += 1
                output += "\n" + str(self.parse_table[t][a])
                stack.pop()
                for symbol in reversed((self.parse_table[t][a]).rhs):
                    stack.append(symbol)
            t = stack[-1]

        return "Input is valid.\nThere were " + str(match_count) +\
               " matched productions.(which are shown below)\n\n" + output
 def __init__(self, filename, grammar):
     self.attempted_matches = 0
     self.matched_then_discarded = 0
     self.correctly_matched = 0
     self.grammar = grammar
     self.token_stream = TokenStream(filename)
예제 #5
0
 def __init__(self, filename, grammar):
     self.token_stream = TokenStream(filename)
     self.grammar = grammar
     temp = ParsingTable(grammar, sentence_symbol)
     self.parse_table = temp.get_table()
 def __init__(self, filename, grammar):
     self.token_stream = TokenStream(filename)
     self.grammar = grammar
     temp = ParsingTable(grammar, sentence_symbol)
     self.parse_table = temp.get_table()