Gysin sequence: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>ChrisGualtieri
m General Fixes using AWB
en>AnomieBOT
m Dating maintenance tags: {{Mergefrom}}
 
Line 1: Line 1:
{{Blacklisted-links|1=
Wilber Berryhill is the name his mothers and fathers gave him and he completely digs that title. What me and my family members love is bungee jumping but I've been taking on new things recently. Distributing production is how he makes a living. Mississippi is exactly where her house is but her husband wants them to move.<br><br>Also visit my weblog - [http://help.ksu.edu.sa/node/65129 accurate psychic predictions]
*http://www.programmersheaven.com/2/Perl6-FAQ-Regex
*:''Triggered by <code>\bprogrammersheaven\.com\b</code> on the global blacklist''|bot=Cyberbot II}}
'''Perl 6 rules''' are the [[regular expression]], [[pattern matching]] and general-purpose [[parsing]] facility of [[Perl 6]], and are a core part of the language. Since Perl's pattern-matching constructs have exceeded the capabilities of [[Formal language|formal]] regular expressions for some time, Perl 6 documentation refers to them exclusively as ''regexes'', distancing the term from the formal definition.
 
Perl 6 provides a superset of [[Perl|Perl 5]] features with respect to regexes, folding them into a larger framework called ''rules'', which provide the capabilities of a [[parsing expression grammar]], as well as acting as a [[Closure (computer science)|closure]] with respect to their lexical scope.<ref>{{cite web | url=http://dev.perl.org/perl6/doc/design/syn/S05.html | title=Synopsis 5: Regexes and Rules | author=Wall, Larry | date=June 24, 2002 }}</ref> Rules are introduced with the <code>rule</code> keyword, which has a usage quite similar to subroutine definition. Anonymous rules can be introduced with the <code>regex</code> (or <code>rx</code>) keyword, or simply be used inline as regexps were in Perl 5 via the <code>m</code> (matching) or <code>s</code> (substitution) operators.
 
==History==
In ''Apocalypse 5'', a document outlining the preliminary design decisions for Perl 6 pattern matching, Larry Wall enumerated 20 problems with "current regex culture". Among these were that Perl's regexes were "too compact and 'cute'", had "too much reliance on too few metacharacters", "little support for named captures", "little support for grammars", and "poor integration with [the] 'real' language".<ref>{{cite web | url=http://dev.perl.org/perl6/doc/design/apo/A05.html | title=Apocalypse 5: Pattern Matching | author=Wall, Larry | date=June 4, 2002 }}</ref>
 
Between late 2004 and mid-2005, a compiler for Perl 6 style rules was developed for the [[Parrot virtual machine]] called Parrot Grammar Engine (PGE), which was later renamed to the more generic, [[Parser Grammar Engine]]. PGE is a combination of runtime and compiler for Perl 6 style grammars that allows any parrot-based compiler to use these tools for parsing, and also to provide rules to their runtimes.
 
Among other Perl 6 features, support for named captures was added to Perl 5.10 in 2007.<ref>[http://perlbuzz.com/2007/12/perl-510-now-available.html Perl 5.10 now available - Perl Buzz<!-- Bot generated title -->]</ref>
 
In May 2012, the reference implementation of Perl 6, Rakudo, shipped its Rakudo Star monthly snapshot with a working [[JSON]] parser built entirely in Perl 6 rules.<ref>{{cite web | url=http://rakudo.org/2012/05/23/rakudo-star-2012-05-released/ | title= Rakudo Star 2012.05 released | author=moritz | date=May 5, 2012}}</ref>
 
==Changes from Perl 5==
 
There are only six unchanged features from Perl 5's regexes:
* Literals: word characters (letters, numbers and [[underscore]]) matched literally
* Capturing: <code>(...)</code>
* Alternatives: <code>|</code>
* Backslash escape: <code>\</code>
* Repetition quantifiers: <code>*</code>, <code>+</code>, and <code>?</code>, but not <code>{m,n}</code>
* Minimal matching suffix: <code>*?</code>, <code>+?</code>, <code>??</code>
 
A few of the most powerful additions include:
* The ability to reference rules using <code><rulename></code> to build up entire grammars.
* A handful of commit operators that allow the programmer to control [[backtracking]] during matching.
 
The following changes greatly improve the readability of regexes:
* Simplified non-capturing groups: <code>[...]</code>, which are the same as Perl 5's: <code>(?:...)</code>
* Simplified code assertions: <code><?{...}></code>
* Extended regex formatting (Perl 5's <code>/x</code>) is now the default.
 
===Implicit changes===
Some of the features of Perl 5 regular expressions become more powerful in Perl 6 because of their ability to encapsulate the expanded features of Perl 6 rules. For example, in Perl 5, there were positive and negative lookahead operators <code>(?=...)</code> and <code>(?!...)</code>. In Perl 6 these same features exist, but are called <code><nowiki><before ...></nowiki></code> and <code><nowiki><!before ...></nowiki></code>.
 
However, because <code>before</code> can encapsulate arbitrary rules, it can be used to express lookahead as a [[syntactic predicate]] for a grammar. For example, the following [[parsing expression grammar]] describes the classic [[context-sensitive grammar|non-context-free]] language <math> \{ a^n b^n c^n : n \ge 1 \} </math>:
 
S ← &(A !b) a+ B
A ← a A? b
B ← b B? c
 
In Perl 6 rules that would be:
 
<source lang="Perl6">
rule S { <before <A> <!before b>> a+ <B> }
rule A { a <A>? b }
rule B { b <B>? c }
</source>
 
Of course, given the ability to mix rules and regular code, that can be simplified even further:
<source lang="Perl6">
rule S { (a+) (b+) (c+) <{$0.elems == $1.elems == $2.elems}> }
</source>
However, this makes use of [[Assertion (computing)|assertions]], which is a subtly different concept in Perl 6 rules but more substantially different in parsing theory, making this a semantic rather than syntactic predicate. The most important difference in practice is performance. There is no way for the rule engine to know what conditions the assertion may match, so no optimization of this process can be made.
 
==Integration with Perl==
In many languages, regular expressions are entered as strings, which are then passed to library routines that parse and compile them into an internal state. In Perl 5, regular expressions shared some of the [[lexical analysis]] with Perl's scanner. This simplified many aspects of regular expression usage, though it added a great deal of complexity to the scanner. In Perl 6, rules are part of the grammar of the language. No separate parser exists for rules, as it did in Perl 5. This means that code, embedded in rules, is parsed at the same time as the rule itself and its surrounding code. For example, it is possible to nest rules and code without re-invoking the parser:
 
<source lang="Perl">
rule ab {
  (a.) # match "a" followed by any character
  # Then check to see if that character was "b"
  # If so, print a message.
  { $0 ~~ /b {say "found the b"}/ }
}
</source>
 
The above is a single block of Perl 6 code that contains an outer rule definition, an inner block of assertion code, and inside of that a regex that contains one more level of assertion.
 
== Implementation ==
 
=== Keywords ===
There are several keywords used in conjunction with Perl 6 rules:
 
;regex: A named or anonymous regex that ignores whitespace within the regex by default.
;token: A named or anonymous regex that implies the <code>:ratchet</code> modifier.
;rule:  A named or anonymous regex that implies the <code>:ratchet</code> and <code>:sigspace</code> modifiers.
;rx:    An anonymous regex that takes arbitrary delimiters such as <code>//</code> where regex only takes braces.
;m:    An operator form of anonymous regex that performs matches with arbitrary delimiters.
;mm:    Shorthand for m with the <code>:sigspace</code> modifier.
;s:    An operator form of anonymous regex that performs substitution with arbitrary delimiters.
;ss:    Shorthand for s with the <code>:sigspace</code> modifier.
;<code>/.../</code>: Simply placing a regex between slashes is shorthand for <code>m/.../</code>.
 
Here is an example of typical use:
<source lang=perl6>
token word { \w+ }
rule phrase { <word> [ \, <word> ]* \. }
if $string ~~ / <phrase> \n / {
  ...
}
</source>
=== Modifiers ===
Modifiers may be placed after any of the regex keywords, and before the delimiter. If a regex is named, the modifier comes after the name. Modifiers control the way regexes are parsed and how they behave. They are always introduced with a leading <code>:</code> character.
 
Some of the more important modifiers include:
 
* <code>:i</code> or <code>:ignorecase</code> &ndash; Perform matching without respect to case.
* <code>:g</code> or <code>:global</code> &ndash; Perform the match more than once on a given target string.
* <code>:s</code> or <code>:sigspace</code> &ndash; Replace whitespace in the regex with a whitespace-matching rule, rather than simply ignoring it.
* <code>:Perl5</code> &ndash; Treat the regex as a Perl 5 regular expression.
* <code>:ratchet</code> &ndash; Never perform backtracking in the rule.
 
For example:
<source lang="Perl6">
regex addition :ratchet :sigspace { <term> \+ <expr> }
</source>
=== Grammars ===
A grammar may be defined using the <code>grammar</code> operator. A grammar is essentially just a [[namespace]] for rules:
<source lang=perl6>
grammar Str::SprintfFormat {
  regex format_token { \%: <index>? <precision>? <modifier>? <directive> }
  token index { \d+ \$ }
  token precision { <flags>? <vector>? <precision_count> }
  token flags { <[\ +0\#\-]>+ }
  token precision_count { [ <[1-9]>\d* | \* ]? [ \. [ \d* | \* ] ]? }
  token vector { \*? v }
  token modifier { ll | <[lhmVqL]> }
  token directive { <[\%csduoxefgXEGbpniDUOF]> }
}
</source>
This is the grammar used to define Perl's <code>[[sprintf]]</code> string formatting notation.
 
Outside of this namespace, you could use these rules like so:
<source lang=perl6>
if / <Str::SprintfFormat::format_token> / { ... }
</source>
A rule used in this way is actually identical to the invocation of a subroutine with the extra semantics and side-effects of pattern matching (e.g., rule invocations can be backtracked).
 
==Examples==
Here are some example rules in Perl 6:
 
<source lang="Perl6">
rx { a [ b | c ] ( d | e ) f : g }
rx { ( ab* ) <{ $1.size % 2 == 0 }> }
</source>
 
That last is identical to:
 
<source lang="Perl6">
rx { ( ab[bb]* ) }
</source>
 
==References==
<references/>
 
==External links==
* [http://perlcabal.org/syn/S05.html Synopsis 05] - The standards document covering Perl 6 regexes and rules.
* [http://www.programmersheaven.com/2/Perl6-FAQ-Regex Perl 6 Regex FAQ] - Answers a range of questions about Perl 6 regexes.
* [http://github.com/perlpilot/perl6-docs/blob/master/intro/p6-regex-intro.pod Perl 6 Regex Introduction] - Gentle introduction to Perl 6 regexes.
 
{{Perl}}
 
{{DEFAULTSORT:Perl 6 Rules}}
[[Category:Perl 6]]
[[Category:Regular expressions]]

Latest revision as of 08:09, 27 December 2014

Wilber Berryhill is the name his mothers and fathers gave him and he completely digs that title. What me and my family members love is bungee jumping but I've been taking on new things recently. Distributing production is how he makes a living. Mississippi is exactly where her house is but her husband wants them to move.

Also visit my weblog - accurate psychic predictions