def compile_get_grammar_table(grammar_file, debug, colors, prefer_shifts, prefer_shifts_over_empty): try: g = Grammar.from_file(grammar_file, _no_check_recognizers=True, debug_colors=colors) if debug: g.print_debug() table = create_load_table( g, prefer_shifts=prefer_shifts, prefer_shifts_over_empty=prefer_shifts_over_empty, force_create=True, debug=debug) if debug or table.sr_conflicts or table.rr_conflicts: table.print_debug() if not table.sr_conflicts and not table.rr_conflicts: h_print("Grammar OK.") if table.sr_conflicts: if len(table.sr_conflicts) == 1: message = 'There is 1 Shift/Reduce conflict.' else: message = 'There are {} Shift/Reduce conflicts.'\ .format(len(table.sr_conflicts)) a_print(message) prints("Either use 'prefer_shifts' parser mode, try to resolve " "manually, or use GLR parsing.") if table.rr_conflicts: if len(table.rr_conflicts) == 1: message = 'There is 1 Reduce/Reduce conflict.' else: message = 'There are {} Reduce/Reduce conflicts.'\ .format(len(table.rr_conflicts)) a_print(message) prints("Try to resolve manually or use GLR parsing.") except (GrammarError, ParseError) as e: print("Error in the grammar file.") print(e) sys.exit(1) return g, table
def trace(ctx, grammar_file, input_file, input): if not (input_file or input): prints('Expected either input_file or input string.') sys.exit(1) colors = ctx.obj['colors'] prefer_shifts = ctx.obj['prefer_shifts'] prefer_shifts_over_empty = ctx.obj['prefer_shifts_over_empty'] grammar, table = compile_get_grammar_table(grammar_file, True, colors, prefer_shifts, prefer_shifts_over_empty) parser = GLRParser(grammar, debug=True, debug_trace=True, debug_colors=colors, prefer_shifts=prefer_shifts, prefer_shifts_over_empty=prefer_shifts_over_empty) if input: parser.parse(input) else: parser.parse_file(input_file)
def check_get_grammar_table(grammar_file, debug, colors): try: g = Grammar.from_file(grammar_file, _no_check_recognizers=True, debug_colors=colors) if debug: g.print_debug() table = create_table(g) if debug: table.print_debug() h_print("Grammar OK.") if table.sr_conflicts: a_print("There are {} Shift/Reduce conflicts.".format( len(table.sr_conflicts))) prints("Either use 'prefer_shifts' parser mode, try to resolve " "manually or use GLR parsing.".format( len(table.sr_conflicts))) if table.rr_conflicts: a_print("There are {} Reduce/Reduce conflicts.".format( len(table.rr_conflicts))) prints("Try to resolve manually or use GLR parsing.") if (table.sr_conflicts or table.rr_conflicts) and not debug: prints("Run in debug mode to print all the states.") except (GrammarError, ParseError) as e: print("Error in the grammar file.") print(e) sys.exit(1) return g, table
def print_debug(self): a_print("*** GRAMMAR ***", new_line=True) h_print("Terminals:") prints(" ".join([text(t) for t in self.terminals])) h_print("NonTerminals:") prints(" ".join([text(n) for n in self.nonterminals])) h_print("Productions:") for p in self.productions: prints(text(p))
def print_debug(self): prints(text(self))
def print_debug(self): prints(str(self))
def _init_dynamic_disambiguation(self, context): if self.dynamic_filter: if self.debug: prints("\tInitializing dynamic disambiguation.") self.dynamic_filter(context, None, None, None, None, None)