From: Brian Candler Date: 2009-12-05T01:42:04+09:00 Subject: Re: ruby language parser in ruby Caleb Clausen wrote: > On 12/4/09, Brian Candler wrote: >> A minor problem with the redparse gem is that it gives most of the files >> root-only permissions. I just did 'sudo gem install redparse' > > Yes, that is indeed most unfortunate and due to be fixed in the next > release, which is coming Real Soon Now. I'm not sure how those > permissions got all weird (again); it's not something I usually give a > lot of thought or attention to, I'm afraid. (Frankly, I wish rubygems > would have warned me when I created the gem that some of the files > were not world-readable.) > >> Looking at this code - I don't think I would dare hack it. I think >> ruby_parser is more what I was looking for. > > Would you care to elaborate on that? What didn't you like and/or find > hard to understand? Well, all of it really. I presume that lib/redparse.rb is the "real" parser (I also found lib/redparse/babyparser.rb and babynodes.rb) It's a monolithic file, and it was hard even to see where the grammar began. I believe it's here: [ -[UNOP, Expr, lower_op]>>UnOpNode, -[DEFOP, ParenedNode]>>UnOpNode, -[Op(/^(?:unary|lhs|rhs)\*$/), ValueNode, lower_op]>>UnaryStarNode, ... etc and an example of a larger rule is like this: -[NumberToken&-{:negative=>true}, Op('**').la]>> stack_monkey("fix_neg_exp",2,Op("-@",true)){|stack| #neg_op.unary=true num=stack[-2] op=OperatorToken.new("-@",num.offset) # op.startline=num.startline stack[-2,0]=op num.ident.sub!(/\A-/,'') num.offset+=1 }, I have no idea how to (a) understand, or (b) modify that. I can see there is quite a lot of Ruby operator abuse going on, but without defined semantics. At least with racc, it's extremely well documented, in the sense that I have a printout of the yacc manual to refer to. So no doubt RedParse is a fine ruby parser, and generates a fine object tree as its output. But it's not so good for me as a starting point for building languages which inherit some of ruby flavour, but are significantly different. -- Posted via http://www.ruby-forum.com/.