line = "bs" while (F and line != ""): line = F.readline().rstrip() if (len(line) > 0 and line[0] != "#"): rules_string = rules_string + line F.close return rules_string if (len(sys.argv) < 2): print "need an argument for the grammar file name:" exit grammar_filename = sys.argv[1] grammar_string = read_file(grammar_filename) (terminal_strings, nonterminal_strings) = eval(grammar_string) rules = [] for s in nonterminal_strings: rules.append(RuleState.parse_RuleState(s)) terminals = [] for s in terminal_strings: # the terminal rules have pipes, but the nonterminals have spaces s = re.sub("\|", " ", s) + ",x" #print "---------->", s terminals.append(RuleState.parse_RuleState(s)) words = "book that flight".split(" ") e = Early(words, rules, terminals) e.early()
while (F and line != ""): line = F.readline().rstrip() if (len(line) > 0 and line[0] != "#") : rules_string = rules_string + line F.close return rules_string if (len(sys.argv) < 2): print "need an argument for the grammar file name:" exit grammar_filename = sys.argv[1] grammar_string = read_file(grammar_filename) (terminal_strings, nonterminal_strings) = eval(grammar_string) rules = [] for s in nonterminal_strings: rules.append(RuleState.parse_RuleState(s)) terminals = [] for s in terminal_strings: # the terminal rules have pipes, but the nonterminals have spaces s=re.sub("\|", " ", s) + ",x" #print "---------->", s terminals.append(RuleState.parse_RuleState(s)) words="book that flight".split(" ") e = Early(words, rules, terminals) e.early()
### main ### if (len(sys.argv) < 3): print "need an argument for the grammar filename and the data filename:" exit grammar_filename = sys.argv[1] grammar_string = read_file(grammar_filename) (terminal_strings, nonterminal_strings) = eval(grammar_string) rules = [] for s in nonterminal_strings: rules.append(RuleState.parse_RuleState(s)) terminals = [] for s in terminal_strings: # the terminal rules have pipes, but the nonterminals have spaces s=re.sub("\|", " ", s) + ",x" #print "---------->", s terminals.append(RuleState.parse_RuleState(s)) words_string = read_txt_file(sys.argv[2]) words_list = eval(words_string) for w in words_list: ##print w words = w.split() e = Early(words, rules, terminals) e.earley()
if (len(sys.argv) < 3): print "need an argument for the grammar filename and the data filename:" exit grammar_filename = sys.argv[1] grammar_string = read_file(grammar_filename) (terminal_strings, nonterminal_strings) = eval(grammar_string) rules = [] for s in nonterminal_strings: rules.append(RuleState.parse_RuleState(s)) terminals = [] for s in terminal_strings: # the terminal rules have pipes, but the nonterminals have spaces s=re.sub("\|", " ", s) #print "---------->", s terminals.append(RuleState.parse_RuleState(s)) words_string = read_txt_file(sys.argv[2]) words_list = eval(words_string) for w in words_list: ##print w words = w.split() e = Early(words, rules, terminals) trees=e.earley() for t in trees: t.print_rule_tree()