#include "c_varieties.h"
#ifdef __EXTERN_C__
	EXTERN_FUNCTION ( extern void yyerror, (char *) );
	EXTERN_FUNCTION ( extern int yylex, () );
	EXTERN_FUNCTION ( int yyparse, () );
#elif defined (__cplusplus) || defined (c_plusplus)
	extern void yyerror(char *);
	extern int yylex();
	extern int yyparse();
#endif

# line 2 "easn.gram"

/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_easn_gram[] = "@(#)easn.gram	1.5 11/6/92 07:29:24  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	8/13/90

	ROUTINE:	Grammar for ASN.1_Encoder
			Compile with yacc++

PURPOSE:
	This is the grammar used to parse a ASN.1 raw-value specification. The
syntax allows the specification of an ASN.1 encoding without benefit of an
encoding specification.  The ultimate output of the parse is a text file of
encoded hex bytes.

CALLED ROUTINES:
	class Asn_1.output
	class Asn_1.definite_length
	class list.append
	
MODIFICATION HISTORY:


*/

#include "asn1.h"
#include <stdio.h>
#include <malloc.h>
#define YYDEBUG
#define TRUE 1
#define FALSE 0
#define Error(X)	asn_error("Error",X)
void asn_error(char *,char *);


# line 62 "easn.gram"
typedef union 
{
   int integer;
   char *string;
   list *lst;
   Asn_1 *asn;
   struct {int def,len;} len;
} YYSTYPE;
# define UNIVERSAL 257
# define APPLICATION 258
# define CONTEXT 259
# define PRIVATE 260
# define CONSTRUCTED 261
# define PRIMITIVE 262
# define LEFTPAREN 263
# define RIGHTPAREN 264
# define LEFTBRACKET 265
# define RIGHTBRACKET 266
# define LEFTBRACE 267
# define RIGHTBRACE 268
# define ELLIPSES 269
# define NUMBER 270
# define HEXNUMBER 271
#define yyclearin yychar = -1
#define yyerrok yyerrflag = 0
extern int yychar;
extern short yyerrflag;
#ifndef YYMAXDEPTH
#define YYMAXDEPTH 150
#endif
YYSTYPE yylval, yyval;
# define YYERRCODE 256

# line 227 "easn.gram"


void yyerror(char *errorarg)
{
   asn_error("Syntax Error","Unexpected Token");
   printf("!!!%s!!!\n",errorarg);
}

short yyexca[] ={
-1, 1,
	0, -1,
	-2, 0,
	};
# define YYNPROD 26
# define YYLAST 53
short yyact[]={

  33,  17,  18,  22,   6,  34,  22,  24,  10,  28,
   6,  36,  35,  30,  26,  27,  12,  13,  14,  15,
   2,  21,   4,   1,   5,   3,   9,  29,   8,  19,
   7,  20,  16,  25,  11,   0,   0,   0,   0,   0,
   0,   0,  23,   0,   0,   0,   0,   0,   0,  31,
   0,   0,  32 };
short yypact[]={

-1000,-255,-1000,-1000,-1000,-259,-241,-269,-1000,-1000,
-1000,-267,-1000,-1000,-1000,-1000,-1000,-1000,-1000,-261,
-1000,-247,-1000,-1000,-1000,-257,-1000,-1000,-250,-1000,
-264,-1000,-252,-1000,-253,-1000,-1000 };
short yypgo[]={

   0,  34,  33,  32,  21,  30,  29,  28,  27,  25,
  22,  24,  23,  20 };
short yyr1[]={

   0,  12,  12,   9,   6,   6,  10,  10,  11,   1,
   1,   1,   1,   2,   2,   8,   8,   8,   8,   5,
   5,   7,   3,   3,   4,  13 };
short yyr2[]={

   0,   2,   1,   1,   2,   1,   2,   2,   6,   1,
   1,   1,   1,   1,   1,   3,   2,   3,   1,   2,
   1,   3,   1,   1,   1,   0 };
short yychk[]={

-1000, -12, -13,  -9, -10, -11, 265,  -5,  -7, -13,
 267,  -1, 257, 258, 259, 260,  -3, 270, 271,  -6,
 -13,  -4, 270, -10, 268,  -2, 261, 262, 266,  -8,
 263, -13,  -4, 264, 269, 264, 264 };
