// Collection of code snippets by Arne VajhÃ¸j 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
/*
 *  NICE - utility
 *
 *  Programmed: Arne Vajhøj (august 1990)
 *
 */

#define std
#ifdef vms
#undef std
#endif
#ifdef tp
#undef std
#endif

#define NEWELM(n) $$ = (char *) nice_malloc(n+1)

#define RES_EQ_NULL NEWELM(0); \
                    strcpy($$,"");
#define RES_EQ_OP_KWRD(s) NEWELM(strlen(s)); \
                          strcpy(tmp,s); \
                          kwrdcvt(tmp); \
                          strcpy($$,tmp);
#define RES_EQ_1 NEWELM(strlen($1)); \
                 strcpy($$,$1); \
                 nice_free($1);
#define RES_EQ_1_OP(s) NEWELM(strlen($1)+strlen(s)); \
                       sprintf($$,"%s%s",$1,s); \
                       nice_free($1);
#define RES_EQ_1_OP_KWRD(s) strcpy(tmp,s); \
                            kwrdcvt(tmp); \
                            RES_EQ_1_OP(tmp)
#define RES_EQ_1_INT NEWELM(10); \
                     sprintf($$,"%d",$1);
#define RES_EQ_OP_2_INT(s) NEWELM(11); \
                           sprintf($$,"%s%d",s,$2);
#define RES_EQ_1_REAL NEWELM(20); \
                      sprintf($$,"%f",$1);
#define RES_EQ_1_OP_3(s) NEWELM(strlen($1)+strlen(s)+strlen($3)); \
                         sprintf($$,"%s%s%s",$1,s,$3); \
                         nice_free($1); \
                         nice_free($3);
#define RES_EQ_1_OP_KWRD_3(s) strcpy(tmp,s); \
                              kwrdcvt(tmp); \
                              RES_EQ_1_OP_3(tmp)
#define RES_EQ_1_OP_3_OP(s1,s2) NEWELM(strlen($1)+strlen(s1)+strlen($3)+strlen(s2)); \
                                sprintf($$,"%s%s%s%s",$1,s1,$3,s2); \
                                nice_free($1); \
                                nice_free($3);
#define RES_EQ_OP_2(s) NEWELM(strlen(s)+strlen($2)); \
                       sprintf($$,"%s%s",s,$2); \
                       nice_free($2);
#define RES_EQ_OP_KWRD_2(s) strcpy(tmp,s); \
                            kwrdcvt(tmp); \
                            RES_EQ_OP_2(tmp)
#define RES_EQ_OP_2_OP(s1,s2) NEWELM(strlen(s1)+strlen($2)+strlen(s2)); \
                              sprintf($$,"%s%s%s",s1,$2,s2); \
                              nice_free($2);

%union {
char *name;
int ival;
double xval;
char *sval;
char *elm;
}

/* value tokens */
%token <name> ID
%token <ival> INTCONST
%token <sval> HEXCONST
%token <xval> REALCONST
%token <sval> STRCONST
%token <sval> COMMENT

/* double character tokens */
%token ASG
%token NE
%token GE
%token LE
%token POW
%token DOT2

/* special single character token */
%token NUMB

/* keywords */
%token PROGRAM
%token LABEL
%token CONST
%token TYPE
%token VAR
%token ARRAY
%token PACKED
%token UNPACKED
%token RECORD
%token ZFILE
%token SET
%token PROCEDURE
%token FUNCTION
%token ZBEGIN
%token IF
%token THEN
%token ELSE
%token GOTO
%token CASE
#ifndef tp
%token OTHERWISE
#endif
%token OF
%token WHILE
%token DO
%token FOR
%token TO
%token DOWNTO
%token REPEAT
%token UNTIL
%token WITH
%token AND
%token OR
%token NOT
%token IN
%token END
%token DIV
%token MOD
%token FORWARD
%token ZEXTERN
%token EXTERNAL
#ifdef vms
%token VARYING
%token MODULE
%token INCLUDE
%token INHERIT
%token VALUE
%token IMMED
%token REF
%token DESCR
%token STDESCR
%token ANDTHEN
%token ORELSE
#endif
#ifdef tp
%token STRING
%token USES
%token UNIT
%token INTERFACE
%token PSEUDOFOREIGN
%token IMPLEMENTATION
#ifdef object
%token OBJECT
%token CONSTRUCTOR
%token DESTRUCTOR
#endif
#endif

/* basic items */
#ifdef vms
%type <elm> include
#endif
%type <elm> args
%type <elm> arg
%type <elm> dim
#ifndef std
%type <elm> onedim
#endif
%type <elm> range
%type <elm> erange
%type <elm> pck
%type <elm> expr
%type <elm> ref
%type <elm> index
%type <elm> arglst
%type <elm> item
#ifdef vms
%type <elm> attr
%type <elm> strlst
#endif
%type <elm> idlst
%type <elm> ordlst
%type <elm> ordval
%type <elm> cns
%type <elm> ordcns
%type <elm> set
%type <elm> setlst
#ifdef vms
%type <elm> passing
#endif
%type <elm> foreign

%%

/* general */

input:
#ifdef vms
          comments                      { outcomm(TRUE,TRUE,FALSE); }
          inherit
#endif
          comments                      { outcomm(TRUE,TRUE,FALSE); }
          input2
;
input2:   head
          comments                      { outcomm(TRUE,TRUE,TRUE); }
          decls
          body                          { fin = TRUE; }
#ifdef tp
        | unit
          comments                      { outcomm(TRUE,TRUE,TRUE); }
          interf
          implemen
          body                          { fin = TRUE; }
#endif
;
#ifdef tp
unit:     UNIT ID ';'                   { newl();
                                          gen("UNIT");
                                          printf(" %s;",$2);
                                          newl();
                                          lind = 0;
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of unit: %s\n",$2);
                                          lind++;
                                          if (translate) transerr("UNIT");
                                          nice_free($2); }
;
interf:   INTERFACE                      { newl();
                                           gen("INTERFACE");
                                           newl();
                                           if (translate) transerr("INTERFACE");
                                           ininterf = TRUE; }
          decls
;
implemen: IMPLEMENTATION                 { ininterf = FALSE;
                                           newl();
                                           gen("IMPLEMENTATION");
                                           newl();
                                           if (translate) transerr("IMPLEMENTATION"); }
          decls
;
#endif
comments: /* empty */                   { comm = (char *)nice_realloc(comm,1);
                                          comm[0] = '\0'; }
        | comments COMMENT              { l = strlen(comm);
                                          if (l>0) {
                                             if (commstyle != 1) {
                                                comm = (char *)nice_realloc(comm,l+strlen($2)+5);
                                                strcat(comm,"*)\n(*");
                                             } else {
                                                comm = (char *)nice_realloc(comm,l+strlen($2)+3);
                                                strcat(comm,"}\n{");
                                             }
                                          } else {
                                             comm = (char *)nice_realloc(comm,l+strlen($2));
                                          }
                                          strcat(comm,$2);
                                          nice_free($2); }
