DEV Community

Discussion on: 100 Languages Speedrun: Episode 74: Python ANTLR 4

Collapse
 
raiph profile image
raiph

In some other kinds of parser generators, there's a single special top level rule (for example Raku's builtin PEG parsers call it TOP).

Raku's parsers aren't PEGs. See Comparing/contrasting Raku Grammars and PEGs.

The TOP rule is only special in the sense it's the rule called to enter a grammar if you don't specify which rule to start at.

You can specify any rule as the parsing entry point. Just pass a rule => 'rule-name' argument to a parsing routine.

Collapse
 
taw profile image
Tomasz Wegrzanowski • Edited

No parser generator really follows the theoretical classes (LR, LL, GLR, PEG etc) exactly, but these are still best ways to describe parsing strategy, and you can't ignore which class you have, as each of them forces grammars to be built certain ways, and won't work otherwise. Raku grammars are definitely PEG style, and very far from other parsing styles.

Even the article you linked states "Raku Grammars: Some folk describe Raku Grammars as PEGs". Their counter-argument that Raku supports putting semantic predicates in the grammar is really weird, as most parser generators do, like ANTLR for sure, and even Bison.

The thing about having dedicated TOP rule or not is more a note about API design. LL and PEG style parsers can generally start from any rule. ANTLR 4 doesn't seem to have any way to designate any rule as default TOP rule for more concise API, I think what Raku is doing here with default rule makes sense.