%{

/*****************************************************************/
/**                                                             **/
/**             (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 *);

%}


%union
{
   int integer;
   char *string;
   list *lst;
   Asn_1 *asn;
   struct {int def,len;} len;
}

%token UNIVERSAL APPLICATION CONTEXT PRIVATE CONSTRUCTED PRIMITIVE 

%token LEFTPAREN RIGHTPAREN LEFTBRACKET RIGHTBRACKET LEFTBRACE RIGHTBRACE
%token ELLIPSES

%token <string> NUMBER HEXNUMBER

%type <integer> class form byte number
%type <lst> byte_list asn_value_list nesting
%type <len> length
%type <asn> complete_value asn_value header

%start complete_value_list

%%

complete_value_list	:	complete_value_list complete_value
				{
				   $2->output(stdout);
				   fprintf(stdout,"\n");
				}
			|	lambda
			;

complete_value		:	asn_value
			;


asn_value_list		:	asn_value_list asn_value
				{
				   $$ = $1;
				   $$->append($2);
				}
			|	lambda
				{
				   $$ = new list;
				}
			;

asn_value		:	header byte_list
				{
				   $$ = $1;
				   $$->values = $2;
				   if ($1->length >= 0 &&
				       $1->length != $1->definite_length())
				      Error("Specified definite length and byte count disagree");
				   $$->length = $2->length();
				   $$->definite = TRUE;
				}
			|	header nesting
				{
				   $$ = $1;
				   $$->values = $2;
				   if ($1->length >= 0 &&
				       $1->length != $1->definite_length())
				      Error("Specified definite length and computed definite length disagree");
				   if ($$->definite)
				      $$->length = $$->definite_length();
				}
			;

header			:	LEFTBRACKET class number form RIGHTBRACKET length
				{
				   $$ = new Asn_1;
				   $$->definite = $6.def;
				   $$->length = $6.len;
				   $$->cl = $2;
				   $$->tag = $3;
				   $$->form = $4;
				}
			;

class			:	UNIVERSAL
				{
				   $$ = 0;
				}
			|	APPLICATION
				{
				   $$ = 1;
				}
			|	CONTEXT
				{
				   $$ = 2;
				}
			|	PRIVATE
				{
				   $$ = 3;
				}
			;

form			:	CONSTRUCTED
				{
				   $$ = 1;
				}
			|	PRIMITIVE
				{
				   $$ = 0;
				}
			;

length			:	LEFTPAREN number RIGHTPAREN
				{	/* definite with specified length */
				   $$.def = 1;
				   $$.len = $2;
				}
			|	LEFTPAREN RIGHTPAREN
				{	/* definite, but unknown length */
				   $$.def = 1;
				   $$.len = -1;
				}
			|	LEFTPAREN ELLIPSES RIGHTPAREN
				{	/* indefinite */
				   $$.def = 0;
				   $$.len = -1;
				}
			|	lambda
				{	/* default is indefinite */
				   $$.def = 0;
				   $$.len = -1;
				}
			;

byte_list		:	byte_list byte
				{
				   $$ = $1;
				   $$->append(new Byte($2));
				}
			|	lambda
				{
				   $$ = new list;
				}
			;

nesting			:	LEFTBRACE asn_value_list RIGHTBRACE
				{
				   $$ = $2;
				}
			;

byte			:	NUMBER
				{
				   sscanf($1,"%x",&($$));
				}
			|	HEXNUMBER
				{
				   sscanf($1,"%x",&($$));
				}
			;

number			:	NUMBER
				{
				   sscanf($1,"%d",&($$));
				}

lambda			:	;

%%

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