;
onecomm:  /* empty */                   { comm = (char *)nice_realloc(comm,1);
                                          comm[0] = '\0'; }
        | COMMENT                       { comm = (char *)nice_realloc(comm,1+strlen($1));
                                          strcat(comm,$1);
                                          nice_free($1); }
;
#ifdef vms
inherit:  /* empty */                   { ; }
        | '[' INHERIT '(' strlst ')' ']'
                                        { newl();
                                          printf("[");
                                          gen("INHERIT");
                                          printf("(%s)]",$4);
                                          newl();
                                          if (translate) transerr("INHERIT");
                                          nice_free($4); }
;
#endif
proc:     phead
          comments                      { outcomm(TRUE,TRUE,TRUE); }
          decls
          sbody
          comments                      { outcomm(TRUE,TRUE,FALSE); }
        | phead foreign ';'             { printf("%s;",$2);
                                          nice_free($2);
                                          top--;
                                          newl(); }
          comments                      { outcomm(TRUE,TRUE,FALSE); }
#ifdef tp
        | phead PSEUDOFOREIGN           { top--;
                                          newl();
                                          misspseudo = FALSE; }
          comments                      { outcomm(TRUE,TRUE,FALSE); }
#endif
;
func:     fhead
          comments                      { outcomm(TRUE,TRUE,TRUE); }
          decls
          sbody
          comments                      { outcomm(TRUE,TRUE,FALSE); }
        | fhead foreign ';'             { printf("%s;",$2);
                                          nice_free($2);
                                          printf(";");
                                          top--; }
          comments                      { outcomm(TRUE,TRUE,FALSE); }
;
head:     PROGRAM ID ';'                { newl();
                                          gen("PROGRAM");
                                          printf(" %s;",$2);
                                          newl();
                                          lind = 0;
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of program: %s\n",$2);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2); }
        | PROGRAM ID '(' idlst ')' ';'  { newl();
                                          gen("PROGRAM");
                                          printf(" %s(%s);",$2,$4);
                                          newl();
                                          lind = 0;
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of program: %s\n",$2);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2); }
#ifdef vms
        | MODULE ID ';'                 { newl();
                                          gen("MODULE");
                                          printf(" %s;",$2);
                                          newl();
                                          lind = 0;
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of module: %s\n",$2);
                                          lind++;
                                          if (translate) transerr("MODULE"); }
        | MODULE ID '(' idlst ')' ';'   { newl();
                                          gen("MODULE");
                                          printf(" %s(%s);",$2,$4);
                                          newl();
                                          lind = 0;
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of module: %s\n",$2);
                                          lind++;
                                          if (translate) transerr("MODULE"); }
#endif
;
phead:    PROCEDURE ID ';'              { newl();
                                          gen("PROCEDURE");
                                          printf(" %s;",$2);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of procedure: %s\n",$2);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          nice_free($2); }
#ifdef vms
        | attr PROCEDURE ID ';'         { newl();
                                          printf("%s ",$1);
                                          gen("PROCEDURE");
                                          printf(" %s;",$3);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of procedure: %s\n",$3);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$3);
                                          if (translate) transerr("attributes");
                                          nice_free($1);
                                          nice_free($3); }
#endif
        | PROCEDURE ID '(' args ')' ';' { newl();
                                          gen("PROCEDURE");
                                          printf(" %s(%s);",$2,$4);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of procedure: %s\n",$2);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          nice_free($2);
                                          nice_free($4); }
#ifdef vms
        | attr PROCEDURE ID '(' args ')' ';'
                                        { newl();
                                          printf("%s ",$1);
                                          gen("PROCEDURE");
                                          printf(" %s(%s);",$3,$5);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of procedure: %s\n",$3);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$3);
                                          if (translate) transerr("attributes");
                                          nice_free($1);
                                          nice_free($3);
                                          nice_free($5); }
#endif
#ifdef tp
#ifdef object
        | PROCEDURE ID '.' ID ';'       { newl();
                                          gen("PROCEDURE");
                                          printf(" %s.%s;",$2,$4);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of procedure: %s.%s\n",$2,$4);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("methods");
                                          nice_free($2);
                                          nice_free($4); }
        | PROCEDURE ID '.' ID '(' args ')' ';'
                                        { newl();
                                          gen("PROCEDURE");
                                          printf(" %s.%s(%s);",$2,$4,$6);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of procedure: %s.%s\n",$2,$4);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("methods");
                                          nice_free($2);
                                          nice_free($4);
                                          nice_free($6); }
        | CONSTRUCTOR ID ';'            { newl();
                                          gen("CONSTRUCTOR");
                                          printf(" %s;",$2);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of constructor: %s\n",$2);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("CONSTRUCTOR");
                                          nice_free($2); }
        | CONSTRUCTOR ID '(' args ')' ';'
                                        { newl();
                                          gen("CONSTRUCTOR");
                                          printf(" %s(%s);",$2,$4);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of constructor: %s\n",$2);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("CONSTRUCTOR");
                                          nice_free($2);
                                          nice_free($4); }
        | DESTRUCTOR ID ';'             { newl();
                                          gen("DESTRUCTOR");
                                          printf(" %s;",$2);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of destructor: %s\n",$2);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("DESTRUCTOR");
                                          nice_free($2); }
        | DESTRUCTOR ID '(' args ')' ';'
                                        { newl();
                                          gen("DESTRUCTOR");
                                          printf(" %s(%s);",$2,$4);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of destructor: %s\n",$2);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("DESTRUCTOR");
                                          nice_free($2);
                                          nice_free($4); }
        | CONSTRUCTOR ID '.' ID ';'     { newl();
                                          gen("CONSTRUCTOR");
                                          printf(" %s.%s;",$2,$4);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of constructor: %s.%s\n",$2,$4);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("methods");
                                          if (translate) transerr("CONSTRUCTOR");
                                          nice_free($2);
                                          nice_free($4); }
        | CONSTRUCTOR ID '.' ID '(' args ')' ';'
                                        { newl();
                                          gen("CONSTRUCTOR");
                                          printf(" %s.%s(%s);",$2,$4,$6);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of constructor: %s.%s\n",$2,$4);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("methods");
                                          if (translate) transerr("CONSTRUCTOR");
                                          nice_free($2);
                                          nice_free($4);
                                          nice_free($6); }
        | DESTRUCTOR ID '.' ID ';'      { newl();
                                          gen("DESTRUCTOR");
                                          printf(" %s.%s;",$2,$4);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of destructor: %s.%s\n",$2,$4);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("methods");
                                          if (translate) transerr("DESTRUCTOR");
                                          nice_free($2);
                                          nice_free($4); }
        | DESTRUCTOR ID '.' ID '(' args ')' ';'
                                        { newl();
                                          gen("DESTRUCTOR");
                                          printf(" %s.%s(%s);",$2,$4,$6);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of destructor: %s.%s\n",$2,$4);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("methods");
                                          if (translate) transerr("DESTRUCTOR");
                                          nice_free($2);
                                          nice_free($4);
                                          nice_free($6); }
#endif
#endif
;
fhead:    FUNCTION ID ':'
          ID ';'                        { newl();
                                          gen("FUNCTION");
                                          printf(" %s:%s;",$2,$4);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of function: %s\n",$2);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          nice_free($2);
                                          nice_free($4); }
#ifdef vms
        | attr FUNCTION ID ':'
          ID ';'                        { newl();
                                          printf("%s ",$1);
                                          gen("FUNCTION");
                                          printf(" %s:%s;",$3,$5);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of function: %s\n",$3);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$3);
                                          if (translate) transerr("attributes");
                                          nice_free($1);
                                          nice_free($3);
                                          nice_free($5); }
#endif
        | FUNCTION ID '(' args ')' ':'
          ID ';'                        { newl();
                                          gen("FUNCTION");
                                          printf(" %s(%s):%s;",$2,$4,$7);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of function: %s\n",$2);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          nice_free($2);
                                          nice_free($4);
                                          nice_free($7); }
#ifdef vms
        | attr FUNCTION ID '(' args ')' ':'
          ID ';'                        { newl();
                                          printf("%s ",$1);
                                          gen("FUNCTION");
                                          printf(" %s(%s):%s;",$3,$5,$8);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of function: %s\n",$3);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$3);
                                          if (translate) transerr("attributes");
                                          nice_free($1);
                                          nice_free($3);
                                          nice_free($5);
                                          nice_free($8); }
#endif
#ifdef tp
#ifdef object
        | FUNCTION ID '.' ID ':'
          ID ';'                        { newl();
                                          gen("FUNCTION");
                                          printf(" %s.%s:%s;",$2,$4,$6);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of function: %s.%s\n",$2,$4);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("methods");
                                          nice_free($2);
                                          nice_free($4);
                                          nice_free($6); }
        | FUNCTION ID '.' ID '(' args ')' ':'
          ID ';'                        { newl();
                                          gen("FUNCTION");
                                          printf(" %s.%s(%s):%s;",$2,$4,$6,$9);
                                          newl();
                                          space(lind);
                                          if (definition) fprintf(lfp,"definition of function: %s.%s\n",$2,$4);
                                          lind++;
                                          top++;
                                          entryname[top] = (char *) nice_malloc(max_name);
                                          strcpy(entryname[top],$2);
                                          if (translate) transerr("methods");
                                          nice_free($2);
                                          nice_free($4);
                                          nice_free($6);
                                          nice_free($9); }
#endif
#endif
;
decls:    /* empty */                   { ; }
        | decls decl                    { ; }
;
decl:     labels                        { ; }
        | consts                        { ; }
        | types                         { ; }
        | vars                          { ; }
#ifdef vms
        | values                        { ; }
        | include                       { newl();
                                          printf("%s",$1); }
#endif
#ifdef tp
        | uses                          { ; }
#endif
        | proc                          { ; }
        | func                          { ; }
;
#ifdef tp
uses:     USES idlst ';' comments       { newl();
                                          gen("USES");
                                          ind++;
                                          newl();
                                          printf("%s;",$2);
                                          outcomm(FALSE,FALSE,TRUE);
                                          ind--;
                                          newl();
                                          if (translate) transerr("USES");
                                          nice_free($2); }
;
#endif
body:     ZBEGIN                        { newl();
                                          gen("BEGIN");
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"START of program %s\n",entryname[top]);
                                                        }
                                          ind++;
                                          dind++; }
          stmts
          END '.'                       { ind--;
                                          dind--;
                                          newl();
                                          gen("END");
                                          printf(".");
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"END of program %s\n",entryname[top]);
                                                        top--;
                                                        } }
        | END '.'                       { ind--;
                                          newl();
                                          gen("END");
                                          printf("."); }
