Hi,

I'm trying to understand the transform functionality of Parslet. For this reason I parsed the string 'a + b + c '. Transforming the parse tree of this string results in the following transformation output:

PARSING:a + b + c

ORIGINAL TREE:

{:result=>

[{:term=>{:factor=>{:ident=>"a"@1}}},

{:plus=>"+ "@3, :term=>{:factor=>{:ident=>"b"@5}}},

{:plus=>"+ "@7, :term=>{:factor=>{:ident=>"c"@9}}}]}

 TRANSFORMED BY RULE: Terminals

{:result=>

[{:term=>"a"@1},

{:plus=>"+ "@3, :term=>"b"@5},

{:plus=>"+ "@7, :term=>"c"@9}]}

 TRANSFORMED BY RULE: Test1

{:result=>

[{:left=>"a"@1, :plus=>"+ "@3, :right=>"b"@5},

{:plus=>"+ "@7, :term=>"c"@9}]}

 TRANSFORMED BY RULE: Test2

{:result=>[{:left=>"t3", :plus=>"+ "@7, :term=>"c"@9}]}


In this example, I used the following rules:

@t.rule(:result=> [{:term=>simple(:a)}, {:plus=>simple(:b), :term=>simple(:c)},

{:plus=>simple(:d), :term=>simple(:e)} ] ){

{:result=> [{:left=>a, :plus=>b, :right=>c},{:plus=>d, :term=>e } ] }

}

and

@t.rule(:result=>[{:left=>simple(:a), :plus=>simple(:b), :right=>simple(:c)},

{:plus=>simple(:d), :term=>simple(:e) } ]) {

{:result=>[{:left=>newtmp, :plus=>d, :term=>e } ]}

}

To make these rules, I used the intermediate results of the parse tree.

The problem is when I altered this string 'a + b + c'  into  'a + b + c + d'
the above rules don't give the required transformation output; see next example:

PARSING:a + b + c + d

ORIGINAL TREE:

{:result=>

[{:term=>{:factor=>{:ident=>"a"@1}}},

{:plus=>"+ "@3, :term=>{:factor=>{:ident=>"b"@5}}},

{:plus=>"+ "@7, :term=>{:factor=>{:ident=>"c"@9}}},

{:plus=>"+ "@11, :term=>{:factor=>{:ident=>"d"@13}}}]}

 TRANSFORMED BY RULE: Test2

{:result=>

[{:term=>"a"@1},

{:plus=>"+ "@3, :term=>"b"@5},

{:plus=>"+ "@7, :term=>"c"@9},

{:plus=>"+ "@11, :term=>"d"@13}]}

For the new input string 'a + b + c + d' it seems to be that I should have to make new rules for every new (random) input string of a given grammar. I hardly can't believe that this is the way I should have to work with Parslet!

My question is how to define transformation rules in a smart way? My guess is that I'm not using the right matching features
like, for example, sequence or subtree?

Many thanks in advance,

Thiel


Reply via email to