Repository navigation
Rust: the LALR table and parser state, readable as in Python Lark - #13
Merged
Merged
Conversation
…able Lark::parse_table() gives a LALR parser's table (ParseTable and its Action, now public as LalrAction), as Python Lark's parser.parser.parser.parse_table; ParserState gives its state and value stacks and pops them with truncate(), as Lark's on_error handlers do through state_stack and value_stack; InteractiveParser gives parser_state_mut() and next_token(), which lexes one token for the current state without feeding it. Tools that print the automaton or recover from errors themselves (etalii.adp spec 035) need nothing else. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requested by Peter · project thread
The Rust crate gets read access to the LALR automaton and the parser state, as Python Lark leaves them public, so that a tool can print the automaton and recover from errors itself. etalii.adp spec 035 needs this (amendment of 2026-10-24, research R9, tasks T164 and T165), to rebuild
adp-atd-grammarandadp-atd-syntaxon etalii-lark. It's meant for release as 0.4.0. This PR is Rust only; the .NET and TypeScript ports are unchanged.What
Lark::parse_table() -> Option<&ParseTable>gives a LALR parser's table, as Python Lark'sparser.parser.parser.parse_table; it isNonefor Earley and CYK.parsers::lalr_analysisis public, withParseTableandLalrAction(itsAction) re-exported at the root.ParserState(now re-exported at the root) gets three methods:state_stack()andvalue_stack(), as Python'sstate_stackandvalue_stack;truncate(depth), which pops both stacks todepthstates and keeps at least the start state. It returns the popped values, as Lark'son_errorhandlers pop them.InteractiveParsergets two methods:parser_state_mut(), to change the state for recovery;next_token(), which lexes one token for the current parser state without feeding it. With the contextual lexer it matches only what the state accepts.Tests
rust/etalii-lark/tests/lalr_access.rscovers:next_tokenlexes per state:if ifreads the keyword, then a name;{, skip what that state does not read, and resume the parse.On this branch,
cargo fmt --check,cargo clippy --all-targets -- -D warningsandcargo testpass, the conformance suite included.I also used the branch to prototype etalii.adp's table printing. Its normalized table for the ATD grammar equals what Python Lark gives (
atd.tables.json) byte for byte.🤖 Generated with Claude Code