Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Regular expressions are great - for <a href="http://en.wikipedia.org/wiki/Regular_language" rel="nofollow">regular languages</a>. However, most programming languages are not regular. Everything that has some sort of braces and recursion is a context free language, or even context dependend. (If these CS terms confuse you, look them up on Wikipedia. They are useful).</p> <p>Especially C has a very complex grammar.</p> <p>However, Perl's Regexes are not restricted to Regular Expressions. We can express context free grammars, i.e define a set of rules that the string must match. In each rule, we can reference other rules. Because of this, we can do recursion, and things like matching nested parens:</p> <pre><code>m{ ^ (?&amp;NESTED_PAREN) $ (?(DEFINE) (?&lt;NESTED_PAREN&gt; [(] (?: [^()]+ | (?&amp;NESTED_PAREN) )* [)] ) ) }x; </code></pre> <p>This regex defines a top rule: The whole string from beginning to end has to be a nested paren. Then follows a <code>DEFINE</code> block. We define one rule <code>NESTED_PAREN</code>, that starts with a <code>(</code> and can contain any number of non-paren characters and nested parens. It is followed by a <code>)</code>. It has to be taken into account that it is easy to write an infinitely recursing grammar, but luckily each recursion will consume characters or fail in this example.</p> <p>For a nicer interface to write grammars in Perl, I highly recommend <a href="http://search.cpan.org/~dconway/Regexp-Grammars-1.021/lib/Regexp/Grammars.pm" rel="nofollow">Regexp::Grammars from CPAN</a>.</p> <p>Now we know how to write grammars in Perl and can create one for your function declarations. Here is a symbolic notation without whitespaces:</p> <pre><code>FUNCTION ::= TYPE VECTOR? NAME '(' ARGUMENTS ')' VECTOR ::= '&lt;' vector '*'? '&gt;' ARGUMENTS::= ( ARGUMENT (',' ARGUMENT)* )? ARGUMENT ::= TYPE VECTOR? NAME </code></pre> <p>You may have noticed that we can re-use some of the rules for the function inside the argument list. Now you just have to map this grammar to a set of <code>(DEFINE)</code> rules, write the top-level rule and you are ready to go. Again, using Regexp::Grammars will make this job much more easy, but it provides another language you will have to learn.</p> <p>See <a href="http://perldoc.perl.org/perlre.html#Extended-Patterns" rel="nofollow">perldoc perlre</a> for the ultimate reference of built-in featurs in Perl regexes.</p> <p>Please note that, (because of the preprocessor, among other things), the C (and C++) syntax is neither regular nor context-free. Everything short of executing the preprocessor will end up being a nice try…</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload