/* Driver template for the LEMON parser generator.
** The author disclaims copyright to this source code.
*/
/* First off, code is include which follows the "include" declaration
** in the input file. */
#include <stdio.h>
#line 7 "..\\pecl\\ffi\\ffi_parser.y"

/*
   +----------------------------------------------------------------------+
   | PHP Version 5                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2004 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.0 of the PHP license,       |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_0.txt.                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: Wez Furlong  <wez@thebrainroom.com>                          |
   +----------------------------------------------------------------------+
 */

/* $Id: ffi_parser.c,v 1.6 2004/01/12 13:47:40 wez Exp $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ffi.h"
#include "php_ffi_internal.h"

static const char *get_ident_string(php_ffi_ident id)
{
	static char idbuf[256];

	memcpy(idbuf, id.val, id.len);
	idbuf[id.len] = '\0';

	return idbuf;
}
	
const char *php_ffi_get_token_string(int major, php_ffi_tokentype t)
{
	static char tokbuf[512];
	char *c;

	switch (major) {
		case PHP_FFI_TOK_INTRINSIC:
			switch (t.type.intrinsic_type) {
				case FFI_TYPE_VOID:	c = "void"; break;
				case FFI_TYPE_INT: c= "int"; break;
				case FFI_TYPE_FLOAT: c = "float"; break;
				case FFI_TYPE_DOUBLE: c = "double"; break;
#if SIZEOF_LONG_DOUBLE != SIZEOF_DOUBLE
				case FFI_TYPE_LONGDOUBLE: c = "long double"; break;
#endif
				case FFI_TYPE_UINT8: c = "uint8"; break;
				case FFI_TYPE_SINT8: c = "sint8"; break;
				case FFI_TYPE_UINT16: c = "uint16"; break;
				case FFI_TYPE_SINT16: c = "sint16"; break;
				case FFI_TYPE_UINT32: c = "uint32"; break;
				case FFI_TYPE_SINT32: c = "sint32"; break;
				case FFI_TYPE_UINT64: c = "uint64"; break;
				case FFI_TYPE_SINT64: c = "sint64"; break;
				case FFI_TYPE_STRUCT: c = "struct"; break;
				case FFI_TYPE_POINTER: c = "pointer"; break;
				default:
					c = "unknown/invalid";
			}
			sprintf(tokbuf, "intrinsic type %d %s", t.type.intrinsic_type, c);
			break;
		case PHP_FFI_TOK_STRING:
			sprintf(tokbuf, "string: %s", get_ident_string(t.ident));
			break;

		case PHP_FFI_TOK_IDENT:
			sprintf(tokbuf, "identifier: %s", get_ident_string(t.ident));
			break;
		default:
#if ZEND_DEBUG
			sprintf(tokbuf, "token: %s", php_ffi_parserTokenName(major));
#else
			sprintf(tokbuf, "token: ???");
#endif
	}
	return tokbuf;
}

#line 94 "..\\pecl\\ffi\\ffi_parser.c"
/* Next is all token values, in a form suitable for use by makeheaders.
** This section will be null unless lemon is run with the -m switch.
*/
/* 
** These constants (all generated automatically by the parser generator)
** specify the various kinds of tokens (terminals) that the parser
** understands. 
**
** Each symbol here is a terminal symbol in the grammar.
*/
/* Make sure the INTERFACE macro is defined.
*/
#ifndef INTERFACE
# define INTERFACE 1
#endif
/* The next thing included is series of defines which control
** various aspects of the generated parser.
**    YYCODETYPE         is the data type used for storing terminal
**                       and nonterminal numbers.  "unsigned char" is
**                       used if there are fewer than 250 terminals
**                       and nonterminals.  "int" is used otherwise.
**    YYNOCODE           is a number of type YYCODETYPE which corresponds
**                       to no legal terminal or nonterminal number.  This
**                       number is used to fill in empty slots of the hash 
**                       table.
**    YYFALLBACK         If defined, this indicates that one or more tokens
**                       have fall-back values which should be used if the
**                       original value of the token will not parse.
**    YYACTIONTYPE       is the data type used for storing terminal
**                       and nonterminal numbers.  "unsigned char" is
**                       used if there are fewer than 250 rules and
**                       states combined.  "int" is used otherwise.
**    php_ffi_parserTOKENTYPE     is the data type used for minor tokens given 
**                       directly to the parser from the tokenizer.
**    YYMINORTYPE        is the data type used for all minor tokens.
**                       This is typically a union of many types, one of
**                       which is php_ffi_parserTOKENTYPE.  The entry in the union
**                       for base tokens is called "yy0".
**    YYSTACKDEPTH       is the maximum depth of the parser's stack.
**    php_ffi_parserARG_SDECL     A static variable declaration for the %extra_argument
**    php_ffi_parserARG_PDECL     A parameter declaration for the %extra_argument
**    php_ffi_parserARG_STORE     Code to store %extra_argument into yypParser
**    php_ffi_parserARG_FETCH     Code to extract %extra_argument from yypParser
**    YYNSTATE           the combined number of states.
**    YYNRULE            the number of rules in the grammar
**    YYERRORSYMBOL      is the code number of the error symbol.  If not
**                       defined, then do no error processing.
*/
/*  */
#define YYCODETYPE unsigned char
#define YYNOCODE 35
#define YYACTIONTYPE unsigned char
#define php_ffi_parserTOKENTYPE  php_ffi_tokentype 
typedef union {
  php_ffi_parserTOKENTYPE yy0;
  int yy69;
} YYMINORTYPE;
#define YYSTACKDEPTH 100
#define php_ffi_parserARG_SDECL  struct php_ffi_def_context *ctx ;
#define php_ffi_parserARG_PDECL , struct php_ffi_def_context *ctx 
#define php_ffi_parserARG_FETCH  struct php_ffi_def_context *ctx  = yypParser->ctx 
#define php_ffi_parserARG_STORE yypParser->ctx  = ctx 
#define YYNSTATE 60
#define YYNRULE 29
#define YYERRORSYMBOL 22
#define YYERRSYMDT yy69
#define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
#define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
#define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)