;
sbody:    ZBEGIN                        { newl();
                                          gen("BEGIN");
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"START of routine %s\n",entryname[top]);
                                                        }
                                          ind++;
                                          dind++; }
          stmts
          END ';'                       { ind--;
                                          newl();
                                          gen("END");
                                          printf(";");
                                          newl();
                                          lind--;
                                          dind--;
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"END of routine %s\n\n",entryname[top]);
                                                        top--;
                                                        } }
;
block:    stmtz                         { ; }
        | ZBEGIN                        { ind--;
                                          if (beginbreak)
                                             newl();
                                          else
                                             printf(" ");
                                          gen("BEGIN");
                                          ind++;
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"{\n");
                                                        } }
          stmts
          END onecomm                   { if (!endind) ind--;
                                          newl();
                                          gen("END");
                                          if (endind) ind--;
                                          outcomm(FALSE,FALSE,FALSE);
                                          ind++;
                                          blocked = TRUE;
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"}\n");
                                                        } }
;
casblock: stmt
        | ZBEGIN                        { newl();
                                          gen("BEGIN");
                                          ind++; }
          stmts
          END ';'                       { ind--;
                                          newl();
                                          gen("END");
                                          printf(";"); }
;
stmts:    /* empty */                   { ; }
        | stmts stmt                    { ; }
;
stmt:     gotolbl comments ';'          { outcomm(TRUE,FALSE,FALSE); }
        | gotolbl stmtz ';'             { printf(";");
                                          blocked = FALSE; }
;
stmtz:    asgmt                         { ; }
        | if                            { ; }
        | goto                          { ; }
        | case                          { ; }
        | while                         { ; }
        | for                           { ; }
        | repunt                        { ; }
        | with                          { ; }
        | pcall                         { ; }
#ifdef vms
        | include                       { newl();
                                          printf("%s",$1);
                                          nice_free($1); }
#endif
        | error                         { newl();
                                          printf("(* error *)"); }
;
gotolbl:  /* empty */                   { ; }
/*
        | ID ':'                        { printf("\n%s :",$1);
                                          nice_free($1); }
*/
        | INTCONST ':'                  { printf("\n%d :",$1); }
;

/* declarations */

labels:   LABEL ordlst ';' comments     { newl();
                                          gen("LABEL");
                                          ind++;
                                          newl();
                                          printf("%s;",$2);
                                          outcomm(FALSE,FALSE,TRUE);
                                          ind--;
                                          newl();
                                          nice_free($2); }
;
consts:   CONST comments                { newl();
                                          gen("CONST");
                                          outcomm(FALSE,FALSE,FALSE);
                                          ind++; }
          constx                        { ind--;
                                          newl(); }
;
constx:   /* empty */                   { ; }
        | constx const1                 { ; }
