From 6a62f03fb4b58a4a5ea3dbe39de9e28d08bd010b Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Thu, 13 Aug 2015 01:48:35 -0400 Subject: [PATCH] Move away from fully_reduces_to_null to rule_position, fixed some bugs. Have not fixed all, still looks up unkown key-value for the full kraken parse test. --- stdlib/grammer.krak | 2 +- stdlib/map.krak | 2 +- stdlib/parser.krak | 48 +++++++++++++++++++++++++++++++++-------- stdlib/vector.krak | 2 +- tests/grammer2.kgm | 4 +++- tests/grammer4.kgm | 5 +++++ tests/test_grammer.krak | 2 ++ 7 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 tests/grammer4.kgm diff --git a/stdlib/grammer.krak b/stdlib/grammer.krak index f0e31a8..b6ac06b 100644 --- a/stdlib/grammer.krak +++ b/stdlib/grammer.krak @@ -518,7 +518,7 @@ obj table (Object) { expand_to(from_state) var cleaned_symbol = clean_symbol(on_symbol) if (items[from_state].contains_key(cleaned_symbol)) - items[from_state][cleaned_symbol].addEnd(action(reduce, by_rule_no)) + items[from_state][cleaned_symbol].addEnd(action(reduce, by_rule_no, rule_position)) else items[from_state].set(cleaned_symbol, vector::vector(action(reduce, by_rule_no, rule_position))) } diff --git a/stdlib/map.krak b/stdlib/map.krak index 36adf6b..3b4ef2e 100644 --- a/stdlib/map.krak +++ b/stdlib/map.krak @@ -51,7 +51,7 @@ obj map (Object) { var key_loc = keys.find(key) if (key_loc == -1) { io::println("trying to access nonexistant key-value!") - /*while (true) {}*/ + while (true) {} } return values.get(key_loc) } diff --git a/stdlib/parser.krak b/stdlib/parser.krak index e5d0c59..9b5ef9e 100644 --- a/stdlib/parser.krak +++ b/stdlib/parser.krak @@ -94,8 +94,12 @@ obj parser (Object) { act.print() if (act.act == push) to_shift.push(make_pair(v0, act.state_or_rule)) - else if (act.act == reduce && fully_reduces_to_null(gram.rules[act.state_or_rule])) + /*else if (act.act == reduce && fully_reduces_to_null(gram.rules[act.state_or_rule])) {*/ + else if (act.act == reduce && act.rule_position == 0) { + print("act == reduce && == 0 Adding reduction from state: ") + println(v0->data) to_reduce.push(reduction(v0, gram.rules[act.state_or_rule].lhs, 0, null_symbol_tree, null_symbol_tree)) + } }) for (var i = 0; i < input.size; i++;) { @@ -129,10 +133,13 @@ obj parser (Object) { return null>() } fun reducer(i: int) { - println("reducing") + print("reducing from state: ") var curr_reduction = to_reduce.pop() + println(curr_reduction.from->data) + print("curr_reduction.length (not length-1) is: ") + println(curr_reduction.length) gss.get_reachable_paths(curr_reduction.from, max(0, curr_reduction.length-1)). - for_each(fun(path: ref vector<*tree>) { + for_each(fun(path: ref vector<*tree>) { println("in get_reachable_paths for_each loop") var path_edges = range(path.size-1).map(fun(indx: int): *tree { return gss.get_edge(path[indx], path[indx+1]);}).reverse() print("path ") @@ -189,11 +196,15 @@ obj parser (Object) { if (curr_reduction.length) { gram.parse_table.get(shift_to, input[i]).for_each(fun(act: action) { var reduce_rule = gram.rules[act.state_or_rule] - if (act.act == reduce && !fully_reduces_to_null(reduce_rule)) + /*if (act.act == reduce && !fully_reduces_to_null(reduce_rule)) {*/ + if (act.act == reduce && act.rule_position != 0) { to_reduce.push(reduction(curr_reached, reduce_rule.lhs, act.rule_position, get_nullable_parts(reduce_rule), new_label)) + print("(non null) Adding reduction from state: ") + println(curr_reached->data) + } }) } } @@ -206,14 +217,20 @@ obj parser (Object) { to_shift.push(make_pair(shift_to_node, act.state_or_rule)) } else { var action_rule = gram.rules[act.state_or_rule] - if (fully_reduces_to_null(action_rule)) { + // tricky tricky tricky. Fully reduces to null is not the same as act.rule_position being 0 + /*if (fully_reduces_to_null(action_rule)) {*/ + if (act.rule_position == 0) { to_reduce.push(reduction(shift_to_node, action_rule.lhs, 0, get_nullable_parts(action_rule), null>() )) + print("null reduces Adding reduction from state: ") + println(shift_to_node->data) } else if (curr_reduction.length != 0) { to_reduce.push(reduction(curr_reached, action_rule.lhs, act.rule_position, get_nullable_parts(action_rule), new_label )) + print("null does not reduce Adding reduction from state: ") + println(curr_reached->data) } } }) @@ -242,10 +259,13 @@ obj parser (Object) { gss.add_edge(shift_to_node, shift.first, new_label) gram.parse_table.get_reduces(shift.second, input[i+1]).for_each(fun(action: action) { var reduce_rule = gram.rules[action.state_or_rule] - if (!fully_reduces_to_null(reduce_rule)) { + /*if (!fully_reduces_to_null(reduce_rule)) {*/ + if (action.rule_position != 0) { to_reduce.push(reduction(shift.first, reduce_rule.lhs, action.rule_position, get_nullable_parts(reduce_rule), new_label )) + print("if shift to node Adding reduction from state: ") + println(shift.first->data) } }) } else { @@ -264,16 +284,23 @@ obj parser (Object) { } else { println("is reduce") var action_rule = gram.rules[action.state_or_rule] - if (!fully_reduces_to_null(action_rule)) { + /*if (!fully_reduces_to_null(action_rule)) {*/ + if (action.rule_position != 0) { println("does not reduce to null") to_reduce.push(reduction(shift.first, action_rule.lhs, action.rule_position, get_nullable_parts(action_rule), new_label )) + print("not shift to, reduce, != 0 Adding reduction from state: ") + println(shift.first->data) + print("added ruduction rule+position: ") + println(action.rule_position) } else { println("does reduce to null") to_reduce.push(reduction(shift_to_node, action_rule.lhs, 0, get_nullable_parts(action_rule), null>() )) + print("not shift to, reduce, == 0 Adding reduction from state: ") + println(shift_to_node->data) } } }) @@ -326,10 +353,13 @@ obj parser (Object) { packed_map.set(node, packed) } fun fully_reduces_to_null(r: ref rule): bool { - return r.position == 0 && gram.first_vector(r.rhs).contains(null_symbol()) + return r.position == 0 && reduces_to_null(r) + } + fun reduces_to_null(r: ref rule): bool { + return gram.first_vector(r.rhs).contains(null_symbol()) } fun get_nullable_parts(r: ref rule): *tree { - if (fully_reduces_to_null(r)) + if (reduces_to_null(r)) return new>()->construct(null_symbol()) return null>() } diff --git a/stdlib/vector.krak b/stdlib/vector.krak index c53a8bf..9138b2c 100644 --- a/stdlib/vector.krak +++ b/stdlib/vector.krak @@ -121,7 +121,7 @@ obj vector (Object) { println(index); print("Max Index of vector: "); println(size-1); - /*while(true) {}*/ + while(true) {} return data[0]; } return data[index]; diff --git a/tests/grammer2.kgm b/tests/grammer2.kgm index 214728d..f860a60 100644 --- a/tests/grammer2.kgm +++ b/tests/grammer2.kgm @@ -7,8 +7,10 @@ c = "a" | d ; d = e post_null post_non_null inherit_null ; inherit_null = e | post_non_null ; e = ; -e = f | ; +e = g ; +#e = e | g ; f = ; +g = f ; post_null = "hi" ; post_non_null = "bye" ; rec = "hmm" rec | ; diff --git a/tests/grammer4.kgm b/tests/grammer4.kgm new file mode 100644 index 0000000..ea06865 --- /dev/null +++ b/tests/grammer4.kgm @@ -0,0 +1,5 @@ +# comment +Goal = a ; +a = "a" d ; +d = g ; +g = ; diff --git a/tests/test_grammer.krak b/tests/test_grammer.krak index cb37ee4..d6cb4d0 100644 --- a/tests/test_grammer.krak +++ b/tests/test_grammer.krak @@ -13,6 +13,7 @@ fun main():int { /*var a = load_grammer(read_file(string("grammer.kgm")))*/ /*var a = load_grammer(read_file(string("grammer2.kgm")))*/ /*var a = load_grammer(read_file(string("grammer3.kgm")))*/ + /*var a = load_grammer(read_file(string("grammer4.kgm")))*/ println(a.to_string()) var doFirstSet = fun() { a.calculate_first_set() @@ -52,6 +53,7 @@ fun main():int { println(a.to_string()) a.calculate_state_automaton() var parse.construct(a): parser + /*var result = parse.parse_input(string("a"), string("fun name"))*/ var result = parse.parse_input(read_file(string("to_parse.krak")), string("fun name")) /*var result = parse.parse_input(string("inport a;"), string("fun name"))*/ /*var result = parse.parse_input(string("fun main():int { return 0; }"), string("fun name"))*/