/* Next are that tables used to determine what action to take based on the
** current state and lookahead token.  These tables are used to implement
** functions that take a state number and lookahead value and return an
** action integer.  
**
** Suppose the action integer is N.  Then the action is determined as
** follows
**
**   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
**                                      token onto the stack and goto state N.
**
**   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
**
**   N == YYNSTATE+YYNRULE              A syntax error has occurred.
**
**   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
**
**   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
**                                      slots in the yy_action[] table.
**
** The action table is constructed as a single large table named yy_action[].
** Given state S and lookahead X, the action is computed as
**
**      yy_action[ yy_shift_ofst[S] + X ]
**
** If the index value yy_shift_ofst[S]+X is out of range or if the value
** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
** and that yy_default[S] should be used instead.  
**
** The formula above is for computing the action when the lookahead is
** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
** a reduce action) then the yy_reduce_ofst[] array is used in place of
** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
** YY_SHIFT_USE_DFLT.
**
** The following are the tables generated in this section:
**
**  yy_action[]        A single table containing all actions.
**  yy_lookahead[]     A table containing the lookahead for each entry in
**                     yy_action.  Used to detect hash collisions.
**  yy_shift_ofst[]    For each state, the offset into yy_action for
**                     shifting terminals.
**  yy_reduce_ofst[]   For each state, the offset into yy_action for
**                     shifting non-terminals after a reduce.
**  yy_default[]       Default action for each state.
*/
static YYACTIONTYPE yy_action[] = {
 /*     0 */    50,   25,   29,    7,   41,   47,   23,   24,   50,    5,
 /*    10 */     4,    7,    2,   90,    1,    6,   30,    5,    4,   62,
 /*    20 */     2,   28,    3,    6,   19,    8,   28,   50,   40,   19,
 /*    30 */     8,   19,    8,   40,   13,   56,   49,   87,   72,   13,
 /*    40 */    56,   17,   19,    8,   11,   19,    8,   83,   67,   10,
 /*    50 */    16,   34,   17,   68,   25,   17,   32,   38,   35,   23,
 /*    60 */    20,   53,   38,   35,   37,   38,   35,   36,   11,   42,
 /*    70 */    41,   43,   67,    9,    9,   86,   31,   18,   76,   61,
 /*    80 */    33,   85,   12,   15,   39,   73,   60,   21,   44,   75,
 /*    90 */    22,   45,   46,   71,   48,   82,   51,   26,   52,   14,
 /*   100 */    54,   27,   55,   57,   58,   59,
};
static YYCODETYPE yy_lookahead[] = {
 /*     0 */    18,   18,   18,   21,   25,   26,   23,   24,   18,   27,
 /*    10 */    28,   21,   30,   31,   32,   33,    5,   27,   28,    0,
 /*    20 */    30,    2,   32,   33,    5,    6,    2,   18,    9,    5,
 /*    30 */     6,    5,    6,    9,   15,   16,   27,   11,    5,   15,
 /*    40 */    16,   15,    5,    6,    1,    5,    6,   10,    5,    1,
 /*    50 */     7,   13,   15,    5,   18,   15,   17,   18,   19,   23,
 /*    60 */    24,   17,   18,   19,   17,   18,   19,    3,    1,    3,
 /*    70 */    25,   26,    5,   20,   20,   11,    8,    5,   12,    0,
 /*    80 */    11,   11,   29,   29,    5,    5,    0,   10,    5,   12,
 /*    90 */    13,    4,   14,    5,   12,   10,    5,    5,    8,    5,
 /*   100 */    11,   13,   13,   18,    5,   13,
};
#define YY_SHIFT_USE_DFLT (-1)
static signed char yy_shift_ofst[] = {
 /*     0 */    24,   86,   19,   79,   -1,   -1,   -1,   -1,   67,   48,
 /*    10 */    -1,   -1,   88,   94,   43,   33,   40,   72,   67,   80,
 /*    20 */    77,   77,   -1,   37,   85,   92,   88,   -1,   40,   11,
 /*    30 */    68,   26,   69,   38,   -1,   64,   26,   70,   79,   -1,
 /*    40 */    83,   66,   83,   77,   87,   78,   -1,   82,   40,   -1,
 /*    50 */    91,   90,   26,   89,   89,   -1,   40,   99,   92,   -1,
};
#define YY_REDUCE_USE_DFLT (-22)
static signed char yy_reduce_ofst[] = {
 /*     0 */   -18,  -22,  -10,  -22,  -22,  -22,  -22,  -22,   53,  -22,
 /*    10 */   -22,  -22,  -22,  -22,   54,  -22,   36,  -22,   54,  -22,
 /*    20 */   -22,  -22,  -22,  -17,  -22,  -22,  -22,  -22,  -16,  -22,
 /*    30 */   -22,   39,  -22,  -22,  -22,  -22,   47,  -22,  -22,  -22,
 /*    40 */   -21,  -22,   45,  -22,  -22,  -22,  -22,  -22,    9,  -22,
 /*    50 */   -22,  -22,   44,  -22,  -22,  -22,   85,  -22,  -22,  -22,
};
static YYACTIONTYPE yy_default[] = {
 /*     0 */    89,   89,   89,   89,   63,   64,   65,   66,   89,   89,
 /*    10 */    70,   69,   89,   89,   89,   89,   89,   89,   89,   89,
 /*    20 */    89,   89,   81,   89,   89,   89,   89,   84,   89,   89,
 /*    30 */    89,   89,   89,   89,   74,   89,   89,   89,   89,   88,
 /*    40 */    89,   89,   89,   89,   89,   89,   77,   89,   89,   78,
 /*    50 */    89,   89,   89,   89,   89,   79,   89,   89,   89,   80,
};
#define YY_SZ_ACTTAB (sizeof(yy_action)/sizeof(yy_action[0]))