;
const1:   ID '=' expr ';' comments      { newl();
                                          printf("%s = %s;",$1,$3);
                                          outcomm(FALSE,FALSE,TRUE);
                                          nice_free($1);
                                          nice_free($3); }
#ifdef vms
        | include comments              { newl();
                                          printf("%s",$1);
                                          outcomm(FALSE,FALSE,FALSE);
                                          nice_free($1); }
#endif
        | error ';'                     { ; }
;
types:    TYPE comments                 { newl();
                                          gen("TYPE");
                                          outcomm(FALSE,FALSE,FALSE);
                                          ind++; }
          typex                         { ind--;
                                          newl(); }
;
typex:    /* empty */                   { ; }
        | typex type1                   { ; }
;
type1:    ID '=' ID ';' comments        { newl();
                                          printf("%s = %s;",$1,$3);
                                          outcomm(FALSE,FALSE,TRUE);
                                          nice_free($1);
                                          nice_free($3); }
#ifdef vms
        | include comments              { newl();
                                          printf("%s",$1);
                                          outcomm(FALSE,FALSE,FALSE);
                                          nice_free($1); }
#endif
        | ID '='                        { newl();
                                          ind2 = strlen($1)+3;
                                          printf("%s = ",$1);
                                          nice_free($1); }
          usetyp ';' comments           { printf(";");
                                          outcomm(FALSE,FALSE,TRUE);
                                          ind2 = 0; }
        | error ';'                     { ; }
;
vars:     VAR comments                  { newl();
                                          gen("VAR");
                                          outcomm(FALSE,FALSE,FALSE);
                                          ind++; }
          varx                          { ind--;
                                          newl(); }
;
varx:     /* empty */                   { ; }
        | varx var1                     { ; }
;
var1:     var1z ';' comments            { printf(";");
                                          outcomm(FALSE,FALSE,TRUE); }
#ifdef vms
        | var1z ASG expr ';' comments   { printf("%s%s;",asgstr,$3);
                                          nice_free($3);
                                          outcomm(FALSE,FALSE,TRUE); }
        | var1z VALUE expr ';' comments { printf(" ");
                                          gen("VALUE");
                                          printf(" %s;",$3);
                                          nice_free($3);
                                          outcomm(FALSE,FALSE,TRUE); }
        | include comments              { newl();
                                          printf("%s",$1);
                                          outcomm(FALSE,FALSE,FALSE);
                                          nice_free($1); }
#endif
;
var1z:    idlst ':' ID                  { newl();
                                          printf("%s : %s",$1,$3);
                                          nice_free($1);
                                          nice_free($3); }
#ifdef vms
        | idlst ':' attr ID             { newl();
                                          printf("%s : %s %s",$1,$3,$4);
                                          if (translate) transerr("attributes");
                                          nice_free($1);
                                          nice_free($3);
                                          nice_free($4); }
#endif
        | idlst ':'                     { newl();
                                          ind2 = strlen($1)+3;
                                          printf("%s : ",$1);
                                          nice_free($1); }
          usetyp                        { ind2 = 0; }
#ifdef vms
        | idlst ':' attr                { newl();
                                          ind2 = strlen($1)+strlen($3)+4;
                                          printf("%s : %s ",$1,$3);
                                          if (translate) transerr("attributes");
                                          nice_free($1);
                                          nice_free($3); }
          usetyp                        { ind2 = 0; }
#endif
        | error                         { ; }
;
usetyp:   pck ARRAY '[' dim ']' OF ID   { gen($1);
                                          gen("ARRAY");
                                          printf(" [%s] ",$4);
                                          gen("OF");
                                          printf(" %s",$7);
                                          nice_free($1);
                                          nice_free($4);
                                          nice_free($7); }
        | pck RECORD                    { gen($1);
                                          gen("RECORD");
                                          ind++;
                                          nice_free($1); }
          flds
          END                           {  ind--;
                                           newl();
                                           gen("END"); }
#ifdef tp
#ifdef object
        | OBJECT                        { gen("OBJECT");
                                          ind++;
                                          if (translate) transerr("OBJECT"); }
          objflds
          END                           {  ind--;
                                           newl();
                                           gen("END"); }
#endif
#endif
        | '^' ID                        { printf("^%s",$2);
                                          nice_free($2); }
        | '(' idlst ')'                 { printf("(%s)",$2);
                                          nice_free($2); }
        | range                         { printf("%s",$1);
                                          nice_free($1); }
        | ZFILE OF ID                   { gen("FILE");
                                          printf(" ");
                                          gen("OF");
                                          printf(" %s",$3);
                                          nice_free($3); }
#ifdef tp
        | ZFILE                         { if (!translate) {
                                             gen("FILE");
                                          } else {
                                             gen("FILE");
                                             printf(" ");
                                             gen("OF");
                                             printf(" ");
                                             gen("CHAR");
                                          } }
#endif
        | SET OF dim                    { gen("SET");
                                          printf(" ");
                                          gen("OF");
                                          printf(" %s",$3);
                                          nice_free($3); }
#ifdef vms
        | VARYING '[' onedim ']' OF ID  { if (!translate) {
                                             gen("VARYING");
                                             printf(" [%s] ",$3);
                                             gen("OF");
                                             printf(" %s",$6);
                                          } else {
                                             gen("STRING");
                                             printf(" [%s]",$3);
                                          }
                                          nice_free($3);
                                          nice_free($6); }
#endif
#ifdef tp
        | STRING '[' onedim ']'         { if (!translate) {
                                             gen("STRING");
                                             printf(" [%s]",$3);
                                          } else {
                                             gen("VARYING");
                                             printf(" [%s] ",$3);
                                             gen("OF");
                                             printf(" ");
                                             gen("CHAR");
                                          }
                                          nice_free($3); }
        | STRING                        { if (!translate) {
                                             gen("STRING");
                                          } else {
                                             gen("VARYING");
                                             printf(" [255] ");
                                             gen("OF");
                                             printf(" ");
                                             gen("CHAR");
                                          } }
#endif
;
flds:     /* empty */                   { ; }
        | flds var1                     { ; }
        | flds CASE ID ':' ID OF        { newl();
                                          gen("CASE");
                                          printf(" %s : %s ",$3,$5);
                                          gen("OF");
                                          ind++;
                                          nice_free($3);
                                          nice_free($5); }
             varflds                    { ind--; }
;
#ifdef tp
#ifdef object
objflds:  /* empty */                   { ; }
        | objflds var1                  { ; }
        | objflds phead comments        { outcomm(FALSE,FALSE,TRUE); }
        | objflds fhead comments        { outcomm(FALSE,FALSE,TRUE); }
;
#endif
#endif
varflds:  /* empty */                   { ; }
        | varflds varfld                { ; }
;
varfld:   ordlst ':''('                 { newl();
                                          printf("%s : (",$1);
                                          skipnewl = TRUE;
                                          nice_free($1); }
          var1z
          ')' ';'                       { printf(");"); }
