home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts1.zip / ANTLR / GLOBALS.C < prev    next >
C/C++ Source or Header  |  1993-09-02  |  10KB  |  333 lines

  1. /*
  2.  * globals.c    --    File containing all variables/tables visible to all files.
  3.  *
  4.  * $Id: globals.c,v 1.11 1993/08/19 16:42:10 pccts Exp pccts $
  5.  * $Revision: 1.11 $
  6.  *
  7.  * SOFTWARE RIGHTS
  8.  *
  9.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  10.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  11.  * company may do whatever they wish with source code distributed with
  12.  * PCCTS or the code generated by PCCTS, including the incorporation of
  13.  * PCCTS, or its output, into commerical software.
  14.  * 
  15.  * We encourage users to develop software with PCCTS.  However, we do ask
  16.  * that credit is given to us for developing PCCTS.  By "credit",
  17.  * we mean that if you incorporate our source code into one of your
  18.  * programs (commercial product, research project, or otherwise) that you
  19.  * acknowledge this fact somewhere in the documentation, research report,
  20.  * etc...  If you like PCCTS and have developed a nice tool with the
  21.  * output, please mention that you developed it using PCCTS.  In
  22.  * addition, we ask that this header remain intact in our source code.
  23.  * As long as these guidelines are kept, we expect to continue enhancing
  24.  * this system and expect to make other tools available as they are
  25.  * completed.
  26.  *
  27.  * Version 1.10
  28.  * Terence Parr
  29.  * Purdue University
  30.  * 1989-1993
  31.  */
  32. #include <stdio.h>
  33. #ifdef __cplusplus
  34. #ifndef __STDC__
  35. #define __STDC__
  36. #endif
  37. #endif
  38. #include "set.h"
  39. #include "syn.h"
  40. #include "hash.h"
  41. #include "generic.h"
  42.  
  43. char Version[] = "1.10";        /* PCCTS version number */
  44. char VersionDef[] = "110";        /* same (except int equiv for preproc symbol) */
  45. char LexStartSymbol[] = "START";/* Name of starting lexical class/automaton */
  46.  
  47. char *RemapFileName = "remap.h";
  48. char *DlgFileName = "parser.dlg";
  49. char *DefFileName = "tokens.h";
  50. char *ErrFileName = "err.c";
  51. char *ModeFileName = "mode.h";
  52.  
  53. char *ParserName = DefaultParserName;
  54.  
  55. /* list of PCCTS supplied support symbols; these are renamed when more than
  56.  * one ANTLR-generated parsers are linked together to avoid name conflicts.
  57.  * Can't use '##' ANSIC preprocessor concat operator with K&R and:
  58.  *        #define zzskip    zzparser ## skip
  59.  * will not work for ANSI/C++ as 'zzparserskip' is created w/o zzparser
  60.  * being substituted--ack!!!
  61.  */
  62. char *StandardSymbols[] = {
  63. /* ANTLR stuff */
  64.     "zzStackOvfMsg",
  65.     "zzasp",
  66.     "zzaStack",
  67.     "inf_tokens",
  68.     "inf_text",
  69.     "inf_text_buffer",
  70.     "inf_text_buffer_ptr",
  71.     "inf_text_buffer_size",
  72.      "inf_labase",
  73.     "inf_last",
  74.     "inf_lap",
  75.     "zztokenLA",
  76.     "zztextLA",
  77.     "zzlap",
  78.     "zzlabase",
  79.     "zztoktext",
  80.     "zztoken",
  81.     "zzdirty",
  82.     "zzguessing",
  83.     "zzguess_start",
  84.     "zzresynch",
  85. #ifdef ZZINF_LOOK
  86.     "zzinf_tokens",
  87.     "zzinf_text",
  88.     "zzinf_text_buffer",
  89.     "zzinf_labase",
  90.     "zzinf_last",
  91.     "zzfill_inf_look",
  92. #endif
  93.     "zzFAIL",
  94.     "zzsave_antlr_state",
  95.     "zzrestore_antlr_state",
  96.     "zzsyn",
  97.     "zzset_el",
  98.     "zzset_deg",
  99.     "zzedecode",
  100.     "_zzmatch",
  101.     "_inf_zzgettok",
  102. /* DLG stuff */
  103.     "zzcharfull",
  104.     "zzerr",
  105.     "zzlextext",
  106.     "zzbegexpr",
  107.     "zzendexpr",
  108.     "zzbufsize",
  109.     "zzbegcol",
  110.     "zzendcol",
  111.     "zzline",
  112.     "zzchar",
  113.     "zzbufovf",
  114.     "zzrdstream",
  115.     "zzrdfunc",
  116.     "zzrdstr",
  117.     "zzclose_stream",
  118.     "zzsave_dlg_state",
  119.     "zzrestore_dlg_state",
  120.     "zzmode",
  121.     "zzskip",
  122.     "zzmore",
  123.     "zzreplchar",
  124.     "zzreplstr",
  125.     "zzgettok",
  126.     "zzadvance",
  127.     "zzerrstd",
  128.     "zzerr_in",
  129.     "zzconstr_attr",
  130.     "zzempty_attr",
  131.     "zzerraction",
  132.     "zztokens",            /* list of token regular expressions */
  133.     "dfa",
  134.     "accepts",
  135.     "actions",
  136.     NULL        /* must be present */
  137. };
  138.  
  139. /* list of PCCTS supplied support functions; these are renamed when more than
  140.  * one ANTLR-generated parsers are linked together to avoid name conflicts.
  141.  */
  142. char *ASTSymbols[] = {
  143.     "AST",
  144.     "zzast_sp",
  145.     "zzastStack",
  146.     "zzlink",
  147.     "zzastnew",
  148.     "zzsubchild",
  149.     "zzsubroot",
  150.     "zzpre_ast",
  151.     "zzfree_ast",
  152.     "zztmake",
  153.     "zzdup_ast",
  154.     "zztfree",
  155.     "zzdouble_link",
  156.     NULL        /* must be present */
  157. };
  158.  
  159. /* Current ambiguity examination information */
  160. int CurAmbigAlt1, CurAmbigAlt2, CurAmbigline, CurAmbigfile;
  161. char *CurAmbigbtype;
  162.  
  163.  
  164.                         /* M e t h o d  T a b l e s */
  165. /*
  166.  * The following tables are used to fill syntax diagram nodes with the correct
  167.  * function pointers for computing FIRST sets and printing themselves.
  168.  */
  169.  
  170. /* fpTraverse[node type] == pointer to function that calculates trees
  171.  * representing the FIRST sets for that node (maintains spatial info).
  172.  * We use 'struct _tree' not 'tree' due to a g++ 2.4.3 bug.
  173.  */
  174. #ifdef __cplusplus
  175. struct _tree *(*fpTraverse[NumNodeTypes+1])(... /* Node *, int, set * */) = {
  176.     NULL,
  177.     (struct _tree *(*)(...)) tJunc,
  178.     (struct _tree *(*)(...)) tRuleRef,
  179.     (struct _tree *(*)(...)) tToken,
  180.     (struct _tree *(*)(...)) tAction
  181. };
  182. #else
  183. Tree *(*fpTraverse[NumNodeTypes+1])() = {
  184.     NULL,
  185.     tJunc,
  186.     tRuleRef,
  187.     tToken,
  188.     tAction
  189. };
  190. #endif
  191.  
  192. /* fpReach[node type] == pointer to function that calculates FIRST set for
  193.  * that node. (r stands for reach).  We use 'struct _set' not 'set'
  194.  * due to a g++ 2.4.3 bug.
  195.  */
  196. #ifdef __cplusplus
  197. struct _set (*fpReach[NumNodeTypes+1])(... /* Node *, int, set * */) = {
  198.     NULL,
  199.     (struct _set (*)(...)) rJunc,
  200.     (struct _set (*)(...)) rRuleRef,
  201.     (struct _set (*)(...)) rToken,
  202.     (struct _set (*)(...)) rAction
  203. };
  204. #else
  205. set (*fpReach[NumNodeTypes+1])() = {
  206.     NULL,
  207.     rJunc,
  208.     rRuleRef,
  209.     rToken,
  210.     rAction
  211. };
  212. #endif
  213.  
  214. /* fpPrint[node type] == pointer to function that knows how to print that node. */
  215. #ifdef __cplusplus
  216. void (*fpPrint[NumNodeTypes+1])(... /* Node * */) = {
  217.     NULL,
  218.     (void (*)(...)) pJunc,
  219.     (void (*)(...)) pRuleRef,
  220.     (void (*)(...)) pToken,
  221.     (void (*)(...)) pAction
  222. };
  223. #else
  224. void (*fpPrint[NumNodeTypes+1])() = {
  225.     NULL,
  226.     pJunc,
  227.     pRuleRef,
  228.     pToken,
  229.     pAction
  230. };
  231. #endif
  232.  
  233. char *decodeJType[] = {
  234.     "invalid",
  235.     "aSubBlk",
  236.     "aOptBlk",
  237.     "aLoopBlk",
  238.     "EndBlk",
  239.     "RuleBlk",
  240.     "Generic",
  241.     "EndRule",
  242.     "aPlusBlk",
  243.     "aLoopBegin"
  244. };
  245.  
  246.  
  247.                             /* H a s h  T a b l e s */
  248.  
  249. Entry    **Tname,            /* Table of all token names (maps name to tok num)*/
  250.         **Texpr,            /* Table of all token expressions
  251.                                (maps expr to tok num) */
  252.         **Rname,            /* Table of all Rules (has ptr to start of rule) */
  253.         **Fcache,            /* Cache of First/Follow Computations */
  254.         **Tcache;            /* Tree cache; First/Follow for permute trees */
  255.  
  256.  
  257.                             /* V a r i a b l e s */
  258.  
  259. int        EpToken=0;            /* Imaginary Epsilon token number */
  260. int        CurFile= -1;        /* Index into FileStr table */
  261. char    *CurRule=NULL;        /* Pointer to current rule name */
  262. RuleEntry *CurRuleNode=NULL;/* Pointer to current rule node in syntax tree */
  263. char    *CurRetDef=NULL;    /* Pointer to current return type definition */
  264. char    *CurParmDef=NULL;    /* Pointer to current parameter definition */
  265. int        NumRules=0;            /* Rules are from 1 to n */
  266. FILE    *output=NULL;        /* current parser output file */
  267. FILE    *input=NULL;        /* current grammar input file */
  268. char    *FileStr[MaxNumFiles];/* Ptr to array of file names on command-line */
  269. int        NumFiles=0;            /* current grammar file number */
  270. #ifdef __cplusplus
  271. void    (**fpTrans)(...),    /* array of ptrs to funcs that translate nodes */
  272.          (**fpJTrans)(...);    /*  ... that translate junctions */
  273. #else
  274. void    (**fpTrans)(),        /* array of ptrs to funcs that translate nodes */
  275.          (**fpJTrans)();        /*  ... that translate junctions */
  276. #endif
  277. int        **FoStack;            /* Array of LL_k ptrs to stacks of rule numbers */
  278. int        **FoTOS;            /* FOLLOW stack top-of-stack pointers */
  279. Junction *SynDiag = NULL;    /* Pointer to start of syntax diagram */
  280. int        BlkLevel=1;            /* Current block level.  Set by antlr.g, used by
  281.                              * scanner to translate $i.j attributes */
  282. int        TokenNum=TokenStart;
  283. char    **TokenStr=NULL;    /* map token # to token name */
  284. char    **ExprStr=NULL;        /* map token # to expr */
  285. Junction **RulePtr=NULL;    /* map rule # to RuleBlk node of rule */
  286. ListNode *ExprOrder=NULL;    /* list of exprs as they are found in grammar */
  287. ListNode *BeforeActions=NULL;/* list of grammar actions before rules */
  288. ListNode *AfterActions=NULL;/* list of grammar actions after rules */
  289. ListNode *LexActions=NULL;    /* list of lexical actions */
  290. ListNode **Cycles=NULL;        /* list of cycles (for each k) found when
  291.                                doing FOLLOWs */
  292. ListNode *eclasses=NULL;    /* list of error classes */
  293. LClass     lclass[MaxLexClasses]; /* array of lex class definitions */
  294. int         CurrentLexClass;    /* index into lclass */
  295. int         NumLexClasses=0;    /* in range 1..MaxLexClasses (init 0) */
  296.  
  297. char    *HdrAction=NULL;    /* action defined with #header */
  298. FILE    *ErrFile;            /* sets and error recovery stuff */
  299. FILE    *DefFile=NULL;        /* list of tokens, return value structs, setwd defs */
  300. int        CannotContinue=FALSE;
  301. int        OutputLL_k = 1;        /* LL_k for parsing must be power of 2 */
  302. int        action_file;        /* used to track start of action */
  303. int        action_line;
  304. int        FoundGuessBlk=0;    /* there is a (...)? block somewhere in grammar */
  305. int        pLevel=0;            /* print Level */
  306. int        pAlt1,pAlt2;        /* print "==>" in front of these alts */
  307.  
  308.                     /* C m d - L i n e  O p t i o n s */
  309.  
  310. int        LL_k=1;                /* how many tokens of full lookahead */
  311. int        CLL_k= -1;            /* how many tokens of compressed lookahead */
  312. int        PrintOut = FALSE;    /* print out the grammar */
  313. int        PrintAnnotate = FALSE;/* annotate printout with FIRST sets */
  314. int        CodeGen=TRUE;        /* Generate output code? */
  315. int        LexGen=TRUE;        /* Generate lexical files? (tokens.h, parser.dlg) */
  316. int        GenAST=FALSE;        /* Generate AST's? */
  317. int        GenANSI=FALSE;        /* Generate ANSI code where necessary */
  318. int        GenExprSets=TRUE;    /* use sets not (LA(1)==tok) expression lists */
  319. int        GenCR=FALSE;        /* Generate cross reference? */
  320. int        GenLineInfo=FALSE;    /* Generate # line "file" stuff? */
  321. int        TraceGen=FALSE;        /* Generate code to trace rule invocation */
  322. int        elevel=1;            /* error level for ambiguity messages */
  323. int        GenEClasseForRules=0;/* don't generate eclass for each rule */
  324. int        TreeResourceLimit= -1;/* don't limit tree resource */
  325. int        DemandLookahead = 0;/* demand/delayed lookahead or not */
  326. char    *RulePrefix = "";    /* prefix each generated rule with this */
  327. char    *stdpccts = "stdpccts.h";/* where to generate std pccts include file */
  328. int        GenStdPccts = 0;    /* don't gen stdpccts.h? */
  329. int        ParseWithPredicates = 1;
  330. int        WarningLevel = 1;
  331. int        AImode = 0;            /* Ambiguity interactive */
  332. int        HoistPredicateContext = 0;
  333.