/* The next table maps tokens into fallback tokens.  If a construct
** like the following:
** 
**      %fallback ID X Y Z.
**
** appears in the grammer, then ID becomes a fallback token for X, Y,
** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
** but it does not parse, the type of the token is changed to ID and
** the parse is retried before an error is thrown.
*/
#ifdef YYFALLBACK
static const YYCODETYPE yyFallback[] = {
};
#endif /* YYFALLBACK */

/* The following structure represents a single element of the
** parser's stack.  Information stored includes:
**
**   +  The state number for the parser at this level of the stack.
**
**   +  The value of the token stored at this level of the stack.
**      (In other words, the "major" token.)
**
**   +  The semantic value stored at this level of the stack.  This is
**      the information used by the action routines in the grammar.
**      It is sometimes called the "minor" token.
*/
struct yyStackEntry {
  int stateno;       /* The state-number */
  int major;         /* The major token value.  This is the code
                     ** number for the token at this stack level */
  YYMINORTYPE minor; /* The user-supplied minor token value.  This
                     ** is the value of the token  */
};
typedef struct yyStackEntry yyStackEntry;

/* The state of the parser is completely contained in an instance of
** the following structure */
struct yyParser {
  int yyidx;                    /* Index of top element in stack */
  int yyerrcnt;                 /* Shifts left before out of the error */
  yyStackEntry *yytop;          /* Pointer to the top stack element */
  php_ffi_parserARG_SDECL                /* A place to hold %extra_argument */
  yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
};
typedef struct yyParser yyParser;

#ifndef NDEBUG
#include <stdio.h>
static FILE *yyTraceFILE = 0;
static const char *yyTracePrompt = 0;
#endif /* NDEBUG */

#ifndef NDEBUG
/* 
** Turn parser tracing on by giving a stream to which to write the trace
** and a prompt to preface each trace message.  Tracing is turned off
** by making either argument NULL 
**
** Inputs:
** <ul>
** <li> A FILE* to which trace output should be written.
**      If NULL, then tracing is turned off.
** <li> A prefix string written at the beginning of every
**      line of trace output.  If NULL, then tracing is
**      turned off.
** </ul>
**
** Outputs:
** None.
*/
void php_ffi_parserTrace(FILE *TraceFILE, const char *zTracePrompt){
  yyTraceFILE = TraceFILE;
  yyTracePrompt = zTracePrompt;
  if( yyTraceFILE==0 ) yyTracePrompt = 0;
  else if( yyTracePrompt==0 ) yyTraceFILE = 0;
}
#endif /* NDEBUG */

#ifndef NDEBUG
/* For tracing shifts, the names of all terminals and nonterminals
** are required.  The following table supplies these names */
static const char *yyTokenName[] = { 
  "$",             "ASTERISK",      "CALLBACK",      "COMMA",       
  "EQUALS",        "IDENT",         "INTRINSIC",     "LBRACE",      
  "LPAREN",        "LSQUARE",       "RBRACE",        "RPAREN",      
  "RSQUARE",       "SEMI",          "STRING",        "STRUCT",      
  "TYPEDEF",       "arg_list",      "arg_type",      "argument",    
  "asterisk_list",  "callback_def",  "error",         "field_def",   
  "field_list",    "func_attribute",  "func_attribute_list",  "func_proto",  
  "func_proto_with_attributes",  "optional_asterisk_list",  "top_item",      "top_level",   
  "top_list",      "type_def",    
};
#endif /* NDEBUG */

