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.
This commit is contained in:
@@ -518,7 +518,7 @@ obj table (Object) {
|
|||||||
expand_to(from_state)
|
expand_to(from_state)
|
||||||
var cleaned_symbol = clean_symbol(on_symbol)
|
var cleaned_symbol = clean_symbol(on_symbol)
|
||||||
if (items[from_state].contains_key(cleaned_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
|
else
|
||||||
items[from_state].set(cleaned_symbol, vector::vector(action(reduce, by_rule_no, rule_position)))
|
items[from_state].set(cleaned_symbol, vector::vector(action(reduce, by_rule_no, rule_position)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ obj map<T,U> (Object) {
|
|||||||
var key_loc = keys.find(key)
|
var key_loc = keys.find(key)
|
||||||
if (key_loc == -1) {
|
if (key_loc == -1) {
|
||||||
io::println("trying to access nonexistant key-value!")
|
io::println("trying to access nonexistant key-value!")
|
||||||
/*while (true) {}*/
|
while (true) {}
|
||||||
}
|
}
|
||||||
return values.get(key_loc)
|
return values.get(key_loc)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,8 +94,12 @@ obj parser (Object) {
|
|||||||
act.print()
|
act.print()
|
||||||
if (act.act == push)
|
if (act.act == push)
|
||||||
to_shift.push(make_pair(v0, act.state_or_rule))
|
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))
|
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++;) {
|
for (var i = 0; i < input.size; i++;) {
|
||||||
@@ -129,10 +133,13 @@ obj parser (Object) {
|
|||||||
return null<tree<symbol>>()
|
return null<tree<symbol>>()
|
||||||
}
|
}
|
||||||
fun reducer(i: int) {
|
fun reducer(i: int) {
|
||||||
println("reducing")
|
print("reducing from state: ")
|
||||||
var curr_reduction = to_reduce.pop()
|
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)).
|
gss.get_reachable_paths(curr_reduction.from, max(0, curr_reduction.length-1)).
|
||||||
for_each(fun(path: ref vector<*tree<int>>) {
|
for_each(fun(path: ref vector<*tree<int>>) {
|
||||||
println("in get_reachable_paths for_each loop")
|
println("in get_reachable_paths for_each loop")
|
||||||
var path_edges = range(path.size-1).map(fun(indx: int): *tree<symbol> { return gss.get_edge(path[indx], path[indx+1]);}).reverse()
|
var path_edges = range(path.size-1).map(fun(indx: int): *tree<symbol> { return gss.get_edge(path[indx], path[indx+1]);}).reverse()
|
||||||
print("path ")
|
print("path ")
|
||||||
@@ -189,11 +196,15 @@ obj parser (Object) {
|
|||||||
if (curr_reduction.length) {
|
if (curr_reduction.length) {
|
||||||
gram.parse_table.get(shift_to, input[i]).for_each(fun(act: action) {
|
gram.parse_table.get(shift_to, input[i]).for_each(fun(act: action) {
|
||||||
var reduce_rule = gram.rules[act.state_or_rule]
|
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,
|
to_reduce.push(reduction(curr_reached, reduce_rule.lhs,
|
||||||
act.rule_position,
|
act.rule_position,
|
||||||
get_nullable_parts(reduce_rule),
|
get_nullable_parts(reduce_rule),
|
||||||
new_label))
|
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))
|
to_shift.push(make_pair(shift_to_node, act.state_or_rule))
|
||||||
} else {
|
} else {
|
||||||
var action_rule = gram.rules[act.state_or_rule]
|
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,
|
to_reduce.push(reduction(shift_to_node, action_rule.lhs, 0,
|
||||||
get_nullable_parts(action_rule),
|
get_nullable_parts(action_rule),
|
||||||
null<tree<symbol>>() ))
|
null<tree<symbol>>() ))
|
||||||
|
print("null reduces Adding reduction from state: ")
|
||||||
|
println(shift_to_node->data)
|
||||||
} else if (curr_reduction.length != 0) {
|
} else if (curr_reduction.length != 0) {
|
||||||
to_reduce.push(reduction(curr_reached, action_rule.lhs, act.rule_position,
|
to_reduce.push(reduction(curr_reached, action_rule.lhs, act.rule_position,
|
||||||
get_nullable_parts(action_rule),
|
get_nullable_parts(action_rule),
|
||||||
new_label ))
|
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)
|
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) {
|
gram.parse_table.get_reduces(shift.second, input[i+1]).for_each(fun(action: action) {
|
||||||
var reduce_rule = gram.rules[action.state_or_rule]
|
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,
|
to_reduce.push(reduction(shift.first, reduce_rule.lhs, action.rule_position,
|
||||||
get_nullable_parts(reduce_rule),
|
get_nullable_parts(reduce_rule),
|
||||||
new_label ))
|
new_label ))
|
||||||
|
print("if shift to node Adding reduction from state: ")
|
||||||
|
println(shift.first->data)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@@ -264,16 +284,23 @@ obj parser (Object) {
|
|||||||
} else {
|
} else {
|
||||||
println("is reduce")
|
println("is reduce")
|
||||||
var action_rule = gram.rules[action.state_or_rule]
|
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")
|
println("does not reduce to null")
|
||||||
to_reduce.push(reduction(shift.first, action_rule.lhs, action.rule_position,
|
to_reduce.push(reduction(shift.first, action_rule.lhs, action.rule_position,
|
||||||
get_nullable_parts(action_rule),
|
get_nullable_parts(action_rule),
|
||||||
new_label ))
|
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 {
|
} else {
|
||||||
println("does reduce to null")
|
println("does reduce to null")
|
||||||
to_reduce.push(reduction(shift_to_node, action_rule.lhs, 0,
|
to_reduce.push(reduction(shift_to_node, action_rule.lhs, 0,
|
||||||
get_nullable_parts(action_rule),
|
get_nullable_parts(action_rule),
|
||||||
null<tree<symbol>>() ))
|
null<tree<symbol>>() ))
|
||||||
|
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)
|
packed_map.set(node, packed)
|
||||||
}
|
}
|
||||||
fun fully_reduces_to_null(r: ref rule): bool {
|
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<symbol> {
|
fun get_nullable_parts(r: ref rule): *tree<symbol> {
|
||||||
if (fully_reduces_to_null(r))
|
if (reduces_to_null(r))
|
||||||
return new<tree<symbol>>()->construct(null_symbol())
|
return new<tree<symbol>>()->construct(null_symbol())
|
||||||
return null<tree<symbol>>()
|
return null<tree<symbol>>()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ obj vector<T> (Object) {
|
|||||||
println(index);
|
println(index);
|
||||||
print("Max Index of vector: ");
|
print("Max Index of vector: ");
|
||||||
println(size-1);
|
println(size-1);
|
||||||
/*while(true) {}*/
|
while(true) {}
|
||||||
return data[0];
|
return data[0];
|
||||||
}
|
}
|
||||||
return data[index];
|
return data[index];
|
||||||
|
|||||||
@@ -7,8 +7,10 @@ c = "a" | d ;
|
|||||||
d = e post_null post_non_null inherit_null ;
|
d = e post_null post_non_null inherit_null ;
|
||||||
inherit_null = e | post_non_null ;
|
inherit_null = e | post_non_null ;
|
||||||
e = ;
|
e = ;
|
||||||
e = f | ;
|
e = g ;
|
||||||
|
#e = e | g ;
|
||||||
f = ;
|
f = ;
|
||||||
|
g = f ;
|
||||||
post_null = "hi" ;
|
post_null = "hi" ;
|
||||||
post_non_null = "bye" ;
|
post_non_null = "bye" ;
|
||||||
rec = "hmm" rec | ;
|
rec = "hmm" rec | ;
|
||||||
|
|||||||
5
tests/grammer4.kgm
Normal file
5
tests/grammer4.kgm
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# comment
|
||||||
|
Goal = a ;
|
||||||
|
a = "a" d ;
|
||||||
|
d = g ;
|
||||||
|
g = ;
|
||||||
@@ -13,6 +13,7 @@ fun main():int {
|
|||||||
/*var a = load_grammer(read_file(string("grammer.kgm")))*/
|
/*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("grammer2.kgm")))*/
|
||||||
/*var a = load_grammer(read_file(string("grammer3.kgm")))*/
|
/*var a = load_grammer(read_file(string("grammer3.kgm")))*/
|
||||||
|
/*var a = load_grammer(read_file(string("grammer4.kgm")))*/
|
||||||
println(a.to_string())
|
println(a.to_string())
|
||||||
var doFirstSet = fun() {
|
var doFirstSet = fun() {
|
||||||
a.calculate_first_set()
|
a.calculate_first_set()
|
||||||
@@ -52,6 +53,7 @@ fun main():int {
|
|||||||
println(a.to_string())
|
println(a.to_string())
|
||||||
a.calculate_state_automaton()
|
a.calculate_state_automaton()
|
||||||
var parse.construct(a): parser
|
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(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("inport a;"), string("fun name"))*/
|
||||||
/*var result = parse.parse_input(string("fun main():int { return 0; }"), string("fun name"))*/
|
/*var result = parse.parse_input(string("fun main():int { return 0; }"), string("fun name"))*/
|
||||||
|
|||||||
Reference in New Issue
Block a user