# I get syntax errors from RDParser example I found on internet

**URL:** <https://www.ruby-forum.com/t/i-get-syntax-errors-from-rdparser-example-i-found-on-internet/121553>\
**Category:** Ruby\
**Created:** [November 28, 2007, 10:01pm UTC](https://www.ruby-forum.com/t/i-get-syntax-errors-from-rdparser-example-i-found-on-internet/121553 "2007-11-28T22:01:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 28, 2007, 10:01pm UTC](https://www.ruby-forum.com/t/i-get-syntax-errors-from-rdparser-example-i-found-on-internet/121553/1 "2007-11-28T22:01:06Z")

</div>

=begin  
I got this recursive decent parser from:  
[http://www.rubyinside.com/recursive-descent-parser-for-ruby-300.html](http://www.rubyinside.com/recursive-descent-parser-for-ruby-300.html)

I get this syntax error:  
C:/rb-play/parser/try.rb:13: syntax error, unexpected ‘]’, expecting  
kEND  
g.operation /[±\*/]/

if I change that line to:  
g.operation /[±]/  
that line no longer has an error, so the / and \* threw it off

then I get an error on another line:

g.string %r("'[“‘])  
C:/rb-play/parser/try.rb:15: warning: character class has `[’ without  
escape  
C:/rb-play/parser/try.rb:15: unmatched (: /"'[”']/

Any ideas on what to change to make it work ?

thanks

=end

require ‘rdparser’

parser = RDParser.new do |g|  
g.main ‘line(s)’  
g.line ‘expression separator(?) comment(?)’  
g.comment ‘“#” rest\_of\_line’  
g.rest\_of\_line /.+$/  
g.separator /;/  
g.expression ‘term operation expression | term’  
g.term ‘number | variable | string | brkt\_expression’  
g.brkt\_expression ‘“(” expression “)”’  
g.number /d+(.d+)?/  
g.operation /[±_/]/  
g.variable /[a-z][a-z0-9]_/  
g.string %r("'["'])  
end

content = %q{  
(34 - 3) \* 42; # Comment here…  
“a” + “bcd”  
}

syntax\_tree = parser.parse(:main, content)  
puts RDParser.text\_syntax\_tree(syntax\_tree)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 28, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/i-get-syntax-errors-from-rdparser-example-i-found-on-internet/121553/2 "2007-11-28T23:05:34Z")

</div>

On Nov 28, 2:59 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> that line no longer has an error, so the / and \* threw it off
> 
> then I get an error on another line:
> 
> g.string %r("'[“‘])  
> C:/rb-play/parser/try.rb:15: warning: character class has `[’ without  
> escape  
> C:/rb-play/parser/try.rb:15: unmatched (: /"'[”']/

In the first, you need the - at the beginning of the (or escaped with  
a backslash) or Ruby interprets it as a character range. You also need  
to escape the first forward slash or Ruby interprets it as the end of  
the Regex. So:

/[-+\*/]/

In the second, the backslash needs to be escaped. If it isn’t, then it  
is used to escape the following ] so the character class isn’t closed  
until the final ] which is just wrong. So:

%r("'["'])

Consider both issues were due to missing backslashes, they were  
probably originally correct. I guess somewhere along the way the  
backslashes were interpreted as escape characters and didn’t make it  
to the final page.

Jeremy

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 29, 2007, 3:51am UTC](https://www.ruby-forum.com/t/i-get-syntax-errors-from-rdparser-example-i-found-on-internet/121553/3 "2007-11-29T03:51:30Z")

</div>

On Nov 28, 5:04 pm, yermej [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > g.operation /[±\*/]/  
> > C:/rb-play/parser/try.rb:15: unmatched (: /"'["']/  
> > until the final ] which is just wrong. So:
> 
> %r("'["'])
> 
> Consider both issues were due to missing backslashes, they were  
> probably originally correct. I guess somewhere along the way the  
> backslashes were interpreted as escape characters and didn’t make it  
> to the final page.
> 
> Jeremy

That first one I had tried before and it didn’t work, but if I move  
the \* to the begining it does work:  
/[\*±/]/
