Skip to content

Instantly share code, notes, and snippets.

@jeffreykegler
Created March 7, 2015 01:06
Show Gist options
  • Save jeffreykegler/1dd56ce50c1dd2395856 to your computer and use it in GitHub Desktop.
Save jeffreykegler/1dd56ce50c1dd2395856 to your computer and use it in GitHub Desktop.
Example of SLIF rewrites for Andreas Kupries
DSL:
:default ::= action => [values] bless => ::lhs
lexeme default = action => [ start, length, value ]
bless => ::name latm => 1
:start ::= Script
Script ::= Expression+ separator => comma
comma ~ [,]
Expression ::=
Number bless => primary
| '(' Expression ')' bless => paren assoc => group
|| Expression '**' Expression bless => exponentiate assoc => right
|| Expression '*' Expression bless => multiply
| Expression '/' Expression bless => divide
|| Expression '+' Expression bless => add
| Expression '-' Expression bless => subtract
Number ~ [\d]+
:discard ~ whitespace
whitespace ~ [\s]+
# allow comments
:discard ~ <hash comment>
<hash comment> ~ <terminated hash comment> | <unterminated
final hash comment>
<terminated hash comment> ~ '#' <hash comment body> <vertical space char>
<unterminated final hash comment> ~ '#' <hash comment body>
<hash comment body> ~ <hash comment char>*
<vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
<hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
Internal symbols:
0 Expression[0] <Expression> at priority 0
1 Expression[1] <Expression> at priority 1
2 Expression[2] <Expression> at priority 2
3 Expression[3] <Expression> at priority 3
4 [:start] Internal G1 start symbol
5 [Lex-0] Internal lexical symbol for "'('"
6 [Lex-1] Internal lexical symbol for "')'"
7 [Lex-2] Internal lexical symbol for "'**'"
8 [Lex-3] Internal lexical symbol for "'*'"
9 [Lex-4] Internal lexical symbol for "'/'"
10 [Lex-5] Internal lexical symbol for "'+'"
11 [Lex-6] Internal lexical symbol for "'-'"
12 Script [no description]
13 Expression [no description]
14 comma [no description]
15 Number [no description]
Internal rules:
0: Script -> Script[Seq]
1: Script -> Script[Seq] comma
2: Script[Seq] -> Expression
3: Script[Seq] -> Script[Seq] comma Expression
4: Expression -> Expression[0]
5: Expression[0] -> Expression[1]
6: Expression[1] -> Expression[2]
7: Expression[2] -> Expression[3]
8: Expression[3] -> Number
9: Expression[3] -> [Lex-0] Expression[0] [Lex-1]
10: Expression[2] -> Expression[3] [Lex-2] Expression[2]
11: Expression[1] -> Expression[1] [Lex-3] Expression[2]
12: Expression[1] -> Expression[1] [Lex-4] Expression[2]
13: Expression[0] -> Expression[0] [Lex-5] Expression[1]
14: Expression[0] -> Expression[0] [Lex-6] Expression[1]
15: [:start] -> Script
16: [:start]['] -> [:start]
#!/usr/bin/perl
# Copyright 2015 Jeffrey Kegler
# This file is part of Marpa::R2. Marpa::R2 is free software: you can
# redistribute it and/or modify it under the terms of the GNU Lesser
# General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Marpa::R2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser
# General Public License along with Marpa::R2. If not, see
# http://www.gnu.org/licenses/.
# A "full" Synopsis for the intro doc to the SLIF
use 5.010;
use strict;
use warnings;
use English qw( -no_match_vars );
use Scalar::Util;
## no critic (ErrorHandling::RequireCarping);
# Marpa::R2::Display
# name: SLIF full synopsis
use Marpa::R2;
my $source = <<'END_OF_SOURCE';
:default ::= action => [values] bless => ::lhs
lexeme default = action => [ start, length, value ]
bless => ::name latm => 1
:start ::= Script
Script ::= Expression+ separator => comma
comma ~ [,]
Expression ::=
Number bless => primary
| '(' Expression ')' bless => paren assoc => group
|| Expression '**' Expression bless => exponentiate assoc => right
|| Expression '*' Expression bless => multiply
| Expression '/' Expression bless => divide
|| Expression '+' Expression bless => add
| Expression '-' Expression bless => subtract
Number ~ [\d]+
:discard ~ whitespace
whitespace ~ [\s]+
# allow comments
:discard ~ <hash comment>
<hash comment> ~ <terminated hash comment> | <unterminated
final hash comment>
<terminated hash comment> ~ '#' <hash comment body> <vertical space char>
<unterminated final hash comment> ~ '#' <hash comment body>
<hash comment body> ~ <hash comment char>*
<vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
<hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
END_OF_SOURCE
my $grammar = Marpa::R2::Scanless::G->new(
{ bless_package => 'My_Nodes', source => \$source } );
my $naif_grammar = $grammar->thick_g1_grammar();
my @naif_symids = $naif_grammar->symbol_ids();
say "DSL:\n$source";
say "Internal symbols:";
for my $naif_symid (@naif_symids) {
say join " ", $naif_symid,
$naif_grammar->symbol_name($naif_symid),
($naif_grammar->symbol_description($naif_symid) // '[no description]');
}
say "";
say "Internal rules:";
say $grammar->show_irls();
# vim: expandtab shiftwidth=4:
@andreas-kupries
Copy link

Thanks. Saved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment