39 releases (6 stable)

1.0.5 Oct 9, 2024
0.18.0 Sep 2, 2024

#45 in Parser tooling

Download history 1148/week @ 2024-08-12 777/week @ 2024-08-19 1375/week @ 2024-08-26 750/week @ 2024-09-02 220/week @ 2024-09-16 4/week @ 2024-09-23 17/week @ 2024-09-30 170/week @ 2024-10-07 10/week @ 2024-10-14

4,171 downloads per month

MIT/Apache

62KB
992 lines

rustylr

Converts a context-free grammar into a deterministic finite automaton (DFA) tables, and generates a Rust code that can be used as a parser for that grammar.

cargo install rustylr

Usage

$ rustylr --help
Usage: rustylr [OPTIONS] <INPUT_FILE> [OUTPUT_FILE]

Arguments:
  <INPUT_FILE>
          input_file to read

  [OUTPUT_FILE]
          output_file to write

          [default: out.tab.rs]

Options:
      --no-format
          do not rustfmt the output

  -l, --lalr
          build LALR(1) parser

  -v, --verbose
          print debug information.

          Print the whole rule set (include auto-generated rules), and the shift/reduce resolving process.

This program searches for '%%' in the input file.

The contents before '%%' will be copied into the output file as it is. Context-free grammar must be followed by '%%'. Each line must follow the syntax of rusty_lr#syntax.

// my_grammar.rs
use some_crate::some_module::SomeStruct;

enum SomeTypeDef {
    A,
    B,
    C,
}

%% // <-- input file splitted here

%tokentype u8;
%start E;
%eof b'\0';

%token a b'a';
%token lparen b'(';
%token rparen b')';

E: lparen E rparen
 | P
 ;

P: a;

Calling the command will generate a Rust code my_parser.rs.

$ rustylr my_grammar.rs my_parser.rs

For usage of the generated code, please refer to the documents rusty_lr#Start Parsing.

Dependencies

~2.7–10MB
~96K SLoC