Skip to content

Commit bfa3454

Browse files
committed
fix potential bug for calc prec
1 parent b8dfce3 commit bfa3454

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

src/lib/driver/driver.mbt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,30 @@ pub fn compile(
219219
augmented_start_nt.productions.push(production)
220220
production
221221
})
222+
223+
// fix prec for productions
224+
for prod in productions {
225+
match prod.prec {
226+
Some(_) => ()
227+
None => {
228+
let rightmost_term = prod.rhs.rev_iter().find_first(fn(x) { x is T(_) })
229+
match rightmost_term {
230+
Some(T(terminal)) => {
231+
let term = get_terminal_by_name(terminal.name)
232+
prod.prec = match term.prec {
233+
Some((prec, _)) => Some(prec)
234+
None => None
235+
}
236+
}
237+
Some(NT(_)) => panic()
238+
None => ()
239+
}
240+
}
241+
}
242+
}
243+
// for prod in productions {
244+
// println(prod)
245+
// }
222246
let grammar : @grm.Grammar = { terminals, nonterminals, productions, starts }
223247
let automaton = @lr1.Automaton::build(grammar, user_eoi=input_mode is Pull)
224248
let errors = @lr1.resolve_conflicts(automaton.conflicts)

src/lib/elab/elaborate.mbt

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -312,27 +312,12 @@ pub fn elaborate(
312312
}
313313
Some(prec)
314314
}
315-
None => {
316-
/// FROM ocamlyacc's manual
317-
// > Tokens and rules have precedences.
318-
// > By default, the precedence of a rule is the precedence of its rightmost terminal.
319-
// I don't what "rightmost terminal" means, but I guess it means the last terminal in the rule.
320-
// So, I'll use the precedence of the last terminal in the rule.
321-
let mut last_prec = None
322-
for item in items.rev_iter() {
323-
match item.term {
324-
Token(token) => {
325-
last_prec = match token.prec {
326-
None => None
327-
Some((prec, _assoc)) => Some(prec)
328-
}
329-
break
330-
}
331-
Param(_) | RuleCall(_) => ()
332-
}
333-
}
334-
last_prec
335-
}
315+
None =>
316+
// The prec should be determined by the rightmost terminal
317+
// in the rule. But the items of rule may be modified later, So
318+
// we will set it to None for now, and delay the decision
319+
// at building lr1 grammar.
320+
None
336321
}
337322
let clause = Clause::{
338323
items,

0 commit comments

Comments
 (0)