#ifndef NDEBUG
/* For tracing reduce actions, the names of all rules are required.
*/
static const char *yyRuleName[] = {
 /*   0 */ "top_level ::= top_list",
 /*   1 */ "top_list ::= top_item top_list",
 /*   2 */ "top_list ::= top_item",
 /*   3 */ "top_item ::= func_proto_with_attributes",
 /*   4 */ "top_item ::= func_proto",
 /*   5 */ "top_item ::= type_def",
 /*   6 */ "top_item ::= callback_def",
 /*   7 */ "optional_asterisk_list ::=",
 /*   8 */ "optional_asterisk_list ::= asterisk_list",
 /*   9 */ "asterisk_list ::= ASTERISK",
 /*  10 */ "asterisk_list ::= asterisk_list ASTERISK",
 /*  11 */ "arg_type ::= INTRINSIC optional_asterisk_list",
 /*  12 */ "arg_type ::= STRUCT IDENT optional_asterisk_list",
 /*  13 */ "arg_type ::= IDENT",
 /*  14 */ "callback_def ::= CALLBACK arg_type IDENT LPAREN arg_list RPAREN SEMI",
 /*  15 */ "func_attribute_list ::= func_attribute COMMA func_attribute_list",
 /*  16 */ "func_attribute_list ::= func_attribute",
 /*  17 */ "func_attribute ::= IDENT EQUALS STRING",
 /*  18 */ "func_proto_with_attributes ::= LSQUARE func_attribute_list RSQUARE func_proto",
 /*  19 */ "func_proto ::= arg_type IDENT LPAREN arg_list RPAREN SEMI",
 /*  20 */ "type_def ::= TYPEDEF arg_type IDENT SEMI",
 /*  21 */ "type_def ::= STRUCT IDENT LBRACE field_list RBRACE SEMI",
 /*  22 */ "field_list ::= field_def field_list",
 /*  23 */ "field_list ::= field_def",
 /*  24 */ "field_def ::= arg_type IDENT SEMI",
 /*  25 */ "arg_list ::= argument COMMA arg_list",
 /*  26 */ "arg_list ::= argument",
 /*  27 */ "arg_list ::=",
 /*  28 */ "argument ::= arg_type IDENT",
};
#endif /* NDEBUG */

/*
** This function returns the symbolic name associated with a token
** value.
*/
const char *php_ffi_parserTokenName(int tokenType){
#ifndef NDEBUG
  if( tokenType>0 && (unsigned int) tokenType<(sizeof(yyTokenName)/sizeof(yyTokenName[0])) ){
    return yyTokenName[tokenType];
  }else{
    return "Unknown";
  }
#else
  return "";
#endif
}

/* 
** This function allocates a new parser.
** The only argument is a pointer to a function which works like
** malloc.
**
** Inputs:
** A pointer to the function used to allocate memory.
**
** Outputs:
** A pointer to a parser.  This pointer is used in subsequent calls
** to php_ffi_parser and php_ffi_parserFree.
*/
void *php_ffi_parserAlloc(void *(*mallocProc)(size_t)){
  yyParser *pParser;
  pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
  if( pParser ){
    pParser->yyidx = -1;
  }
  return pParser;
}

/* The following function deletes the value associated with a
** symbol.  The symbol can be either a terminal or nonterminal.
** "yymajor" is the symbol code, and "yypminor" is a pointer to
** the value.
*/
static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){
  switch( yymajor ){
    /* Here is inserted the actions which take place when a
    ** terminal or non-terminal is destroyed.  This can happen
    ** when the symbol is popped from the stack during a
    ** reduce or during error processing or when a parser is 
    ** being destroyed before it is finished parsing.
    **
    ** Note: during a reduce, the only symbols destroyed are those
    ** which appear on the RHS of the rule, but which are not used
    ** inside the C code.
    */
    default:  break;   /* If no destructor action specified: do nothing */
  }
}

/*
** Pop the parser's stack once.
**
** If there is a destructor routine associated with the token which
** is popped from the stack, then call it.
**
** Return the major token number for the symbol popped.
*/
static int yy_pop_parser_stack(yyParser *pParser){
  YYCODETYPE yymajor;

  if( pParser->yyidx<0 ) return 0;
#ifndef NDEBUG
  if( yyTraceFILE && pParser->yyidx>=0 ){
    fprintf(yyTraceFILE,"%sPopping %s\n",
      yyTracePrompt,
      yyTokenName[pParser->yytop->major]);
  }
#endif
  yymajor = pParser->yytop->major;
  yy_destructor( yymajor, &pParser->yytop->minor);
  pParser->yyidx--;
  pParser->yytop--;
  return yymajor;
}

