# Treetop Parser whitespace

**URL:** <https://www.ruby-forum.com/t/treetop-parser-whitespace/162415>\
**Category:** Ruby\
**Created:** [March 9, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/treetop-parser-whitespace/162415 "2009-03-09T19:49:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wayhome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5fc32e/32.png) [@wayhome](https://www.ruby-forum.com/u/wayhome)\
**Post date:** [March 9, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/treetop-parser-whitespace/162415/1 "2009-03-09T19:49:43Z")

</div>

I can’t understand why the following test fails.  
Could you tell me why? Thanks, in advance.

# in arithmetic.treetop

grammar Arithmetic  
rule white\_space  
[\t\n\r]+ {  
def value  
text\_value  
end  
}  
end  
end

# in test\_parser.rb

require “rubygems”  
require “treetop”  
require “arithmetic”

parser = ArithmeticParser.new

p parser.parse(" \n\t\r")

# the execution result

$ ruby test\_parser.rb  
nil

---

<div class="post-metadata">

**Author:** ![wayhome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5fc32e/32.png) [@wayhome](https://www.ruby-forum.com/u/wayhome)\
**Post date:** [March 9, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/treetop-parser-whitespace/162415/2 "2009-03-09T22:45:24Z")

</div>

Young tae Kim wrote:

> I can’t understand why the following test fails.  
> Could you tell me why? Thanks, in advance.

It works for me. What version of Treetop and Polyglot  
are you using. Can you please tell us what is printed  
by “puts parser.failure\_reason”?

Clifford H…

---

<div class="post-metadata">

**Author:** ![wayhome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5fc32e/32.png) [@wayhome](https://www.ruby-forum.com/u/wayhome)\
**Post date:** [March 10, 2009, 12:22am UTC](https://www.ruby-forum.com/t/treetop-parser-whitespace/162415/3 "2009-03-10T00:22:48Z")

</div>

Clifford H. wrote:

> Young tae Kim wrote:
> 
> > I can’t understand why the following test fails.  
> > Could you tell me why? Thanks, in advance.
> 
> It works for me. What version of Treetop and Polyglot  
> are you using. Can you please tell us what is printed  
> by “puts parser.failure\_reason”?
> 
> Clifford H…

First of all, many thanks for your reply!

The version of the operating system is Ubuntu 8.10

the ruby version is ruby 1.8.7 (2008-08-11 patchlevel 72) [x86\_64-linux]

The following is the result of gem list --local

$ gem list --local

\*\*\* LOCAL GEMS \*\*\*

actionmailer (2.2.2)  
actionpack (2.2.2)  
activerecord (2.2.2)  
activeresource (2.2.2)  
activesupport (2.2.2)  
cgi\_multipart\_eof\_fix (2.5.0)  
daemons (1.0.10)  
fastthread (1.0.1)  
gem\_plugin (0.2.3)  
hoe (1.9.0)  
mongrel (1.1.5)  
polyglot (0.2.5) \<----  
rails (2.2.2)  
rake (0.8.3)  
rubyforge (1.0.3)  
sqlite3-ruby (1.2.4)  
treetop (1.2.4) \<----

What’s printed by “puts parser.failure\_reason” is just “nil”.

---

<div class="post-metadata">

**Author:** ![wayhome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5fc32e/32.png) [@wayhome](https://www.ruby-forum.com/u/wayhome)\
**Post date:** [March 10, 2009, 12:38am UTC](https://www.ruby-forum.com/t/treetop-parser-whitespace/162415/4 "2009-03-10T00:38:37Z")

</div>

Clifford H. wrote:

> Young tae Kim wrote:
> 
> > I can’t understand why the following test fails.  
> > Could you tell me why? Thanks, in advance.
> 
> It works for me. What version of Treetop and Polyglot  
> are you using. Can you please tell us what is printed  
> by “puts parser.failure\_reason”?
> 
> Clifford H…

I revised my example like this but it doesn’t work as well.

# in arithmetic.treetop

grammar Arithmetic  
rule num  
[1-9] [0-9]\* {  
def value  
text\_value  
end  
}  
end

rule space  
[\t\n\r]+ {  
def value  
text\_value  
end  
}  
end  
end

# in test\_parser.rb

require “rubygems”  
require “treetop”  
require “arithmetic”

parser = ArithmeticParser.new

p parser.parse(“123”)  
p parser.parse(" \n\t\r")  
puts parser.failure\_reason

# the execution result

$ ruby test\_parser.rb  
SyntaxNode+Num1+Num0 offset=0, “123” (value):  
SyntaxNode offset=0, “1”  
SyntaxNode offset=1, “23”:  
SyntaxNode offset=1, “2”  
SyntaxNode offset=2, “3”  
nil  
nil

---

<div class="post-metadata">

**Author:** ![wayhome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5fc32e/32.png) [@wayhome](https://www.ruby-forum.com/u/wayhome)\
**Post date:** [March 10, 2009, 2:51am UTC](https://www.ruby-forum.com/t/treetop-parser-whitespace/162415/5 "2009-03-10T02:51:13Z")

</div>

I got an answer. If you face the same problem, please refer to the  
following site.

[http://groups.google.com/group/treetop-dev/browse\_thread/thread/417aaf6fdac25fe3](http://groups.google.com/group/treetop-dev/browse_thread/thread/417aaf6fdac25fe3)

---

<div class="post-metadata">

**Author:** ![wayhome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5fc32e/32.png) [@wayhome](https://www.ruby-forum.com/u/wayhome)\
**Post date:** [March 10, 2009, 1:55am UTC](https://www.ruby-forum.com/t/treetop-parser-whitespace/162415/6 "2009-03-10T01:55:43Z")

</div>

Young Tae Kim wrote:

> What’s printed by “puts parser.failure\_reason” is just “nil”.

I think that’s a bug. I don’t have time to investigate it right now.  
BTW, if you run the command “tt arithmetic.tt”, you’ll get the  
generated “arithmetic.rb” code that you can explore.

Clifford H.
