Parse::RecDescent and autotree

Colin Kuskie ckuskie at dalsemi.com
Thu Jun 13 17:01:43 CDT 2002


On Thu, Jun 13, 2002 at 02:54:53PM -0700, Colin Kuskie wrote:

Crap!  Here's the script:


#!/usr/local/bin/perl -w

use strict;
use lib '~/perl/modules';
use Parse::RecDescent;
use Data::Dumper;

my $grammar = <<'EOGRAMMAR';

<autotree>

#rules

netlist : elements(s)
{ bless \%item, $item{__RULE__} }

elements : comment_line
         | subckt
         | subckt_def
#        | comment
{ bless \%item, $item{__RULE__} }

comment_line :<skip: qr/[ \t]*/> '*' comment newline
{ bless \%item, $item{__RULE__} }

subckt:<skip: qr/[ \t]*/> 'X' name node(s) word(s?) newline
{ bless \%item, $item{__RULE__} }

subckt_def:<skip: qr/[ \t]*/> '.SUBCKT' name node(s) newline subckt(s) '.ENDS' word(s) newline
{ bless \%item, $item{__RULE__} }

#terminals

comment : /.*/
{ bless {__VALUE__ => $item[1] }, $item{__RULE__} }

name :  /\w+/
{ bless {__VALUE__ => $item[1] }, $item{__RULE__} }
word:  /\S+/
{ bless {__VALUE__ => $item[1] }, $item{__RULE__} }

node : <skip: qr/[ \t]*/> /\w+(?!=)/
{ bless {__VALUE__ => $item[2] }, $item{__RULE__} }

newline : /\n/
{ bless {__VALUE__ => $item[1] }, $item{__RULE__} }

EOGRAMMAR

my ($grammar1,$grammar2) = ($grammar,$grammar);
$grammar1 =~ s/<autotree>//;
$grammar2 =~ s/{\s*bless.+$//mg;


my $p1 = Parse::RecDescent->new($grammar1)
  or die "bad grammar";
my $p2 = Parse::RecDescent->new($grammar2)
  or die "bad grammar";

undef $/;
my $text = <DATA>;

my $t1 = $p1->netlist($text)
  or die "bad netlist";

my $t2 = $p2->netlist($text)
  or die "bad netlist";

my ($tree1,$tree2);

$tree1 = $t1->descend($tree1);
$tree2 = $t2->descend($tree2);

#print Dumper($t1);

print join "\n", "#Actions", $grammar1, ('-'x60), '#<autotree>', $grammar2, ('-'x60);
print join "\n", "#Actions", $tree1, ('-'x60), '#<autotree>', $tree2, ('-'x60);

sub netlist::descend {
  my ($self,$tree) = @_;
  $tree .= "Netlist\n";
  my $num = scalar @{ $self->{elements} };
  $tree .= "Found $num elements\n";
  $tree =  join ':', $tree, keys %{$self}, "\n";
  foreach (@{ $self->{elements} }) {
    $tree = join '', $tree, ref $_, "\n";
    #$tree = join '', $tree,  $_->descend($tree);
  }
  return $tree;
}

sub elements::descend {
  my ($self,$tree) = @_;
  $tree =  join ':', $tree, keys %{$self}, "\n";
  return $tree;
}

__DATA__
* # FILE NAME: /DESIGN/NWDC/91D35_PDX/SIM/HSPICE/CKUSKIE/AMP5T/HSPICES/         
* schematic/netlist/amp5T.c.raw
* Netlist output for hspiceS.
* Generated on May 6 16:30:58 2002
* File name: colinLib_amp5T_schematic.s.
* Subcircuit for cell: amp5T.
* Generated for: hspiceS.
* Generated on May  6 16:30:58 2002.
XP2 AVDD LOAD GATE AVDD PCH4_D35W_1  M=104.0 
XP1 AVDD GATE OUTN AVDD PCH4_D35W_2  M=1.0 
XP3 AVDD OUTN OUTN AVDD PCH4_D35W_2  M=1.0 
XN3 AGND IBAS IBAS AGND NCH4_D35W_3  M=1.0 
XN2 AGND CS IBAS AGND NCH4_D35W_3  M=1.0 
XN1 AGND GATE VREF CS NCH4_D35W_4  M=1.0 
XN0 AGND OUTN INN CS NCH4_D35W_4  M=1.0 
* File name: d35w_pch4_d35w_schematic.s.
* Subcircuit for cell: pch4_d35w.
* Generated for: hspiceS.
* Generated on May  6 16:30:58 2002.
* terminal mapping: B = B
*                   D = D
*                   G = G
*                   S = S
* End of subcircuit definition.
* File name: d35w_nch4_d35w_schematic.s.
* Subcircuit for cell: nch4_d35w.
* Generated for: hspiceS.
* Generated on May  6 16:30:58 2002.
* terminal mapping: B = B
*                   D = D
*                   G = G
*                   S = S
* End of subcircuit definition.
* File name: d35w_nch4_d35w_schematic.s.
* Subcircuit for cell: nch4_d35w.
* Generated for: hspiceS.
* Generated on May  6 16:30:58 2002.
* terminal mapping: B = B
*                   D = D
*                   G = G
*                   S = S
* End of subcircuit definition.
* File name: d35w_pch4_d35w_schematic.s.
* Subcircuit for cell: pch4_d35w.
* Generated for: hspiceS.
* Generated on May  6 16:30:58 2002.
* terminal mapping: B = B
*                   D = D
*                   G = G
*                   S = S
* End of subcircuit definition.
* Include files
* End of Netlist
.SUBCKT PCH4_D35W_2 B D G S 
XM0 D G S B MPGSXHD4I_PCH5V W=(102.0) L=(1.0) AD=+9.69000000E+01  AS=+9.69000000E+01 PD=+2.05900000E+02 PS=+2.05900000E+02 
.ENDS PCH4_D35W_2 
.SUBCKT NCH4_D35W_4 B D G S 
XM0 D G S B MNGSXHD4I_NCH5V W=(50.0) L=(1.0) AD=+4.75000000E+01  AS=+4.75000000E+01 PD=+1.01900000E+02 PS=+1.01900000E+02 
.ENDS NCH4_D35W_4 
.SUBCKT NCH4_D35W_3 B D G S 
XM0 D G S B MNGSXHD4I_NCH5V W=(20.0) L=(4.0) AD=+1.90000000E+01  AS=+1.90000000E+01 PD=+4.19000000E+01 PS=+4.19000000E+01 
.ENDS NCH4_D35W_3 
.SUBCKT PCH4_D35W_1 B D G S 
XM0 D G S B MPGSXHD4I_PCH5V W=(200.0) L=(1.0) AD=+1.90000000E+02  AS=+1.90000000E+02 PD=+4.01900000E+02 PS=+4.01900000E+02 
.ENDS PCH4_D35W_1 
TIMTOWTDI



More information about the Pdx-pm-list mailing list