Zeev,

It is not a hasard that I provided the XML stuff. Searching in XML using 
XPath and nagivating in an XML is far easier that with Perl's blessed 
structures.

   - printout the ast using  
   MarpaX::Languages::SQL2003::AST->new()->asXML($input)->toString(1) instead 
   of trying to digest the grammar
   - you will see the hierarchy of what you are looking for
   - use XPath
   
Example in attachement on how to find the table names.

If you insist on the blessed mode, perl people would suggest modules like 
Data::Find or Data::Walk, I'd personnaly revisit 
MarpaX::Languages::C::AST::Util::Data::Find 
<https://github.com/jddurand/MarpaX-Languages-C-AST/blob/master/lib/MarpaX/Languages/C/AST/Util/Data/Find.pm>,
 
taking into account that with MarpaX::Languages::SQL2003::AST, a lexeme is 
a blessed hash that is guaranteed to have keys "start", "length", "text" 
and "value", when an lhs is blessed array.

Jean-Damien.

Le jeudi 18 décembre 2014 16:11:37 UTC+1, Zeev Atlas a écrit :
>
> Thank you again for advice and support
> Is there any 'accessor' or a template for such an accessor for the blessed 
> structure that is available anywhere in the Marpa world?  (before I code 
> such an accessor myself and risk being incompatible with next version.)
> ZA
>
> On Tuesday, December 16, 2014 12:39:00 PM UTC-5, Durand Jean-Damien wrote:
>
>> Ok, so I do nothing / JD.
>>
>> Le mardi 16 décembre 2014 07:16:38 UTC+1, [email protected] a écrit :
>>>
>>> Thank you... regardless, I tried your suggestion and will move to 
>>> strawberry anyway, since I can install XML::LibXML and I guess anything else
>>> In any case, for my purpose, the AST is all what I need.  I intend to 
>>> write an application that analyze SQL statements and deduce source tables 
>>> and where/join-on clauses, basically find the flow of data and put it in 
>>> csv/excel format.
>>> ZA
>>>
>>> On Monday, December 15, 2014 11:30:00 PM UTC-5, Durand Jean-Damien wrote:
>>>>
>>>> Yes, this is easy... will keep you informed
>>>>
>>>> Le mardi 16 décembre 2014 04:46:43 UTC+1, Zeev Atlas a écrit :
>>>>>
>>>>> XML::LibXML is not available (yet) for ActiveState Perl 5.16 and above 
>>>>> and I use 5.18.  Is it possible to create a package that produces AST 
>>>>> version only and does not have 'use XML::LibXML;' at all.  I do not have 
>>>>> too much experience with Linux (barely have one machine with Ubuntu).  I 
>>>>> will go for strawberry only if I have no other choice (because it 
>>>>> resembles 
>>>>> the Linux environment)
>>>>> Thanks
>>>>> ZA
>>>>>
>>>>> On Sunday, December 14, 2014 10:48:22 AM UTC-5, Durand Jean-Damien 
>>>>> wrote:
>>>>>>
>>>>>> ActiveState will certainly work on that, I bet users of this 
>>>>>> dsitribution can't stand without XML::LibXML for a while, usual bribes 
>>>>>> or 
>>>>>> whatever PPDs can perhaps help, although I guess you (perhaps) already 
>>>>>> tried.
>>>>>> I personnaly stopped using this distribution in favour of strawberry, 
>>>>>> that has both the advantage and drawback that it is not using nmake/cl.
>>>>>>
>>>>>> http://code.activestate.com/ppm/XML-LibXML/
>>>>>>
>>>>>> JD.
>>>>>>
>>>>>> Le dimanche 14 décembre 2014 02:00:20 UTC+1, Zeev Atlas a écrit :
>>>>>>>
>>>>>>> Thanks again... I work on a Windows platform (active state Perl) and 
>>>>>>> there is no XML::LibXML.  I'll have to make some adjustments in the 
>>>>>>> next 
>>>>>>> few days or just look at the module and do my own.  
>>>>>>>
>>>>>>> On Saturday, December 13, 2014 9:40:54 AM UTC-5, Durand Jean-Damien 
>>>>>>> wrote:
>>>>>>>>
>>>>>>>> Zeev,
>>>>>>>>
>>>>>>>> Instead I invite you to try the sql2003toxml binary distributed 
>>>>>>>> with MarpaX::Languages::SQL2003::AST, c.f. my g+ post 
>>>>>>>> <https://plus.google.com/105243759955561553019/posts/Mn7DQSJuKc8>.
>>>>>>>>
>>>>>>>> Jean-Damien.
>>>>>>>>
>>>>>>>> Le vendredi 12 décembre 2014 14:10:07 UTC+1, Zeev Atlas a écrit :
>>>>>>>>>
>>>>>>>>> COuld you please migrate the POC as well
>>>>>>>>> ZA
>>>>>>>>>
>>>>>>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"marpa parser" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
#!env perl
use strict;
use diagnostics;
use MarpaX::Languages::SQL2003::AST;
use Data::Dumper;

my $input = do {local $/; <DATA>};

my %tables = ();
my $ast = MarpaX::Languages::SQL2003::AST->new();
my $xml = eval {$ast->asXML($input)};
print STDERR $xml->toString(1) . "\n";

if ($@) {
  print STDERR "Failed to parse $input: $@";
} else {
  #
  # Get table.column information
  #
  our $xpath = XML::LibXML::XPathExpression->new("//Table_Primary");
  #
  # Find the table names and their eventual aliases
  #
  foreach ($xml->findnodes($xpath)) {
    my $name = $_
      ->firstChild()                     # Table_Or_Query_Name
        ->firstChild()                   # Table_Name
          -> firstChild()       # Local_Or_Schema_Qualified_Name
            ->getAttribute('value');
    my $alias = $_
      ->firstChild()                      # Table_Or_Query_Name
        ->nextSibling()                   # Gen1649_Maybe
          ->firstChild();
    if (defined($alias)) {
      $alias = $alias->firstChild()        # As_Maybe
        ->nextSibling()                    # Correlation_Name
          ->firstChild()                   # Identifier
            ->getAttribute('value');
    }
    $alias ||= $name;
    print STDOUT "Table alias $alias <=> name $name\n";
    $tables{$alias} = $name;
  }
}

print Dumper(\%tables);

__DATA__
select t.i, t.j, u.k, v.m, v.n, w.o from myTableT as t, myTableU as u, myTableV as v, w

Reply via email to