;
#ifdef vms
values:   VALUE comments                { newl();
                                          gen("VALUE");
                                          outcomm(FALSE,FALSE,FALSE);
                                          ind++; }
          valuex                        { ind--;
                                          newl(); }
;
valuex:   /* empty */                   { ; }
        | valuex value1                 { ; }
;
value1:   ID ASG expr ';' comments      { newl();
                                          printf("%s%s%s;",$1,asgstr,$3);
                                          outcomm(FALSE,FALSE,TRUE);
                                          nice_free($1);
                                          nice_free($3); }
        | include comments              { newl();
                                          printf("%s",$1);
                                          outcomm(FALSE,FALSE,FALSE);
                                          nice_free($1); }
        | error ';'                     { ; }
;
#endif

/* statements */

asgmt:    comments
          ref ASG expr                   { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          printf("%s%s%s",$2,asgstr,$4);
                                          nice_free($2);
                                          nice_free($4); }
;
if:       comments
          IF expr THEN                  { outcomm(TRUE,FALSE,TRUE);
                                          newl();
                                          gen("IF");
                                          printf(" %s ",$3);
                                          gen("THEN");
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"IF ()\n");
                                                        }
                                          ind++;
                                          dind++;
                                          nice_free($3); }
             ifbody                     { ind--;
                                          dind--; }
;
ifbody:   block
        | block
          ELSE                          { ind--;
                                          dind--;
                                          if (blocked&&(!beginbreak))
                                              printf(" ");
                                          else
                                              newl();
                                          gen("ELSE");
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"ELSE\n");
                                                        }
                                          ind++;
                                          dind++; }
          block
;
goto:     comments
          GOTO ID                       { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          gen("GOTO");
                                          printf(" %s",$3);
                                          nice_free($3); }
        | comments
          GOTO INTCONST                  { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          gen("GOTO");
                                          printf(" %d",$3); }
;
case:     comments
          CASE expr OF                  { outcomm(TRUE,FALSE,TRUE);
                                          newl();
                                          gen("CASE");
                                          printf(" %s ",$3);
                                          gen("OF");
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"CASE ()\n");
                                                        }
                                          ind++;
                                          dind++;
                                          nice_free($3); }
             lbls
          END                           { ind--;
                                          dind--;
                                          newl();
                                          gen("END"); }
;
lbls:     /* empty */                   { ; }
        | lbls lbl                      { ; }
;
lbl:      comments
          ordlst ':'                    { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          printf("%s : ",$2);
                                          ind2 = strlen($2)+3;
                                          skipnewl = TRUE;
                                          nice_free($2); }
          casblock                      { ind2 = 0; }
#ifndef tp
        | comments
          OTHERWISE                     { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          if (!translate) {
                                             gen("OTHERWISE");
                                          } else {
                                             gen("ELSE");
                                          }
                                          printf(" ");
                                          ind2 = 12;
                                          skipnewl = TRUE; }
          casblock                      { ind2 = 0; }
#endif
#ifdef tp
        | comments
          ELSE                          { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          if (!translate) {
                                             gen("ELSE");
                                          } else {
                                             gen("OTHERWISE");
                                          }
                                          printf(" ");
                                          ind2 = 7;
                                          skipnewl = TRUE; }
          casblock                      { ind2 = 0; }
#endif
;
while:    comments
          WHILE expr DO                 { outcomm(TRUE,FALSE,TRUE);
                                          newl();
                                          gen("WHILE");
                                          printf(" %s ",$3);
                                          gen("DO");
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"WHILE ()\n");
                                                        }
                                          ind++;
                                          dind++;
                                          nice_free($3); }
             block                      { ind--;
                                          dind--; }
;
for:      comments
          FOR ref ASG expr TO expr DO
                                        { outcomm(TRUE,FALSE,TRUE);
                                          newl();
                                          gen("FOR");
                                          printf(" %s%s%s ",$3,asgstr,$5);
                                          gen("TO");
                                          printf(" %s ",$7);
                                          gen("DO");
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"FOR ()\n");
                                                        }
                                          ind++;
                                          dind++;
                                          nice_free($3);
                                          nice_free($5);
                                          nice_free($7); }
             block                      { ind--;
                                          dind--; }
        | comments
          FOR ref ASG expr DOWNTO expr DO
                                        { outcomm(TRUE,FALSE,TRUE);
                                          newl();
                                          gen("FOR");
                                          printf(" %s%s%s ",$3,asgstr,$5);
                                          gen("DOWNTO");
                                          printf(" %s ",$7);
                                          gen("DO");
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"FOR ()\n");
                                                        }
                                          ind++;
                                          dind++;
                                          nice_free($3);
                                          nice_free($5);
                                          nice_free($7); }
             block                      { ind--;
                                          dind--; }
;
repunt:   comments
          REPEAT                        { outcomm(TRUE,FALSE,TRUE);
                                          newl();
                                          gen("REPEAT");
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"REPEAT {\n");
                                                        }
                                          ind++;
                                          dind++; }
             stmts
          endrep
;
endrep:   UNTIL expr                    { ind--;
                                          dind--;
                                          newl();
                                          gen("UNTIL");
                                          printf(" %s",$2);
                                          if (document) {
                                                        space2(dind);
                                                        fprintf(dfp,"} UNTIL ()\n");
                                                        }
                                          nice_free($2); }
;
with:     comments
          WITH ref DO                   { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          gen("WITH");
                                          printf(" %s ",$3);
                                          gen("DO");
                                          ind++;
                                          nice_free($3); }
             block                      { ind--; }
