Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Forcing parse to fail in Parse::RecDescent

by suaveant (Parson)
on Sep 14, 2007 at 15:14 UTC ( [id://639026]=perlquestion: print w/replies, xml ) Need Help??

suaveant has asked for the wisdom of the Perl Monks concerning the following question:

Ok.. I am sure I am missing something obvious...

How would one just tell the parser to fail when a certain spot is reached? <commit> skips the remaining rules, but basically in my code I have a block { STUFF } and once I hit that first { if the rule doesn't succeed (like a missing }) I just want to stop the whole parse and try to make up a useful error message.

Is there something like <fatalcommit> or some combination of tags I can do this with. Or do I need to start a line with <uncommit> and force the parser to end some way in an action block?

                - Ant
                - Some of my best work - (1 2 3)

  • Comment on Forcing parse to fail in Parse::RecDescent

Replies are listed 'Best First'.
Re: Forcing parse to fail in PArse::RecDescent
by ikegami (Patriarch) on Sep 14, 2007 at 15:22 UTC
    <error>
      Hrm... yes... playing around some more I find the problem is more insidious than I had thought...

      When I pass the following to it

      {201({2}}
      I get the error from the abstraction rule...
      When I pass
      4 * {201({2}}
      My match fails, but I don't get the error from abstraction... maybe I am being bitten by leftop somehow...
      Here follows the whole grammar.
      startrule: { ($abscb,$self) = @arg } <reject> startrule: bin_op /^\Z/ { $item[1] } bin_op: logical_op # Lowest precendance the comparitive operators... logical_op: <leftop: equality_op LOGICAL equality_op> { treeify(@{$item[1]}); } equality_op: <leftop: comp_op EQUALITY comp_op> { treeify(@{$item[1]}); } comp_op: <leftop: add_op COMP add_op> { treeify(@{$item[1]}); } add_op: <leftop: prod_op SUM prod_op > { treeify(@{$item[1]}); } # Highest precendance. prod_op : <leftop: term PROD term > { treeify(@{$item[1]}); } SUM : '+' | '-' PROD : '*' | '/' | '%' COMP : />=?|<=?|le|ge|lt|gt/ EQUALITY : /!=|==?|eq|ne/ LOGICAL : 'AND' | 'OR' | '&&' | '||' arg_list: list_bin_op(s?) bin_op { [@{$item[1]},$item[2]] } # Functions, parenthesized equations, abstractions numbers and strin +gs are all basically atomic. term: function | abstraction | '(' <commit> bin_op ')' { $item[2] } | number | /\$[1-9]/ { [ 'arg', substr($item[1],1,1) ] } | string | 'NULL' | <error> abstraction: '{' number '(' <commit> arg_list ')' '}' { $abscb->($it +em[2],$item[4]) } | '{' <commit> number '}' { $abscb->($item[2]) } | <error?: GRRRRRRRRRRRR> <reject> # There is probably a prettier way to do these, but this works if: /if/i '(' list_bin_op(2) bin_op ')' { [$item[1],@{$item[3]},$ite +m[4]] } concat: /concat/i '(' list_bin_op(s) bin_op ')' { [$item[1],@{$item[ +3]},$item[4]] } left: /left/i '(' list_bin_op bin_op ')' { [@item[1,3,4]] } right: /right/i '(' list_bin_op bin_op ')' { [@item[1,3,4]] } ifnull: /ifnull/i '(' list_bin_op bin_op ')' { [@item[1,3,4]] } now: /now/i '(' ')' { [$item[1],''] } # if you don't return two item +s, they seem to get flattened somewhere, making it the string NOW length_func: /length/i '(' bin_op ')' { [@item[1,3]] } interval: /interval/i /\d+/ /MONTH|DAY|WEEK|YEAR/ { [@item[1,2,3]] } last_day: /last_day/i '(' bin_op ')' { [@item[1,3]] } date_format: /date_format/i '(' list_bin_op bin_op ')' { [@item[1,3 +,4]] } # just the list of all the functions function: if | concat | left | right | ifnull | now | length_func | +interval | last_day | date_format number: /[+-]?(?:\d+\.?\d*|\.\d+)/ { $item[1] } list_bin_op: bin_op ',' { $item[1] } string: <perl_quotelike> { if(!$item[1][0] && ($item[1][1] eq '"' || $item[1][1] eq "'")) { [$item[0],$item[1][2]]; } else { undef; } }

                      - Ant
                      - Some of my best work - (1 2 3)

        <leftop> backtracks and successfully matches 4. Since the production matches, pending errors are canceled. The parsing error happens later, when * {201({2}} attempts to match /^\Z/.

        This problem would be solved by committing after matching the *. Since <leftop> doesn't provide a means for us to do that, we'll have to roll out an alternative to <leftop>. The code in section 3.c.ii of Operator Associativity and Eliminating Left-Recursion in Parse::RecDescent suits your needs perfectly.

        prod_op : term prod_op_[ $item[1] ] prod_op_ : PROD <commit> term prod_op_[ [ $item[1], $arg[0], $item[2] +] ] | { $arg[0] }

        We can even add some extra diagnostics:

        prod_op : term prod_op_[ $item[1] ] prod_op_ : PROD <commit> term prod_op_[ [ $item[1], $arg[0], $item[2] +] ] | <error?: Expecting term following operator> <reject> | { $arg[0] }

        Using the test program found below generated the following output:

        4 * 5 >> match ===== {201({2}} ERROR (line 1): GRRRRRRRRRRRR >> no match ===== 4 * {201({2}} ERROR (line 1): GRRRRRRRRRRRR ERROR (line 1): Expecting term following operator >> no match

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://639026]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found