[antlr-interest] order tree nodes

Jose Ventura jose.ventura.roda at gmail.com
Fri Apr 28 00:29:17 PDT 2006


Hi Erik,

I'm very interesting in parsing cobol program. I can exchange information
about this goal.

I've solved this problem so:

- I have a vector with the pointer to last declaration of each cobol level
- When parse a new declaration this is link with the previous level
declaration

I hope this help you. Do you interest to exchange the grammars?

I paste my grammar code to nest level

Regards,
José Ventura


AST levelsVector [];

//To nest level
int lastLevel=0;
int lastActualLevel=0;

private void inicLevelsVector(AST t)
{
levelsVector = new AST[50];
levelsVector[0]=t;
for (int i=1;i<50;i++)
levelsVector[i]=null;
}

[...] more code



working_sec
: w:WORKING^ {inicLevelsVector(#w);} SECTION! dot (data_declaration)*
;
data_declaration
{String d="FILLER";}
: n:NUM (d=dato)? (clausulas)* dot
{
int level = Integer.valueOf(#n.getText());
int actualLevel = level;
switch (level)
{
case 77:
level=1;
break;
case 88:
if (lastActualLevel<50)
level = lastLevel + 1;
else
level = lastLevel;
break;
case 66:
level = lastLevel;
break;
}

while (lastLevel >= level || levelsVector[lastLevel]==null)
{
levelsVector[lastLevel]=null;
lastLevel--;
}
AST aux=#( #[DECLARACION, d] ,#data_declaration);
levelsVector[lastLevel].addChild(aux);
lastLevel=level;
lastActualLevel=actualLevel;
levelsVector[lastLevel]=aux;
#data_declaration=null;
}
;

dato returns [String n]
{n="";}
:i:IDENT {n=#i.getText();}
;
2006/4/27, Olivier Dragon <dragonoe at mcmaster.ca>:
>
> Hi Erik,
>
> I think it would be better not to use "couples" because your tree won't
> be right.
>
> I don't know COBOL very much but from your example I assume that those
> numbers just keep on increasing. In that case you could keep track of
> the current parent and have your rule look at each record individually
> comparing it to the parent. If it's larger, do parent.addChild(record),
> if it's the same do parent.setNextSibling(record); parent = record;
>
> Does that help?
>
> -Olivier
>
> On Thu, Apr 27, 2006 at 04:58:59PM -0400, Putrycz, Erik wrote:
> > I'm currently parsing a cobol file and I'd like to create a proper AST
> > for the records.
> >
> > A record list looks like
> >
> > 003634*01  RAREA.
> >
> > 003635     05  R00-999FIXED-AREA.
> >
> > 003636         10  RKEY-AREA                             PIC X(62).
> >
> > 003637         10  R906REC-TYPE                          PIC XX.
> >
> > 003638         10  R907BLKS                              PIC 9(4) COMP.
> >
> > 003639         10  CRTN-DT-TS-GRP.
> >
> > 003640           15  CRTN-CN                             PIC 9(2).
> >
> > 003641           15  CRTN-DT-TS.
> >
> >
> >
> > What I'd like to do is have the node with 01 as root for the 05 and have
> > the 05 be the root of all the "10" nodes.
> >
> > I tried to write something like
> >
> >
> >
> > recordList
> >
> >             : (recordCouple)+ { #recordList =
> > #([RECORD_LIST],#recordList);}
> >
> >             ;
> >
> >
> >
> > recordCouple
> >
> >             :! (record record) => r1:record r2:record
> >
> >                          {
> >
> >                                     if (#r1 != null && #r2!= null &&
> > #r1.getText() != null && #r2.getText() != null) {
> >
> >                                     int r1Val =
> > Integer.parseInt(#r1.getText());
> >
> >                                     int r2Val =
> > Integer.parseInt(#r2.getText());
> >
> >                                     if (r2Val > r1Val) {
> >
> >                                                 #recordCouple =
> > #(r1,r2);
> >
> >                                     }
> >
> >                                     }
> >
> >                         }
> >
> >                         |record
> >
> >             ;
> >
> >
> >
> > unfortunately this is not working.
> >
> > If I put the "!" to ignore the default tree construction, how can I
> > return a list instead of a tree? In my code I miss an else alternative
> > to return (r1,r2) and not #(r1,r2).
> >
> > Also, I don't believe that this rule will do the recursivity I need... I
> > rather need something like (r1:record r2:record) => record  but that
> > doesn't work.
> >
> > Any suggestions or clues???
> >
> >
> >
> > Thanks,
> >
> >
> >
> >
> >
> > Erik Putrycz, Ph.D - Research Associate /
> > <mailto:erik.putrycz at nrc-cnrc.gc.ca> erik.putrycz at nrc-cnrc.gc.ca / (613)
> > 990 0681
> >
> > Institute for Information Technology - Software Engineering Group
> >
> > National Research Council, Canada - Building M-50, 1200 Montreal Road
> >
> > Ottawa, Ontario, CANADA K1A 0R6
> >
> >
> >
>
> --
>          __-/|    ? ?     |\-__
>     __--/  /  \   (^^)   /  \  \--__
> _-/   /   /  /\ / ( )  /\  \   \   \-_
> /  /   /  /  /  (   ^^ ~  \  \  \   \  \
> / Oli Dragon    ( dragonoe at mcmaster.ca \
> /  B.Eng. Sfwr   ( dragon.homelinux.org  \
> /  /  /    /__--_ (   ) __--__\    \  \  \
> |  /  /  _/        \_ \_       \_  \  \  |
> \/  / _/            \_ \_       \_ \  \/
> \_/ /                -\_\        \ \_/
>    \/                    )         \/
>                        *~
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.1 (GNU/Linux)
>
> iD8DBQFEUT6BgKW2i4QOkbARAkk4AJ4tDpDUCCZXgZXNNL3Fb+AE/J5ovgCgrSwy
> pqwzZ4pfPGJaaRhc4ujT5H4=
> =+az3
> -----END PGP SIGNATURE-----
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060428/9e5d1e19/attachment-0001.html


More information about the antlr-interest mailing list