short yydef[]={

  25,  -2,   2,   1,   3,  25,   0,   6,   7,  20,
  25,   0,   9,  10,  11,  12,  19,  22,  23,   0,
   5,   0,  24,   4,  21,   0,  13,  14,  25,   8,
   0,  18,   0,  16,   0,  15,  17 };
#ifndef lint
static	char yaccpar_sccsid[] = "@(#)yaccpar 1.9 89/09/18 SMI"; /* from UCB 4.1 83/02/11 */
#endif

#
# define YYFLAG -1000
# define YYERROR goto yyerrlab
# define YYACCEPT return(0)
# define YYABORT return(1)

/*	parser for yacc output	*/

#ifdef YYDEBUG
/* 
 * include stdio.h since YYDEBUG will use printf() which is defined
 * in stdio.h 
 */
# ifndef FILE
#include <stdio.h>
# endif
int yydebug = 0; /* 1 for debugging */
#endif
YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
int yychar = -1; /* current input token number */
int yynerrs = 0;  /* number of errors */
short yyerrflag = 0;  /* error recovery flag */

yyparse() {

	short yys[YYMAXDEPTH];
	short yyj, yym;
	register YYSTYPE *yypvt;
	register short yystate, *yyps, yyn;
	register YYSTYPE *yypv;
	register short *yyxi;

	yystate = 0;
	yychar = -1;
	yynerrs = 0;
	yyerrflag = 0;
	yyps= &yys[-1];
	yypv= &yyv[-1];

 yystack:    /* put a state and value onto the stack */

#ifdef YYDEBUG
	if( yydebug  ) printf( "state %d, char 0%o\n", yystate, yychar );
#endif
		if( ++yyps>= &yys[YYMAXDEPTH] ) { yyerror( "yacc stack overflow" ); return(1); }
		*yyps = yystate;
		++yypv;
		*yypv = yyval;

 yynewstate:

	yyn = yypact[yystate];

	if( yyn<= YYFLAG ) goto yydefault; /* simple state */

	if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0;
	if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault;

	if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */
		yychar = -1;
		yyval = yylval;
		yystate = yyn;
		if( yyerrflag > 0 ) --yyerrflag;
		goto yystack;
		}

 yydefault:
	/* default state action */

	if( (yyn=yydef[yystate]) == -2 ) {
		if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0;
		/* look through exception table */

		for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */

		while( *(yyxi+=2) >= 0 ){
			if( *yyxi == yychar ) break;
			}
		if( (yyn = yyxi[1]) < 0 ) return(0);   /* accept */
		}

	if( yyn == 0 ){ /* error */
		/* error ... attempt to resume parsing */

		switch( yyerrflag ){

		case 0:   /* brand new error */

			yyerror( "syntax error" );
#ifdef YYERRLAB
		yyerrlab:
#endif
			++yynerrs;

		case 1:
		case 2: /* incompletely recovered error ... try again */

			yyerrflag = 3;

			/* find a state where "error" is a legal shift action */

			while ( yyps >= yys ) {
			   yyn = yypact[*yyps] + YYERRCODE;
			   if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){
			      yystate = yyact[yyn];  /* simulate a shift of "error" */
			      goto yystack;
			      }
			   yyn = yypact[*yyps];

			   /* the current yyps has no shift onn "error", pop stack */

#ifdef YYDEBUG
			   if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
#endif
			   --yyps;
			   --yypv;
			   }

			/* there is no state on the stack with an error shift ... abort */

	yyabort:
			return(1);


		case 3:  /* no shift yet; clobber input char */

#ifdef YYDEBUG
			if( yydebug ) printf( "error recovery discards char %d\n", yychar );
#endif

			if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
			yychar = -1;
			goto yynewstate;   /* try again in the same state */

			}

		}

	/* reduction by production yyn */

#ifdef YYDEBUG
		if( yydebug ) printf("reduce %d\n",yyn);
