recdescent.pod

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· POD 代码 · 共 1,783 行 · 第 1/5 页

POD
1,783
字号
=head1 NAMEParse::RecDescent - Generate Recursive-Descent Parsers=head1 VERSIONThis document describes version 1.94 of Parse::RecDescent,released April  9, 2003.=head1 SYNOPSIS use Parse::RecDescent; # Generate a parser from the specification in $grammar:	 $parser = new Parse::RecDescent ($grammar); # Generate a parser from the specification in $othergrammar	 $anotherparser = new Parse::RecDescent ($othergrammar); # Parse $text using rule 'startrule' (which must be # defined in $grammar):	$parser->startrule($text); # Parse $text using rule 'otherrule' (which must also # be defined in $grammar):	 $parser->otherrule($text); # Change the universal token prefix pattern # (the default is: '\s*'):	$Parse::RecDescent::skip = '[ \t]+'; # Replace productions of existing rules (or create new ones) # with the productions defined in $newgrammar:	$parser->Replace($newgrammar); # Extend existing rules (or create new ones) # by adding extra productions defined in $moregrammar:	$parser->Extend($moregrammar); # Global flags (useful as command line arguments under -s):	$::RD_ERRORS	   # unless undefined, report fatal errors	$::RD_WARN	   # unless undefined, also report non-fatal problems	$::RD_HINT	   # if defined, also suggestion remedies	$::RD_TRACE	   # if defined, also trace parsers' behaviour	$::RD_AUTOSTUB	   # if defined, generates "stubs" for undefined rules	$::RD_AUTOACTION   # if defined, appends specified action to productions=head1 DESCRIPTION=head2 OverviewParse::RecDescent incrementally generates top-down recursive-descent textparsers from simple I<yacc>-like grammar specifications. It provides:=over 4=item *Regular expressions or literal strings as terminals (tokens),=item *Multiple (non-contiguous) productions for any rule,=item *Repeated and optional subrules within productions,=item *Full access to Perl within actions specified as part of the grammar,=item *Simple automated error reporting during parser generation and parsing,=item *The ability to commit to, uncommit to, or reject particularproductions during a parse,=item *The ability to pass data up and down the parse tree ("down" via subruleargument lists, "up" via subrule return values)=item *Incremental extension of the parsing grammar (even during a parse),=item *Precompilation of parser objects,=item *User-definable reduce-reduce conflict resolution via"scoring" of matching productions.=back=head2 Using C<Parse::RecDescent>Parser objects are created by calling C<Parse::RecDescent::new>, passing in agrammar specification (see the following subsections). If the grammar iscorrect, C<new> returns a blessed reference which can then be used to initiateparsing through any rule specified in the original grammar. A typical sequencelooks like this:	$grammar = q {			# GRAMMAR SPECIFICATION HERE		     };	$parser = new Parse::RecDescent ($grammar) or die "Bad grammar!\n";	# acquire $text	defined $parser->startrule($text) or print "Bad text!\n";The rule through which parsing is initiated must be explicitly definedin the grammar (i.e. for the above example, the grammar must include arule of the form: "startrule: <subrules>".If the starting rule succeeds, its value (see below)is returned. Failure to generate the original parser or failure to match a textis indicated by returning C<undef>. Note that it's easy to set up grammarsthat can succeed, but which return a value of 0, "0", or "".  So don't betempted to write:	$parser->startrule($text) or print "Bad text!\n";Normally, the parser has no effect on the original text. So in theprevious example the value of $text would be unchanged after havingbeen parsed.If, however, the text to be matched is passed by reference:	$parser->startrule(\$text)then any text which was consumed during the match will be removed from thestart of $text.=head2 RulesIn the grammar from which the parser is built, rules are specified bygiving an identifier (which must satisfy /[A-Za-z]\w*/), followed by acolon I<on the same line>, followed by one or more productions,separated by single vertical bars. The layout of the productionsis entirely free-format:	rule1:	production1	     |  production2 |		production3 | production4At any point in the grammar previously defined rules may be extended withadditional productions. This is achieved by redeclaring the rule with the newproductions. Thus:	rule1: a | b | c	rule2: d | e | f	rule1: g | his exactly equivalent to:	rule1: a | b | c | g | h	rule2: d | e | fEach production in a rule consists of zero or more items, each of which may be either: the name of another rule to be matched (a "subrule"),a pattern or string literal to be matched directly (a "token"), ablock of Perl code to be executed (an "action"), a special instructionto the parser (a "directive"), or a standard Perl comment (which isignored).A rule matches a text if one of its productions matches. A productionmatches if each of its items match consecutive substrings of thetext. The productions of a rule being matched are tried in the sameorder that they appear in the original grammar, and the first matchingproduction terminates the match attempt (successfully). If allproductions are tried and none matches, the match attempt fails.Note that this behaviour is quite different from the "prefer the longer match"behaviour of I<yacc>. For example, if I<yacc> were parsing the rule:	seq : 'A' 'B'	    | 'A' 'B' 'C'upon matching "AB" it would look ahead to see if a 'C' is next and, ifso, will match the second production in preference to the first. Inother words, I<yacc> effectively tries all the productions of a rulebreadth-first in parallel, and selects the "best" match, where "best"means longest (note that this is a gross simplification of the truebehaviour of I<yacc> but it will do for our purposes).In contrast, C<Parse::RecDescent> tries each production depth-first insequence, and selects the "best" match, where "best" means first. This isthe fundamental difference between "bottom-up" and "recursive descent"parsing.Each successfully matched item in a production is assigned a value,which can be accessed in subsequent actions within the sameproduction (or, in some cases, as the return value of a successfulsubrule call). Unsuccessful items don't have an associated value,since the failure of an item causes the entire surrounding productionto immediately fail. The following sections describe the various typesof items and their success values.=head2 SubrulesA subrule which appears in a production is an instruction to the parser toattempt to match the named rule at that point in the text beingparsed. If the named subrule is not defined when requested theproduction containing it immediately fails (unless it was "autostubbed" - seeL<Autostubbing>).A rule may (recursively) call itself as a subrule, but I<not> as theleft-most item in any of its productions (since such recursions are usuallynon-terminating).The value associated with a subrule is the value associated with itsC<$return> variable (see L<"Actions"> below), or with the last successfullymatched item in the subrule match.Subrules may also be specified with a trailing repetition specifier,indicating that they are to be (greedily) matched the specified numberof times. The available specifiers are:		subrule(?)	# Match one-or-zero times		subrule(s)	# Match one-or-more times		subrule(s?)	# Match zero-or-more times		subrule(N)	# Match exactly N times for integer N > 0		subrule(N..M)	# Match between N and M times		subrule(..M)	# Match between 1 and M times		subrule(N..)	# Match at least N timesRepeated subrules keep matching until either the subrule fails tomatch, or it has matched the minimal number of times but fails toconsume any of the parsed text (this second condition prevents thesubrule matching forever in some cases).Since a repeated subrule may match many instances of the subrule itself, thevalue associated with it is not a simple scalar, but rather a reference to alist of scalars, each of which is the value associated with one of theindividual subrule matches. In other words in the rule:		program: statement(s)the value associated with the repeated subrule "statement(s)" is a referenceto an array containing the values matched by each call to the individualsubrule "statement".Repetition modifieres may include a separator pattern:		program: statement(s /;/)specifying some sequence of characters to be skipped between each repetition.This is really just a shorthand for the E<lt>leftop:...E<gt> directive(see below).=head2 TokensIf a quote-delimited string or a Perl regex appears in a production,the parser attempts to match that string or pattern at that point inthe text. For example:		typedef: "typedef" typename identifier ';'		identifier: /[A-Za-z_][A-Za-z0-9_]*/As in regular Perl, a single quoted string is uninterpolated, whilsta double-quoted string or a pattern is interpolated (at the timeof matching, I<not> when the parser is constructed). Hence, it ispossible to define rules in which tokens can be set at run-time:		typedef: "$::typedefkeyword" typename identifier ';'		identifier: /$::identpat/Note that, since each rule is implemented inside a special namespacebelonging to its parser, it is necessary to explicitly quantifyvariables from the main package.Regex tokens can be specified using just slashes as delimitersor with the explicit C<mE<lt>delimiterE<gt>......E<lt>delimiterE<gt>> syntax:		typedef: "typedef" typename identifier ';'		typename: /[A-Za-z_][A-Za-z0-9_]*/		identifier: m{[A-Za-z_][A-Za-z0-9_]*}A regex of either type can also have any valid trailing parameter(s)(that is, any of [cgimsox]):		typedef: "typedef" typename identifier ';'		identifier: / [a-z_] 		# LEADING ALPHA OR UNDERSCORE			      [a-z0-9_]*	# THEN DIGITS ALSO ALLOWED			    /ix			# CASE/SPACE/COMMENT INSENSITIVEThe value associated with any successfully matched token is a stringcontaining the actual text which was matched by the token.It is important to remember that, since each grammar is specified in aPerl string, all instances of the universal escape character '\' withina grammar must be "doubled", so that they interpolate to single '\'s whenthe string is compiled. For example, to use the grammar:		word:	    /\S+/ | backslash		line:	    prefix word(s) "\n"		backslash:  '\\'the following code is required:		$parser = new Parse::RecDescent (q{			word:	    /\\S+/ | backslash			line:	    prefix word(s) "\\n"			backslash:  '\\\\'		});=head2 Terminal SeparatorsFor the purpose of matching, each terminal in a production is consideredto be preceded by a "prefix" - a pattern which must bematched before a token match is attempted. By default, the prefix is optional whitespace (which always matches, atleast trivially), but this default may be reset in any production.The variable C<$Parse::RecDescent::skip> stores the universalprefix, which is the default for all terminal matches in all parsersbuilt with C<Parse::RecDescent>.The prefix for an individual production can be alteredby using the C<E<lt>skip:...E<gt>> directive (see below).=head2 Actions

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?