[antlr-interest] Problem to build AST (kind of reverse AST) (Jesse McGrew)

Atul Dambalkar atul at entrib.com
Mon Aug 27 07:19:40 PDT 2012


Hi Aurlien,

 

I don't think if it's possible for Antlr to generate the tree structure you
want. Simply because, there is some semantics involved here. You will have
to keep track of the package name seen earlier in the parsing to append the
respective data name nodes. You either will have to tweak your grammar to
chain the package along with data nodes or do another in-memory pass on the
current generated AST to turn into the way you want it.

 

Also cc'ing it to the group so that if anyone has any other comments.

 

HTH,

 

-Atul

 

From: Aurelien Thiriet [mailto:thiriet.aurelien at gmail.com] 
Sent: Monday, August 27, 2012 1:20 PM
To: atul at entrib.com
Subject: Re: [antlr-interest] Problem to build AST (kind of reverse AST)
(Jesse McGrew)

 

Hi Atul,

Thanks for your interest.


This is the grammar I start with to parse :
"
package1 data11;
package1 data12;

package2 data21;
package2 data22;
"

"
grammar myDebug;

options
{ 
    output=AST;
}

tokens
{
    DOCUMENT;
    PACKAGE;
    PACKAGENAME;
    PACKAGECONTENT;
    
    DATA;
}

myDocument
    :
        (myData)*
        -> ^(DOCUMENT myData*)
    ;

myData    :
        thePackage=ID  theData=ID ';'
        -> ^(DATA ^(PACKAGE ^(PACKAGENAME $thePackage) ^(PACKAGECONTENT) )
$theData)
    ;


ID  :    ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;
"


With this one this is the output AST : see ExportGrammarForm1.JPG



But I'd like to directly output this AST : see ExportGrammarForm2.JPG








2012/8/27 Atul Dambalkar <atul at entrib.com>

Hi Aurelien,

Can you pass the current grammar for parsing following,


package1 data11;
package1 data12;

package2 data21;
package2 data22;

I can give it a try to help.

-Atul


-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Aurelien Thiriet
Sent: Monday, August 27, 2012 12:23 PM
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Problem to build AST (kind of reverse AST)

(Jesse McGrew)

Hi Jesse,


You got it, this is my need.

For sure I could have done it's parsing directly in raw C, but the exemple I
give is a simplified one... the grammar I'm working on is much more complex
but I didn't want to mix problems.

If I could solve this little one first, I would be happy !



Aurelien

> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 24 Aug 2012 12:46:11 -0700
> From: Jesse McGrew <jmcgrew at hansprestige.com>
> Subject: Re: [antlr-interest] Problem to build AST (kind of reverse
>         AST)
> To: antlr-interest <antlr-interest at antlr.org>
> Message-ID:
>         <CAJ3AhhXA6A-4TdcRq6cq=
> HQFxQm46CSVfeg8T_vS8_SoqvXhNQ at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> It sounds like he just wants to group the AST by the package name
> (first token on the line). So all the "package1" data nodes go in one
> tree, all the "package2" data nodes go in another, etc.
>
> Jesse
>
> On Fri, Aug 24, 2012 at 11:36 AM, Jim Idle <jimi at temporal-wave.com> wrote:
> > I can't see why you would want to do this. Perhaps you are asking
> > the wrong question - can you explain what you are trying to achieve,
> > as there may be better ways to achieve what you are trying to do.
> >
> > Your question as it stands cannot really be answered as you don't
> > say anything about the bounds/cardinality. If your input is always
> > four lines like this, then there is no need for a grammar, as a few
> > lines of C would do it. If there is an unlimited set, then how do
> > you determine what swaps with what?
> >
> > Jim
> >
> >> -----Original Message-----
> >> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> >> bounces at antlr.org] On Behalf Of Aurelien Thiriet
> >> Sent: Friday, August 24, 2012 2:42 AM
> >> To: antlr-interest at antlr.org
> >> Subject: [antlr-interest] Problem to build AST (kind of reverse
> >> AST)
> >>
> >> Hi,
> >>
> >> I'm new to ANTLR (I've read the books by the way ;)).
> >>
> >>
> >> I'd like to parse and build AST for this kind of file :
> >>
> >> "
> >> package1 data11;
> >> package1 data12;
> >>
> >> package2 data21;
> >> package2 data22;
> >> "
> >>
> >> Which means  data11 and data12 belongs to package1, data21 and
> >> data22 belongs to package2.
> >>
> >>
> >> Its very easy to build this kind of AST :
> >>
> >> (DOCUMENT (DATA ( PACKAGE(package1) data11) DATA (
> >> PACKAGE(package1)
> >> data12) DATA ( PACKAGE(package2) data21) DATA ( PACKAGE(package1)
> >> data22)))
> >>
> >>
> >> But my point is to reverse it like this :
> >> (DOCUMENT ( PACKAGE (package1 DATA(data11) DATA(data12)))    ( PACKAGE
> >> (package2 DATA(data21) DATA(data22)))   )
> >>
> >>
> >> How would you write the grammar to perform this AST ?
> >>
> >> I've almost succed using many  java code into grammar file, but I'd
> >> like to use grammar only if possible.
> >>
> >>
> >> Thanks a lot
> >>
> >> Aur?lien
> >>
> >> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> >> Unsubscribe:
> >> http://www.antlr.org/mailman/options/antlr-interest/your-
> >> email-address
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

 



More information about the antlr-interest mailing list