lex.c revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/*
*
* SOFTWARE RIGHTS
*
* We reserve no LEGAL rights to the Purdue Compiler Construction Tool
* Set (PCCTS) -- PCCTS is in the public domain. An individual or
* company may do whatever they wish with source code distributed with
* PCCTS or the code generated by PCCTS, including the incorporation of
* PCCTS, or its output, into commerical software.
*
* We encourage users to develop software with PCCTS. However, we do ask
* that credit is given to us for developing PCCTS. 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 PCCTS and have developed a nice tool with the
* output, please mention that you developed it using PCCTS. 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.
*
* ANTLR 1.33
* Terence Parr
* Parr Research Corporation
* with Purdue University and AHPCRC, University of Minnesota
* 1989-2001
*/
#include <stdio.h>
#include <ctype.h>
/* MR1 */
/* MR1 10-Apr-97 MR1 Replace use of __STDC__ with __USE_PROTOS */
/* MR1 */
#include "pcctscfg.h"
#include "set.h"
#include "syn.h"
#include "hash.h"
#include "generic.h"
#define DLGErrorString "invalid token"
/* Generate a complete lexical description of the lexemes found in the grammar */
void
#ifdef __USE_PROTOS
genLexDescr( void )
#else
genLexDescr( )
#endif
{
ListNode *p;
#ifdef SPECIAL_FOPEN
#endif
if ( GenCC )
{
}
else
{
if ( FoundGuessBlk )
{
}
if (TraceGen) {
};
if ( GenAST ) {
}
if ( UserDefdTokens )
/* still need this one as it has the func prototypes */
}
/* dump all actions */
/* MR1 */
/* MR1 11-Apr-97 Provide mechanism for inserting code into DLG class */
/* MR1 via <<%%lexmember ....>> & <<%%lexprefix ...>> */
/* MR1 */
if (LexActions != NULL) {
{
}
};
/* MR1 */ if (GenCC) {
/* MR1 */ };
/* MR1 */ {
/* MR1 */ }
/* MR1 */ };
/* MR1 */ {
/* MR1 */ }
/* MR1 */ };
warnNoFL("no regular expressions found in grammar");
}
else dumpLexClasses(dlgFile);
}
/* For each lexical class, scan ExprOrder looking for expressions
* in that lexical class. Print out only those that match.
* Each element of the ExprOrder list has both an expr and an lclass
* field.
*/
void
#ifdef __USE_PROTOS
#else
#endif
{
int i;
TermEntry *t;
ListNode *p;
Expr *q;
for (i=0; i<NumLexClasses; i++)
{
{
if ( q->lclass != i ) continue;
lexmode(i);
/* replace " killed by StripQuotes() */
if ( !GenCC ) {
else
}
if ( GenCC ) {
else
}
}
}
}
/* Strip the leading path (if any) from a filename */
char *
#ifdef __USE_PROTOS
#else
char *fileName;
#endif
{
char *p;
p++;
else
p = fileName;
return(p);
}
/* Generate a list of #defines && list of struct definitions for
* aggregate retv's */
void
#ifdef __USE_PROTOS
genDefFile( void )
#else
genDefFile( )
#endif
{
int i;
/* If C++ mode and #tokdef used, then don't need anything in here since
* C++ puts all definitions in the class file name.
*/
if ( GenCC && UserTokenDefsFile ) return;
if ( MR_Inhibit_Tokens_h_Gen) return;
#ifdef SPECIAL_FOPEN
#endif
}
if ( !UserDefdTokens )
{
int first=1;
for (i=1; i<TokenNum; i++)
{
/* Don't do EpToken or expr w/o labels */
{
TermEntry *p;
if ( WarningLevel>1 )
{
int j;
/* look in all lexclasses for the reg expr */
/* MR10 Derek Pappas */
/* MR10 A #tokclass doesn't have associated regular expressiones */
/* MR10 so don't warn user about it's omission */
for (j=0; j<NumLexClasses; j++)
{
lexmode(j);
if ( ExprString(i)!=NULL ) break;
}
if ( j>=NumLexClasses )
{
}
};
}
"token not in sym tab when it should be");
if ( !p->classname )
{
if ( GenCC ) {
first = 0;
}
else
}
}
}
/* MR1 */
/* MR1 10-Apr-97 133MR1 Prevent use of varying sizes of integer */
/* MR1 for the enum ANTLRTokenType */
/* MR1 */
if ( GenCC ) { /* MR1 */
}; /* MR1 */
}
}
void
#ifdef __USE_PROTOS
GenRemapFile( void )
#else
GenRemapFile( )
#endif
{
{
FILE *f;
int i;
#ifdef SPECIAL_FOPEN
#endif
fprintf(f, " *\n");
fprintf(f, " * Generated from:");
fprintf(f, "\n");
fprintf(f, " *\n");
fprintf(f, " * Terence Parr, Will Cohen, and Hank Dietz: 1989-2001\n");
fprintf(f, " * Purdue University Electrical Engineering\n");
fprintf(f, " */\n");
GenRuleFuncRedefs(f, SynDiag);
if ( GenAST ) GenASTSymbolRedefs(f);
GenSetRedefs(f);
fclose(f);
}
}
/* Generate a bunch of #defines that rename all functions to be "ParserName_func" */
void
#ifdef __USE_PROTOS
#else
GenRuleFuncRedefs( f, p )
FILE *f;
Junction *p;
#endif
{
fprintf(f, "\n/* rename rule functions to be 'ParserName_func' */\n");
while ( p!=NULL )
{
}
}
/* Generate a bunch of #defines that rename all standard symbols to be
* "ParserName_symbol". The list of standard symbols to change is in
* globals.c.
*/
void
#ifdef __USE_PROTOS
#else
FILE *f;
#endif
{
char **p;
fprintf(f, "\n/* rename PCCTS-supplied symbols to be 'ParserName_symbol' */\n");
for (p = &StandardSymbols[0]; *p!=NULL; p++)
{
}
}
/* Generate a bunch of #defines that rename all AST symbols to be
* "ParserName_symbol". The list of AST symbols to change is in
* globals.c.
*/
void
#ifdef __USE_PROTOS
GenASTSymbolRedefs( FILE *f )
#else
GenASTSymbolRedefs( f )
FILE *f;
#endif
{
char **p;
fprintf(f, "\n/* rename PCCTS-supplied AST symbols to be 'ParserName_symbol' */\n");
for (p = &ASTSymbols[0]; *p!=NULL; p++)
{
}
}
/* redefine all sets generated by ANTLR; WARNING: 'zzerr', 'setwd' must match
* use in bits.c (DumpSetWd() etc...)
*/
void
#ifdef __USE_PROTOS
GenSetRedefs( FILE *f )
#else
GenSetRedefs( f )
FILE *f;
#endif
{
int i;
for (i=1; i<=wordnum; i++)
{
}
for (i=1; i<=esetnum; i++)
{
}
}
/* Find all return types/parameters that require structs and def
* all rules with ret types.
*
* This is for the declaration, not the definition.
*/
void
#ifdef __USE_PROTOS
#else
GenRulePrototypes( f, p )
FILE *f;
Junction *p;
#endif
{
int i;
i = 1;
while ( p!=NULL )
{
{
{
DumpRetValStruct(f, p->ret, i);
}
fprintf(f, "\n#ifdef __USE_PROTOS\n");
{
fprintf(f, "extern struct _rv%d", i);
}
else
{
fprintf(f, "extern ");
}
fprintf(f, ";\n");
fprintf(f, "#else\n");
{
fprintf(f, "extern struct _rv%d", i);
}
else
{
fprintf(f, "extern ");
}
fprintf(f, "#endif\n");
}
else
{
fprintf(f, "\n#ifdef __USE_PROTOS\n");
fprintf(f, ";\n");
#ifdef OLD
{
if ( GenAST ) {
}
}
else fprintf(f, "void");
fprintf(f, ");\n");
#endif
fprintf(f, "#else\n");
fprintf(f, "#endif\n");
}
i++;
}
}
/* Define all rules in the class.h file; generate any required
* struct definitions first, however.
*/
void
#ifdef __USE_PROTOS
#else
GenRuleMemberDeclarationsForCC( f, q )
FILE *f;
Junction *q;
#endif
{
Junction *p = q;
int i;
fprintf(f, "private:\n");
/* Dump dflt handler declaration */
fprintf(f, "\tvoid zzdflthandlers( int _signal, int *_retsignal );\n\n");
fprintf(f, "public:\n");
/* Dump return value structs */
i = 1;
while ( p!=NULL )
{
{
{
DumpRetValStruct(f, p->ret, i);
}
}
i++;
}
/* Dump member func defs && CONSTRUCTOR */
/*
fprintf(f, "\t%s(ANTLRTokenBuffer *input, ANTLRTokenType eof);\n",
CurrentClassName);
*/
i = 1;
p = q;
while ( p!=NULL )
{
{
{
fprintf(f, "\tstruct _rv%d", i);
}
else
{
fprintf(f, "\t");
}
fprintf(f, ";\n");
#ifdef OLD
{
}
fprintf(f, ");\n");
#endif
}
else
{
fprintf(f, ";\n");
#ifdef OLD
{
}
fprintf(f, ");\n");
#endif
}
i++;
}
}
/* Given a list of ANSI-style parameter declarations, print out a
* comma-separated list of the symbols (w/o types).
* Basically, we look for a comma, then work backwards until start of
* the symbol name. Then print it out until 1st non-alnum char. Now,
* move on to next parameter.
*
*/
/* MR5 Jan Mikkelsen 26-May-97 - added initalComma parameter */
void
#ifdef __USE_PROTOS
#else
char *pdecl; /* MR5 */
int initialComma; /* MR5 */
#endif
{
while ( !done )
{
firstTime = 0;
}
}
/* given a list of parameters or return values, dump the next
* name to output. Return 1 if last one just printed, 0 if more to go.
*/
/* MR23 Total rewrite */
int
#ifdef __USE_PROTOS
#else
DumpNextNameInDef( q, output )
char **q;
#endif
{
char *p;
char *t;
char *pDataType;
char *pSymbol;
char *pEqualSign;
char *pValue;
char *pSeparator;
int nest = 0;
p = endFormal(*q,
&pSymbol,
&pValue,
&nest);
/* MR26 Handle rule arguments such as: IIR_Bool (IIR_Decl::*contstraint)()
For this we need to strip off anything which follows the symbol.
*/
/* MR26 */ t = pSymbol;
/* MR26 */ if (t != NULL) {
/* MR26 */ for (t = pSymbol; *t != 0; t++) {
/* MR26 */ }
/* MR26 */ }
*q = p;
return (*pSeparator == 0);
}
/* Given a list of ANSI-style parameter declarations, dump K&R-style
* declarations, one per line for each parameter. Basically, convert
* comma to semi-colon, newline.
*/
void
#ifdef __USE_PROTOS
#else
char *pdecl;
#endif
{
while ( *pdecl != '\0' )
{
if ( *pdecl == ',' )
{
pdecl++;
}
}
}
/* Take in a type definition (type + symbol) and print out type only */
/* MR23 Total rewrite */
void
#ifdef __USE_PROTOS
#else
DumpType( s, f )
char *s;
FILE *f;
#endif
{
char *p;
char *pDataType;
char *pSymbol;
char *pEqualSign;
char *pValue;
char *pSeparator;
int nest = 0;
p = endFormal(s,
&pSymbol,
&pValue,
&nest);
}
/* check to see if string e is a word in string s */
int
#ifdef __USE_PROTOS
strmember( char *s, char *e )
#else
strmember( s, e )
char *s;
char *e;
#endif
{
register char *p;
if ( *e=='\0' ) return 1; /* empty string is always member */
do {
++s;
p = e;
while ( *p!='\0' && *p==*s ) {p++; s++;}
if ( *p=='\0' ) {
if ( *s=='\0' ) return 1;
}
while ( isalnum(*s) || *s == '_' )
++s;
} while ( *s!='\0' );
return 0;
}
#if 0
/* MR23 Replaced by hasMultipleOperands() */
int
#ifdef __USE_PROTOS
HasComma( char *s )
#else
HasComma( s )
char *s;
#endif
{
while (*s!='\0')
if ( *s++ == ',' ) return 1;
return 0;
}
#endif
/* MR23 Total rewrite */
void
#ifdef __USE_PROTOS
#else
DumpRetValStruct( f, ret, i )
FILE *f;
char *ret;
int i;
#endif
{
char *p = ret;
char *pDataType;
char *pSymbol;
char *pEqualSign;
char *pValue;
char *pSeparator;
int nest = 0;
fprintf(f, "\nstruct _rv%d {\n", i);
while (*p != 0 && nest == 0) {
p = endFormal(p,
&pSymbol,
&pValue,
&nest);
fprintf(f,"\t");
fprintf(f," ");
fprintf(f,";\n");
}
fprintf(f,"};\n");
}
/* given "s" yield s -- DESTRUCTIVE (we modify s if starts with " else return s) */
char *
#ifdef __USE_PROTOS
StripQuotes( char *s )
#else
StripQuotes( s )
char *s;
#endif
{
if ( *s == '"' )
{
return( s+1 ); /* return address past initial quote */
}
return( s );
}