Skip to content

Add text-based parser for easier pushdown automaton creation#26

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-2a3f5ca2-e0ee-4321-8113-43c432616d53
Draft

Add text-based parser for easier pushdown automaton creation#26
Copilot wants to merge 3 commits intomainfrom
copilot/fix-2a3f5ca2-e0ee-4321-8113-43c432616d53

Conversation

Copy link

Copilot AI commented Aug 14, 2025

This PR implements a text-based parser that allows users to create pushdown automata using mathematical notation instead of manually constructing objects. This addresses the need for a more "text-parsy" approach to defining PDAs.

Problem

Previously, creating a pushdown automaton required verbose manual construction:

const automaton = new PushdownAutomaton();
const startState = new State("q0");
const endState = new State("q1");
const transition = new TransitionFunction("a", endState, "$", ["X", "$"]);
startState.addTransitionFunction(transition);
automaton.setStartSate(startState);
automaton.addEndState(endState);

Solution

Now users can define PDAs using standard mathematical notation:

const definition = `
  States: q0, q1, q2
  Start State: q0
  Final States: q2
  Initial Stack Symbol: $
  
  # Language: a^n b^n (n >= 1)
  δ(q0, a, $) = (q0, Z$)
  δ(q0, a, Z) = (q0, ZZ)
  δ(q0, b, Z) = (q1, ε)
  δ(q1, b, Z) = (q1, ε)
  δ(q1, ε, $) = (q2, $)
`;

const automaton = PushdownAutomaton.fromDefinition(definition);

Features Added

  • Mathematical notation parser: Supports standard PDA transition notation δ(state, input, stack) = (next_state, stack_push)
  • Simple configuration API: Alternative object-based configuration for programmatic use
  • Static factory methods: PushdownAutomaton.fromDefinition() and PushdownAutomaton.fromSimpleConfig()
  • Epsilon transition support: Handles ε, epsilon, and empty string notations
  • Comment support: Lines starting with # are ignored
  • Flexible notation: Supports both δ and delta function names

Enhanced Automaton Engine

The parser required improvements to the core automaton to properly handle epsilon transitions:

  • Enhanced epsilon transition processing for empty input strings
  • Added stack empty state handling to prevent runtime errors
  • Improved transition ordering for correct stack semantics
  • Maintains full backward compatibility with existing API

Examples

Balanced Parentheses:

const definition = `
  States: q0, q1
  Start State: q0
  Final States: q1
  Initial Stack Symbol: $
  
  δ(q0, ε, $) = (q1, $)
  δ(q0, (, $) = (q0, X$)
  δ(q0, (, X) = (q0, XX)
  δ(q0, ), X) = (q0, ε)
  δ(q1, (, $) = (q0, X$)
`;

Context-Free Language a^n b^n:

const definition = `
  States: q0, q1, q2
  Start State: q0
  Final States: q2
  Initial Stack Symbol: $
  
  δ(q0, a, $) = (q0, Z$)
  δ(q0, a, Z) = (q0, ZZ)
  δ(q0, b, Z) = (q1, ε)
  δ(q1, b, Z) = (q1, ε)
  δ(q1, ε, $) = (q2, $)
`;

Testing

  • 66 tests with 100% pass rate
  • 94.81% statement coverage, 87.01% branch coverage
  • Comprehensive unit tests for parser functionality
  • Integration tests with real-world PDA examples (balanced parentheses, context-free languages, palindromes)
  • All existing functionality remains unchanged and tested

This enhancement makes the library significantly more accessible while maintaining the full power and flexibility of the underlying automaton engine.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits August 14, 2025 09:17
Co-authored-by: aneshodza <75726773+aneshodza@users.noreply.github.com>
Co-authored-by: aneshodza <75726773+aneshodza@users.noreply.github.com>
Copilot AI changed the title [WIP] Please make an easier way to use the library. something more "text-parsy" like giving the math noitation of the automaton and then we create the object structure etc. in the background\ Add text-based parser for easier pushdown automaton creation Aug 14, 2025
Copilot AI requested a review from aneshodza August 14, 2025 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments