parser
changeset 18 8d541766cd1f
parent 17 d39ff14a8964
child 22 78285474e8d3
equal deleted inserted replaced
17:d39ff14a8964 18:8d541766cd1f
     1 #!/usr/bin/perl -w
     1 #!/usr/bin/perl -w
       
     2 
       
     3 # Copyright (c) 2009, Tomas Zeman <tzeman@volny.cz>
       
     4 # All rights reserved.
       
     5 #
       
     6 # Redistribution and use in source and binary forms, with or without
       
     7 # modification, are permitted providing that the following conditions 
       
     8 # are met:
       
     9 # 1. Redistributions of source code must retain the above copyright
       
    10 #    notice, this list of conditions and the following disclaimer.
       
    11 # 2. Redistributions in binary form must reproduce the above copyright
       
    12 #    notice, this list of conditions and the following disclaimer in the
       
    13 #    documentation and/or other materials provided with the distribution.
       
    14 #
       
    15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
       
    16 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    18 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
       
    19 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
       
    20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
       
    21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
       
    23 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
       
    24 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    25 # POSSIBILITY OF SUCH DAMAGE.
       
    26 #
       
    27 $main::VERSION = "1.0";
     2 
    28 
     3 use strict;
    29 use strict;
     4 use Parse::RecDescent;
    30 use Parse::RecDescent;
     5 use Data::Dumper;
    31 use Data::Dumper;
     6 use Storable;
    32 use Storable;
       
    33 use Getopt::Std;
     7 
    34 
     8 my $grammar_file = shift;
    35 my $opts = {};
     9 my $storable = shift; # optional
    36 getopts("do:g:", $opts);
       
    37 
       
    38 my $grammar_file = $opts->{g};
    10 unless (defined $grammar_file) {
    39 unless (defined $grammar_file) {
    11 	print "Usage: $0 grammar_file\n";
    40 	HELP_MESSAGE();
    12 	exit 1;
    41 	exit 1;
    13 }
    42 }
    14 
    43 
    15 unless (-f $grammar_file) {
    44 unless (-f $grammar_file) {
    16 	die "Grammar file $grammar_file does not exit";
    45 	die "Grammar file $grammar_file does not exit";
    35 {
    64 {
    36 	local $/;
    65 	local $/;
    37 	$content = <>;
    66 	$content = <>;
    38 }
    67 }
    39 
    68 
    40 $::res = {};
    69 $::res = {}; # parse tree result
    41 my $p_res = $parser->file($content);
    70 my $p_res = $parser->file($content);
       
    71 my $storable = $opts->{o};
    42 store($::res, $storable) if (defined $storable && length($storable) > 0);
    72 store($::res, $storable) if (defined $storable && length($storable) > 0);
    43 print Dumper $::res;
    73 print Dumper $::res if ($opts->{d});
       
    74 
       
    75 
       
    76 sub main::VERSION_MESSAGE {
       
    77 	my $fh = shift;
       
    78 	print $fh "parser version $main::VERSION - Copyright 2009 Tomas Zeman\n";
       
    79 }
       
    80 
       
    81 sub main::HELP_MESSAGE {
       
    82 	print <<EOF;
       
    83 Usage: parser [ -d ] [ -o data ] -g grammar_file
       
    84 
       
    85    -o data	store parse tree into file (using Storable module)
       
    86    -d		dump parse tree to stdout
       
    87    -g grammar_file
       
    88    		file containing parse grammar
       
    89 
       
    90 EOF
       
    91 }
       
    92 
       
    93