librelist archives

« back to archive

Problem with transformation rule

Problem with transformation rule

Thiel Chang
2011-04-28 @ 12:57
Hi guys,

I tried to transform the following parse tree:



{:plus=>"+ "@2,



{:mult=>"* "@6, :factor1=>{:ident=>"c"@8}}]}]}

  One of the transformation  rules I applied is:

@t.rule(:factor1 => simple(:str)) { str

puts "Test Factor1"


And the result after the transformation is:


["a"@0, {:plus=>"+ "@2, :term1=>["b"@4, {:mult=>"* "@6, :factor1=>"c"@8}]}]}

However, I expected in the output: "Test Factor1"  but it did not.

It seems if this rule was not or ( could not be)  applied.

Can anyone explain why it did not?

Thanks in advance ,


Re: Problem with transformation rule

Kaspar Schiess
2011-04-28 @ 13:56
Hi Thiel,

> {:mult=>"* "@6, :factor1=>{:ident=>"c"@8}}]}]}
> @t.rule(:factor1 =>  simple(:str)) { ... }
>    {:ae=>
> ["a"@0, {:plus=>"+ "@2, :term1=>["b"@4, {:mult=>"* "@6, :factor1=>"c"@8}]}]}

The fact that :factor1 contains a different value after the transform 
means that another rule but the one you show here has applied. The 
transformation code is crafted such that you cannot create endless loops 
with it - it replaces leaves first, moving up the tree until it reaches 
the root. Only once.

So once a rule has applied, no other rules are applied to the result of 
the first rule. I am sure that if you were to reapply the same set of 
transformation rules to the result you give above, you would trigger 
:factor1 => simple(...).

Please give the code samples on something like 
next time - They will be easier to work on.