in reply to Re^2: Help with Parse::RecDescent grammar
in thread Help with Parse::RecDescent grammar
Start by not using <autotree>. You need to override it 90% of the time (100% in this case), so you might as well make everything explicit.
use strict; use warnings; use Data::Dumper qw( Dumper ); use Parse::RecDescent qw( ); $::RD_ERRORS = 1; $::RD_WARN = 1; $::RD_HINT = 1; #$::RD_TRACE = 1; my $grammar = <<'_EOGRAMMAR_'; { # These apply to code in those block and all actions. use strict; use warnings; my %escapes = ( n => "\n", ); sub dequote_double { for (my $s = @_ ? $_[0] : $_) { s/^"//g; s/"$//g; s/\\(.)/$escapes{$1} || $1/eg; return $_; } } } parse : <skip:'(?:\s+|#[^\n]*\n)*'> portDef(s?) /\Z/ { $item[2] } portDef : "dbSetCellPortTypes" QSTRING QSTRING portList { [ @item[1..4] ] } portList : "'" "(" record(s?) ")" { $item[3] } record : "(" QSTRING(s?) ")" { $item[2] } QSTRING : /"(?:[^"\\]|\\.)*"/ { dequote_double($item[1]) } _EOGRAMMAR_ my $parser = Parse::RecDescent->new($grammar) or die "Bad grammar\n"; my $text = <<'_EOT_'; dbSetCellPortTypes "/opt/mylib/s956M" "*" '( ("gnd!" "Inout" "Ground" ) ("vint!" "Inout" "Power" ) ) #f _EOT_ my $net = $parser->parse($text) or die "bad netlist"; print Dumper $net;
Other fixes:
Update: Fixed portList. (Replaced { $item[2] } with { $item[3] }.)
Update: Fixed comment and missing (s?) in parse.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Help with Parse::RecDescent grammar
by dramguy (Novice) on Dec 12, 2006 at 17:43 UTC | |
by ikegami (Patriarch) on Dec 12, 2006 at 20:20 UTC | |
by dramguy (Novice) on Dec 13, 2006 at 14:25 UTC | |
by ikegami (Patriarch) on Dec 13, 2006 at 16:47 UTC | |
by dramguy (Novice) on Dec 13, 2006 at 17:29 UTC | |
|