-
-
30 Jun 2021 06:36:52 UTC
- Distribution: Tree-Term
- Module version: 20210631
- Source (raw)
- Browse (raw)
- Changes
- How to Contribute
- Repository
- Issues
- Testers (70 / 0 / 7)
- Kwalitee
Bus factor: 1- 87.85% Coverage
- License: perl_5
- Activity
24 month- Tools
- Download (9.04KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
- Dependencies
- Carp
- Data::Dump
- Data::Table::Text
- Test2::API
- Test::Most
- feature
- and possibly others
- Reverse dependencies
- CPAN Testers List
- Dependency graph
- Name
- Synopsis
- Description
- Parse
- Hash Definitions
- Private Methods
- Index
- Installation
- Author
- Copyright
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 20210631.
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.
Tree::Term::Codes Definition
Lexical item codes.
Output fields
B
Close parenthesis.
a
Infix operator with priority 2 binding right to left typically used in an assignment.
b
Open parenthesis.
d
Infix operator with priority 3 binding left to right typically used in arithmetic.
p
Monadic prefix operator.
q
Monadic suffix operator.
s
Infix operator with priority 1 binding left to right typically used to separate statements.
t
A term in the expression.
v
A variable in the expression.
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
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.
Module Install Instructions
To install Tree::Term, copy and paste the appropriate command in to your terminal.
cpanm Tree::Term
perl -MCPAN -e shell install Tree::Term
For more information on module installation, please visit the detailed CPAN module installation guide.