### Run LALRPOP Executable Manually Source: https://lalrpop.github.io/lalrpop/index.html/quick_start_guide This command shows how to execute the `lalrpop` binary directly after installation via `cargo install lalrpop`. Running `lalrpop file.lalrpop` will generate `file.rs`. The command only executes if the `.lalrpop` file is newer than the generated `.rs` file, but this can be overridden with the `-f` flag. ```Shell lalrpop file.lalrpop ``` -------------------------------- ### Add LALRPOP Dependencies to Cargo.toml Source: https://lalrpop.github.io/lalrpop/index.html/quick_start_guide This snippet shows how to add the necessary `lalrpop-util` and `lalrpop` dependencies to your `Cargo.toml` file. `lalrpop-util` is a runtime dependency for the generated code, while `lalrpop` is a build-time dependency. An option to disable default features for `lalrpop` is also provided for custom lexer scenarios. ```TOML # The generated code depends on lalrpop-util. [dependencies] lalrpop-util = "0.22.2" # Add a build-time dependency on the lalrpop library: [build-dependencies] lalrpop = "0.22.2" # If you are supplying your own external lexer you can disable default features so that the # built-in lexer feature is not included # lalrpop = { version = "0.22.2", default-features = false } ``` -------------------------------- ### Create build.rs for LALRPOP Source Processing Source: https://lalrpop.github.io/lalrpop/index.html/quick_start_guide This Rust code snippet demonstrates the minimal `build.rs` file content required to process LALRPOP grammar files. The `lalrpop::process_src()` function automatically finds `.lalrpop` files in `src/` and generates corresponding Rust source files in `OUT_DIR`. For multi-crate workspaces, `process_root()` can be used. ```Rust fn main() { lalrpop::process_src().unwrap(); } ``` -------------------------------- ### Example: Binding Variables with String Literals Source: https://lalrpop.github.io/lalrpop/index.html/print Illustrates a simple language where single-character variables are bound to string literals, demonstrating the input format for a lexer. ```Example Language Input x = "a" y = "bc" ``` -------------------------------- ### Use lalrpop_mod! Macro for Parser Module Generation Source: https://lalrpop.github.io/lalrpop/index.html/quick_start_guide This Rust snippet illustrates how to use the `lalrpop_mod!` macro to generate a wrapper module for your LALRPOP parser. By adding `lalrpop_mod!(grammar);` to `lib.rs` (or another module), a `grammar` submodule is created, allowing you to use the generated parser from your Rust code. ```Rust #![allow(unused)] fn main() { lalrpop_mod!(grammar); } ``` -------------------------------- ### Default LALRPOP Build Configuration Source: https://lalrpop.github.io/lalrpop/index.html/advanced_setup Illustrates the basic `build.rs` setup for LALRPOP, which processes `.lalrpop` files in the `src` directory, updating them in-place only when changes are detected. ```Rust fn main() { lalrpop::process_src().unwrap(); } ``` -------------------------------- ### LALRPOP Shorthand Expansion Examples Source: https://lalrpop.github.io/lalrpop/index.html/print Table illustrating how various LALRPOP shorthand notations for action code and value selection are expanded into their equivalent explicit forms, including cases with struct constructors. ```LALRPOP | Alternative | Equivalent to | | --- | --- | | `A => bar(<>)` | ` => bar(a)` | | `A B => bar(<>)` | ` => bar(a, b)` | | `A B => (<>)` | ` => (a, b)` | | ` B => bar(<>)` | ` B => bar(a)` | | ` B => bar(<>)` | ` B => bar(p)` | | ` => bar(<>)` | ` => bar(a, b)` | | ` => bar(<>)` | ` => bar(p, q)` | | ` B => Foo {<>}` | ` B => Foo {p:p}` | | ` => Foo {<>}` | ` => Foo {p:p, q:q}` | ``` -------------------------------- ### Example of Simple Variable Binding Language Source: https://lalrpop.github.io/lalrpop/index.html/lexer_tutorial/002_raw_delimited_content Demonstrates the syntax of a simple custom language designed for variable binding, where variables are single characters and values are string literals. This serves as the target language for the LALRPOP grammar definition. ```Custom Language Example x = "a" y = "bc" ``` -------------------------------- ### LALRPOP: Examples of '<>' shorthand expansion in action code Source: https://lalrpop.github.io/lalrpop/index.html/tutorial/003_type_inference A reference table demonstrating how LALRPOP's '<>' shorthand expressions within grammar alternatives are expanded into their equivalent explicit forms, showing how values are implicitly selected and named. ```LALRPOP Alternative | Equivalent to A => bar(<>) | => bar(a) A B => bar(<>) | => bar(a, b) A B => (<>) | => (a, b) B => bar(<>) | B => bar(a) B => bar(<>) | B => bar(p) => bar(<>) | => bar(a, b) => bar(<>) | => bar(p, q) B => Foo {<>} | B => Foo {p:p} => Foo {<>} | => Foo {p:p, q:q} ``` -------------------------------- ### Example of Calling the Calculator Parser in Rust Source: https://lalrpop.github.io/lalrpop/index.html/print This Rust snippet demonstrates how to instantiate and use the generated calculator parser. It initializes the lexer with an input string, parses the expression, and then asserts the correctness of the parsed Abstract Syntax Tree (AST) representation. ```Rust let input = "22 * pi + 66"; let lexer = Lexer::new(input); let expr = calculator9::ExprParser::new() .parse(input,lexer) .unwrap(); assert_eq!(&format!("{:?}", expr), "((\"22\" * \"pi\") + \"66\")"); ``` -------------------------------- ### Example Program to Parse with Logos Source: https://lalrpop.github.io/lalrpop/index.html/lexer_tutorial/005_external_lib This code snippet shows a simple program written in the toy language that the lexer, built using Logos, will be able to parse. It includes variable declarations, comments, and an arithmetic expression. ```Text var a = 42; var b = 23; # a comment print (a - b); ``` -------------------------------- ### LALRPOP: Example grammar rules using custom tokens Source: https://lalrpop.github.io/lalrpop/index.html/lexer_tutorial/003_writing_custom_lexer Provides an example of LALRPOP grammar rules for `FlowCtrl` that utilize the custom terminal tokens defined in the `extern` block. This demonstrates how the parser automatically substitutes string literals in rules with their corresponding `Tok` enum variants, allowing existing rules to work without modification. ```LALRPOP FlowCtrl: ast::Stmt = { " " " "