Last active
May 14, 2022 13:35
-
-
Save rns/17853fa8f539f4f964c9 to your computer and use it in GitHub Desktop.
Heckendorn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use 5.010; | |
| use strict; | |
| use warnings; | |
| =pod Almost SLIF Example from http://marvin.cs.uidaho.edu/Teaching/CS445/grammar.html | |
| Here is a tree structure as nested parentheses. For instance (ant) or (cat, bat) | |
| or ((cat), (bat)) or ((cat, bat, dog), ant). | |
| Note the hierarchical handling of each feature. | |
| Note also that when <tree> points up from <thing> to the top of the grammar | |
| creating the nesting effect. Matching <tree> requires that parentheses | |
| be removed for each time through the loop to the top. This prevents infinite | |
| recursion because you will run out of parentheses if you remove a pair each time through. | |
| <tree> ::= "(" <list> ")" // deal with parentheses | |
| <list> ::= <thing> | <list> "," <thing> // do the list of things | |
| <thing> ::= <tree> | <name> // things are more trees or names | |
| <name> ::= ant | bat | cow | dog | |
| =cut | |
| use Marpa::R2; | |
| use Data::Dumper; | |
| my $grammar_source = q{ | |
| :default ::= action => [symbol, values] | |
| lexeme default = action => [symbol, values] latm => 1 | |
| <tree> ::= '(' <list > ')' | |
| <list> ::= <thing> | <list> ',' <thing> | |
| <thing> ::= <tree> | <name> | |
| <name> ::= 'ant' | 'bat' | 'cow' | 'dog' | 'cat' | |
| :discard ~ whitespace | |
| whitespace ~ [\s+] | |
| }; | |
| my $g = Marpa::R2::Scanless::G->new( { source => \$grammar_source } ); | |
| my @input = ( | |
| '(cat, bat)', | |
| '((cat), (bat))', | |
| '(ant, (cat, bat, dog), ant)', | |
| ); | |
| for my $input (@input){ | |
| warn "# $input"; | |
| my $r = Marpa::R2::Scanless::R->new( { grammar => $g } ); | |
| $r->read( \$input ); | |
| my $ast = ${ $r->value() }; | |
| warn Dumper $ast; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment