13eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#ifndef sorcerer_h 23eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define sorcerer_h 33eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 43eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang/* 53eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * sorcerer.h -- header for all sorcerer files 63eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * 73eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * SOFTWARE RIGHTS 83eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * 93eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * We reserve no LEGAL rights to SORCERER -- SORCERER is in the public 103eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * domain. An individual or company may do whatever they wish with 113eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * source code distributed with SORCERER or the code generated by 123eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * SORCERER, including the incorporation of SORCERER, or its output, into 133eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * commerical software. 143eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * 153eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * We encourage users to develop software with SORCERER. However, we do 163eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * ask that credit is given to us for developing SORCERER. By "credit", 173eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * we mean that if you incorporate our source code into one of your 183eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * programs (commercial product, research project, or otherwise) that you 193eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * acknowledge this fact somewhere in the documentation, research report, 203eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * etc... If you like SORCERER and have developed a nice tool with the 213eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * output, please mention that you developed it using SORCERER. In 223eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * addition, we ask that this header remain intact in our source code. 233eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * As long as these guidelines are kept, we expect to continue enhancing 243eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * this system and expect to make other tools available as they are 253eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * completed. 263eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * 273eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * SORCERER 1.00B 283eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * Terence Parr 293eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * AHPCRC, University of Minnesota 303eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang * 1992-1994 313eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang */ 323eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 333eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#include "pcctscfg.h" 343eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 353eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#ifdef __USE_PROTOS 363eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#include <stdlib.h> 373eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#else 383eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#include <malloc.h> 393eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#endif 403eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 413eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#include <setjmp.h> 423eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 433eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang/* SUPERCLASS SORAST (your tree must look at minimum like this) 443eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangtypedef struct _node { 453eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang struct _node *right, *down; 463eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang int token; 473eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang -- user-defined stuff goes here 483eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang } SORAST; 493eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang*/ 503eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 513eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang/* Can be used sort of like inheritance to get the desired struct def */ 523eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define AST_REQD_FIELDS \ 533eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang struct _node *right, *down; \ 543eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang int token; 553eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 563eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang/* C MATCH */ /* MR21 Short circuit on null pointer */ 573eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _MATCH(tok) if (! _t || _t->token!=tok ) if ( _parser->guessing ) _GUESS_FAIL; else mismatched_token(_parser, tok, _t) 583eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _MATCHRANGE(tok,tok2) \ 593eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang if (! _t || _t->token<tok || _t->token>tok2 ) if ( _parser->guessing ) _GUESS_FAIL; else mismatched_range(_parser, tok, tok2, _t) 603eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 613eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang/* C++ MATCH */ 623eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _CPPMATCH(tok) if ( _t->token()!=tok ) if ( _parser->guessing ) _GUESS_FAIL; else mismatched_token(_parser, tok, _t) 633eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _CPPMATCHRANGE(tok,tok2) \ 643eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang if ( _t->token()<tok || _t->token()>tok2 ) if ( _parser->guessing ) _GUESS_FAIL; else mismatched_range(_parser, tok, tok2, _t) 653eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 663eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang/* Normal DOWN and RIGHT */ 673eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _DOWN _t=_t->down 683eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _RIGHT _t=_t->right 693eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 703eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang/* C++ DOWN and RIGHT */ 713eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _CPPDOWN _t=(SORAST *) _t->down() 723eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _CPPRIGHT _t=(SORAST *) _t->right() 733eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 743eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _SAVE SORAST *_save=_t 753eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _RESTORE _t = _save 763eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _SETLABEL(u) u=_t 773eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _WILDCARD if ( _t==NULL ) if ( _parser->guessing ) _GUESS_FAIL; else missing_wildcard(_parser) 783eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _GUESS_BLOCK STreeParser _st; int _gv; SORAST *_savet=NULL; 793eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _GUESS {_st = *_parser; \ 803eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang _savet = _t; \ 813eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang _parser->guessing = 1; \ 823eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang _gv = setjmp(_parser->startofguess.state);} 833eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _GUESS_FAIL longjmp(_parser->startofguess.state, 1) 843eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _GUESS_DONE {*_parser = _st; _t = _savet;} 853eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 863eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang/* These are used mainly by the C output */ 873eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#ifndef ast_down 883eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define ast_down down 893eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#endif 903eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#ifndef ast_right 913eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define ast_right right 923eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#endif 933eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 943eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define STreeTry(r,p,t) \ 953eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang (p)->try_result = NULL; \ 963eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang (p)->sjrv = setjmp((p)->startofguess); \ 973eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang if ( !(p)->sjrv ) { \ 983eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang rule(p,t,&try_result); \ 993eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang (p)->try_ok = 1; \ 1003eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang } \ 1013eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang else { \ 1023eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang (p)->try_ok = 0; \ 1033eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang } \ 1043eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang if ( (p)->try_ok ) 1053eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1063eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1073eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang/* Used only during TRANSFORM mode */ 1083eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define TREE_CONSTR_PTRS SORAST *_r=NULL,*_s=NULL,*_e=NULL 1093eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1103eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangtypedef struct _Sjmp_buf { 1113eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang jmp_buf state; 1123eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang } Sjmp_buf; 1133eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1143eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#ifndef _PARSER_VARS 1153eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _PARSER_VARS 1163eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#endif 1173eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1183eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#ifndef _REFVARS 1193eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define _REFVARS 1203eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#endif 1213eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1223eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangtypedef struct _STreeParser { 1233eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang int try_ok, sjrv; /* used by STreeTry macro */ 1243eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang SORAST *try_result; /* tree coming back from try */ 1253eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang int guessing; 1263eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang Sjmp_buf startofguess; 1273eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang SORAST *t; 1283eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang _REFVARS 1293eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang _PARSER_VARS 1303eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang } STreeParser; 1313eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1323eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define STreeParserInit(_p) { (_p)->guessing = 0; _refvar_inits(_p); } 1333eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1343eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1353eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang /* S a n i t y C h e c k i n g */ 1363eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1373eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#ifndef require 1383eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define require(expr, err) {if ( !(expr) ) sorcerer_panic(err);} 1393eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#endif 1403eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1413eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1423eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang /* T r a n s f o r m M a c r o s */ 1433eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#define ast_return(_t) *_result = _t 1443eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1453eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1463eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#ifdef __USE_PROTOS 1473eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void mismatched_range(STreeParser *_parser, int looking_for, int upper_token, SORAST *found); 1483eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void missing_wildcard(STreeParser *_parser); 1493eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void mismatched_token(STreeParser *_parser, int looking_for, SORAST *found); 1503eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void no_viable_alt(STreeParser *_parser, char *rulename, SORAST *root); 1513eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void sorcerer_panic(char *err); 1523eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void _refvar_inits(STreeParser *); /* MR15 Kevin J. Cummings */ 1533eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void _mkroot(SORAST **, SORAST **, SORAST **, SORAST *); 1543eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void _mkchild(SORAST **, SORAST **, SORAST **, SORAST *); 1553eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern SORAST *ast_alloc(void); 1563eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern SORAST *ast_dup(SORAST *t); 1573eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern SORAST *ast_dup_node(SORAST *t); 1583eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#else 1593eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void mismatched_range(); 1603eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void missing_wildcard(); 1613eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void mismatched_token(); 1623eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void no_viable_alt(); 1633eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void sorcerer_panic(); 1643eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void _refvar_inits(); /* MR15 Kevin J. Cummings */ 1653eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void _mkroot(); 1663eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern void _mkchild(); 1673eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern SORAST *ast_alloc(); 1683eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern SORAST *ast_dup(); 1693eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwangextern SORAST *ast_dup_node(); 1703eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#endif 1713eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang 1723eb9473ea9a949badfe06ae61d2d3fcfa53651c7qwang#endif 173