Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use Marpa::R2 2.100;
use Data::Dumper;
# array: 'A' count ':' element+
# string: 'S' length ':' byte+
@rns
rns / output
Created December 31, 2014 07:12
if precedence is removed "a+"a"a"+a"a"+a can be parsed and is ambiguous
# input: "a+"a"a"+a"a"+a
Ambiguous parse: 3 alternatives:
# ast 0:
['x',['x','"',['x',['x',['x',['a']],'+','"',['x',['a']],'"'],['x',['a']]],'"','+',['x',['a']]],['x','"',['x',['a']],'"','+',['x',['a']]]]
# ast 1:
['x','"',['x',['x',['x',['a']],'+','"',['x',['x',['a']],['x','"',['x',['a']],'"','+',['x',['a']]]],'"'],['x',['a']]],'"','+',['x',['a']]]
# ast 2:
['x','"',['x',['x',['x',['a']],'+','"',['x',['a']],'"'],['x',['a']]],'"','+',['x',['x',['a']],['x','"',['x',['a']],'"','+',['x',['a']]]]]
# input: a"a"+a
# ast 0:
# input: "a+"a""+a
# ast 0:
['x','"',['x',['x',['a']],'+','"',['x',['a']],'"'],'"','+',['x',['a']]]
# + left-associative: a+a+a
Ambiguous parse: 2 alternatives: at C:\cygwin\home\Ruslan\Marpa-R2-work\q27655176.t line 69.
# ast 0:
['x',['x',['x',['a']],'+',['x',['a']]],'+',['x',['a']]]
# ast 1:
['x',['x',['a']],'+',['x',['x',['a']],'+',['x',['a']]]]
# + lazily eating operands: aa+aa
# input: "a+"a""+a
# ast dump:
[
'x',
'"',
[
'x',
[
'x',
[
# + left-associative: a+a+a
[
'x',
[
'x',
[
'x',
[
'a',
'a'
@rns
rns / general-lexer-named-capture-group.pl
Created November 21, 2014 12:37
capture-based group lexer
use 5.010;
use strict;
#
# this lexer supports adding lexemes as regexes (not only literals)
# and finding tokens by matched alternative number
#
my $string = <<END;
# comment
use 5.010;
use strict;
use warnings;
use Marpa::R2;
my $g = Marpa::R2::Scanless::G->new( { source => \(<<'END_OF_SOURCE'),
:default ::= action => [ value ]
lexeme default = action => [ name, value ] latm => 1
# Output of $r->ambiguous(): at C:\cygwin\home\Ruslan\Marpa-R2-work\q26173372-asf.pl line 84.
Length of symbol "table" at line 3, column 1 is ambiguous
Choices start with: +Team:US
Choice 1, length=415, ends at C:\cygwin\home\Ruslan\Marpa-R2-work\q26173372-asf.pl line 85.
# Output of $r->value(), max_parses is in effect, so works ok: at C:\cygwin\home\Ruslan\Marpa-R2-work\q26173372-asf.pl line 89.
['data',['heading',['string','Soccer']],['tables',['table',['row',['level','+'],['name',['string','Team']],':',['value',['string','US']]],['row',['level','++'],['name',['string','Shirt']],':',['value',['string','Red & White Stripes']]],['row',['level','++'],['name',['string','Shorts']],':',['value',['string','Blue']]],['row',['level','++'],['name',['string','Players']],':',['value',['string','17']]],['row',['level','+++'],['name',['string','Active']],':',['value',['string','11']]],['row',['level','++++'],['name',['string','Forward']],':',['value',['string','2']]],['row',['level','++++'],['name',['string','Midf
@rns
rns / gl.pl
Created October 11, 2014 09:34
general lexer
use 5.010;
use strict;
#
# this lexer supports adding lexemes as regexes (not only literals)
# and finding tokens by matched alternative number
#
my $string = <<END;
# comment