in reply to Re: Parsing and Translation pt.2
in thread Parsing and Translation pt.2

I don't want to post the entire code because it is unnecessary. In fact I don't have any problems with the parser I have written. The grammar works fine and my evals all return what I have asked them to. All I want to know/do is read in a file and parse it. I'm having trouble understanding if this is in fact how Parse::RecDescent could potentially be used.

Here is part of the code:
use strict; use warnings; use Parse::RecDescent; $::RD_ERRORS = 1; $::RD_WARN = 1; $::RD_HINT = 1; my $grammar = q{ identifier : /[a-z]\w+/ binops : '+' | '-' | '/' | '*' | '%' | '**' lbinops: '!' | '<' | '>' | '>='| '<='| '&&'| '||' | '==' integer: /-\d+/ {print "hello $item[-1]" if $::debugging;} number : /(\d+|\d*\.\d+)/ {print "hello $item[-1]" if $::debuggin +g;} }; #evals: these will evaluate to "1" and print #print "valid number\n" if $parser->number(2); #print "valid integer\n" if $parser->integer(-2);

Replies are listed 'Best First'.
Re^3: Parsing and Translation pt.2 (Parse::RecDescent)
by toolic (Bishop) on Aug 07, 2010 at 01:21 UTC
    All I want to know/do is read in a file and parse it.
    The following code will read an input file, parse using Parse::RecDescent, then write the output to a file. I adapted the example code from Parse-RecDescent-FAQ (Super Search is your friend).

    The input file consists of just a single line: aa

    The output file also consists of just a single line: a

    use strict; use warnings; use Parse::RecDescent; use File::Slurp; my $grammar = 'startrule: ( "a" | "aa" ) "a"'; my $parser = Parse::RecDescent->new($grammar); my $text = read_file('input.txt'); write_file('output.txt', $parser->startrule($text), "\n");

    Hopefully, you can apply this trivial example to your code. (Keep in mind that this is the 1st time I've ever used Parse::RecDescent.)

Re^3: Parsing and Translation pt.2
by ikegami (Patriarch) on Aug 09, 2010 at 16:45 UTC
    So the problem is that you don't know how to read a file?
    my $file; { open(my $fh, '<', ...) or die ...; local $/; $file = <$fh>; }

    But it also seems that your grammar isn't complete. You've defined some basic blocks, but not how they fit together. If you just want a tokenizer, you can use something like

    use strict; use warnings; use Parse::RecDescent; $::RD_ERRORS = 1; $::RD_WARN = 1; $::RD_HINT = 1; my $grammar = <<'__EOI__'; { use strict; use warnings; } tokenize : token(s?) /\Z/ { $item[1] } token : ident | binops | lbinops | number | integer | /./ { [ skip => $items[1] ] } ident : /[a-zA-Z][a-zA-Z0-9_]*/ { [ @items ] } binops : /\*\*|[+\-\/*%]/ { [ @items ] } lbinops : />=?|<=?|!|&&|\|\||==/ { [ @items ] } number : /-?[0-9]*\.[0-9]+/ { [ @items ] } integer : /-?[0-9]/ { [ @items ] } __EOI__ ... my @tokens = $parser->tokenize($file);