PCCTSAST.cpp revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/*
* PCCTSAST.C
*
* SOFTWARE RIGHTS
*
* We reserve no LEGAL rights to SORCERER -- SORCERER is in the public
* domain. An individual or company may do whatever they wish with
* source code distributed with SORCERER or the code generated by
* SORCERER, including the incorporation of SORCERER, or its output, into
* commerical software.
*
* We encourage users to develop software with SORCERER. However, we do
* ask that credit is given to us for developing SORCERER. By "credit",
* we mean that if you incorporate our source code into one of your
* programs (commercial product, research project, or otherwise) that you
* acknowledge this fact somewhere in the documentation, research report,
* etc... If you like SORCERER and have developed a nice tool with the
* output, please mention that you developed it using SORCERER. In
* addition, we ask that this header remain intact in our source code.
* As long as these guidelines are kept, we expect to continue enhancing
* this system and expect to make other tools available as they are
* completed.
*
* SORCERER 1.00B14 and ANTLR 1.33
* Terence Parr
* Parr Research Corporation
* AHPCRC, University of Minnesota
* 1992-2000
*/
#define ANTLR_SUPPORT_CODE
#include "pcctscfg.h"
#include "PCCTSAST.h"
#include "pccts_stdarg.h"
#include <ctype.h>
//#include "SList.h"
"invalid", /* 0 */
"LPAREN", /* 1 */
"RPAREN", /* 2 */
"PERCENT", /* 3 */
"INT", /* 4 */
"COLON", /* 5 */
"POUND", /* 6 */
"PERIOD", /* 7 */
};
void PCCTS_AST::
{
if ( t==NULL ) return;
if ( s!=NULL )
{
s->setRight(t);
}
else
this->setDown(t);
}
void PCCTS_AST::
{
lisp_action(f);
}
/* build a tree (root child1 child2 ... NULL)
* If root is NULL, simply make the children siblings and return ptr
* to 1st sibling (child1). If root is not single node, return NULL.
*
* Siblings that are actually sibling lists themselves are handled
* correctly. For example #( NULL, #( NULL, A, B, C), D) results
* in the tree ( NULL A B C D ).
*
* Requires at least two parameters with the last one being NULL. If
* both are NULL, return NULL.
*
*/
{
{
/* find end of child */
}
return root;
}
/* The following push and pop routines are only used by ast_find_all() */
void PCCTS_AST::
{
(*sp)--;
}
{
(*sp)++;
return e;
}
/* Find all occurrences of u in t.
* 'cursor' must be initialized to 't'. It eventually
* returns NULL when no more occurrences of 'u' are found.
*/
{
////static int nesting = 0; /* MR23 Not referenced */
else {
/* else, first time--start at top of template 't' */
sib = this;
/* bottom of stack is always a NULL--"cookie" indicates "done" */
}
{
}
{
/* look for another match */
{
goto keep_looking;
}
/* nothing below to try, try next sibling */
goto keep_looking;
}
/* found a matching root node, try to match what's below */
if ( match_partial(sib, u) )
{
/* record sibling cursor so we can pick up next from there */
{
}
return sib;
}
/* no match, keep searching */
{
}
goto keep_looking;
}
/* are two trees exactly alike? */
int PCCTS_AST::
{
PCCTS_AST *t = this;
if ( u==NULL ) return 0;
{
}
return 1;
}
/* Is 'u' a subtree of 't' beginning at the root? */
int PCCTS_AST::
{
if ( u==NULL ) return 1;
if ( t==NULL ) return 0; /* MR23 removed unreachable code */
{
}
return 1;
}
#ifdef _MSC_VER // MR23
//Turn off "unreachable code" warning
#endif
/* Walk the template tree 't' (matching against 'this'), filling in the
* 'labels' array, and setting 'n' according to how many labels were matched.
*/
int PCCTS_AST::
{
PCCTS_AST *u = this;
if ( u==NULL ) return 0;
{
/* make sure tokens match; token of '0' means wildcard match */
/* we have a matched token here; set label pointers if exists */
{
(*n)++;
}
/* match what's below if something there and current node is not wildcard */
{
{
return 0;
else
return 1;
}
}
}
return 1;
}
#ifdef _MSC_VER // MR23
#pragma warning(default : 4702)
#endif
void PCCTS_AST::
{
if ( b==NULL ) return;
/* find end of b's child list */
this->setRight(b);
}
void PCCTS_AST::
{
/* find end of child list */
}
tail()
{
/* find end of child list */
return end;
}
bottom()
{
/* find end of child list */
return end;
}
{
/* find node pointing to b */
{;}
a->setRight(b);
return ret;
}
#ifdef NOT_YET
to_slist()
{
PCCTS_AST *p;
{
}
return list;
}
#endif
void PCCTS_AST::
tfree()
{
PCCTS_AST *t = this;
delete t;
}
int PCCTS_AST::
{
PCCTS_AST *t = this;
int n=0;
while ( t!=NULL )
{
n++;
t = t->right();
}
return n;
}
sibling_index(int i)
{
PCCTS_AST *t = this;
int j=1;
require(i>0, "sibling_index: i<=0");
while ( t!=NULL )
{
if ( j==i ) return t;
j++;
t = t->right();
}
return NULL;
}
/* Assume this is a root node of a tree--
* duplicate that node and what's below; ignore siblings of root node.
*/
// MR9 23-Sep-97 RJV
// MR9
// MR9 RJV: Original version only duplicated the node and down elements.
// MR9 Made copies of the pointers to sibling.
// MR9 Changed call "down()->deepCopy()" to "down()->deepCopyBushy()"
// MR9
deepCopy()
{
PCCTS_AST *u = this->shallowCopy();
return u;
}
/* Copy all nodes including siblings of root. */
{
PCCTS_AST *u = this->shallowCopy();
/* copy the rest of the tree */
return u;
}
void PCCTS_AST::
scanast_free(ScanAST *t)
{
if ( t == NULL ) return;
scanast_free( t->down() );
scanast_free( t->right() );
free( (char *) t ); // MR1
}
/*
* scan
*
* This function is like scanf(): it attempts to match a template
* against an input tree. A variable number of tree pointers
* may be set according to the '%i' labels in the template string.
* For example:
*
* t->ast_scan("#( 6 #(5 %1:4 %2:3) #(1 %3:3 %4:3) )",
* &w, &x, &y, &z);
*
* Naturally, you'd want this converted from
*
* t->ast_scan("#( RangeOp #(Minus %1:IConst %2:Var) #(Plus %3:Var %4Var) )",
* &w, &x, &y, &z);
*
* by SORCERER.
*
* This function call must be done withing a SORCERER file because SORCERER
* must convert the token references to the associated token number.
*
* This functions parses the template and creates trees which are then
* matched against the input tree. The labels are set as they are
* encountered; hence, partial matches may leave some pointers set
* and some NULL. This routines initializes all argument pointers to NULL
* at the beginning.
*
* This function returns the number of labels matched.
*/
int PCCTS_AST::
{
int n, i, found=0;
/* make a ScanAST tree out of the template */
/* make an array out of the labels */
if ( n>0 )
{
for (i=1; i<=n; i++)
{
}
}
/* match the input tree against the template */
return found;
}
new_scanast(int tok)
{
//
// 7-Apr-97 133MR1
//
if ( p == NULL )
return p;
}
{
ScanAST *t;
t = stringparser_parse_tree(&parser);
return t;
}
void PCCTS_AST::
{
}
/*
* Match a tree of the form:
* (root child1 child2 ... childn)
* or,
* node
*
* where the elements are integers or labeled integers.
*/
{
{
return stringparser_parse_element(parser);
}
{
}
return root;
}
{
char ebuf[100];
int label = 0;
{
return stringparser_parse_tree(parser);
}
{
parser->num_labels++;
/* can label tokens and wildcards */
panic("can only label tokens");
}
{
return p;
}
{
return p;
}
return NULL;
}
void PCCTS_AST::
{
parser->num_labels = 0;
}
void PCCTS_AST::
{
}
void PCCTS_AST::
{
}
int PCCTS_AST::
{
{
}
*index = '\0';
return tok;
}
switch ( scanner->c )
{
case '\0' : return __StringScanEOF;
case __StringScanEOF : return __StringScanEOF;
default :
}
return __StringScanEOF; // never reached
}
const char *PCCTS_AST:: /* MR20 const */
scan_token_str(int t)
{
if ( VALID_SCAN_TOKEN(t) ) return scan_token_tbl[t];
else if ( t==__StringScanEOF ) return "<end-of-string>";
else return "<invalid-token>";
}
//MR23
{
return iRet;
}