Parsing issue 
Author Message
 Parsing issue

Hello,

I'm developing an application for selecting items from a bibliographic
database. For several reasons we do not let the user work directly
with SQL, but via a simpler language. The queries will then be
translated into SQL. E.g. the users enters:
medium='Book' or medium='Paper'. This must be translated to
select * from Items where medium = 'Book' or medium='Paper';
More complex queries are also possible.
I have the following questions.

I want to use Parse::RecDescent for this job. As I'm absolutely new to
parsing and formal languages: do you need to traverse the parse tree
to construct valid SQL? I do not quite understand the data structure
generated by the <autotree> directive. Are there ways to make it more
explicit?

Are there other ways to build the application or is my decision to use
RecDescent the right one?

Below you will find the script I build so far.

Thanks for any help.

Peter van der Kamp
----------------------------------------------------------------
use Parse::RecDescent;
use Data::Dumper;

my $parser = new Parse::RecDescent q{
   <autotree>

   zoekVraag:
      andZoekVraag 'or' zoekVraag
      |
      andZoekVraag
      |
      <error>

   andZoekVraag:
      notZoekVraag 'and' andZoekVraag
      |
      notZoekVraag

   notZoekVraag:
      'not' haakZoekVraag
      |
      haakZoekVraag

   haakZoekVraag:
      '(' zoekVraag ')'
      |
      basisZoekVraag

   basisZoekVraag:
      mediumZoekVraag
      |
      onderwerpZoekVraag
      |
      titelZoekVraag
      |
      auteurZoekVraag
      |
      periodZoekVraag

   mediumZoekVraag:
      'medium=' zoekString

   onderwerpZoekVraag:
      'topic=' zoekString

   titelZoekVraag:
      'title=' zoekString

   auteurZoekVraag:
      'author=' zoekString

   periodZoekVraag:
      rangePeriodZoekVraag
      |
      singlePeriodZoekVraag

   singlePeriodZoekVraag:
      'p=' cijfers

   rangePeriodZoekVraag:
      'p=' cijfers '-' cijfers
      |
      'p=' '-' cijfers
      |
      'p=' cijfers '-'

   zoekString: /\'[a-zA-Z:\. ]+\'/
   cijfers: /[0-9]{4}/

Quote:
};

undef $/;
my $text = <STDIN>;

print Data::Dumper->Dump([$parser->zoekVraag($text)]);



Mon, 21 Nov 2005 13:02:00 GMT  
 Parsing issue

Quote:

> I'm developing an application for selecting items from a
> bibliographic database. For several reasons we do not let the user
> work directly with SQL, but via a simpler language. The queries will
> then be translated into SQL. E.g. the users enters: medium='Book' or
> medium='Paper'. This must be translated to select * from Items where
> medium = 'Book' or medium='Paper'; More complex queries are also
> possible.  I have the following questions.

> I want to use Parse::RecDescent for this job. As I'm absolutely new
> to parsing and formal languages: do you need to traverse the parse
> tree to construct valid SQL? I do not quite understand the data
> structure generated by the <autotree> directive. Are there ways to
> make it more explicit?

> Are there other ways to build the application or is my decision to
> use RecDescent the right one?

Your approach looks fine.  You should look at the Parse::RecDescent
mailing list, they are likely to be more helpful than c.l.p.misc in
discussing your design.  But it looks like you have the syntax right,
now you just need to interpret the parse tree.

Instead of <autotree> you may want to generate your own tree
structure, so it will be easier to traverse.  For instance,

mediumZoekVraag: 'medium=' zoekString { $return = { medium => $item{zoekString} }; 1 }

will produce hash references with a single key "medium" in your parse
tree wherever mediumZoekVraag matches.

As for other ways to do it, sure, there's a million and one.  P:RD is
not a good choice is speed is essential, but otherwise it's quite
solid.  The Perl 6 matching rules will actually be similar to P::RD
rules, so consider it bonus training for the Perl 6 regexes :)

Ted



Mon, 21 Nov 2005 16:24:57 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Parsing issue

2. Range Parsing Issue

3. newbie...To parse or not to parse

4. Text Parsing - Parse::RecDescent or another method?

5. Parsing with Parse::RecDescent

6. Parse::RecDescent and parsing comments

7. Help: Problem with simple parsing and Parse::RecDescent

8. Parsing with Parse::RecDescent

9. Parse::RecDescent stops parsing.

10. Security issue in Sys::Hostname ?

11. Issues with interpolating regexs

12. PL_na issues (was ANNOUNCE: 5.005_58 perldelta page)

 

 
Powered by phpBB® Forum Software