in reply to Parsing and Translation pt.2

I posted a question earlier about how to parse a file containing 4Test code with Parse::RecDescent
It is a good idea to provide a link to that node. Is this the one? Parsing and Translation
Parse::RecDescent is quite difficult to understand fully, but I hope someone can comprehend what I'm trying to do
There are people here who can advise you, but I think you need to post a small sample of the code you have tried, and where you are having problems.

Replies are listed 'Best First'.
Re^2: Parsing and Translation pt.2
by speedyshady (Novice) on Aug 06, 2010 at 18:27 UTC

    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);
      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.)

      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);