bnf_syntax_parser
The syntax parser based on BNF rules.
Usage
[]
= "0.1.0"
Quickstart
use HashMap;
use ;
// Compile the rules string to BNFRules
const RULES: &str = "
Integer = Number {Number};
MultiplicationExpression = Integer {(OperatorMul | OperatorDiv) Integer};
Expression = MultiplicationExpression {(OperatorAdd | OperatorSub) MultiplicationExpression};
";
let TOKEN_TYPE_CONVERTER = from;
let rules_vec_char = RULES.chars.;
let rules = match new ;
println!;
rules.display.unwrap;
println!;
// Two ways to create a parser
let parser = new;
let parser = try_from.unwrap;
const EXPRESSION_STRING: &str = "12*3+456/78";
let TOKEN_CONVERTER = from;
// Convert the expression string to expression tokens
let expression_tokens = EXPRESSION_STRING.chars.map.;
assert!;
// Parse the tokens to match_records buffer
let mut match_records = Vec new;
match parser.parse_to_buffer ;
// Convert the match_records to parse tree data structure
let parse_tree = new.unwrap;
println!;
parse_tree.display.unwrap;
Run the above code and you can get the output:
The compiled rules:
0 Integer = Number { Number } ;
1 MultiplicationExpression = Integer { ( OperatorMul | OperatorDiv ) Integer } ;
2 Expression = MultiplicationExpression { ( OperatorAdd | OperatorSub ) MultiplicationExpression } ;
The parse tree:
{ Expression
{ MultiplicationExpression
{ Integer
Number 1
Number 2
} Integer
OperatorMul
{ Integer
Number 3
} Integer
} MultiplicationExpression
OperatorAdd
{ MultiplicationExpression
{ Integer
Number 4
Number 5
Number 6
} Integer
OperatorDiv
{ Integer
Number 7
Number 8
} Integer
} MultiplicationExpression
} Expression
BNF
Each BNF rule has four parts: a left-hand side, a right-hand side, the "=" character separating these two sides and the ";" character marking the end of rule. The left-hand side is the name of the rule and the right-hand side is the description of the rule. The four description forms is explained below.
Form | Semantic |
---|---|
Sequence | Items appear left–to–right, their order in important. |
Choice | Alternative items are enclosed between "(" and ")" (parenthesis) and separated by a "|" (stroke), one item is chosen from this list of alternatives, their order is unimportant. |
Option | The optional item is enclosed between "[" and "]" (square–brackets), the item can be either included or discarded. |
Repetition | The repeatable item is enclosed between "{" and "}" (curly–braces), the item can be repeated zero or more times. |