/* 
** Deallocate and destroy a parser.  Destructors are all called for
** all stack elements before shutting the parser down.
**
** Inputs:
** <ul>
** <li>  A pointer to the parser.  This should be a pointer
**       obtained from php_ffi_parserAlloc.
** <li>  A pointer to a function used to reclaim memory obtained
**       from malloc.
** </ul>
*/
void php_ffi_parserFree(
  void *p,                    /* The parser to be deleted */
  void (*freeProc)(void*)     /* Function used to reclaim memory */
){
  yyParser *pParser = (yyParser*)p;
  if( pParser==0 ) return;
  while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
  (*freeProc)((void*)pParser);
}

/*
** Find the appropriate action for a parser given the terminal
** look-ahead token iLookAhead.
**
** If the look-ahead token is YYNOCODE, then check to see if the action is
** independent of the look-ahead.  If it is, return the action, otherwise
** return YY_NO_ACTION.
*/
static int yy_find_shift_action(
  yyParser *pParser,        /* The parser */
  int iLookAhead            /* The look-ahead token */
){
  int i;
 
  /* if( pParser->yyidx<0 ) return YY_NO_ACTION;  */
  i = yy_shift_ofst[pParser->yytop->stateno];
  if( i==YY_SHIFT_USE_DFLT ){
    return yy_default[pParser->yytop->stateno];
  }
  if( iLookAhead==YYNOCODE ){
    return YY_NO_ACTION;
  }
  i += iLookAhead;
  if( i<0 || (unsigned int)i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
#ifdef YYFALLBACK
    int iFallback;            /* Fallback token */
    if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
           && (iFallback = yyFallback[iLookAhead])!=0 ){
#ifndef NDEBUG
      if( yyTraceFILE ){
        fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
           yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
      }
#endif
      return yy_find_shift_action(pParser, iFallback);
    }
#endif
    return yy_default[pParser->yytop->stateno];
  }else{
    return yy_action[i];
  }
}

/*
** Find the appropriate action for a parser given the non-terminal
** look-ahead token iLookAhead.
**
** If the look-ahead token is YYNOCODE, then check to see if the action is
** independent of the look-ahead.  If it is, return the action, otherwise
** return YY_NO_ACTION.
*/
static int yy_find_reduce_action(
  yyParser *pParser,        /* The parser */
  int iLookAhead            /* The look-ahead token */
){
  int i;
 
  i = yy_reduce_ofst[pParser->yytop->stateno];
  if( i==YY_REDUCE_USE_DFLT ){
    return yy_default[pParser->yytop->stateno];
  }
  if( iLookAhead==YYNOCODE ){
    return YY_NO_ACTION;
  }
  i += iLookAhead;
  if( i<0 || (unsigned int)i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
    return yy_default[pParser->yytop->stateno];
  }else{
    return yy_action[i];
  }
}

/*
** Perform a shift action.
*/
static void yy_shift(
  yyParser *yypParser,          /* The parser to be shifted */
  int yyNewState,               /* The new state to shift in */
  int yyMajor,                  /* The major token to shift in */
  YYMINORTYPE *yypMinor         /* Pointer ot the minor token to shift in */
){
  yypParser->yyidx++;
  yypParser->yytop++;
  if( yypParser->yyidx>=YYSTACKDEPTH ){
     php_ffi_parserARG_FETCH;
     yypParser->yyidx--;
     yypParser->yytop--;
#ifndef NDEBUG
     if( yyTraceFILE ){
       fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
     }
#endif
     while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
     /* Here code is inserted which will execute if the parser
     ** stack every overflows */
     php_ffi_parserARG_STORE; /* Suppress warning about unused %extra_argument var */
     return;
  }
  yypParser->yytop->stateno = yyNewState;
  yypParser->yytop->major = yyMajor;
  yypParser->yytop->minor = *yypMinor;
#ifndef NDEBUG
  if( yyTraceFILE && yypParser->yyidx>0 ){
    int i;
    fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
    fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
    for(i=1; i<=yypParser->yyidx; i++)
      fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
    fprintf(yyTraceFILE,"\n");
  }
#endif
}

/* The following table contains information about every rule that
** is used during the reduce.
*/
static struct {
  YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
  unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
} yyRuleInfo[] = {
  { 31, 1 },
  { 32, 2 },
  { 32, 1 },
  { 30, 1 },
  { 30, 1 },
  { 30, 1 },
  { 30, 1 },
  { 29, 0 },
  { 29, 1 },
  { 20, 1 },
  { 20, 2 },
  { 18, 2 },
  { 18, 3 },
  { 18, 1 },
  { 21, 7 },
  { 26, 3 },
  { 26, 1 },
  { 25, 3 },
  { 28, 4 },
  { 27, 6 },
  { 33, 4 },
  { 33, 6 },
  { 24, 2 },
  { 24, 1 },
  { 23, 3 },
  { 17, 3 },
  { 17, 1 },
  { 17, 0 },
  { 19, 2 },
};

static void yy_accept(yyParser*);  /* Forward Declaration */

