Hi--

I'm working on a grammar to parse SQL, and here's some of what I've so far...

my $grammar = q { select : /SELECT/i select_list /FROM/i table_source select_list : column(s /,/) column : identifier /=/ col_name | col_name /AS/i alias | col_name alias(?) | string | number table_source : table alias(?) with_hint(?) table_join(s?) | table alias(?) table_join(s) | table alias(?) /,/ table_list | table alias(?) yada...yada...

And something simple to parse:

select colA, colB, colC from customer x

The problem I'm having is that when RecDescent gets to colC, it matches "from" as an **alias** instead of being a token on the **start** production. ColA and ColB work fine, as the comma effective terminates a column. If I add a column alias to ColC, everything works fine. So it seems that "from" is sent down to select_list->column and matching there...and not where I had intended.

Any ideas as to how I can fix this? Do I explicitly look for "col_name /from/i" in the column production and then reject it if it matches (and if so what would that syntax be?), or should my grammar structured differently?

Let me know if you want the entire script and not just the snippet I've posted.

Thanks!

In reply to Parse::RecDescent greedy matches by zuma53

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.