.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "TREEREG 1" .TH TREEREG 1 "2017-06-14" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" treereg \- Compiler for Tree Regular Expressions .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 6 \& treereg [\-m packagename] [[no]syntax] [[no]numbers] [\-severity 0|1|2|3] \e \& [\-p treeprefix] [\-o outputfile] [\-lib /path/to/library/] \-i filename[.trg] \& treereg [\-m packagename] [[no]syntax] [[no]numbers] [\-severity 0|1|2|3] \e \& [\-p treeprefix] [\-lib /path/to/library/] [\-o outputfile] filename[.trg] \& treereg \-v \& treereg \-h .Ve .SH "OPTIONS" .IX Header "OPTIONS" Options can be used both with one dash and double dash. It is not necessary to write the full name of the option. A disambiguation prefix suffices. .IP "\(bu" 4 \&\f(CW\*(C`\-i[n] filename\*(C'\fR .Sp Input file. Extension \f(CW\*(C`.trg\*(C'\fR is assumed if no extension is provided. .IP "\(bu" 4 \&\f(CW\*(C`\-o[ut] filename\*(C'\fR .Sp Output file. By default is the name of the input file (concatenated with .pm) .IP "\(bu" 4 \&\f(CW\*(C`\-m[od] packagename\*(C'\fR .Sp Name of the package containing the generated subroutines. By default is the longest prefix of the input file name that conforms to the classic definition of integer \f(CW\*(C`[a\-z_A\-Z]\ew*\*(C'\fR. .IP "\(bu" 4 \&\f(CW\*(C`\-l[ib] /path/to/library/\*(C'\fR .Sp Specifies that \f(CW\*(C`/path/to/library/\*(C'\fR will be included in \f(CW@INC\fR. Useful when the \f(CW\*(C`syntax\*(C'\fR option is on. Can be inserted as many times as necessary. .IP "\(bu" 4 \&\f(CW\*(C`\-p[refix] treeprefix\*(C'\fR .Sp Tree nodes automatically generated using \f(CW\*(C`Parse::Eyapp\*(C'\fR are objects blessed into the name of the production. To avoid crashes the programmer may prefix the class names with a given prefix when calling the parser; for example: .Sp .Vb 1 \& $self\->YYParse( yylex => \e&_Lexer, yyerror => \e&_Error, yyprefix => _\|_PACKAGE_\|_."::") .Ve .Sp The \f(CW\*(C`\-prefix treeprefix\*(C'\fR option simplifies the process of writing the tree grammar so that instead of writing with the full names .Sp .Vb 1 \& CLASS::TIMES(CLASS::NUM, $x) and { $NUM\->{VAL} == 0) => { $NUM } .Ve .Sp it can be written: .Sp .Vb 1 \& TIMES(NUM, $x) and { $NUM\->{VAL} == 0) => { $NUM } .Ve .IP "\(bu" 4 \&\f(CW\*(C`\-n[umbers]\*(C'\fR .Sp Produces \f(CW\*(C`#line\*(C'\fR directives. .IP "\(bu" 4 \&\f(CW\*(C`\-non[umbers]\*(C'\fR .Sp Disable source file line numbering embedded in your parser .IP "\(bu" 4 \&\f(CW\*(C`\-sy[ntax]\*(C'\fR .Sp Checks that Perl code is syntactically correct. .IP "\(bu" 4 \&\f(CW\*(C`\-nosy[ntax]\*(C'\fR .Sp Does not check the syntax of Perl code .IP "\(bu" 4 \&\f(CW\*(C`\-se[verity] number\*(C'\fR .RS 4 .IP "\- 0 = Don't check arity (default). Matching does not check the arity. The actual node being visited may have more children." 2 .IX Item "- 0 = Don't check arity (default). Matching does not check the arity. The actual node being visited may have more children." .PD 0 .IP "\- 1 = Check arity. Matching requires the equality of the number of children and the actual node and the pattern." 2 .IX Item "- 1 = Check arity. Matching requires the equality of the number of children and the actual node and the pattern." .IP "\- 2 = Check arity and give a warning" 2 .IX Item "- 2 = Check arity and give a warning" .IP "\- 3 = Check arity, give a warning and exit" 2 .IX Item "- 3 = Check arity, give a warning and exit" .RE .RS 4 .RE .IP "\(bu" 4 .PD \&\f(CW\*(C`\-v[ersion]\*(C'\fR .Sp Gives the version .IP "\(bu" 4 \&\f(CW\*(C`\-u[sage]\*(C'\fR .Sp Prints the usage info .IP "\(bu" 4 \&\f(CW\*(C`\-h[elp]\*(C'\fR .Sp Print this help .SH "DESCRIPTION" .IX Header "DESCRIPTION" \&\f(CW\*(C`Treereg\*(C'\fR translates a tree grammar specification file (default extension \f(CW\*(C`.trg\*(C'\fR describing a set of tree patterns and the actions to modify them using tree-terms like: .PP .Vb 1 \& TIMES(NUM, $x) and { $NUM\->{VAL} == 0) => { $NUM } .Ve .PP which says that wherever an abstract syntax tree representing the product of a numeric expression with value 0 times any other kind of expression, the \f(CW\*(C`TIMES\*(C'\fR tree can be substituted by its left child. .PP The compiler produces a Perl module containing the subroutines implementing those sets of pattern-actions. .SH "EXAMPLE" .IX Header "EXAMPLE" Consider the following \f(CW\*(C`eyapp\*(C'\fR grammar (see the \f(CW\*(C`Parse::Eyapp\*(C'\fR documentation to know more about \f(CW\*(C`Parse::Eyapp\*(C'\fR grammars): .PP .Vb 10 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& nereida:~/LEyapp/examples> cat Rule6.yp \& %{ \& use Data::Dumper; \& %} \& %right \*(Aq=\*(Aq \& %left \*(Aq\-\*(Aq \*(Aq+\*(Aq \& %left \*(Aq*\*(Aq \*(Aq/\*(Aq \& %left NEG \& %tree \& \& %% \& line: exp { $_[1] } \& ; \& \& exp: %name NUM \& NUM \& | %name VAR \& VAR \& | %name ASSIGN \& VAR \*(Aq=\*(Aq exp \& | %name PLUS \& exp \*(Aq+\*(Aq exp \& | %name MINUS \& exp \*(Aq\-\*(Aq exp \& | %name TIMES \& exp \*(Aq*\*(Aq exp \& | %name DIV \& exp \*(Aq/\*(Aq exp \& | %name UMINUS \& \*(Aq\-\*(Aq exp %prec NEG \& | \*(Aq(\*(Aq exp \*(Aq)\*(Aq { $_[2] } /* Let us simplify a bit the tree */ \& ; \& \& %% \& \& sub _Error { \& die "Syntax error.\en"; \& } \& \& sub _Lexer { \& my($parser)=shift; \& \& $parser\->YYData\->{INPUT} \& or $parser\->YYData\->{INPUT} = \& or return(\*(Aq\*(Aq,undef); \& \& $parser\->YYData\->{INPUT}=~s/^\es+//; \& \& for ($parser\->YYData\->{INPUT}) { \& s/^([0\-9]+(?:\e.[0\-9]+)?)// and return(\*(AqNUM\*(Aq,$1); \& s/^([A\-Za\-z][A\-Za\-z0\-9_]*)// and return(\*(AqVAR\*(Aq,$1); \& s/^(.)//s and return($1,$1); \& } \& } \& \& sub Run { \& my($self)=shift; \& $self\->YYParse( yylex => \e&_Lexer, yyerror => \e&_Error ); \& } \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- .Ve .PP Compile it using \f(CW\*(C`eyapp\*(C'\fR: .PP .Vb 5 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& nereida:~/LEyapp/examples> eyapp Rule6.yp \& nereida:~/LEyapp/examples> ls \-ltr | tail \-1 \& \-rw\-rw\-\-\-\- 1 pl users 4976 2006\-09\-15 19:56 Rule6.pm \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- .Ve .PP Now consider this tree grammar: .PP .Vb 5 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& nereida:~/LEyapp/examples> cat Transform2.trg \& %{ \& my %Op = (PLUS=>\*(Aq+\*(Aq, MINUS => \*(Aq\-\*(Aq, TIMES=>\*(Aq*\*(Aq, DIV => \*(Aq/\*(Aq); \& %} \& \& fold: \*(AqTIMES|PLUS|DIV|MINUS\*(Aq:bin(NUM($n), NUM($m)) \& => { \& my $op = $Op{ref($bin)}; \& $n\->{attr} = eval "$n\->{attr} $op $m\->{attr}"; \& $_[0] = $NUM[0]; \& } \& zero_times_whatever: TIMES(NUM($x), .) and { $x\->{attr} == 0 } => { $_[0] = $NUM } \& whatever_times_zero: TIMES(., NUM($x)) and { $x\->{attr} == 0 } => { $_[0] = $NUM } \& \& /* rules related with times */ \& times_zero = zero_times_whatever whatever_times_zero; \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- .Ve .PP Compile it with \f(CW\*(C`treereg\*(C'\fR: .PP .Vb 5 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& nereida:~/LEyapp/examples> treereg Transform2.trg \& nereida:~/LEyapp/examples> ls \-ltr | tail \-1 \& \-rw\-rw\-\-\-\- 1 pl users 1948 2006\-09\-15 19:57 Transform2.pm \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- .Ve .PP The following program makes use of both modules \f(CW\*(C`Rule6.pm\*(C'\fR and \f(CW\*(C`Transform2.pm\*(C'\fR: .PP .Vb 8 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& nereida:~/LEyapp/examples> cat foldand0rule6_3.pl \& #!/usr/bin/perl \-w \& use strict; \& use Rule6; \& use Parse::Eyapp::YATW; \& use Data::Dumper; \& use Transform2; \& \& $Data::Dumper::Indent = 1; \& my $parser = new Rule6(); \& my $t = $parser\->Run; \& print "\en***** Before ******\en"; \& print Dumper($t); \& $t\->s(@Transform2::all); \& print "\en***** After ******\en"; \& print Dumper($t); \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- .Ve .PP When the program runs with input \f(CW\*(C`b*(2\-2)\*(C'\fR produces the following output: .PP .Vb 3 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \& nereida:~/LEyapp/examples> foldand0rule6_3.pl \& b*(2\-2) \& \& ***** Before ****** \& $VAR1 = bless( { \& \*(Aqchildren\*(Aq => [ \& bless( { \& \*(Aqchildren\*(Aq => [ \& bless( { \*(Aqchildren\*(Aq => [], \*(Aqattr\*(Aq => \*(Aqb\*(Aq, \*(Aqtoken\*(Aq => \*(AqVAR\*(Aq }, \*(AqTERMINAL\*(Aq ) \& ] \& }, \*(AqVAR\*(Aq ), \& bless( { \& \*(Aqchildren\*(Aq => [ \& bless( { \*(Aqchildren\*(Aq => [ \& bless( { \*(Aqchildren\*(Aq => [], \*(Aqattr\*(Aq => \*(Aq2\*(Aq, \*(Aqtoken\*(Aq => \*(AqNUM\*(Aq }, \*(AqTERMINAL\*(Aq ) \& ] \& }, \*(AqNUM\*(Aq ), \& bless( { \& \*(Aqchildren\*(Aq => [ \& bless( { \*(Aqchildren\*(Aq => [], \*(Aqattr\*(Aq => \*(Aq2\*(Aq, \*(Aqtoken\*(Aq => \*(AqNUM\*(Aq }, \*(AqTERMINAL\*(Aq ) \& ] \& }, \*(AqNUM\*(Aq ) \& ] \& }, \*(AqMINUS\*(Aq ) \& ] \& }, \*(AqTIMES\*(Aq ); \& \& ***** After ****** \& $VAR1 = bless( { \& \*(Aqchildren\*(Aq => [ \& bless( { \*(Aqchildren\*(Aq => [], \*(Aqattr\*(Aq => 0, \*(Aqtoken\*(Aq => \*(AqNUM\*(Aq }, \*(AqTERMINAL\*(Aq ) \& ] \& }, \*(AqNUM\*(Aq ); \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- .Ve .PP See also the section \*(L"Compiling: More Options\*(R" in Parse::Eyapp for a more contrived example. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\(bu" 4 Parse::Eyapp, .IP "\(bu" 4 eyapptut .IP "\(bu" 4 The pdf file in .IP "\(bu" 4 (Spanish), .IP "\(bu" 4 eyapp, .IP "\(bu" 4 treereg, .IP "\(bu" 4 Parse::yapp, .IP "\(bu" 4 \&\fByacc\fR\|(1), .IP "\(bu" 4 \&\fBbison\fR\|(1), .IP "\(bu" 4 the classic book \*(L"Compilers: Principles, Techniques, and Tools\*(R" by Alfred V. Aho, Ravi Sethi and .IP "\(bu" 4 Jeffrey D. Ullman (Addison-Wesley 1986) .IP "\(bu" 4 Parse::RecDescent. .SH "AUTHOR" .IX Header "AUTHOR" Casiano Rodriguez-Leon .SH "LICENSE AND COPYRIGHT" .IX Header "LICENSE AND COPYRIGHT" Copyright © 2006, 2007, 2008, 2009, 2010, 2011, 2012 Casiano Rodriguez-Leon. Copyright © 2017 William N. Braswell, Jr. All Rights Reserved. .PP Parse::Yapp is Copyright © 1998, 1999, 2000, 2001, Francois Desarmenien. Parse::Yapp is Copyright © 2017 William N. Braswell, Jr. All Rights Reserved. .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. .SH "POD ERRORS" .IX Header "POD ERRORS" Hey! \fBThe above document had some coding errors, which are explained below:\fR .IP "Around line 416:" 4 .IX Item "Around line 416:" Non-ASCII character seen before =encoding in '©'. Assuming \s-1UTF\-8\s0