Function ielr::compute_table
source · pub fn compute_table(
algorithm: Algorithm,
grammar: &Grammar,
start_nodes: impl IntoIterator<Item = Node>
) -> Result<(Table, Statistics), Error>
Expand description
Main function of this crate.
This computes the LR table for a given grammar.
Parameters
-
algorithm
: whichAlgorithm
to use to build the LR table.The actual computation may use a less powerful algorithm: in this case, you need to check
algorithm_needed
field ofStatistics
. -
max_states
: the maximum number of states the algorithm is allowed to compute.This is to prevent the algorithm from taking too many time. Most real-life programming language generate no more than 5000 states. Anything higher than
1_000_000
will certainly take way too many time and memory in practice. -
grammar
: the specification of the grammar used. Seeinput
to build it. -
start_nodes
: which nodes may be used to start parsing.
Panics
At the moment, any algorithm different from Algorithm::Lalr(1)
or
Algorithm::Lr(1)
will result in a panic.
Examples found in repository?
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
fn main() {
use Symbol::{Node as N, Token as T};
let mut grammar = Grammar::new();
// productions that do not need precedence annotations
for (lhs, rhs) in [
(START, vec![N(STATEMENTS)]),
(STATEMENTS, vec![N(STATEMENT), N(STATEMENTS)]),
(STATEMENTS, vec![]),
(STATEMENT, vec![N(FUNCTION)]),
(STATEMENT, vec![T(RETURN_KW), N(EXPRESSION), T(SEMICOLON)]),
(
STATEMENT,
vec![T(LET_KW), T(IDENT), T(EQUAL), N(EXPRESSION), T(SEMICOLON)],
),
(STATEMENT, vec![N(EXPRESSION), T(SEMICOLON)]),
(
FUNCTION,
vec![
T(FN_KW),
T(IDENT),
T(PARENTHESIS_LEFT),
N(FUNCTION_ARGS),
T(PARENTHESIS_RIGHT),
T(BRACE_LEFT),
N(STATEMENTS),
T(BRACE_RIGHT),
],
),
(
FUNCTION_ARGS,
vec![N(FUNCTION_ARG), T(COMMA), N(FUNCTION_ARGS)],
),
(FUNCTION_ARGS, vec![N(FUNCTION_ARG)]),
(FUNCTION_ARGS, vec![]),
(FUNCTION_ARG, vec![T(IDENT)]),
(EXPRESSION, vec![T(INT)]),
(EXPRESSION, vec![T(IDENT)]),
(ARGS, vec![N(EXPRESSION), T(COMMA), N(ARGS)]),
(ARGS, vec![N(EXPRESSION)]),
(ARGS, vec![]),
] {
grammar.add_production(lhs, rhs).unwrap();
}
let precedence_family = grammar.add_precedence_family();
// productions that need precedence annotations
for (lhs, rhs, left, right) in [
(
EXPRESSION,
vec![N(EXPRESSION), T(PLUS), N(EXPRESSION)],
Some(1),
Some(2),
),
(
EXPRESSION,
vec![N(EXPRESSION), T(MINUS), N(EXPRESSION)],
Some(1),
Some(2),
),
(
EXPRESSION,
vec![N(EXPRESSION), T(STAR), N(EXPRESSION)],
Some(3),
Some(4),
),
(
EXPRESSION,
vec![N(EXPRESSION), T(SLASH), N(EXPRESSION)],
Some(3),
Some(4),
),
(EXPRESSION, vec![T(MINUS), N(EXPRESSION)], None, Some(5)),
] {
let production = grammar.add_production(lhs, rhs).unwrap();
let production = grammar.get_production_mut(production).unwrap();
if let Some(left) = left {
production.set_left_precedence(precedence_family, left);
}
if let Some(right) = right {
production.set_right_precedence(precedence_family, right);
}
}
// Now we build the parsing table !
let (_tables, _statistics) = ielr::compute_table(
// This grammar only requires LALR(1)
ielr::Algorithm::Lalr(std::num::NonZeroU8::new(1).unwrap()),
// We do not care about a maximum number of states
&grammar,
[START],
)
.unwrap();
}
More examples
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
fn main() {
use Symbol::{Node as N, Token as T};
let mut grammar = Grammar::new();
// productions that do not need precedence annotations
for (lhs, rhs) in [
(START, vec![N(STATEMENTS)]),
(STATEMENTS, vec![N(STATEMENT), N(STATEMENTS)]),
(STATEMENTS, vec![]),
(STATEMENT, vec![N(FUNCTION)]),
(STATEMENT, vec![T(RETURN_KW), N(EXPRESSION), T(SEMICOLON)]),
(
STATEMENT,
vec![T(LET_KW), T(IDENT), T(EQUAL), N(EXPRESSION), T(SEMICOLON)],
),
(STATEMENT, vec![N(EXPRESSION), T(SEMICOLON)]),
(
FUNCTION,
vec![
T(FN_KW),
T(IDENT),
T(PARENTHESIS_LEFT),
N(FUNCTION_ARGS),
T(PARENTHESIS_RIGHT),
T(BRACE_LEFT),
N(STATEMENTS),
T(BRACE_RIGHT),
],
),
(
FUNCTION_ARGS,
vec![N(FUNCTION_ARG), T(COMMA), N(FUNCTION_ARGS)],
),
(FUNCTION_ARGS, vec![N(FUNCTION_ARG)]),
(FUNCTION_ARGS, vec![]),
(FUNCTION_ARG, vec![T(IDENT)]),
(EXPRESSION, vec![T(INT)]),
(EXPRESSION, vec![T(IDENT)]),
(ARGS, vec![N(EXPRESSION), T(COMMA), N(ARGS)]),
(ARGS, vec![N(EXPRESSION)]),
(ARGS, vec![]),
] {
grammar.add_production(lhs, rhs).unwrap();
}
let add_prod = grammar
.add_production(EXPRESSION, vec![N(EXPRESSION), T(PLUS), N(EXPRESSION)])
.unwrap();
let sub_prod = grammar
.add_production(EXPRESSION, vec![N(EXPRESSION), T(MINUS), N(EXPRESSION)])
.unwrap();
let mul_prod = grammar
.add_production(EXPRESSION, vec![N(EXPRESSION), T(STAR), N(EXPRESSION)])
.unwrap();
let div_prod = grammar
.add_production(EXPRESSION, vec![N(EXPRESSION), T(SLASH), N(EXPRESSION)])
.unwrap();
let neg_prod = grammar
.add_production(EXPRESSION, vec![T(MINUS), N(EXPRESSION)])
.unwrap();
// Add conflicts solutions.
let operator_to_production = HashMap::from([
(PLUS, add_prod),
(MINUS, sub_prod),
(STAR, mul_prod),
(SLASH, div_prod),
]);
// - all binary operators are left-associative here
// - '-' has precedence over all binary operators
for operator in [PLUS, MINUS, STAR, SLASH] {
grammar.add_conflict_solution(ConflictSolution {
prefer: ConflictingAction::Reduce(operator_to_production[&operator]),
over: ConflictingAction::Shift(Lookahead::Token(operator)),
});
grammar.add_conflict_solution(ConflictSolution {
prefer: ConflictingAction::Reduce(neg_prod),
over: ConflictingAction::Shift(Lookahead::Token(operator)),
});
}
// operator with the same precedence
for (operator1, operator2) in [(PLUS, MINUS), (STAR, SLASH)] {
grammar.add_conflict_solution(ConflictSolution {
prefer: ConflictingAction::Reduce(operator_to_production[&operator1]),
over: ConflictingAction::Shift(Lookahead::Token(operator2)),
});
grammar.add_conflict_solution(ConflictSolution {
prefer: ConflictingAction::Reduce(operator_to_production[&operator2]),
over: ConflictingAction::Shift(Lookahead::Token(operator1)),
});
}
// operator with the different precedence
for (prefer, over) in [(STAR, PLUS), (STAR, MINUS), (SLASH, PLUS), (SLASH, MINUS)] {
grammar.add_conflict_solution(ConflictSolution {
prefer: ConflictingAction::Shift(Lookahead::Token(prefer)),
over: ConflictingAction::Reduce(operator_to_production[&over]),
});
grammar.add_conflict_solution(ConflictSolution {
prefer: ConflictingAction::Reduce(operator_to_production[&prefer]),
over: ConflictingAction::Shift(Lookahead::Token(over)),
});
}
// Now we build the parsing table !
let (_tables, _statistics) = ielr::compute_table(
// We are required to specify LR(1) when using conflict resolution.
ielr::Algorithm::Lr(std::num::NonZeroU8::new(1).unwrap()),
// We do not care about a maximum number of states
&grammar,
[START],
)
.unwrap();
}