From 3c741567df287b8ecbe9b191f7c05a094a144adf Mon Sep 17 00:00:00 2001 From: jusax23 Date: Sat, 9 Nov 2024 21:33:32 +0100 Subject: [PATCH] low stack fix --- src/bin/g10.rs | 5 +++-- src/cfg/lr_parser.rs | 12 ++++-------- src/main.rs | 4 ++-- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/bin/g10.rs b/src/bin/g10.rs index 5b247b1..8ad117f 100644 --- a/src/bin/g10.rs +++ b/src/bin/g10.rs @@ -79,9 +79,10 @@ fn main() { println!("automaton: {:?}", grammar.lr1_automaton); println!("conflict: {}", grammar.gen_slr_parse_table()); println!("conflict: {}", grammar.gen_lr1_parse_table()); - println!("parse_table: {:?}", grammar.lr1_automaton); + println!("parse_table: {:?}", grammar.slr_parse_table.as_ref().unwrap().0.len()); + println!("parse_table: {:?}", grammar.lr1_automaton.as_ref().unwrap().0.len()); println!( "parsed: {:?}", grammar.slr_parser(&mut m.iter_mut()).parse() - ) + ); } diff --git a/src/cfg/lr_parser.rs b/src/cfg/lr_parser.rs index c3b279b..ef8184b 100644 --- a/src/cfg/lr_parser.rs +++ b/src/cfg/lr_parser.rs @@ -78,9 +78,7 @@ where .unwrap_or("end of file".to_string()) )); }; - - println!("next: {next:?}, state: {current_state:?}, stack: {stack:?}"); - + match current_state { LrAction::Shift(to) => { stack.push((NodeChild::Data(next.expect("Can not shift on EOF.")), *to)); @@ -110,13 +108,11 @@ where childs: childs.into_iter().rev().map(|(a, _)| a.clone()).collect(), }); } - let Some(state) = stack.last() else { - return Err("Unexpected EOS 2".into()); - }; - let Some(next) = self.get_goto(&state.1, rule) else { + let state = stack.last().map(|s| s.1).unwrap_or(self.parse_table.2); + let Some(next) = self.get_goto(&state, rule) else { return Err(format!( "Invalid reduction: state: {} rule: {:?}", - state.1, rule + state, rule )); }; stack.push(( diff --git a/src/main.rs b/src/main.rs index 7ba4c19..36b8fb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -197,7 +197,7 @@ fn main() { //println!("follow: {:?}", grammar.follow); grammar.gen_lr1_automaton(); println!("conflict: {:?}", grammar.lr1_automaton); - /* let conflict = grammar.gen_ll_parse_table(); + let conflict = grammar.gen_ll_parse_table(); println!("conflict: {conflict}"); println!("prase table: {:?}", grammar.ll_parse_table); println!("parse\n\n"); @@ -207,5 +207,5 @@ fn main() { .ll_parser(&mut m.iter_mut()) .parse() .map(|tree| tree.clean()) - ) */ + ) }