I've been playing with a little grammar here to parse SQL "CREATE TABLE" syntax:
my $grammar = q{ startrule: create_table_syntax create_table_syntax: 'CREATE TABLE' '(' column_definition(s /,/) + ')' column_definition: column_name data_type column_attributes(?) column_name: /\w+/ data_type: "INT" | "VARCHAR" column_attributes: key_or_not_null key_or_not_null: primary(?) 'KEY' }
At the moment I'm experimenting with AUTOSTUB to get the key_or_not_null bit working. In BNF it looks like:
<key_or_not_null_spec> ::=   [PRIMARY] KEY | NOT NULL [WITH DEFAULT]
As I understand it, the square braces around PRIMARY indicate that it can be matched "zero or one" times. So I tried "primary"(?) in PRD grammar and that bombed. Now I'm trying primary(?) with AUTOSTUB turned on and that's bombing too. I really don't want to have to make a subrule that just says:
primary: 'primary'
But I don't really like the idea of using AUTOSTUB either - it seems kludgy. Ideally I want to be able to say "match this literal string zero or one times". Should I use a regex? Or is there some PRD grammar wisdom that I'm missing?

--
Ash OS durbatulk, ash OS gimbatul,
Ash OS thrakatulk, agh burzum-ishi krimpatul!
Uzg-Microsoft-ishi amal fauthut burguuli.

In reply to Parse::RecDescent and AUTOSTUB by hagus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.