/*
** Perform a reduce action and the shift that must immediately
** follow the reduce.
*/
static void yy_reduce(
  yyParser *yypParser,         /* The parser */
  int yyruleno                 /* Number of the rule by which to reduce */
){
  int yygoto;                     /* The next state */
  int yyact;                      /* The next action */
  YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
  yyStackEntry *yymsp;            /* The top of the parser's stack */
  int yysize;                     /* Amount to pop the stack */
  php_ffi_parserARG_FETCH;
  yymsp = yypParser->yytop;
#ifndef NDEBUG
  if( yyTraceFILE && yyruleno>=0 
        && (unsigned int)yyruleno<sizeof(yyRuleName)/sizeof(yyRuleName[0]) ){
    fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
      yyRuleName[yyruleno]);
  }
#endif /* NDEBUG */

  switch( yyruleno ){
  /* Beginning here are the reduction cases.  A typical example
  ** follows:
  **   case 0:
  **  #line <lineno> <grammarfile>
  **     { ... }           // User supplied code
  **  #line <lineno> <thisfile>
  **     break;
  */
      case 0:
        /* No destructor defined for top_list */
        break;
      case 1:
        /* No destructor defined for top_item */
        /* No destructor defined for top_list */
        break;
      case 2:
        /* No destructor defined for top_item */
        break;
      case 3:
        /* No destructor defined for func_proto_with_attributes */
        break;
      case 4:
        /* No destructor defined for func_proto */
        break;
      case 5:
        /* No destructor defined for type_def */
        break;
      case 6:
        /* No destructor defined for callback_def */
        break;
      case 7:
#line 102 "..\\pecl\\ffi\\ffi_parser.y"
{ yygotominor.yy0.type.ptr_levels = 0; }
#line 712 "..\\pecl\\ffi\\ffi_parser.c"
        break;
      case 8:
#line 103 "..\\pecl\\ffi\\ffi_parser.y"
{ yygotominor.yy0 = yymsp[0].minor.yy0; }
#line 717 "..\\pecl\\ffi\\ffi_parser.c"
        break;
      case 9:
#line 105 "..\\pecl\\ffi\\ffi_parser.y"
{ yygotominor.yy0.type.ptr_levels = 1; }
#line 722 "..\\pecl\\ffi\\ffi_parser.c"
        /* No destructor defined for ASTERISK */
        break;
      case 10:
#line 106 "..\\pecl\\ffi\\ffi_parser.y"
{ yygotominor.yy0 = yymsp[-1].minor.yy0; yygotominor.yy0.type.ptr_levels++; }
#line 728 "..\\pecl\\ffi\\ffi_parser.c"
        /* No destructor defined for ASTERISK */
        break;
      case 11:
#line 108 "..\\pecl\\ffi\\ffi_parser.y"
{ yygotominor.yy0 = yymsp[-1].minor.yy0; yygotominor.yy0.type.ptr_levels = yymsp[0].minor.yy0.type.ptr_levels; }
#line 734 "..\\pecl\\ffi\\ffi_parser.c"
        break;
      case 12:
#line 109 "..\\pecl\\ffi\\ffi_parser.y"
{
   	yygotominor.yy0.type.intrinsic_type = FFI_TYPE_STRUCT;
	yygotominor.yy0.type.struct_name = yymsp[-1].minor.yy0.ident;
   	yygotominor.yy0.type.ptr_levels = yymsp[0].minor.yy0.type.ptr_levels;
}
#line 743 "..\\pecl\\ffi\\ffi_parser.c"
        /* No destructor defined for STRUCT */
        break;
      case 13:
#line 115 "..\\pecl\\ffi\\ffi_parser.y"
{
	CTX_TSRMLS_FETCH();
	/* TODO: lookup ident (including callback types) */
	php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unrecognized type name `%s'", get_ident_string(yymsp[0].minor.yy0.ident));	
}
#line 753 "..\\pecl\\ffi\\ffi_parser.c"
        break;
      case 14:
#line 123 "..\\pecl\\ffi\\ffi_parser.y"
{
	CTX_TSRMLS_FETCH();
	php_error_docref(NULL TSRMLS_CC, E_WARNING, "callback support not complete");	
}
#line 761 "..\\pecl\\ffi\\ffi_parser.c"
        /* No destructor defined for CALLBACK */
        /* No destructor defined for LPAREN */
        /* No destructor defined for arg_list */
        /* No destructor defined for RPAREN */
        /* No destructor defined for SEMI */
        break;
      case 15:
        /* No destructor defined for func_attribute */
        /* No destructor defined for COMMA */
        /* No destructor defined for func_attribute_list */
        break;
      case 16:
        /* No destructor defined for func_attribute */
        break;
      case 17:
#line 131 "..\\pecl\\ffi\\ffi_parser.y"
{
	if (IDENT_EQUALS("lib", yymsp[-2].minor.yy0.ident)) {
		ctx->libname = yymsp[0].minor.yy0.ident;
	} else {
		CTX_TSRMLS_FETCH();
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported attribute %s", get_ident_string(yymsp[-2].minor.yy0.ident));
	}
}
#line 786 "..\\pecl\\ffi\\ffi_parser.c"
        /* No destructor defined for EQUALS */
        break;
      case 18:
        /* No destructor defined for LSQUARE */
        /* No destructor defined for func_attribute_list */
        /* No destructor defined for RSQUARE */
        break;
      case 19:
#line 142 "..\\pecl\\ffi\\ffi_parser.y"
{
	yygotominor.yy0.func = php_ffi_parser_register_func(ctx, yymsp[-5].minor.yy0.type, yymsp[-4].minor.yy0.ident);
	ctx->n_args = 0;
}
#line 800 "..\\pecl\\ffi\\ffi_parser.c"
        /* No destructor defined for LPAREN */
        /* No destructor defined for arg_list */
        /* No destructor defined for RPAREN */
        /* No destructor defined for SEMI */
        break;
      case 20:
#line 147 "..\\pecl\\ffi\\ffi_parser.y"
{
	CTX_TSRMLS_FETCH();
	php_error_docref(NULL TSRMLS_CC, E_WARNING, "aliasing typedefs are not yet implemented");
}
#line 812 "..\\pecl\\ffi\\ffi_parser.c"
        /* No destructor defined for TYPEDEF */
        /* No destructor defined for SEMI */
        break;
      case 21:
#line 152 "..\\pecl\\ffi\\ffi_parser.y"
{
	php_ffi_parser_register_type(ctx, yymsp[-4].minor.yy0.ident);	
	ctx->n_args = 0;
}
#line 822 "..\\pecl\\ffi\\ffi_parser.c"
        /* No destructor defined for STRUCT */
        /* No destructor defined for LBRACE */
        /* No destructor defined for field_list */
        /* No destructor defined for RBRACE */
        /* No destructor defined for SEMI */
        break;
      case 22:
        /* No destructor defined for field_def */
        /* No destructor defined for field_list */
        break;
      case 23:
        /* No destructor defined for field_def */
        break;
      case 24:
#line 160 "..\\pecl\\ffi\\ffi_parser.y"
{
	php_ffi_parser_add_arg(ctx, yymsp[-2].minor.yy0.type, yymsp[-1].minor.yy0.ident);
}
#line 841 "..\\pecl\\ffi\\ffi_parser.c"
        /* No destructor defined for SEMI */
        break;
      case 25:
        /* No destructor defined for argument */
        /* No destructor defined for COMMA */
        /* No destructor defined for arg_list */
        break;
      case 26:
        /* No destructor defined for argument */
        break;
      case 27:
        break;
      case 28:
#line 168 "..\\pecl\\ffi\\ffi_parser.y"
{
	php_ffi_parser_add_arg(ctx, yymsp[-1].minor.yy0.type, yymsp[0].minor.yy0.ident);
}
#line 859 "..\\pecl\\ffi\\ffi_parser.c"
        break;
  };
  yygoto = yyRuleInfo[yyruleno].lhs;
  yysize = yyRuleInfo[yyruleno].nrhs;
  yypParser->yyidx -= yysize;
  yypParser->yytop -= yysize;
  yyact = yy_find_reduce_action(yypParser,yygoto);
  if( yyact < YYNSTATE ){
    yy_shift(yypParser,yyact,yygoto,&yygotominor);
  }else if( yyact == YYNSTATE + YYNRULE + 1 ){
    yy_accept(yypParser);
  }
}

/*
** The following code executes when the parse fails
*/
static void yy_parse_failed(
  yyParser *yypParser           /* The parser */
){
  php_ffi_parserARG_FETCH;
#ifndef NDEBUG
  if( yyTraceFILE ){
    fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
  }
#endif
  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
  /* Here code is inserted which will be executed whenever the
  ** parser fails */
#line 5 "..\\pecl\\ffi\\ffi_parser.y"
 ctx->failed = 1; printf("FAIL: parser failed - %d errors\n", ctx->errors); 
#line 891 "..\\pecl\\ffi\\ffi_parser.c"
  php_ffi_parserARG_STORE; /* Suppress warning about unused %extra_argument variable */
}

/*
** The following code executes when a syntax error first occurs.
*/
static void yy_syntax_error(
  yyParser *yypParser,           /* The parser */
  int yymajor,                   /* The major type of the error token */
  YYMINORTYPE yyminor            /* The minor type of the error token */
){
  php_ffi_parserARG_FETCH;
#define TOKEN (yyminor.yy0)
#line 6 "..\\pecl\\ffi\\ffi_parser.y"
 ctx->errors++; printf("SYNTAX: entering error recovery near token %s\n", php_ffi_get_token_string(yymajor, TOKEN)); 
#line 907 "..\\pecl\\ffi\\ffi_parser.c"
  php_ffi_parserARG_STORE; /* Suppress warning about unused %extra_argument variable */
}

/*
** The following is executed when the parser accepts
*/
static void yy_accept(
  yyParser *yypParser           /* The parser */
){
  php_ffi_parserARG_FETCH;
#ifndef NDEBUG
  if( yyTraceFILE ){
    fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
  }
#endif
  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
  /* Here code is inserted which will be executed whenever the
  ** parser accepts */
  php_ffi_parserARG_STORE; /* Suppress warning about unused %extra_argument variable */
}

/* The main parser program.
** The first argument is a pointer to a structure obtained from
** "php_ffi_parserAlloc" which describes the current state of the parser.
** The second argument is the major token number.  The third is
** the minor token.  The fourth optional argument is whatever the
** user wants (and specified in the grammar) and is available for
** use by the action routines.
**
** Inputs:
** <ul>
** <li> A pointer to the parser (an opaque structure.)
** <li> The major token number.
** <li> The minor token number.
** <li> An option argument of a grammar-specified type.
** </ul>
**
** Outputs:
** None.
*/
void php_ffi_parser(
  void *yyp,                   /* The parser */
  int yymajor,                 /* The major token code number */
  php_ffi_parserTOKENTYPE yyminor       /* The value for the token */
  php_ffi_parserARG_PDECL               /* Optional %extra_argument parameter */
){
  YYMINORTYPE yyminorunion;
  int yyact;            /* The parser action. */
  int yyendofinput;     /* True if we are at the end of input */
  int yyerrorhit = 0;   /* True if yymajor has invoked an error */
  yyParser *yypParser;  /* The parser */

  /* (re)initialize the parser, if necessary */
  yypParser = (yyParser*)yyp;
  if( yypParser->yyidx<0 ){
    if( yymajor==0 ) return;
    yypParser->yyidx = 0;
    yypParser->yyerrcnt = -1;
    yypParser->yytop = &yypParser->yystack[0];
    yypParser->yytop->stateno = 0;
    yypParser->yytop->major = 0;
  }
  yyminorunion.yy0 = yyminor;
  yyendofinput = (yymajor==0);
  php_ffi_parserARG_STORE;

#ifndef NDEBUG
  if( yyTraceFILE ){
    fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
  }
#endif

  do{
    yyact = yy_find_shift_action(yypParser,yymajor);
    if( yyact<YYNSTATE ){
      yy_shift(yypParser,yyact,yymajor,&yyminorunion);
      yypParser->yyerrcnt--;
      if( yyendofinput && yypParser->yyidx>=0 ){
        yymajor = 0;
      }else{
        yymajor = YYNOCODE;
      }
    }else if( yyact < YYNSTATE + YYNRULE ){
      yy_reduce(yypParser,yyact-YYNSTATE);
    }else if( yyact == YY_ERROR_ACTION ){
#ifndef NDEBUG
      if( yyTraceFILE ){
        fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
      }
#endif
#ifdef YYERRORSYMBOL
      /* A syntax error has occurred.
      ** The response to an error depends upon whether or not the
      ** grammar defines an error token "ERROR".  
      **
      ** This is what we do if the grammar does define ERROR:
      **
      **  * Call the %syntax_error function.
      **
      **  * Begin popping the stack until we enter a state where
      **    it is legal to shift the error symbol, then shift
      **    the error symbol.
      **
      **  * Set the error count to three.
      **
      **  * Begin accepting and shifting new tokens.  No new error
      **    processing will occur until three tokens have been
      **    shifted successfully.
      **
      */
      if( yypParser->yyerrcnt<0 ){
        yy_syntax_error(yypParser,yymajor,yyminorunion);
      }
      if( yypParser->yytop->major==YYERRORSYMBOL || yyerrorhit ){
#ifndef NDEBUG
        if( yyTraceFILE ){
          fprintf(yyTraceFILE,"%sDiscard input token %s\n",
             yyTracePrompt,yyTokenName[yymajor]);
        }
#endif
        yy_destructor(yymajor,&yyminorunion);
        yymajor = YYNOCODE;
      }else{
         while(
          yypParser->yyidx >= 0 &&
          yypParser->yytop->major != YYERRORSYMBOL &&
          (yyact = yy_find_shift_action(yypParser,YYERRORSYMBOL)) >= YYNSTATE
        ){
          yy_pop_parser_stack(yypParser);
        }
        if( yypParser->yyidx < 0 || yymajor==0 ){
          yy_destructor(yymajor,&yyminorunion);
          yy_parse_failed(yypParser);
          yymajor = YYNOCODE;
        }else if( yypParser->yytop->major!=YYERRORSYMBOL ){
          YYMINORTYPE u2;
          u2.YYERRSYMDT = 0;
          yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
        }
      }
      yypParser->yyerrcnt = 3;
      yyerrorhit = 1;
#else  /* YYERRORSYMBOL is not defined */
      /* This is what we do if the grammar does not define ERROR:
      **
      **  * Report an error message, and throw away the input token.
      **
      **  * If the input token is $, then fail the parse.
      **
      ** As before, subsequent error messages are suppressed until
      ** three input tokens have been successfully shifted.
      */
      if( yypParser->yyerrcnt<=0 ){
        yy_syntax_error(yypParser,yymajor,yyminorunion);
      }
      yypParser->yyerrcnt = 3;
      yy_destructor(yymajor,&yyminorunion);
      if( yyendofinput ){
        yy_parse_failed(yypParser);
      }
      yymajor = YYNOCODE;
#endif
    }else{
      yy_accept(yypParser);
      yymajor = YYNOCODE;
    }
  }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
  return;
}

/*
Local variables:
tab-width: 4
c-basic-offset: 4
End:
vim600: noet sw=4 ts=4 fdm=marker
vim<600: noet sw=4 ts=4
*/
