Skip to content

Latest commit

 

History

History
36 lines (24 loc) · 999 Bytes

parser_generation.md

File metadata and controls

36 lines (24 loc) · 999 Bytes

Parser Generation

In this example, we will use a grammar, named my_grammar.rustemo, with the following content:

Expr: left=Expr '+' right=Expr {Add, 1, left}
  | left=Expr '*' right=Expr {Multiply, 2, left}
  | Number
;

terminals

Number: /\d+/;
Addition: '+';
Multiplication: '*';

We will explain the content of the grammar later.

By giving a grammar, we can generate a parser based on the grammar. We use rcomp, which we have just installed in the previous tutorial, to generate the parser.

rcomp my_grammar.rustemo

This command outputs two files: my_grammar.rs and my_grammar_actions.rs.

  • my_grammar.rs is the parser itself.
  • my_grammar_actions.rs is the actions that will be performed after a successful parsing.

This parser will be called later in this example when we need to parse an input string.

➡️ Next: A Project Template For Parsing

📘 Back: Table of contents