About Trees

General APL language issues

About Trees

Postby Yves on Thu Aug 09, 2018 9:25 am

Hi All,
With help of this page http://dfns.dyalog.com/n_parse.htm
i try :
      parse '{,⍵∘.,,⍵∘.,,⍵∘.,⍵}'
┌─┬────────────────────────────────┬─┐
│{│ A │}│
│ │┌───┴───┐ │ │
│ │, ┌────┴─────┐ │ │
│ │ ┌┴─┐ ┌───┴───┐ │ │
│ │ ⍵ ┌┴─┐ , ┌────┴────┐ │ │
│ │ ∘ ┌┴┐ ┌┴─┐ ┌──┴──┐ │ │
│ │ . , ⍵ ┌┴─┐ , ┌──┴───┐│ │
│ │ ∘ ┌┴┐ ┌┴─┐ ⍵│ │
│ │ . , ⍵ ┌┴─┐ │ │
│ │ ∘ ┌┴┐ │ │
│ │ . , │ │
└─┴─────────────────────────────────┴─┘
sorry for the fixed font.

good result for automatic process, hardest for (standard) human reader.

Have you idea for how to transform it in
      ┌─┬────────────────────────────────────┬─┐
│{│ A │}│
│ │ ┌──┴───┐ │ │
│ │ , ┌───┼───────┐ │ │
│ │ ⍵ ∘ ┌──┴──┐ │ │
│ │ ┌┴┐ , ┌───┼──────┐ │ │
│ │ . , ⍵ ∘ ┌──┴──┐ │ │
│ │ ┌┴┐ , ┌───┼───┐ │ │
│ │ . , ⍵ ∘ ⍵ │ │
│ │ ┌┴┐ │ │
│ │ . , │ │
└─┴─────────────────────────────────────┴─┘

easier to see the difffrence for ,,

but for human reader, .∘, make senses in ⍵ F ⍵
      ┌─┬────────────────────────────────────┬─┐
│{│ A │}│
│ │ ┌─┴───┐ │ │
│ │ , ┌───┼──────┐ │ │
│ │ ⍵ .∘, ┌──┴──┐ │ │
│ │ , ┌───┼───────┐ │ │
│ │ ⍵ .∘, ┌───┴──┐ │ │
│ │ , ┌───┼───┐ │ │
│ │ ⍵ .∘, ⍵ │ │
└─┴────────────────────────────────────┴─┘

each leaves make senses for human reader

Have a good day,
Yves
Yves
 
Posts: 39
Joined: Mon Nov 30, 2015 11:33 am

Re: About Trees

Postby JohnS|Dyalog on Sat Aug 11, 2018 1:01 pm

The Bunda-Gerth parser at http://dfns.dyalog.com/n_parse.htm returns a binary expression tree by:
- binding left arguments and right operands to derive monadic functions and operators
- binding adjacent pairs of array items (vectors) and functions (trains) from the left.
It also preserves brackets.

      )xload dfns
... saved ...

try ← scripts._dyalog∘parse ⍝ parser with sample Dyalog syntax table

try'1 2 3 4' ⍝ vector
A
┌─┴─┐
┌┴─┐ 4
┌┴┐ 3
1 2
try'+-×÷' ⍝ train
F
┌─┴─┐
┌┴─┐ ÷
┌┴┐ ×
+ -
try'(+⌿÷≢)⍺(⍺+⍵)(⍺+.×⍵)⍵' ⍝ a more complex expression
A
┌─────────┴──────────┐
┌──┴──┐ ┌──────┴───────┐
( ┌─┴─┐ ┌────┴─────┐ ⍵
┌┴─┐ ≢ ┌─┴─┐ ┌──┴──┐
┌┴┐ ÷ ⍺ ┌─┴─┐ ( ┌──┴───┐
+ ⌿ ( ┌┴─┐ ┌┴─┐ ⍵
┌┴┐ ⍵ ⍺ ┌┴─┐
⍺ + + ┌┴┐
. ×

I think what Yves would prefer is an n-ary tree with the components of vectors and tacit functions at the same level.
      try'1 2 3 4'
┌─┬┴┬─┐
1 2 3 4

try'+-×÷'
┌─┬┴┬─┐
+ - × ÷

try'(+⌿÷≢)⍺(⍺+⍵)(⍺+.×⍵)⍵'
┌─────┴─────┐
┌──┼─┐ ┌───┬───┴───┬─────┐
┌┴┐ ÷ ≢ ⍺ ┌─┼─┐ ┌───┼───┐ ⍵
+ ⌿ ⍺ + ⍵ ⍺ ┌─┼─┐ ⍵
+ . ×

or collecting together "simple" components such as +⌿ and +.×:
      try'(+⌿÷≢)⍺(⍺+⍵)(⍺+.×⍵)⍵'
┌───┴─────┐
┌─┼─┐ ┌──┬──┴──┬────┐
+⌿ ÷ ≢ ⍺ ⍺+⍵ ┌──┼──┐ ⍵
⍺ +.× ⍵

I don't have such a function but perhaps someone out there does?
JohnS|Dyalog
 


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest