.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" 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 "Parse::Yapp 3pm" .TH Parse::Yapp 3pm "2022-12-06" "perl v5.36.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" Parse::Yapp \- Perl extension for generating and using LALR parsers. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& yapp \-m MyParser grammar_file.yp \& \& ... \& \& use MyParser; \& \& $parser=new MyParser(); \& $value=$parser\->YYParse(yylex => \e&lexer_sub, yyerror => \e&error_sub); \& \& $nberr=$parser\->YYNberr(); \& \& $parser\->YYData\->{DATA}= [ \*(AqAnything\*(Aq, \*(AqYou Want\*(Aq ]; \& \& $data=$parser\->YYData\->{DATA}[0]; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Parse::Yapp (Yet Another Perl Parser compiler) is a collection of modules that let you generate and use yacc like thread safe (reentrant) parsers with perl object oriented interface. .PP The script yapp is a front-end to the Parse::Yapp module and let you easily create a Perl \s-1OO\s0 parser from an input grammar file. .SS "The Grammar file" .IX Subsection "The Grammar file" .ie n .IP """Comments""" 4 .el .IP "\f(CWComments\fR" 4 .IX Item "Comments" Through all your files, comments are either Perl style, introduced by \fI#\fR up to the end of line, or C style, enclosed between \fI/*\fR and \fI*/\fR. .ie n .IP """Tokens and string literals""" 4 .el .IP "\f(CWTokens and string literals\fR" 4 .IX Item "Tokens and string literals" Through all the grammar files, two kind of symbols may appear: \&\fINon-terminal\fR symbols, called also \fIleft-hand-side\fR symbols, which are the names of your rules, and \fITerminal\fR symbols, called also \fITokens\fR. .Sp Tokens are the symbols your lexer function will feed your parser with (see below). They are of two flavours: symbolic tokens and string literals. .Sp Non-terminals and symbolic tokens share the same identifier syntax: .Sp .Vb 1 \& [A\-Za\-z][A\-Za\-z0\-9_]* .Ve .Sp String literals are enclosed in single quotes and can contain almost anything. They will be output to your parser file double-quoted, making any special character as such. '"', '$' and '@' will be automatically quoted with '\e', making their writing more natural. On the other hand, if you need a single quote inside your literal, just quote it with '\e'. .Sp You cannot have a literal \fI'error'\fR in your grammar as it would confuse the driver with the \fIerror\fR token. Use a symbolic token instead. In case you inadvertently use it, this will produce a warning telling you you should have written it \fIerror\fR and will treat it as if it were the \&\fIerror\fR token, which is certainly \s-1NOT\s0 what you meant. .ie n .IP """Grammar file syntax""" 4 .el .IP "\f(CWGrammar file syntax\fR" 4 .IX Item "Grammar file syntax" It is very close to yacc syntax (in fact, \fIParse::Yapp\fR should compile a clean \fIyacc\fR grammar without any modification, whereas the opposite is not true). .Sp This file is divided in three sections, separated by \f(CW\*(C`%%\*(C'\fR: .Sp .Vb 5 \& header section \& %% \& rules section \& %% \& footer section .Ve .RS 4 .IP "\fBThe Header Section\fR section may optionally contain:" 4 .IX Item "The Header Section section may optionally contain:" .RS 4 .PD 0 .IP "\(bu" 4 .PD One or more code blocks enclosed inside \f(CW\*(C`%{\*(C'\fR and \f(CW\*(C`%}\*(C'\fR just like in yacc. They may contain any valid Perl code and will be copied verbatim at the very beginning of the parser module. They are not as useful as they are in yacc, but you can use them, for example, for global variable declarations, though you will notice later that such global variables can be avoided to make a reentrant parser module. .IP "\(bu" 4 Precedence declarations, introduced by \f(CW%left\fR, \f(CW%right\fR and \f(CW%nonassoc\fR specifying associativity, followed by the list of tokens or litterals having the same precedence and associativity. The precedence being the latter declared will be having the highest level. (see the yacc or bison manuals for a full explanation of how they work, as they are implemented exactly the same way in Parse::Yapp) .IP "\(bu" 4 \&\f(CW%start\fR followed by a rule's left hand side, declaring this rule to be the starting rule of your grammar. The default, when \f(CW%start\fR is not used, is the first rule in your grammar section. .IP "\(bu" 4 \&\f(CW%token\fR followed by a list of symbols, forcing them to be recognized as tokens, generating a syntax error if used in the left hand side of a rule declaration. Note that in Parse::Yapp, you \fIdon't\fR need to declare tokens as in yacc: any symbol not appearing as a left hand side of a rule is considered to be a token. Other yacc declarations or constructs such as \f(CW%type\fR and \f(CW%union\fR are parsed but (almost) ignored. .IP "\(bu" 4 \&\f(CW%expect\fR followed by a number, suppress warnings about number of Shift/Reduce conflicts when both numbers match, a la bison. .RE .RS 4 .RE .RE .RS 4 .RE .IP "\fBThe Rule Section\fR contains your grammar rules:" 4 .IX Item "The Rule Section contains your grammar rules:" A rule is made of a left-hand-side symbol, followed by a \f(CW\*(Aq:\*(Aq\fR and one or more right-hand-sides separated by \f(CW\*(Aq|\*(Aq\fR and terminated by a \f(CW\*(Aq;\*(Aq\fR: .Sp .Vb 3 \& exp: exp \*(Aq+\*(Aq exp \& | exp \*(Aq\-\*(Aq exp \& ; .Ve .Sp A right hand side may be empty: .Sp .Vb 3 \& input: #empty \& | input line \& ; .Ve .Sp (if you have more than one empty rhs, Parse::Yapp will issue a warning, as this is usually a mistake, and you will certainly have a reduce/reduce conflict) .Sp A rhs may be followed by an optional \f(CW%prec\fR directive, followed by a token, giving the rule an explicit precedence (see yacc manuals for its precise meaning) and optional semantic action code block (see below). .Sp .Vb 4 \& exp: \*(Aq\-\*(Aq exp %prec NEG { \-$_[1] } \& | exp \*(Aq+\*(Aq exp { $_[1] + $_[3] } \& | NUM \& ; .Ve .Sp Note that in Parse::Yapp, a lhs \fIcannot\fR appear more than once as a rule name (This differs from yacc). .ie n .IP """The footer section""" 4 .el .IP "\f(CWThe footer section\fR" 4 .IX Item "The footer section" may contain any valid Perl code and will be appended at the very end of your parser module. Here you can write your lexer, error report subs and anything relevant to you parser. .ie n .IP """Semantic actions""" 4 .el .IP "\f(CWSemantic actions\fR" 4 .IX Item "Semantic actions" Semantic actions are run every time a \fIreduction\fR occurs in the parsing flow and they must return a semantic value. .Sp They are (usually, but see below \f(CW\*(C`In rule actions\*(C'\fR) written at the very end of the rhs, enclosed with \f(CW\*(C`{ }\*(C'\fR, and are copied verbatim to your parser file, inside of the rules table. .Sp Be aware that matching braces in Perl is much more difficult than in C: inside strings they don't need to match. While in C it is very easy to detect the beginning of a string construct, or a single character, it is much more difficult in Perl, as there are so many ways of writing such literals. So there is no check for that today. If you need a brace in a double-quoted string, just quote it (\f(CW\*(C`\e{\*(C'\fR or \f(CW\*(C`\e}\*(C'\fR). For single-quoted strings, you will need to make a comment matching it \fIin the right order\fR. Sorry for the inconvenience. .Sp .Vb 9 \& { \& "{ My string block }". \& "\e{ My other string block \e}". \& qq/ My unmatched brace \e} /. \& # Force the match: { \& q/ for my closing brace } / \& q/ My opening brace { / \& # must be closed: } \& } .Ve .Sp All of these constructs should work. .Sp In Parse::Yapp, semantic actions are called like normal Perl sub calls, with their arguments passed in \f(CW@_\fR, and their semantic value are their return values. .Sp \&\f(CW$_\fR[1] to \f(CW$_\fR[n] are the parameters just as \f(CW$1\fR to \f(CW$n\fR in yacc, while \&\f(CW$_\fR[0] is the parser object itself. .Sp Having \f(CW$_\fR[0] being the parser object itself allows you to call parser methods. That's how the yacc macros are implemented: .Sp .Vb 4 \& yyerrok is done by calling $_[0]\->YYErrok \& YYERROR is done by calling $_[0]\->YYError \& YYACCEPT is done by calling $_[0]\->YYAccept \& YYABORT is done by calling $_[0]\->YYAbort .Ve .Sp All those methods explicitly return \fIundef\fR, for convenience. .Sp .Vb 1 \& YYRECOVERING is done by calling $_[0]\->YYRecovering .Ve .Sp Four useful methods in error recovery sub .Sp .Vb 4 \& $_[0]\->YYCurtok \& $_[0]\->YYCurval \& $_[0]\->YYExpect \& $_[0]\->YYLexer .Ve .Sp return respectively the current input token that made the parse fail, its semantic value (both can be used to modify their values too, but \&\fIknow what you are doing\fR! See \fIError reporting routine\fR section for an example), a list which contains the tokens the parser expected when the failure occurred and a reference to the lexer routine. .Sp Note that if \f(CW\*(C`$_[0]\->YYCurtok\*(C'\fR is declared as a \f(CW%nonassoc\fR token, it can be included in \f(CW\*(C`$_[0]\->YYExpect\*(C'\fR list whenever the input try to use it in an associative way. This is not a bug: the token \&\s-1IS\s0 expected to report an error if encountered. .Sp To detect such a thing in your error reporting sub, the following example should do the trick: .Sp .Vb 4 \& grep { $_[0]\->YYCurtok eq $_ } $_[0]\->YYExpect \& and do { \& #Non\-associative token used in an associative expression \& }; .Ve .Sp Accessing semantics values on the left of your reducing rule is done through the method .Sp .Vb 1 \& $_[0]\->YYSemval( index ) .Ve .Sp where index is an integer. Its value being \fI1 .. n\fR returns the same values than \fI\f(CI$_\fI[1] .. \f(CI$_\fI[n]\fR, but \fI\-n .. 0\fR returns values on the left of the rule being reduced (It is related to \fI$\-n .. \f(CI$0\fI .. \f(CI$n\fI\fR in yacc, but you cannot use \fI\f(CI$_\fI[0]\fR or \fI\f(CI$_\fI[\-n]\fR constructs in Parse::Yapp for obvious reasons) .Sp There is also a provision for a user data area in the parser object, accessed by the method: .Sp .Vb 1 \& $_[0]\->YYData .Ve .Sp which returns a reference to an anonymous hash, which let you have all of your parsing data held inside the object (see the Calc.yp or ParseYapp.yp files in the distribution for some examples). That's how you can make you parser module reentrant: all of your module states and variables are held inside the parser object. .Sp Note: unfortunately, method calls in Perl have a lot of overhead, and when YYData is used, it may be called a huge number of times. If you are not a \fIreal\fR purist, and efficiency is your concern, you may access directly the user-space in the object: \f(CW$parser\fR\->{\s-1USER\s0} which is a reference to an anonymous hash array, and then benchmark. .Sp If no action is specified for a rule, the equivalant of a default action is run, which returns the first parameter: .Sp .Vb 1 \& { $_[1] } .Ve .ie n .IP """In rule actions""" 4 .el .IP "\f(CWIn rule actions\fR" 4 .IX Item "In rule actions" It is also possible to embed semantic actions inside of a rule: .Sp .Vb 1 \& typedef: TYPE { $type = $_[1] } identlist { ... } ; .Ve .Sp When the Parse::Yapp's parser encounter such an embedded action, it modifies the grammar as if you wrote (although \f(CW@x\fR\-1 is not a legal lhs value): .Sp .Vb 2 \& @x\-1: /* empty */ { $type = $_[1] }; \& typedef: TYPE @x\-1 identlist { ... } ; .Ve .Sp where \fIx\fR is a sequential number incremented for each \*(L"in rule\*(R" action, and \fI\-1\fR represents the \*(L"dot position\*(R" in the rule where the action arises. .Sp In such actions, you can use \fI\f(CI$_\fI[1]..$_[n]\fR variables, which are the semantic values on the left of your action. .Sp Be aware that the way Parse::Yapp modifies your grammar because of \&\fIin-rule actions\fR can produce, in some cases, spurious conflicts that wouldn't happen otherwise. .ie n .IP """Generating the Parser Module""" 4 .el .IP "\f(CWGenerating the Parser Module\fR" 4 .IX Item "Generating the Parser Module" Now that you grammar file is written, you can use yapp on it to generate your parser module: .Sp .Vb 1 \& yapp \-v Calc.yp .Ve .Sp will create two files \fICalc.pm\fR, your parser module, and \fICalc.output\fR a verbose output of your parser rules, conflicts, warnings, states and summary. .Sp What your are missing now is a lexer routine. .ie n .IP """The Lexer sub""" 4 .el .IP "\f(CWThe Lexer sub\fR" 4 .IX Item "The Lexer sub" is called each time the parser need to read the next token. .Sp It is called with only one argument that is the parser object itself, so you can access its methods, specially the .Sp .Vb 1 \& $_[0]\->YYData .Ve .Sp data area. .Sp It is its duty to return the next token and value to the parser. They \f(CW\*(C`must\*(C'\fR be returned as a list of two variables, the first one is the token known by the parser (symbolic or literal), the second one being anything you want (usually the content of the token, or the literal value) from a simple scalar value to any complex reference, as the parsing driver only uses it to call semantic actions: .Sp .Vb 5 \& ( \*(AqNUMBER\*(Aq, $num ) \&or \& ( \*(Aq>=\*(Aq, \*(Aq>=\*(Aq ) \&or \& ( \*(AqARRAY\*(Aq, [ @values ] ) .Ve .Sp When the lexer reach the end of input, it must return the \f(CW\*(Aq\*(Aq\fR empty token with an undef value: .Sp .Vb 1 \& ( \*(Aq\*(Aq, undef ) .Ve .Sp Note that your lexer should \fInever\fR return \f(CW\*(Aqerror\*(Aq\fR as token value: for the driver, this is the error token used for error recovery and would lead to odd reactions. .Sp Now that you have your lexer written, maybe you will need to output meaningful error messages, instead of the default which is to print \&'Parse error.' on \s-1STDERR.\s0 .Sp So you will need an Error reporting sub. .ie n .IP """Error reporting routine""" 4 .el .IP "\f(CWError reporting routine\fR" 4 .IX Item "Error reporting routine" If you want one, write it knowing that it is passed as parameter the parser object. So you can share information with the lexer routine quite easily. .Sp You can also use the \f(CW\*(C`$_[0]\->YYErrok\*(C'\fR method in it, which will resume parsing as if no error occurred. Of course, since the invalid token is still invalid, you're supposed to fix the problem by yourself. .Sp The method \f(CW\*(C`$_[0]\->YYLexer\*(C'\fR may help you, as it returns a reference to the lexer routine, and can be called as .Sp .Vb 1 \& ($tok,$val)=&{$_[0]\->Lexer} .Ve .Sp to get the next token and semantic value from the input stream. To make them current for the parser, use: .Sp .Vb 1 \& ($_[0]\->YYCurtok, $_[0]\->YYCurval) = ($tok, $val) .Ve .Sp and know what you're doing... .ie n .IP """Parsing""" 4 .el .IP "\f(CWParsing\fR" 4 .IX Item "Parsing" Now you've got everything to do the parsing. .Sp First, use the parser module: .Sp .Vb 1 \& use Calc; .Ve .Sp Then create the parser object: .Sp .Vb 1 \& $parser=new Calc; .Ve .Sp Now, call the YYParse method, telling it where to find the lexer and error report subs: .Sp .Vb 2 \& $result=$parser\->YYParse(yylex => \e&Lexer, \& yyerror => \e&ErrorReport); .Ve .Sp (assuming Lexer and ErrorReport subs have been written in your current package) .Sp The order in which parameters appear is unimportant. .Sp Et voila. .Sp The YYParse method will do the parse, then return the last semantic value returned, or \f(CW\*(C`undef\*(C'\fR if error recovery cannot recover. .Sp If you need to be sure the parse has been successful (in case your last returned semantic value \fIis\fR \f(CW\*(C`undef\*(C'\fR), make a call to: .Sp .Vb 1 \& $parser\->YYNberr() .Ve .Sp which returns the total number of time the error reporting sub has been called. .ie n .IP """Error Recovery""" 4 .el .IP "\f(CWError Recovery\fR" 4 .IX Item "Error Recovery" in Parse::Yapp is implemented the same way it is in yacc. .ie n .IP """Debugging Parser""" 4 .el .IP "\f(CWDebugging Parser\fR" 4 .IX Item "Debugging Parser" To debug your parser, you can call the YYParse method with a debug parameter: .Sp .Vb 1 \& $parser\->YYParse( ... , yydebug => value, ... ) .Ve .Sp where value is a bitfield, each bit representing a specific debug output: .Sp .Vb 6 \& Bit Value Outputs \& 0x01 Token reading (useful for Lexer debugging) \& 0x02 States information \& 0x04 Driver actions (shifts, reduces, accept...) \& 0x08 Parse Stack dump \& 0x10 Error Recovery tracing .Ve .Sp To have a full debugging output, use .Sp .Vb 1 \& debug => 0x1F .Ve .Sp Debugging output is sent to \s-1STDERR,\s0 and be aware that it can produce \&\f(CW\*(C`huge\*(C'\fR outputs. .ie n .IP """Standalone Parsers""" 4 .el .IP "\f(CWStandalone Parsers\fR" 4 .IX Item "Standalone Parsers" By default, the parser modules generated will need the Parse::Yapp module installed on the system to run. They use the Parse::Yapp::Driver which can be safely shared between parsers in the same script. .Sp In the case you'd prefer to have a standalone module generated, use the \f(CW\*(C`\-s\*(C'\fR switch with yapp: this will automagically copy the driver code into your module so you can use/distribute it without the need of the Parse::Yapp module, making it really a \f(CW\*(C`Standalone Parser\*(C'\fR. .Sp If you do so, please remember to include Parse::Yapp's copyright notice in your main module copyright, so others can know about Parse::Yapp module. .ie n .IP """Source file line numbers""" 4 .el .IP "\f(CWSource file line numbers\fR" 4 .IX Item "Source file line numbers" by default will be included in the generated parser module, which will help to find the guilty line in your source file in case of a syntax error. You can disable this feature by compiling your grammar with yapp using the \f(CW\*(C`\-n\*(C'\fR switch. .SH "BUGS AND SUGGESTIONS" .IX Header "BUGS AND SUGGESTIONS" If you find bugs, think of anything that could improve Parse::Yapp or have any questions related to it, feel free to contact the author. .SH "AUTHOR" .IX Header "AUTHOR" William N. Braswell, Jr. (Remove \*(L"\s-1NOSPAM\*(R".\s0) .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fByapp\fR\|(1) \fBperl\fR\|(1) \fByacc\fR\|(1) \fBbison\fR\|(1). .SH "COPYRIGHT" .IX Header "COPYRIGHT" The Parse::Yapp module and its related modules and shell scripts are copyright: Copyright © 1998, 1999, 2000, 2001, Francois Desarmenien. Copyright © 2017 William N. Braswell, Jr. .PP You may use and distribute them under the terms of either the \s-1GNU\s0 General Public License or the Artistic License, as specified in the Perl \s-1README\s0 file. .PP If you use the \*(L"standalone parser\*(R" option so people don't need to install Parse::Yapp on their systems in order to run you software, this copyright noticed should be included in your software copyright too, and the copyright notice in the embedded driver should be left untouched.