;
#ifndef object
pcall:    comments
          ID                            { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          printf("%s",$2);
                                          space(lind);
                                          if (calling) fprintf(lfp,"call of procedure: %s\n",$2);
                                          if (document) docpcall($2);
                                          nice_free($2); }
        | comments
          ID '(' ')'                    { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          printf("%s()",$2);
                                          space(lind);
                                          if (calling) fprintf(lfp,"call of procedure: %s\n",$2);
                                          if (document) docpcall($2);
                                          nice_free($2); }
        | comments
          ID '(' arglst ')'             { if (translate) {
                                             cvtupp($2,tmp);
#ifdef vms
                                             if (strcmp(tmp,"OPEN")==0) {
                                                $2 = (char *)nice_realloc($2,7);
                                                strcpy($2,"ASSIGN");
                                                idcvt($2);
                                                ws = $4;
                                                while ((*ws!=',')&&(*ws!='\0'))
                                                   ws++;
                                                if (*ws!='\0') ws++;
                                                while ((*ws!=',')&&(*ws!='\0'))
                                                   ws++;
                                                *ws = '\0';
                                             }
                                             if (strcmp(tmp,"WRITEV")==0) {
                                                $2 = (char *)nice_realloc($2,4);
                                                strcpy($2,"STR");
                                                idcvt($2);
                                             }
                                             if (strcmp(tmp,"READV")==0) {
                                                $2 = (char *)nice_realloc($2,4);
                                                strcpy($2,"VAL");
                                                idcvt($2);
                                             }
#endif
#ifdef tp
                                             if (strcmp(tmp,"ASSIGN")==0) {
                                                $2 = (char *)nice_realloc($2,5);
                                                strcpy($2,"OPEN");
                                                idcvt($2);
                                                strcat($4,",UNKNOWN");
                                                kwrdcvt($4);
                                             }
                                             if (strcmp(tmp,"STR")==0) {
                                                $2 = (char *)nice_realloc($2,7);
                                                strcpy($2,"WRITEV");
                                                idcvt($2);
                                             }
                                             if (strcmp(tmp,"VAL")==0) {
                                                $2 = (char *)nice_realloc($2,6);
                                                strcpy($2,"READV");
                                                idcvt($2);
                                             }
                                             if (strcmp(tmp,"INC")==0) {
                                                idcvt($4);
                                                outcomm(TRUE,FALSE,FALSE);
                                                newl();
                                                printf("%s%s%s+1",$4,asgstr,$4);
                                                space(lind);
                                                goto jump;
                                             }
                                             if (strcmp(tmp,"DEC")==0) {
                                                idcvt($4);
                                                outcomm(TRUE,FALSE,FALSE);
                                                newl();
                                                printf("%s%s%s+1",$4,asgstr,$4);
                                                space(lind);
                                                goto jump;
                                             }
#endif
                                          }
                                          outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          printf("%s(%s)",$2,$4);
                                          space(lind);
                                          if (calling) fprintf(lfp,"call of procedure: %s\n",$2);
                                          if (document) docpcall($2);
                                          jump:
                                          nice_free($2);
                                          nice_free($4); }
;
#else
pcall:    comments
          ref                           { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          printf("%s",$2);
                                          space(lind);
                                          if (calling) fprintf(lfp,"call of procedure: %s\n",$2);
                                          if (document) docpcall($2);
                                          nice_free($2); }
        | comments
          ref '(' ')'                   { outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          printf("%s()",$2);
                                          space(lind);
                                          if (calling) fprintf(lfp,"call of procedure: %s\n",$2);
                                          if (document) docpcall($2);
                                          nice_free($2); }
        | comments
          ref '(' arglst ')'            { if (translate) {
                                             cvtupp($2,tmp);
#ifdef vms
                                             if (strcmp(tmp,"OPEN")==0) {
                                                $2 = (char *)nice_realloc($2,7);
                                                strcpy($2,"ASSIGN");
                                                idcvt($2);
                                                ws = $4;
                                                while ((*ws!=',')&&(*ws!='\0'))
                                                   ws++;
                                                if (*ws!='\0') ws++;
                                                while ((*ws!=',')&&(*ws!='\0'))
                                                   ws++;
                                                *ws = '\0';
                                             }
                                             if (strcmp(tmp,"WRITEV")==0) {
                                                $2 = (char *)nice_realloc($2,4);
                                                strcpy($2,"STR");
                                                idcvt($2);
                                             }
                                             if (strcmp(tmp,"READV")==0) {
                                                $2 = (char *)nice_realloc($2,4);
                                                strcpy($2,"VAL");
                                                idcvt($2);
                                             }
#endif
#ifdef tp
                                             if (strcmp(tmp,"ASSIGN")==0) {
                                                $2 = (char *)nice_realloc($2,5);
                                                strcpy($2,"OPEN");
                                                idcvt($2);
                                                strcat($4,",UNKNOWN");
                                                kwrdcvt($4);
                                             }
                                             if (strcmp(tmp,"STR")==0) {
                                                $2 = (char *)nice_realloc($2,7);
                                                strcpy($2,"WRITEV");
                                                idcvt($2);
                                             }
                                             if (strcmp(tmp,"VAL")==0) {
                                                $2 = (char *)nice_realloc($2,6);
                                                strcpy($2,"READV");
                                                idcvt($2);
                                             }
                                             if (strcmp(tmp,"INC")==0) {
                                                idcvt($4);
                                                outcomm(TRUE,FALSE,FALSE);
                                                newl();
                                                printf("%s%s%s+1",$4,asgstr,$4);
                                                space(lind);
                                                goto jump;
                                             }
                                             if (strcmp(tmp,"DEC")==0) {
                                                idcvt($4);
                                                outcomm(TRUE,FALSE,FALSE);
                                                newl();
                                                printf("%s%s%s+1",$4,asgstr,$4);
                                                space(lind);
                                                goto jump;
                                             }
#endif
                                          }
                                          outcomm(TRUE,FALSE,FALSE);
                                          newl();
                                          printf("%s(%s)",$2,$4);
                                          space(lind);
                                          if (calling) fprintf(lfp,"call of procedure: %s\n",$2);
                                          if (document) docpcall($2);
                                          jump:
                                          nice_free($2);
                                          nice_free($4); }
;
#endif

/* basic items */

#ifdef vms
include:  '%' INCLUDE STRCONST          { NEWELM(1+7+1+strlen($3));
                                          strcpy(tmp,"INCLUDE");
                                          kwrdcvt(tmp);
                                          sprintf($$,"%%%s %s",tmp,$3);
                                          if (translate) transerr("%INCLUDE"); }
;
#endif
args:     arg                           { RES_EQ_1; }
        | args ';' arg                  { RES_EQ_1_OP_3(";"); }
;
arg:      VAR idlst ':' ID              { NEWELM(4+strlen($2)+1+strlen($4));
                                          strcpy(tmp,"VAR");
                                          kwrdcvt(tmp);
                                          sprintf($$,"%s %s:%s",tmp,$2,$4);
                                          nice_free($2);
                                          nice_free($4); }
#ifdef vms
        | VAR idlst ':' attr ID         { NEWELM(4+strlen($2)+1+strlen($4)+1+strlen($5));
                                          strcpy(tmp,"VAR");
                                          kwrdcvt(tmp);
                                          sprintf($$,"%s %s:%s %s",tmp,$2,$4,$5);
                                          nice_free($2);
                                          nice_free($4);
                                          nice_free($5);
                                          if (translate) transerr("attributes"); }
#endif
        | idlst ':' ID                  { RES_EQ_1_OP_3(":"); }
#ifdef vms
        | idlst ':' attr ID             { NEWELM(strlen($1)+1+strlen($3)+1+strlen($4));
                                          sprintf($$,"%s:%s %s",$1,$3,$4);
                                          nice_free($1);
                                          nice_free($3);
                                          nice_free($4);
                                          if (translate) transerr("attributes"); }
