| Crates.io | parlex-calc |
| lib.rs | parlex-calc |
| version | 0.3.0 |
| created_at | 2025-10-14 11:25:40.480776+00 |
| updated_at | 2025-10-23 10:16:44.720785+00 |
| description | Parlex example: simple calculator |
| homepage | |
| repository | https://github.com/ikhomyakov/parlex.git |
| max_upload_size | |
| id | 1882087 |
| size | 106,222 |
A sample calculator built using the parlex and parlex-gen toolchain.
This crate demonstrates the full workflow of defining, generating, and running a small but complete language processor using ALEX and ASLR.
SymTab) for variable storage and lookupCalcLexer — Tokenizes numeric literals, identifiers, operators, parentheses, and separatorsCalcParser — Parses arithmetic expressions, statements, and assignmentsCalcLexerDriver / CalcParserDriver — Implement runtime actions for tokenization and parsingSymTab — Maintains variable bindings, allowing expressions like a = 2 + 3; b = a * 10;CalcError — Unified error type covering lexical, parsing, and semantic errorsuse parlex_calc::{CalcParser, SymTab};
use try_next::{IterInput, TryNextWithContext};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut symtab = SymTab::new();
let input = IterInput::from("x = 2 + 3; y = x * 4;".bytes());
let mut parser = CalcParser::try_new(input)?;
let results = parser.try_collect_with_context(&mut symtab)?;
for r in results {
println!("Parsed: {:?}", r);
}
Ok(())
}
This example shows how to:
IterInput)SymTab)The calculator serves as:
A reference implementation of how to connect a lexer and parser generated by Parlex
A template project for building interpreters, DSLs, or scripting languages in Rust
A demonstration of end-to-end Parlex integration with try_next::TryNextWithContext streaming APIs
parlex-calc binarycargo run -p parlex-calc -- parse <<EOS
/* examples */
/* statement 1 */
x = 2 + 3;
/* statement 2 */
y = x * 4
EOS
Prints both the symbol table (SymTab) and the parsed token stream.
[parlex-calc/src/main.rs:171:13] &parser.stats() = (
LexerStats {
unreads: 63,
chars: 72,
matches: 34,
},
ParserStats {
tokens: 16,
shifts: 13,
reductions: 11,
ambigs: 0,
},
)
[parlex-calc/src/main.rs:172:13] &symtab = SymTab {
tab: {
"x": 5,
"y": 20,
},
}
[parlex-calc/src/main.rs:173:13] &toks = [
CalcToken {
token_id: Stat,
value: Stat {
comments: [
"/* examples */",
"/* statement 1 */",
],
value: Some(
5,
),
},
span: Some(
Span {
start: Position {
line: 0,
column: 0,
},
end: Position {
line: 2,
column: 9,
},
},
),
},
CalcToken {
token_id: Stat,
value: Stat {
comments: [
"/* statement 2 */",
],
value: Some(
20,
),
},
span: Some(
Span {
start: Position {
line: 3,
column: 0,
},
end: Position {
line: 4,
column: 9,
},
},
),
},
]
At the repository root:
cargo build
cargo test
Copyright (c) 2005–2025 IKH Software, Inc.
Released under the terms of the GNU Lesser General Public License, version 3.0 or (at your option) any later version (LGPL-3.0-or-later).
Contributions are welcome! Please feel free to submit issues or pull requests.