#!/usr/bin/perl use warnings; use strict; use Marpa::R2; my $dsl = << '__DSL__'; :default ::= action => concat lexeme default = latm => 1 Expression ::= '(' Expression ')' assoc => group | literal | Expression quote action => negation || Expression sp operator sp Expression quote ~ ['] # ' operator ~ [&|] literal ~ [A-Z] sp ~ [\s]+ __DSL__ my $i = 0; sub concat { $i++; join q(), @_[ 1 .. $#_ ] } sub negation { $i++; "!$_[1]" } my $grammar = 'Marpa::R2::Scanless::G'->new({ source => \$dsl }); for my $given ( "(A & B)'", "((A & B)' | (A & C & (A & B & D)'))", "((A | B)' & (C | (D & (E & F)))')'", ) { my $calc = $grammar->parse(\$given, { semantics_package => 'main' }); print "Given: $given\n"; print "Calculated: $$calc\n"; }