# Racc: when is on\_error called?

**URL:** <https://www.ruby-forum.com/t/racc-when-is-on-error-called/118635>\
**Category:** Ruby\
**Created:** [October 29, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/racc-when-is-on-error-called/118635 "2007-10-29T17:52:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [October 29, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/racc-when-is-on-error-called/118635/1 "2007-10-29T17:52:03Z")

</div>

When is on\_error supposed to be called? Will on\_error be called when  
the error rule is triggered?

After reading the lex/yacc book, it seems as though the error rule is  
supposed to eat up tokens until it can find a pivot token. But is  
on\_error supposed to be called? I did a couple experiments and found  
that on\_error is not called if the error rule matches only one token,  
but if it matches many tokens on\_error is called.

Consider the following grammar:

class Tester

```
token A LBRACE

```

rule  
a\_a\_lbrace  
: A A LBRACE { puts val }  
| A error LBRACE { puts “recovered”; puts val }  
;

end

and the following script:

class Foo \< Tester  
def initialize  
@tokens = [  
[:A, ‘a’],  
[:b, ‘b’],  
[:LBRACE, ‘{’],  
[false, false],  
]  
end

```
def parse; do_parse; end

def next_token; @tokens.shift; end

def on_error(*args)
  puts "on_error called"
end

```

end

Foo.new.parse

on\_error does not get called, but the recovery rule matches. However,  
change the tokens to this:

```
  @tokens = [
    [:A, 'a'],
    [:b, 'b'],
    [:b, 'b'],
    [:b, 'b'],
    [:LBRACE, '{'],
    [false, false],
  ]

```

on\_error gets called _and_ the recover rule matches. It seems on\_error  
gets called if the error rule matches \> 1 token. Is that expected  
behavior? Am I using the error rule correctly?

Any help would be greatly appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [December 4, 2009, 5:35am UTC](https://www.ruby-forum.com/t/racc-when-is-on-error-called/118635/2 "2009-12-04T05:35:20Z")

</div>

Hi Aaron,

Did you get the answers to your questions? If so, could you share it  
please.

Thanks,  
Vasco Andrade e Silva
