I started writing a simple parser using Marpa.Made some progress and
however as I started adding new rule, my parser started failing.
use strict;
use warnings;
use warnings qw(FATAL utf8); # Fatalize encoding glitches.
use feature 'say' ;
#use Data::Dumper;
use Data::Dumper::Concise;
#use Log::Handler;
use Marpa::R2;
use PyGen;
#use Moo;
my @flt;
sub flattenArray {
foreach (@_) {
if (ref $_ eq "ARRAY") {
flattenArray(@$_);
}
else {
push @flt,$_;
}
}
}
sub PYGEN_Action::ifAction {
my (undef,@a,@b) = @_;
my $cndHsh = {
'var' => $a[0]->[1]->[0],
'op' => $a[0]->[1]->[1],
'val' => $a[0]->[1]->[2],
};
my $actHsh = {
'act' => $a[1]->[0]->[0],
'var' => $a[1]->[0]->[1],
};
print $$cndHsh{'var'};
print "\n";
my $pygen = PyGen->new();
$pygen->write_if($cndHsh);
$pygen->write_act($actHsh);
}
sub PYGEN_Action::verbAction {
print "verbAction inputs\n";
print @_;
}
sub PYGEN_Action::listAction {
#unpack lol. output is global @flt;
flattenArray(@_);
my $pygen = PyGen->new();
shift @flt; #discard empty hash.
$pygen->write_list(\@flt);
}
my $g = Marpa::R2::Scanless::G->new({
default_action => '::array',
source => \(<<'END_OF_SOURCE'),
:start ::= language
#
# include begin
language ::= if_statement action_statements action => ifAction
| action_statements action => verbAction
| ('List') variable ('is') comma_list action =>
listAction
action_statements ::= action_statement*
if_statement ::= if_type var_op_val
| if_type var_in_list
action_statement ::= action_verb variable
| action_verb var_op_val
var_op_val ::= variable operator value
| variable operator variable
var_in_list ::= variable 'in' variable
comma_list ::= str
| comma_list (',') str
#comma_list ::= variable (',') variable
:discard ~ ws
ws ~ [\s]+
# include end
action_verb ~ 'remove' | 'Create' | 'Get'| 'Set' | 'CREATE' |'Revise'
if_type ~ 'If'
variable ~ [\w]+
operator ~ 'has' | '>' | '<' | '='
value ~ 'duplicate'
str ~ [\w]+
END_OF_SOURCE
});
my $re = Marpa::R2::Scanless::R->new({ grammar => $g });
my $input = <<'INPUT';
If documentTitle = A100
remove Document
List Author is Dante, Murakami, Ash, Harris
INPUT
print "Trying to parse:\n$input\n\n";
print "\n", $re->show_progress(0, -1);
$g->parse(\$input, 'PYGEN_Action');
=====================================
Output is
P0 @0-0 L0c0 language -> . if_statement action_statements
P1 @0-0 L0c0 language -> . action_statements
P2 @0-0 L0c0 language -> . 'List' variable 'is' comma_list
P3 @0-0 L0c0 action_statements -> . action_statement *
P4 @0-0 L0c0 if_statement -> . if_type var_op_val
P5 @0-0 L0c0 if_statement -> . if_type var_in_list
P6 @0-0 L0c0 action_statement -> . action_verb variable
P7 @0-0 L0c0 action_statement -> . action_verb var_op_val
P13 @0-0 L0c0 :start -> . language
Error in SLIF parse: No lexemes accepted at line 3, column 2
Rejected lexeme #0: str; value="List"; length = 4
Rejected lexeme #1: variable; value="List"; length = 4
Rejected lexeme #2: 'List'; value="List"; length = 4
* String before error: If documentTitle = A100\n\tremove Document\n\s
* The error was at line 3, column 2, and at character 0x004c 'L', ...
* here: List Author is Dante, Murakami, Ash, Harris\n
Marpa::R2 exception at gendeb.py line 99.
================================
1. I tested the grammar for c('List') variable ('is') comma_list action =>
listAction
in a separate parser program, and it worked fine.
Any insights into why this is happening?
Thanks,
Rk
--
You received this message because you are subscribed to the Google Groups
"marpa parser" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/marpa-parser/0969de68-b9de-4eb1-8887-9cbce3bd9272n%40googlegroups.com.