summaryrefslogtreecommitdiff
path: root/src/lemon.c
diff options
context:
space:
mode:
authorArno Töll <arno@debian.org>2012-11-21 23:57:42 +0100
committerArno Töll <arno@debian.org>2012-11-21 23:57:42 +0100
commit47236ea4c1d5601fc6bea1b280d36152ec5ff32b (patch)
tree89c109c0bef460fa747a3413610034002fac1cb4 /src/lemon.c
parentdae92c799e85ec3b65f46da6a776e4386bc99d6c (diff)
downloadlighttpd-47236ea4c1d5601fc6bea1b280d36152ec5ff32b.tar.gz
Imported Upstream version 1.4.32
Diffstat (limited to 'src/lemon.c')
-rw-r--r--src/lemon.c118
1 files changed, 71 insertions, 47 deletions
diff --git a/src/lemon.c b/src/lemon.c
index dd87cdf..c3d72eb 100644
--- a/src/lemon.c
+++ b/src/lemon.c
@@ -12,12 +12,26 @@
#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)
@@ -25,8 +39,14 @@ extern int atoi();
# endif
#endif
+#if __GNUC__ > 2
+#define NORETURN __attribute__ ((__noreturn__))
+#else
+#define NORETURN
+#endif
+
/* #define PRIVATE static */
-#define PRIVATE
+#define PRIVATE static
#ifdef TEST
#define MAXRHS 5 /* Set low to exercise exception code */
@@ -37,13 +57,15 @@ extern int atoi();
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();
+void myassert() NORETURN;
#ifndef NDEBUG
# define assert(X) if(!(X))myassert(__FILE__,__LINE__)
#else
@@ -278,7 +300,6 @@ struct lemon {
};
#define MemoryCheck(X) if((X)==0){ \
- extern void memory_error(); \
memory_error(); \
}
@@ -336,10 +357,10 @@ void Configtable_clear(/* int(*)(struct config *) */);
/* Allocate a new parser action */
struct action *Action_new(){
- static struct action *freelist = 0;
+ static struct action *freelist = NULL;
struct action *new;
- if( freelist==0 ){
+ if( freelist==NULL ){
int i;
int amt = 100;
freelist = (struct action *)malloc( sizeof(struct action)*amt );
@@ -383,7 +404,7 @@ void Action_add(app,type,sp,arg)
struct action **app;
enum e_action type;
struct symbol *sp;
-char *arg;
+void *arg;
{
struct action *new;
new = Action_new();
@@ -432,14 +453,16 @@ struct acttab {
#define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
/* Free all memory associated with the given acttab */
-void acttab_free(acttab *p){
+/*
+PRIVATE void acttab_free(acttab *p){
free( p->aAction );
free( p->aLookahead );
free( p );
}
+*/
/* Allocate a new acttab structure */
-acttab *acttab_alloc(void){
+PRIVATE acttab *acttab_alloc(void){
acttab *p = malloc( sizeof(*p) );
if( p==0 ){
fprintf(stderr,"Unable to allocate memory for a new acttab.");
@@ -451,7 +474,7 @@ acttab *acttab_alloc(void){
/* Add a new action to the current transaction set
*/
-void acttab_action(acttab *p, int lookahead, int action){
+PRIVATE void acttab_action(acttab *p, int lookahead, int action){
if( p->nLookahead>=p->nLookaheadAlloc ){
p->nLookaheadAlloc += 25;
p->aLookahead = realloc( p->aLookahead,
@@ -484,7 +507,7 @@ void acttab_action(acttab *p, int lookahead, int action){
**
** Return the offset into the action table of the new transaction.
*/
-int acttab_insert(acttab *p){
+PRIVATE int acttab_insert(acttab *p){
int i, j, k, n;
assert( p->nLookahead>0 );
@@ -579,7 +602,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
@@ -869,7 +892,7 @@ struct lemon *lemp;
cfp->status = INCOMPLETE;
}
}
-
+
do{
progress = 0;
for(i=0; i<lemp->nstate; i++){
@@ -900,7 +923,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.
*/
@@ -983,6 +1006,7 @@ 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;
@@ -1017,7 +1041,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 ||
@@ -1315,7 +1339,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);
}
@@ -1347,10 +1371,11 @@ 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");
@@ -1589,7 +1614,6 @@ 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++){
@@ -1651,7 +1675,7 @@ FILE *err;
}else if( op[j].type==OPT_FLAG ){
*((int*)op[j].arg) = v;
}else if( op[j].type==OPT_FFLAG ){
- (*(void(*)())(op[j].arg))(v);
+ (*(void(*)())(intptr_t)(op[j].arg))(v);
}else{
if( err ){
fprintf(err,"%smissing argument on switch.\n",emsg);
@@ -1733,19 +1757,19 @@ FILE *err;
*(double*)(op[j].arg) = dv;
break;
case OPT_FDBL:
- (*(void(*)())(op[j].arg))(dv);
+ (*(void(*)())(intptr_t)(op[j].arg))(dv);
break;
case OPT_INT:
*(int*)(op[j].arg) = lv;
break;
case OPT_FINT:
- (*(void(*)())(op[j].arg))((int)lv);
+ (*(void(*)())(intptr_t)(op[j].arg))((int)lv);
break;
case OPT_STR:
*(char**)(op[j].arg) = sv;
break;
case OPT_FSTR:
- (*(void(*)())(op[j].arg))(sv);
+ (*(void(*)())(intptr_t)(op[j].arg))(sv);
break;
}
}
@@ -2031,7 +2055,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,
@@ -2286,10 +2310,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;
@@ -2317,6 +2341,7 @@ 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;
}
@@ -2324,6 +2349,7 @@ 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;
}
@@ -2546,7 +2572,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;
@@ -2588,7 +2614,7 @@ struct lemon *lemp;
}
}
-void ConfigPrint(fp,cfp)
+PRIVATE void ConfigPrint(fp,cfp)
FILE *fp;
struct config *cfp;
{
@@ -2625,7 +2651,7 @@ struct lemon *lemp;
}
/* Print a plink chain */
-PRIVATE void PlinkPrint(out,plp,tag)
+void PlinkPrint(out,plp,tag)
FILE *out;
struct plink *plp;
char *tag;
@@ -2642,7 +2668,7 @@ char *tag;
/* Print an action to the given file descriptor. Return FALSE if
** nothing was actually printed.
*/
-int PrintAction(struct action *ap, FILE *fp, int indent){
+PRIVATE int PrintAction(struct action *ap, FILE *fp, int indent){
int result = 1;
switch( ap->type ){
case SHIFT:
@@ -2716,6 +2742,7 @@ 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)
@@ -2726,7 +2753,6 @@ int modemask;
char *pathlist;
char *path,*cp;
char c;
- extern int access();
#ifdef __WIN32__
cp = strrchr(argv0,'\\');
@@ -2740,7 +2766,6 @@ 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 );
@@ -2822,7 +2847,7 @@ int *lineno;
PRIVATE FILE *tplt_open(lemp)
struct lemon *lemp;
{
-
+
char buf[1000];
FILE *in;
char *tpltname;
@@ -2879,7 +2904,7 @@ int *lineno;
** The following routine emits code for the destructor for the
** symbol sp
*/
-void emit_destructor_code(out,sp,lemp,lineno)
+PRIVATE void emit_destructor_code(out,sp,lemp,lineno)
FILE *out;
struct symbol *sp;
struct lemon *lemp;
@@ -2895,7 +2920,7 @@ int *lineno;
}else if( sp->destructor ){
cp = sp->destructor;
fprintf(out,"#line %d \"%s\"\n{",sp->destructorln,lemp->filename);
- }else if( lemp->vardest ){
+ }else{
cp = lemp->vardest;
if( cp==0 ) return;
fprintf(out,"#line %d \"%s\"\n{",lemp->vardestln,lemp->filename);
@@ -2917,7 +2942,7 @@ int *lineno;
/*
** Return TRUE (non-zero) if the given symbol has a destructor.
*/
-int has_destructor(sp, lemp)
+PRIVATE int has_destructor(sp, lemp)
struct symbol *sp;
struct lemon *lemp;
{
@@ -2930,7 +2955,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.
*/
@@ -3018,13 +3043,13 @@ int *lineno;
** union, also set the ".dtnum" field of every terminal and nonterminal
** symbol.
*/
-void print_stack_union(out,lemp,plineno,mhflag)
+PRIVATE 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 = *plineno; /* The line number of the output */
+ int lineno; /* 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. */
@@ -3384,7 +3409,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++){
@@ -3405,7 +3430,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++){
@@ -3480,7 +3505,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 ){
@@ -3522,7 +3547,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.
@@ -3589,7 +3614,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;
}
@@ -3630,7 +3655,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;
@@ -3669,7 +3694,6 @@ 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;
@@ -3781,7 +3805,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);
@@ -3943,7 +3967,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);
@@ -4149,7 +4173,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);
@@ -4295,7 +4319,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);