esolang

Elevated Parser

The program is a grammar and the input is a text. The answer is whether the text parses.

2020 · Turing complete: no · ●●○ moderate · esolangs.org

Tested with 22 known programs · Test method

Run it

Expected: 1

The idea

Elevated Parser, from 2020, parses the majority of the languages Hakerh400 invented. It is a parser generator that goes straight from syntax rules to a syntax tree, without writing a parser program in between. The specification says it can also be seen as a programming language that decides whether a text can be parsed with the given rules, and this implementation does that. It prints 1 or 0.

Read the story

Two choices make it unusual. A rule always takes the longest text it can match at a place, and once found that match is never parsed again, even when the parser backtracks. And repeats are greedy and never give anything back, so "/*" .* "*/" fails, because .* eats the closing */ too. A lazy .*? tries the rest of the pattern after each character instead. Rules that call themselves first, like add{ expr "+" num | num }, are allowed, which many parser generators refuse.

The machine

A rule is name{ pattern | pattern }, and the first rule must match the whole input.

ElementMatches
"abc" "abc"iThat text; with i in any case
[a-z0-9]One character in the ranges
.Any one character
nameThe longest match of that rule
e* e+ e? e{3, 5}Repeats, as many as possible
e*sepRepeats with sep between each two
e*?Repeats, as few as the rest allows
:e !eLooks ahead for e, or for no e, without moving

Example

The lookahead example.

The lookahead example. !letter keeps inc from running into another word, and the separator space may be empty, so 3()4 is three elements.

The same rules refuse inc(inc(incn)), since incn has a letter after inc and is nothing else either.

Expected: 0

The function call from the specification needs the longest match. At d(y) an arg could be just the name d, but the call d(y) is longer, and only the longer one lets the brackets close.

Expected: 1

Further

Also in Rewriting

Write your own in Run Convert a program from Elevated Parser

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: elevated-parser.

Command line

shib run elevated-parser -e 'comment{ "/*" .* "*/" }' --input '/* a */'
shib manual elevated-parser

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/elevated-parser.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org