parlex-calc
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.
Highlights
- Implements a complete expression parser with arithmetic, identifiers, and assignments
- Shows how to integrate generated lexer and parser code with user-defined driver logic
- Demonstrates runtime ambiguity resolution using operator precedence
- Uses a simple symbol table (
SymTab) for variable storage and lookup
Components
CalcLexer— 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 likea = 2 + 3; b = a * 10;CalcError— Unified error type covering lexical, parsing, and semantic errors
Example
use ;
use ;
This example shows how to:
- Create an input stream (
IterInput) - Instantiate a parser and shared context (
SymTab) - Parse and evaluate multiple statements from the same stream
Learning Purposes
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::TryNextWithContextstreaming APIs
Running parlex-calc binary
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,
},
},
),
},
]
Building & testing
At the repository root:
License
Copyright (c) 2005–2026 IKH Software, Inc.
Released under the terms of the MIT License.
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.