Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rusty_lr_core/src/parser/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::parser::terminalclass::TerminalClass;
#[derive(Debug, Clone, Copy)]
pub struct ShiftTarget<StateIndex> {
pub state: StateIndex,
/// true if the data should be pushed, false if data should not be pushed
/// true if the data should be pushed, false if data should not be pushed (so `Empty` tag will be pushed)
pub push: bool,
}
impl<StateIndex> ShiftTarget<StateIndex> {
Expand Down
8 changes: 7 additions & 1 deletion rusty_lr_parser/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,13 @@ impl Grammar {
// Token -> Option<stack_name> map, `None` for empty
let token_to_stack_name = |token: Token<TerminalSymbol<usize>, usize>| match token {
Token::Term(term) => match term {
TerminalSymbol::Term(_) => Some(&terminal_stack_name),
TerminalSymbol::Term(term) => {
if self.terminal_classes[term].data_used {
Some(&terminal_stack_name)
} else {
None
}
}
TerminalSymbol::Error | TerminalSymbol::Eof => None,
},
Token::NonTerm(nonterm_idx) => stack_names_for_nonterm[nonterm_idx].as_ref(),
Expand Down
31 changes: 31 additions & 0 deletions rusty_lr_parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub struct TerminalClassDefinition {

/// compressed ranges, only if %tokentype is char or u8
pub ranges: Vec<(u32, u32)>,

/// Whether this class's data was used in any reduce action
pub data_used: bool,
}

pub enum OptimizeRemove {
Expand Down Expand Up @@ -1021,6 +1024,7 @@ impl Grammar {
terminals: vec![i],
multiterm_counter,
ranges,
data_used: true,
});
}
grammar.other_terminal_class_id = grammar.terminal_class_id[grammar.other_terminal_index];
Expand Down Expand Up @@ -1191,6 +1195,7 @@ impl Grammar {
terminals: terms,
multiterm_counter,
ranges: Vec::new(),
data_used: self.terminal_classes[old_classes[0]].data_used,
};
new_class_defs.push(class_def);
}
Expand Down Expand Up @@ -1542,6 +1547,24 @@ impl Grammar {
diag.removed.push(OptimizeRemove::NonTermDataNotUsed(i));
}

// check for any data of terminal symbol was used in any reduce action
for class_def in &mut self.terminal_classes {
class_def.data_used = false;
}
for nonterm in &self.nonterminals {
for rule in &nonterm.rules {
for token in &rule.tokens {
if let Token::Term(TerminalSymbol::Term(term)) = token.token {
if let Some(mapto) = &token.mapto {
if rule.reduce_action_contains_ident(mapto) {
self.terminal_classes[term].data_used = true;
}
}
}
}
}
}

for _ in 0..max_iter {
let ret = self.optimize_iterate();
match ret {
Expand Down Expand Up @@ -1941,6 +1964,14 @@ impl Grammar {
}
}

for state in &mut new_states {
for (term, shift_target) in &mut state.shift_goto_map_term {
if let TerminalSymbol::Term(term) = *term {
shift_target.push = self.terminal_classes[term].data_used;
}
}
}

self.states = new_states;

collector
Expand Down
Loading