#endif
		yyps -= yyr2[yyn];
		yypvt = yypv;
		yypv -= yyr2[yyn];
		yyval = yypv[1];
		yym=yyn;
			/* consult goto table to find next state */
		yyn = yyr1[yyn];
		yyj = yypgo[yyn] + *yyps + 1;
		if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
		switch(yym){
			
case 1:
# line 88 "easn.gram"
{
				   yypvt[-0].asn->output(stdout);
				   fprintf(stdout,"\n");
				} break;
case 4:
# line 100 "easn.gram"
{
				   yyval.lst = yypvt[-1].lst;
				   yyval.lst->append(yypvt[-0].asn);
				} break;
case 5:
# line 105 "easn.gram"
{
				   yyval.lst = new list;
				} break;
case 6:
# line 111 "easn.gram"
{
				   yyval.asn = yypvt[-1].asn;
				   yyval.asn->values = yypvt[-0].lst;
				   if (yypvt[-1].asn->length >= 0 &&
				       yypvt[-1].asn->length != yypvt[-1].asn->definite_length())
				      Error("Specified definite length and byte count disagree");
				   yyval.asn->length = yypvt[-0].lst->length();
				   yyval.asn->definite = TRUE;
				} break;
case 7:
# line 121 "easn.gram"
{
				   yyval.asn = yypvt[-1].asn;
				   yyval.asn->values = yypvt[-0].lst;
				   if (yypvt[-1].asn->length >= 0 &&
				       yypvt[-1].asn->length != yypvt[-1].asn->definite_length())
				      Error("Specified definite length and computed definite length disagree");
				   if (yyval.asn->definite)
				      yyval.asn->length = yyval.asn->definite_length();
				} break;
case 8:
# line 133 "easn.gram"
{
				   yyval.asn = new Asn_1;
				   yyval.asn->definite = yypvt[-0].len.def;
				   yyval.asn->length = yypvt[-0].len.len;
				   yyval.asn->cl = yypvt[-4].integer;
				   yyval.asn->tag = yypvt[-3].integer;
				   yyval.asn->form = yypvt[-2].integer;
				} break;
case 9:
# line 144 "easn.gram"
{
				   yyval.integer = 0;
				} break;
case 10:
# line 148 "easn.gram"
{
				   yyval.integer = 1;
				} break;
case 11:
# line 152 "easn.gram"
{
				   yyval.integer = 2;
				} break;
case 12:
# line 156 "easn.gram"
{
				   yyval.integer = 3;
				} break;
case 13:
# line 162 "easn.gram"
{
				   yyval.integer = 1;
				} break;
case 14:
# line 166 "easn.gram"
{
				   yyval.integer = 0;
				} break;
case 15:
# line 172 "easn.gram"
{	/* definite with specified length */
				   yyval.len.def = 1;
				   yyval.len.len = yypvt[-1].integer;
				} break;
case 16:
# line 177 "easn.gram"
{	/* definite, but unknown length */
				   yyval.len.def = 1;
				   yyval.len.len = -1;
				} break;
case 17:
# line 182 "easn.gram"
{	/* indefinite */
				   yyval.len.def = 0;
				   yyval.len.len = -1;
				} break;
case 18:
# line 187 "easn.gram"
{	/* default is indefinite */
				   yyval.len.def = 0;
				   yyval.len.len = -1;
				} break;
case 19:
# line 194 "easn.gram"
{
				   yyval.lst = yypvt[-1].lst;
				   yyval.lst->append(new Byte(yypvt[-0].integer));
				} break;
case 20:
# line 199 "easn.gram"
{
				   yyval.lst = new list;
				} break;
case 21:
# line 205 "easn.gram"
{
				   yyval.lst = yypvt[-1].lst;
				} break;
case 22:
# line 211 "easn.gram"
{
				   sscanf(yypvt[-0].string,"%x",&(yyval.integer));
				} break;
case 23:
# line 215 "easn.gram"
{
				   sscanf(yypvt[-0].string,"%x",&(yyval.integer));
				} break;
case 24:
# line 221 "easn.gram"
{
				   sscanf(yypvt[-0].string,"%d",&(yyval.integer));
				} break;
		}
		goto yystack;  /* stack new state and value */

	}
