#!/usr/bin/perl -w

# Using XML::PYX
# Author: Michel Rodriguez <m.v.rodriguez@ieee.org>

use strict;

my $i=0;
my $write=0;
my $first_rhs;
my $prod='';

# REC gets the output of pyx REC-xml-19980210.xml
open( REC, 'pyx REC-xml-19980210.xml |');

while( <REC>)
  { chomp;
    if(    m/^\(prod$/)     { $i++;
                              $prod= "[$i] "; 
                              $first_rhs=1;
                            }
    elsif( m/^\(lhs$/)      { $write=1 ;  }
    elsif( m/^\)lhs$/)      { $write=0 ;  }
    elsif( m/^\(rhs$/)      { $write=1 ;   
                              $prod .= " ::= " if( $first_rhs); 
                              $first_rhs=0;          
                            }
    elsif( m/^-/ && $write) { if( /^-\\n/) { $prod .= ' ';            }
                              else         { $prod .= substr( $_, 1); }
                            }

    elsif( m/^\)rhs$/)      { $write=0 ; }
    elsif( m/^\)prod$/)     { print clean( $prod), "\n";              }
  }

close REC;

sub clean { 
        my( $string)= @_;
        $string =~ s/\xc2\xa0/ /sg;
        $string =~ s/\s+/ /g;
        $string=~ s{\s$}{}g;
        return $string;
}                               
