diff options
author | Arno Töll <arno@debian.org> | 2012-11-21 23:04:05 +0100 |
---|---|---|
committer | Arno Töll <arno@debian.org> | 2012-11-21 23:04:05 +0100 |
commit | e787ca6640e45e5c28913d149b0ecd9810930b8a (patch) | |
tree | 0864b0dfde2872cf629fc0402f9dd2b8ef6c5f9c /src/lemon.c | |
parent | 6c8b72678daa33f7e9df2a97d6404380e1b2e8ca (diff) | |
download | lighttpd-e787ca6640e45e5c28913d149b0ecd9810930b8a.tar.gz |
Imported Upstream version 1.4.8upstream/1.4.8
Diffstat (limited to 'src/lemon.c')
-rw-r--r-- | src/lemon.c | 112 |
1 files changed, 44 insertions, 68 deletions
diff --git a/src/lemon.c b/src/lemon.c index 48df45f..dd87cdf 100644 --- a/src/lemon.c +++ b/src/lemon.c @@ -12,26 +12,12 @@ #include <ctype.h> #include <stdlib.h> -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#ifdef HAVE_STDINT_H -# include <stdint.h> -#endif -#ifdef HAVE_INTTYPES_H -# include <inttypes.h> -#endif - -#define UNUSED(x) ( (void)(x) ) - extern void qsort(); extern double strtod(); extern long strtol(); extern void free(); extern int access(); extern int atoi(); -extern char *getenv(); #ifndef __WIN32__ # if defined(_WIN32) || defined(WIN32) @@ -39,14 +25,8 @@ extern char *getenv(); # endif #endif -#if __GNUC__ > 2 -#define NORETURN __attribute__ ((__noreturn__)) -#else -#define NORETURN -#endif - /* #define PRIVATE static */ -#define PRIVATE static +#define PRIVATE #ifdef TEST #define MAXRHS 5 /* Set low to exercise exception code */ @@ -57,15 +37,13 @@ extern char *getenv(); char *msort(); extern void *malloc(); -extern void memory_error() NORETURN; - /******** From the file "action.h" *************************************/ struct action *Action_new(); struct action *Action_sort(); void Action_add(); /********* From the file "assert.h" ************************************/ -void myassert() NORETURN; +void myassert(); #ifndef NDEBUG # define assert(X) if(!(X))myassert(__FILE__,__LINE__) #else @@ -300,6 +278,7 @@ struct lemon { }; #define MemoryCheck(X) if((X)==0){ \ + extern void memory_error(); \ memory_error(); \ } @@ -453,16 +432,14 @@ struct acttab { #define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead) /* Free all memory associated with the given acttab */ -/* -PRIVATE void acttab_free(acttab *p){ +void acttab_free(acttab *p){ free( p->aAction ); free( p->aLookahead ); free( p ); } -*/ /* Allocate a new acttab structure */ -PRIVATE acttab *acttab_alloc(void){ +acttab *acttab_alloc(void){ acttab *p = malloc( sizeof(*p) ); if( p==0 ){ fprintf(stderr,"Unable to allocate memory for a new acttab."); @@ -474,7 +451,7 @@ PRIVATE acttab *acttab_alloc(void){ /* Add a new action to the current transaction set */ -PRIVATE void acttab_action(acttab *p, int lookahead, int action){ +void acttab_action(acttab *p, int lookahead, int action){ if( p->nLookahead>=p->nLookaheadAlloc ){ p->nLookaheadAlloc += 25; p->aLookahead = realloc( p->aLookahead, @@ -507,7 +484,7 @@ PRIVATE void acttab_action(acttab *p, int lookahead, int action){ ** ** Return the offset into the action table of the new transaction. */ -PRIVATE int acttab_insert(acttab *p){ +int acttab_insert(acttab *p){ int i, j, k, n; assert( p->nLookahead>0 ); @@ -602,7 +579,7 @@ int line; */ /* Find a precedence symbol of every rule in the grammar. -** +** ** Those rules which have a precedence symbol coded in the input ** grammar using the "[symbol]" construct will already have the ** rp->precsym field filled. Other rules take as their precedence @@ -892,7 +869,7 @@ struct lemon *lemp; cfp->status = INCOMPLETE; } } - + do{ progress = 0; for(i=0; i<lemp->nstate; i++){ @@ -923,7 +900,7 @@ struct lemon *lemp; struct symbol *sp; struct rule *rp; - /* Add all of the reduce actions + /* Add all of the reduce actions ** A reduce action is added for each element of the followset of ** a configuration which has its dot at the extreme right. */ @@ -1006,7 +983,6 @@ struct symbol *errsym; /* The error symbol (if defined. NULL otherwise) */ { struct symbol *spx, *spy; int errcnt = 0; - UNUSED(errsym); assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */ if( apx->type==SHIFT && apy->type==REDUCE ){ spx = apx->sp; @@ -1041,7 +1017,7 @@ struct symbol *errsym; /* The error symbol (if defined. NULL otherwise) */ apx->type = RD_RESOLVED; } }else{ - assert( + assert( apx->type==SH_RESOLVED || apx->type==RD_RESOLVED || apx->type==CONFLICT || @@ -1339,7 +1315,7 @@ void ErrorMsg(const char *filename, int lineno, const char *format, ...){ /* Report an out-of-memory condition and abort. This function ** is used mostly by the "MemoryCheck" macro in struct.h */ -void memory_error() { +void memory_error(){ fprintf(stderr,"Out of memory. Aborting...\n"); exit(1); } @@ -1371,11 +1347,10 @@ char **argv; struct lemon lem; char *def_tmpl_name = "lempar.c"; - UNUSED(argc); OptInit(argv,options,stderr); if( version ){ printf("Lemon version 1.0\n"); - exit(0); + exit(0); } if( OptNArgs() < 1 ){ fprintf(stderr,"Exactly one filename argument is required.\n"); @@ -1614,6 +1589,7 @@ int k; FILE *err; { int spcnt, i; + spcnt = 0; if( argv[0] ) fprintf(err,"%s",argv[0]); spcnt = strlen(argv[0]) + 1; for(i=1; i<n && argv[i]; i++){ @@ -1675,7 +1651,7 @@ FILE *err; }else if( op[j].type==OPT_FLAG ){ *((int*)op[j].arg) = v; }else if( op[j].type==OPT_FFLAG ){ - (*(void(*)())(intptr_t)(op[j].arg))(v); + (*(void(*)())(op[j].arg))(v); }else{ if( err ){ fprintf(err,"%smissing argument on switch.\n",emsg); @@ -1757,19 +1733,19 @@ FILE *err; *(double*)(op[j].arg) = dv; break; case OPT_FDBL: - (*(void(*)())(intptr_t)(op[j].arg))(dv); + (*(void(*)())(op[j].arg))(dv); break; case OPT_INT: *(int*)(op[j].arg) = lv; break; case OPT_FINT: - (*(void(*)())(intptr_t)(op[j].arg))((int)lv); + (*(void(*)())(op[j].arg))((int)lv); break; case OPT_STR: *(char**)(op[j].arg) = sv; break; case OPT_FSTR: - (*(void(*)())(intptr_t)(op[j].arg))(sv); + (*(void(*)())(op[j].arg))(sv); break; } } @@ -2055,7 +2031,7 @@ to follow the previous rule."); case IN_RHS: if( x[0]=='.' ){ struct rule *rp; - rp = (struct rule *)malloc( sizeof(struct rule) + + rp = (struct rule *)malloc( sizeof(struct rule) + sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs ); if( rp==0 ){ ErrorMsg(psp->filename,psp->tokenlineno, @@ -2310,10 +2286,10 @@ to follow the previous rule."); ** token is passed to the function "parseonetoken" which builds all ** the appropriate data structures in the global state vector "gp". */ -struct pstate ps; void Parse(gp) struct lemon *gp; { + struct pstate ps; FILE *fp; char *filebuf; size_t filesize; @@ -2341,7 +2317,6 @@ struct lemon *gp; if( filebuf==0 ){ ErrorMsg(ps.filename,0,"Can't allocate %d of memory to hold this file.", filesize+1); - fclose(fp); gp->errorcnt++; return; } @@ -2349,7 +2324,6 @@ struct lemon *gp; ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.", filesize); free(filebuf); - fclose(fp); gp->errorcnt++; return; } @@ -2572,7 +2546,7 @@ char *mode; return fp; } -/* Duplicate the input file without comments and without actions +/* Duplicate the input file without comments and without actions ** on rules */ void Reprint(lemp) struct lemon *lemp; @@ -2614,7 +2588,7 @@ struct lemon *lemp; } } -PRIVATE void ConfigPrint(fp,cfp) +void ConfigPrint(fp,cfp) FILE *fp; struct config *cfp; { @@ -2651,7 +2625,7 @@ struct lemon *lemp; } /* Print a plink chain */ -void PlinkPrint(out,plp,tag) +PRIVATE void PlinkPrint(out,plp,tag) FILE *out; struct plink *plp; char *tag; @@ -2668,7 +2642,7 @@ char *tag; /* Print an action to the given file descriptor. Return FALSE if ** nothing was actually printed. */ -PRIVATE int PrintAction(struct action *ap, FILE *fp, int indent){ +int PrintAction(struct action *ap, FILE *fp, int indent){ int result = 1; switch( ap->type ){ case SHIFT: @@ -2742,7 +2716,6 @@ struct lemon *lemp; return; } - extern int access(); /* Search for the file "name" which is in the same directory as ** the exacutable */ PRIVATE char *pathsearch(argv0,name,modemask) @@ -2753,6 +2726,7 @@ int modemask; char *pathlist; char *path,*cp; char c; + extern int access(); #ifdef __WIN32__ cp = strrchr(argv0,'\\'); @@ -2766,6 +2740,7 @@ int modemask; if( path ) sprintf(path,"%s/%s",argv0,name); *cp = c; }else{ + extern char *getenv(); pathlist = getenv("PATH"); if( pathlist==0 ) pathlist = ".:/bin:/usr/bin"; path = (char *)malloc( strlen(pathlist)+strlen(name)+2 ); @@ -2847,7 +2822,7 @@ int *lineno; PRIVATE FILE *tplt_open(lemp) struct lemon *lemp; { - + char buf[1000]; FILE *in; char *tpltname; @@ -2904,7 +2879,7 @@ int *lineno; ** The following routine emits code for the destructor for the ** symbol sp */ -PRIVATE void emit_destructor_code(out,sp,lemp,lineno) +void emit_destructor_code(out,sp,lemp,lineno) FILE *out; struct symbol *sp; struct lemon *lemp; @@ -2920,7 +2895,7 @@ int *lineno; }else if( sp->destructor ){ cp = sp->destructor; fprintf(out,"#line %d \"%s\"\n{",sp->destructorln,lemp->filename); - }else{ + }else if( lemp->vardest ){ cp = lemp->vardest; if( cp==0 ) return; fprintf(out,"#line %d \"%s\"\n{",lemp->vardestln,lemp->filename); @@ -2942,7 +2917,7 @@ int *lineno; /* ** Return TRUE (non-zero) if the given symbol has a destructor. */ -PRIVATE int has_destructor(sp, lemp) +int has_destructor(sp, lemp) struct symbol *sp; struct lemon *lemp; { @@ -2955,7 +2930,7 @@ struct lemon *lemp; return ret; } -/* +/* ** Generate code which executes when the rule "rp" is reduced. Write ** the code to "out". Make sure lineno stays up-to-date. */ @@ -3043,13 +3018,13 @@ int *lineno; ** union, also set the ".dtnum" field of every terminal and nonterminal ** symbol. */ -PRIVATE void print_stack_union(out,lemp,plineno,mhflag) +void print_stack_union(out,lemp,plineno,mhflag) FILE *out; /* The output stream */ struct lemon *lemp; /* The main info structure for this parser */ int *plineno; /* Pointer to the line number */ int mhflag; /* True if generating makeheaders output */ { - int lineno; /* The line number of the output */ + int lineno = *plineno; /* The line number of the output */ char **types; /* A hash table of datatypes */ int arraysize; /* Size of the "types" array */ int maxdtlength; /* Maximum length of any ".datatype" field. */ @@ -3409,7 +3384,7 @@ int mhflag; /* Output in makeheaders format if true */ /* Output the yy_shift_ofst[] table */ fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++; - fprintf(out, "static %s yy_shift_ofst[] = {\n", + fprintf(out, "static %s yy_shift_ofst[] = {\n", minimum_size_type(mnTknOfst-1, mxTknOfst)); lineno++; n = lemp->nstate; for(i=j=0; i<n; i++){ @@ -3430,7 +3405,7 @@ int mhflag; /* Output in makeheaders format if true */ /* Output the yy_reduce_ofst[] table */ fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++; - fprintf(out, "static %s yy_reduce_ofst[] = {\n", + fprintf(out, "static %s yy_reduce_ofst[] = {\n", minimum_size_type(mnNtOfst-1, mxNtOfst)); lineno++; n = lemp->nstate; for(i=j=0; i<n; i++){ @@ -3505,7 +3480,7 @@ int mhflag; /* Output in makeheaders format if true */ tplt_xfer(lemp->name,in,out,&lineno); /* Generate code which executes every time a symbol is popped from - ** the stack while processing errors or while destroying the parser. + ** the stack while processing errors or while destroying the parser. ** (In other words, generate the %destructor actions) */ if( lemp->tokendest ){ @@ -3547,7 +3522,7 @@ int mhflag; /* Output in makeheaders format if true */ tplt_print(out,lemp,lemp->overflow,lemp->overflowln,&lineno); tplt_xfer(lemp->name,in,out,&lineno); - /* Generate the table of rule information + /* Generate the table of rule information ** ** Note: This code depends on the fact that rules are number ** sequentually beginning with 0. @@ -3614,7 +3589,7 @@ struct lemon *lemp; for(i=1; i<lemp->nterminal; i++){ fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i); } - fclose(out); + fclose(out); } return; } @@ -3655,7 +3630,7 @@ struct lemon *lemp; rbest = rp; } } - + /* Do not make a default if the number of rules to default ** is not at least 2 */ if( nbest<2 ) continue; @@ -3694,6 +3669,7 @@ char *SetNew(){ int i; s = (char*)malloc( global_size ); if( s==0 ){ + extern void memory_error(); memory_error(); } for(i=0; i<global_size; i++) s[i] = 0; @@ -3805,7 +3781,7 @@ void Strsafe_init(){ if( x1a ){ x1a->size = 1024; x1a->count = 0; - x1a->tbl = (x1node*)malloc( + x1a->tbl = (x1node*)malloc( (sizeof(x1node) + sizeof(x1node*))*1024 ); if( x1a->tbl==0 ){ free(x1a); @@ -3967,7 +3943,7 @@ void Symbol_init(){ if( x2a ){ x2a->size = 128; x2a->count = 0; - x2a->tbl = (x2node*)malloc( + x2a->tbl = (x2node*)malloc( (sizeof(x2node) + sizeof(x2node*))*128 ); if( x2a->tbl==0 ){ free(x2a); @@ -4173,7 +4149,7 @@ void State_init(){ if( x3a ){ x3a->size = 128; x3a->count = 0; - x3a->tbl = (x3node*)malloc( + x3a->tbl = (x3node*)malloc( (sizeof(x3node) + sizeof(x3node*))*128 ); if( x3a->tbl==0 ){ free(x3a); @@ -4319,7 +4295,7 @@ void Configtable_init(){ if( x4a ){ x4a->size = 64; x4a->count = 0; - x4a->tbl = (x4node*)malloc( + x4a->tbl = (x4node*)malloc( (sizeof(x4node) + sizeof(x4node*))*64 ); if( x4a->tbl==0 ){ free(x4a); |