So if I'm reading your correctly, I should take a flat(ter) parse tree and
then transform it into the structure I really want. I suppose I could have
multiple transform passes, first to nest the flat list, second to convert
the now-normalized tree into a propert AST?

Using this approach the parse tree would initially look like:
[
  {:base=>{:reference=>"x"}},
  {:regular_invocation=>{:callable=>{:property_name=>{:reference=>"y"}},
:arguments=>[]}},
  {:property_name=>{:reference=>"z"}}
]

I suppose this wouldn't be all that awful. I did end up having to take a
similar (multi-pass) approach when I tried to hand-roll the whole parser.
Let me know if you think I'm way out in left field, otherwise I'll see how
far I get this time. Thanks a bunch!


On Thu, Mar 28, 2013 at 12:16 AM, Nigel Thorne <[email protected]>wrote:

> There is some good discussion around LR in Parslet's mailing list history.
>
>
> http://librelist.com/browser//ruby.parslet/2011/11/4/ruby-parslet-left-recursion/
>
> is the approach I would favor..
>
> so the key is viewing the parser as only part of your solution.. that
> changes your code to a tree you can manipulate.
>
> Once it's a tree you can apply transformations to make it represent the
> structure you are looking for.
>
> You can imagine parsing your code to  (forgive my notation)
>   dot ( 'x', dot ( eval( 'y', [] ) , 'z' ) )
> then transforming that to
>   dot ( eval ( dot ( 'x' , 'y' ) , [] ), 'z' )
>
> Cheers
> Nigel
> ---
> "Man, I'm going to have so many chickens when this lot hatch!"
>
>
> On Thu, Mar 28, 2013 at 9:30 AM, Thomas Ingram <[email protected]>wrote:
>
>> I am working on a new language which supports method/property chaining,
>> very much like JavaScript. For instance:
>>
>>     x.y().z
>>
>> `y` is a property of `x`, which happens to be a function. This makes `y`
>> a method of `x`. I call the method with parens. Then I am accessing a
>> property on the result of that method, `z`. I'm trying to produce a parse
>> tree that looks like:
>>
>>     {
>>       :property => {
>>         :object => {
>>           :regular_invocation => {
>>             :callable => {
>>               :property => {
>>                 :object => { :reference => 'x' },
>>                 :property_name => { :reference => 'y' }
>>               }
>>             },
>>             :arguments => []
>>           }
>>         },
>>         :property_name => { :reference => 'z' }
>>       }
>>     }
>>
>> With all that out of the way, I have some questions. :)
>>
>> 1) Is my target parse tree reasonable? I was talking showing this to
>> somebody the other day, and they vaguely suggested that flat is better than
>> nested. I think this tree is entirely reasonable, but I wanted to get some
>> feedback.
>>
>> 2) Assuming the parse tree is reasonable, how should I go about parsing?
>> I have my current best attempt on GitHub (see below), and all I can produce
>> is left-recursion. My understanding is that what I have would be fine with
>> support for left-recursion, and there exists a non-left-recursive way to
>> parse the source, but I'm finding it difficult to write.
>>
>> Please help!
>>
>> The relevant code and associated tests are at:
>>
>> https://github.com/rip-lang/rip/blob/take-two/lib/rip/compiler/parser.rb#L69
>>
>> https://github.com/rip-lang/rip/blob/take-two/spec/rip/compiler/parser_spec.rb#L535
>>
>> --
>>  Thomas Ingram
>>
>
>


-- 
Thomas Ingram

Reply via email to