PDA-er
Build a push-down automaton, feed it input, and print the 35th way it can say yes.
Largejamie, 2021 · Turing complete: no · ●●○ moderate · esolangs.org
Tested with 6 known programs · Test method
Run it
The idea
Largejamie made PDA-er in March 2021. It is written like DFA-er, which runs a finite automaton instead. A program written with only ., -, 0 and 1 builds a push-down automaton. Its states are named by binary numbers, and its transitions read a symbol, pop one and push one. The automaton may be nondeterministic, so there can be many paths that end in an accepting state. PDA-er searches them in order of length and prints the one the program asks for.
Read the story
A path prints itself. Every state on it, the start too, comes out as the character with that code. So the names of the states are the output, and the program picks which walk through them to take.
The machine
Before the first !.
| Code | Does |
|---|---|
.b. | Make a failing state named b |
..b. | Make an accepting state named b |
-r-p-u-d- | From the state made last, go to d reading r, popping p and pushing u; a blank r, p or u means none |
After the !.
| Code | Does |
|---|---|
.b. | The first one picks the path, 1 or 0 for the shortest; the rest are input symbols |
- | Read a line and feed each character as its code |
Paths of the same length come in the order their transitions were made. A path accepts when it ends in an accepting state with all input read. A blank destination is state 0. Every other character is a comment.
Example
The Hello World uses only empty transitions.
The Hello World uses only empty transitions. From l there are three ways on, to l, o and d, so there are many paths from H to !. Counting by length and then by the order the transitions were written, the 35th is the one that spells the greeting.
Ask for path 1 instead and the shortest walk comes out, straight from l to d.
A stack at work. S pushes a bottom mark 1 and goes to A, which pushes a 0 for every a. Each b pops a 0, and B reaches the accepting ! only by popping the mark, so the input has to be some as followed by as many bs. The path shows the work, one state for every step.
The Balanced? program prints Balanced! for balanced parentheses and nothing otherwise. Its first states are named 1 and 0, so on balanced input the path starts with those two control characters.
Patterns it teaches
- Nondeterminism: every accepting path is an answer, and the program picks one by its number.
- Stack machines: one stack, as much as a push-down automaton has.
Further
See also
Stack-based
Write your own in Run Convert a program from PDA-er
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: pda-er.
Command line
shib run pda-er -e '.1001000.----1100101-.1100101.----1101100-.1101100.----1101100-----1101111-----1100100-.1101111.----101100-----1110010-.101100.----100000-.100000.----1110111-.1110111.----1101111-.1110010.----1101100-.1100100.----100001-..100001.!.100011.'shib manual pda-erSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/pda-er.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org