Name

Tree::Term - Create a parse tree from an array of terms representing an expression.

Synopsis

The expression to parse is presented as an array of words, the first letter of each word indicates its lexical role as in:

my @e = qw(b b p2 p1 v1 q1 q2 B  d3 b p4 p3 v2 q3 q4  d4 p6 p5 v3 q5 q6 B s B s);

Where:

a assign     - infix operator with priority 2 binding right to left
b open       - open parenthesis
B close      - close parenthesis
d dyad       - infix operator with priority 3 binding left to right
p prefix     - monadic prefix operator
q suffix     - monadic suffix operator
s semi-colon - infix operator with priority 1 binding left to right
v variable   - a variable in the expression

The results of parsing the expression can be printed with flat which provides a left to right representation of the parse tree.

is_deeply parse(@e)->flat, <<END;

    d3
 q2       d4
 q1    q4    q6
 p2    q3    q5
 p1    p4    p6
 v1    p3    p5
       v2    v3
END

Description

Create a parse tree from an array of terms representing an expression.

Version 20210629.

The following sections describe the methods in each functional area of this module. For an alphabetic listing of all methods by name see Index.

Parse

Create a parse tree from an array of terms representing an expression.

parse(@expression)

Parse an expression.

   Parameter    Description
1  @expression  Expression to parse

Example:

ok T [qw(b b p2 p1 v1 q1 q2 B  d3 b p4 p3 v2 q3 q4  d4 p6 p5 v3 q5 q6 B s B s)], <<END; 
    d3
 q2       d4
 q1    q4    q6
 p2    q3    q5
 p1    p4    p6
 v1    p3    p5
       v2    v3
END

Print

Print a parse tree to make it easy to visualize its structure.

flat($expression, @title)

Print the terms in the expression as a tree from left right to make it easier to visualize the structure of the tree.

   Parameter    Description
1  $expression  Root term
2  @title       Optional title

Example:

ok T [qw(p2 p1 v1 q1 q2 d3 p4 p3 v2 q3 q4  d4 p6 p5 v3 q5 q6 s)], <<END;        
    d3
 q2       d4
 q1    q4    q6
 p2    q3    q5
 p1    p4    p6
 v1    p3    p5
       v2    v3
END

Hash Definitions

Tree::Term Definition

Description of a term in the expression.

Output fields

operands

Operands to which the operator will be applied.

operator

Operator to be applied to one or more operands.

up

Parent term if this is a sub term.

Private Methods

new($operator, @operands)

New term.

   Parameter  Description
1  $operator  Operator
2  @operands  Operands.

depth($term)

Depth of a term in an expression.

   Parameter  Description
1  $term      Term

listTerms($expression)

List the terms in an expression in post order

   Parameter    Description
1  $expression  Root term

Index

1 depth - Depth of a term in an expression.

2 flat - Print the terms in the expression as a tree from left right to make it easier to visualize the structure of the tree.

3 listTerms - List the terms in an expression in post order

4 new - New term.

5 parse - Parse an expression.

Installation

This module is written in 100% Pure Perl and, thus, it is easy to read, comprehend, use, modify and install via cpan:

sudo cpan install Tree::Term

Author

philiprbrenan@gmail.com

http://www.appaapps.com

Copyright

Copyright (c) 2016-2021 Philip R Brenan.

This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.