npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pushdown-automaton

v1.2.1

Published

A library that allows you to create a deterministic pushdown automaton

Downloads

5

Readme

Pushdown Automaton

This package allows the user to create deterministic pushdown automata. If you need an explanation for what that is, you're in the wrong place.
Maybe start by reading the Wikipedia article.


⚠️ ATTENTION ⚠️
The Pushdown Automaton package currently only supports deterministic automata. It currently checks following things:

  • Two consecutive epsilon transitions
  • Two matching transitions

It only checks for these two things on the node it's currently on (while being in the run()) so if for example node q1 is not deterministic but the run() function doesn't reach q1 it won't throw.


Usage

Important: This just goes over creating an automaton using every configuration option. Here you can find real use-cases.
Creating a pushdown automaton involves following steps:

  1. Creating the automaton instance
  2. Creating all the states
  3. Setting start and all end states
/*
 * 0. Import everything you need
 */
import { PushdownAutomaton, State, TransitionFunction } from 'pushdown-automaton';

/*
 * 1. Instantiate the pushdown automaton with the input word
 *    Provide a 2nd parameter to define the initial stack token.
 *    Normally that's a dollar sign, but here I define it as %
 */
let automaton: PushdownAutomaton;
automaton = new PushdownAutomaton("test", "%");

/*
 * 2. Instantiate states and give them names
 */
let oneState: State;
oneState = new State("q0");
let otherState: State;
otherState = new State("q1");

/* 
 * 3. Instantiate transitions
 *    First parameter is the token to be consumed
 *    The second parameter is the state where the transition leads to
 *    The third parameter shows the token to be popped off the stack
 *    The fourth parameter is an array of things that will be pushed onto the stack
 */
let transitionFunction: Transition;
transitionFunction = new Transition("t", otherState, "$", ["c", "d"]);

/*
 * 4. Add the transition to it's origin
 */
oneState.addTransitionFunction(transitionFunction);

/*
 * 5. Set start state and add end state
 */
automaton.setStartSate(oneState);
automaton.addEndState(otherState);

/*
 * 6. Run the automaton and check it's return value
 */
let successful: boolean;
successful = automaton.run();

/*
 * 7. In case you want a function to run after every state change
 */
const someFunction = (automaton) => {
    // Do some stuff here
}
pushdownAutomaton.addOperation(someFunction);

/*
 * 8. Now you can re-run it. But make sure to add a new word, as
 *    the old one was consumed by the automaton
 */ 
pushdownAutomaton.run("some new word");

Return values of run()

The run() Method makes use of the TerminationMessage interface, which looks as follows:

interface TerminationMessage {
  reason: string;
  successful: boolean;
  code: number;
}

It holds a message, if it was sucessful and the return code. Following codes are possible:

| Code | Meaning | |:--------:|:---------------------------------------------------------------------| |0 |Everything went okay. The machine terminated in a valid end state | |1 |The automaton didn't terminate in a valid end state | |2 |The automaton didn't find a valid transition, so went to a sink state | |Exception |The automaton isn't deterministic | |Exception |The input word is undefined |

Other links

Credits

This library was written and published by Anes Hodza