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)]);



Sun, 20 Nov 2005 21:20:25 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';

If you're using Perl 5+, wouldn't it be simpler to do something like the
following:

From command line (modify for any other input method):

my $buffer = <STDIN>;
chop($buffer);

my ($name,$value) = split /=/,$buffer;

my $sqlStatement = "select * from Items where $name = ".$value;

Or I am missing something?


Quote:
> 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}/
> };

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

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



Mon, 21 Nov 2005 06:38:23 GMT  
 Parsing issue
On Wed, 04 Jun 2003 22:38:23 GMT, "HongKong{*filter*}"

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';

>If you're using Perl 5+, wouldn't it be simpler to do something like the
>following:

>From command line (modify for any other input method):

>my $buffer = <STDIN>;
>chop($buffer);

>my ($name,$value) = split /=/,$buffer;

>my $sqlStatement = "select * from Items where $name = ".$value;

>Or I am missing something?

I'm afraid so. More complex queries are also possible. E.g.

(medium='Book' or (medium=Paper' and topic='Economy')) and
period=1980-1985

I think the split-solution would be difficult to implement in this
case.

Peter



Mon, 21 Nov 2005 14:50:06 GMT  
 
 [ 3 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