#endif
#ifdef tp
        | VAR idlst ':' STRING          { NEWELM(4+strlen($2)+1+6);
                                          strcpy(tmp,"VAR");
                                          kwrdcvt(tmp);
                                          sprintf($$,"%s %s:STRING",tmp,$2);
                                          nice_free($2); }
        | idlst ':' STRING              { RES_EQ_1_OP(":STRING"); }
#endif
;
dim:      erange                        { RES_EQ_1; }
        | ID                            { RES_EQ_1; }
        | dim ',' dim                   { RES_EQ_1_OP_3(","); }
;
#ifndef std
onedim:   INTCONST                      { RES_EQ_1_INT; }
        | ID                            { RES_EQ_1; }
;
#endif
range:    ordval DOT2 ordval            { RES_EQ_1_OP_3(".."); }
;
erange:   expr DOT2 expr                { RES_EQ_1_OP_3(".."); }
;
pck:      /* empty */                   { RES_EQ_NULL; }
        | PACKED                        { RES_EQ_OP_KWRD("PACKED "); }
        | UNPACKED                      { RES_EQ_OP_KWRD("UNPACKED "); }
;
expr:     ref                           { RES_EQ_1; }
        | ID '(' ')'                    { space(lind);
                                          if (calling) fprintf(lfp,"call of function: %s\n",$1);
                                          RES_EQ_1_OP("()"); }
        | ID '(' arglst ')'
#ifdef vms
                                        { if (translate) {
                                             cvtupp($1,tmp);
                                             if (strcmp(tmp,"IADDRESS")==0) {
                                                NEWELM(1+strlen($3));
                                                sprintf($$,"@%s",$3);
                                                nice_free($1);
                                                nice_free($3);
                                             } else {
                                                space(lind);
                                                if (calling) fprintf(lfp,"call of function: %s\n",$1);
                                                RES_EQ_1_OP_3_OP("(",")");
                                             }
                                          } else {
                                             space(lind);
                                             if (calling) fprintf(lfp,"call of function: %s\n",$1);
                                             RES_EQ_1_OP_3_OP("(",")");
                                          } }
#else
                                        { space(lind);
                                          if (calling) fprintf(lfp,"call of function: %s\n",$1);
                                          RES_EQ_1_OP_3_OP("(",")"); }
#endif
        | cns                           { RES_EQ_1; }
        | '(' expr ')'                  { RES_EQ_OP_2_OP("(",")"); }
        | '-' expr                      { RES_EQ_OP_2("-"); }
        | expr '+' expr                 { RES_EQ_1_OP_3("+"); }
        | expr '-' expr                 { RES_EQ_1_OP_3("-"); }
        | expr '*' expr                 { RES_EQ_1_OP_3("*"); }
        | expr '/' expr                 { RES_EQ_1_OP_3("/"); }
#ifdef vms
        | expr POW expr                 { if (!translate) {
                                             RES_EQ_1_OP_3("**");
                                          } else {
                                             NEWELM(4+strlen($3)+3+strlen($1)+2);
                                             sprintf($$,"EXP(%s*LN(%s))",$3,$1);
                                             nice_free($3);
                                             nice_free($1);
                                          } }
#endif
#ifdef tp
        | '@' ref                       { if (!translate) {
                                             RES_EQ_OP_2("@");
                                          } else {
                                             strcpy(tmp,"IADDRESS");
                                             idcvt(tmp);
                                             NEWELM(8+1+strlen($2)+1);
                                             sprintf($$,"%s(%s)",tmp,$2);
                                             nice_free($2);
                                          } }
#endif
        | expr DIV expr                 { RES_EQ_1_OP_KWRD_3(" DIV "); }
        | expr MOD expr                 { RES_EQ_1_OP_KWRD_3(" MOD "); }
        | expr '=' expr                 { RES_EQ_1_OP_3("="); }
        | expr '>' expr                 { RES_EQ_1_OP_3(">"); }
        | expr '<' expr                 { RES_EQ_1_OP_3("<"); }
        | expr GE expr                  { RES_EQ_1_OP_3(">="); }
        | expr LE expr                  { RES_EQ_1_OP_3("<="); }
        | expr NE expr                  { RES_EQ_1_OP_3("<>"); }
        | expr AND expr                 { RES_EQ_1_OP_KWRD_3(" AND "); }
        | expr OR expr                  { RES_EQ_1_OP_KWRD_3(" OR "); }
        | NOT expr                      { RES_EQ_OP_KWRD_2("NOT "); }
        | expr IN expr                  { RES_EQ_1_OP_KWRD_3(" IN "); }
#ifdef vms
        | expr ANDTHEN expr             { RES_EQ_1_OP_KWRD_3(" AND_THEN "); }
        | expr ORELSE expr              { RES_EQ_1_OP_KWRD_3(" OR_ELSE "); }
#endif
;
ref:      ID                            { RES_EQ_1; }
        | ref '[' index ']'             { RES_EQ_1_OP_3_OP("[","]"); }
        | ref '^'                       { RES_EQ_1_OP("^"); }
        | ref '.' ref                   { RES_EQ_1_OP_3("."); }
;
index:    expr                          { RES_EQ_1; }
        | index ',' expr                { RES_EQ_1_OP_3(","); }
;
arglst:   item                          { RES_EQ_1; }
        | arglst ',' item               { RES_EQ_1_OP_3(","); }
;
item:     expr                          { RES_EQ_1; }
        | expr ':' expr                 { RES_EQ_1_OP_3(":"); }
        | expr ':' expr ':' expr        { NEWELM(strlen($1)+1+strlen($3)+1+strlen($5));
                                          sprintf($$,"%s:%s:%s",$1,$3,$5);
                                          nice_free($1);
                                          nice_free($3);
                                          nice_free($5); }
#ifdef vms
        | ID ASG expr                   { RES_EQ_1_OP_3(asgstr);
                                          if (translate) transerr("keyword-arguments"); }
        | passing expr                  { NEWELM(strlen($1)+1+strlen($2));
                                          sprintf($$,"%s %s",$1,$2);
                                          nice_free($1);
                                          nice_free($2); }
        | ID ASG passing expr            { NEWELM(strlen($1)+2+strlen($3)+1+strlen($4));
                                          sprintf($$,"%s%s%s %s",$1,asgstr,$3,$4);
                                          nice_free($1);
                                          nice_free($3);
                                          nice_free($4); }
#endif
;
#ifdef vms
attr:     '[' idlst ']'                 { RES_EQ_OP_2_OP("[","]"); }
;
strlst:   STRCONST                      { RES_EQ_1; }
        | strlst ',' STRCONST           { RES_EQ_1_OP_3(","); }
;
#endif
idlst:    ID                            { RES_EQ_1; }
        | idlst ',' ID                  { RES_EQ_1_OP_3(","); }
/* terrible hack to use EXTERNAL as both keyword and attribute */
        | EXTERNAL                      { RES_EQ_OP_KWRD("EXTERNAL"); }
        | idlst ',' EXTERNAL            { RES_EQ_1_OP_KWRD(",EXTERNAL"); }
