#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Marpa::R2; use Data::Dumper; my $input = q(sys ecm cloud-provider /Common/aws-ec2 { description "The aws-ec2 parameters" property-template { account { } availability-zone { valid-values { a b c d } } instance-type { valid-values { t2.micro t2.small t2.medium } } region { valid-values { us-east-1 us-west-1 } } } }); my $dsl = << '__DSL__'; lexeme default = latm => 1 :default ::= action => first Top ::= atom Attrs Struct action => top Attrs ::= atom Attrs action => merge | atom action => newlist Struct ::= ('{') Elements ('}') Elements ::= Element Elements action => merges | Element Element ::= atom Value action => struct | atom Struct action => struct Value ::= Struct | ('"') string ('"') | ('{ }') action => empty || ('{') List ('}') List ::= atom List action => merge | atom action => newlist :discard ~ [\s] string ~ [^"]+ atom ~ [^\s{}]+ __DSL__ sub top { +{ $_[1] => { attrs => $_[2], contents => $_[3] } } } sub first { $_[1] } sub empty { [] } sub newlist { [ $_[1] ] } sub merge { [ $_[1], @{ $_[2] } ] } sub struct { +{ $_[1] => $_[2] } } sub merges { +{ %{ $_[1] }, %{ $_[2] } } } my $grammar = 'Marpa::R2::Scanless::G'->new({ source => \$dsl }); my $recce = 'Marpa::R2::Scanless::R'->new({ grammar => $grammar, semantics_package => 'main', }); $recce->read(\$input); use Data::Dumper; print Dumper($recce->value) =~ s/ / /gr;