;
ordlst:   ordval                        { RES_EQ_1; }
        | ordlst ',' ordval             { RES_EQ_1_OP_3(","); }
;
ordval:   ordcns                        { RES_EQ_1; }
        | ID                            { RES_EQ_1; }
;
cns:      ordcns                        { RES_EQ_1; }
        | REALCONST                     { RES_EQ_1_REAL; }
        | set                           { RES_EQ_1; }
;
ordcns:   INTCONST                      { RES_EQ_1_INT; }
        | STRCONST                      { RES_EQ_1; }
#ifdef vms
        | INTCONST NUMB HEXCONST        { if (!translate) {
                                             NEWELM(10+1+strlen($3));
                                             sprintf($$,"%d#%s",$1,$3);
                                          } else {
                                             if ($1==16) {
                                                NEWELM(1+strlen($3));
                                                sprintf($$,"$%s",$3);
                                             } else {
                                                transerr("non-hexadecimal");
                                             }
                                          }
                                          nice_free($3); }
        | INTCONST NUMB INTCONST        { if (!translate) {
                                             NEWELM(1+1+10);
                                             sprintf($$,"%d#%d",$1,$3);
                                          } else {
                                             if ($1==16) {
                                                NEWELM(1+10);
                                                sprintf($$,"$%d",$3);
                                             } else {
                                                transerr("non-hexadecimal");
                                             }
                                          } }
#endif
#ifdef tp
        | NUMB INTCONST                 { if (!translate) {
                                             RES_EQ_OP_2_INT("#");
                                          } else {
                                             strcpy(tmp,"CHR");
                                             idcvt(tmp);
                                             NEWELM(3+1+10+1); \
                                             sprintf($$,"%s(%d)",tmp,$2);
                                          } }
        | '$' HEXCONST                  { if (!translate) {
                                             RES_EQ_OP_2("$");
                                          } else {
                                             NEWELM(2+1+10); \
                                             sprintf($$,"16#%s",$2);
                                          }
                                          nice_free($2); }
        | '$' INTCONST                  { if (!translate) {
                                             RES_EQ_OP_2_INT("$");
                                          } else {
                                             NEWELM(2+1+10); \
                                             sprintf($$,"16#%d",$2);
                                          } }
#endif
;
set:     '[' setlst ']'                 { RES_EQ_OP_2_OP("[","]"); }
;
setlst:   range                         { RES_EQ_1; }
        | ordval                        { RES_EQ_1; }
        | setlst ',' range              { RES_EQ_1_OP_3(","); }
        | setlst ',' ordval             { RES_EQ_1_OP_3(","); }
;
#ifdef vms
passing:  '%' IMMED                     { RES_EQ_OP_KWRD("%IMMED"); }
        | '%' REF                       { RES_EQ_OP_KWRD("%REF"); }
        | '%' DESCR                     { RES_EQ_OP_KWRD("%DESCR"); }
        | '%' STDESCR                   { RES_EQ_OP_KWRD("%STDESCR"); }
;
#endif
foreign:  FORWARD                       { RES_EQ_OP_KWRD("FORWARD"); }
        | ZEXTERN                       { RES_EQ_OP_KWRD("EXTERN"); }
        | EXTERNAL                      { RES_EQ_OP_KWRD("EXTERNAL"); }
;

%%

/* PASCAL keywords */
int nokeywrd = 39
#ifdef tp
               - 1
#endif
#ifdef vms
               + 11
#endif
#ifdef tp
               + 6
#ifdef object
               + 3
#endif
#endif
               ;
char *keywrd[] = {
                 "PROGRAM"
                 ,"LABEL"
                 ,"CONST"
                 ,"TYPE"
                 ,"VAR"
                 ,"ARRAY"
                 ,"PACKED"
                 ,"UNPACKED"
                 ,"RECORD"
                 ,"FILE"
                 ,"SET"
                 ,"PROCEDURE"
                 ,"FUNCTION"
                 ,"BEGIN"
                 ,"IF"
                 ,"THEN"
                 ,"ELSE"
                 ,"GOTO"
                 ,"CASE"
#ifndef tp
                 ,"OTHERWISE"
#endif
                 ,"OF"
                 ,"WHILE"
                 ,"DO"
                 ,"FOR"
                 ,"TO"
                 ,"DOWNTO"
                 ,"REPEAT"
                 ,"UNTIL"
                 ,"WITH"
                 ,"AND"
                 ,"OR"
                 ,"NOT"
                 ,"IN"
                 ,"END"
                 ,"DIV"
                 ,"MOD"
                 ,"FORWARD"
                 ,"EXTERN"
                 ,"EXTERNAL"
#ifdef vms
                 ,"VARYING"
                 ,"MODULE"
                 ,"INCLUDE"
                 ,"INHERIT"
                 ,"VALUE"
                 ,"IMMED"
                 ,"REF"
                 ,"DESCR"
                 ,"STDESCR"
                 ,"AND_THEN"
                 ,"OR_ELSE"
#endif
#ifdef tp
                 ,"STRING"
                 ,"USES"
                 ,"UNIT"
                 ,"INTERFACE"
                 ,"PSEUDOFOREIGN"
                 ,"IMPLEMENTATION"
#ifdef object
                 ,"OBJECT"
                 ,"CONSTRUCTOR"
                 ,"DESTRUCTOR"
#endif
#endif
                 };

int token[] = {
              PROGRAM
              ,LABEL
              ,CONST
              ,TYPE
              ,VAR
              ,ARRAY
              ,PACKED
              ,UNPACKED
              ,RECORD
              ,ZFILE
              ,SET
              ,PROCEDURE
              ,FUNCTION
              ,ZBEGIN
              ,IF
              ,THEN
              ,ELSE
              ,GOTO
              ,CASE
#ifndef tp
              ,OTHERWISE
#endif
              ,OF
              ,WHILE
              ,DO
              ,FOR
              ,TO
              ,DOWNTO
              ,REPEAT
              ,UNTIL
              ,WITH
              ,AND
              ,OR
              ,NOT
              ,IN
              ,END
              ,DIV
              ,MOD
              ,FORWARD
              ,ZEXTERN
              ,EXTERNAL
#ifdef vms
              ,VARYING
              ,MODULE
              ,INCLUDE
              ,INHERIT
              ,VALUE
              ,IMMED
              ,REF
              ,DESCR
              ,STDESCR
              ,ANDTHEN
              ,ORELSE
#endif
#ifdef tp
              ,STRING
              ,USES
              ,UNIT
              ,INTERFACE
              ,PSEUDOFOREIGN
              ,IMPLEMENTATION
#ifdef object
              ,OBJECT
              ,CONSTRUCTOR
              ,DESTRUCTOR
#endif
#endif
              };

