Yet Another Shell (part 7 of 11)

Dave Clemans dclemans.falcon at mntgfx.mentor.com
Thu Mar 16 08:31:05 AEST 1989


With all the talk about shells that has been going on recently...

Here's an early pre-release of a "Korn"-like shell for anyone
who might want to experiment.  It is definitely NOT complete,
but it starting to be usable.  It does use some GNU code (for
expression evaluation), so it presumably comes under their
"copyleft".

It basically runs on BSD/USG Unix systems, and on the Atari ST.
I'm currently working on a port to the Amiga.  It is NOT yet
bug free; I am VERY interested in fixes; enhancements, etc.

dgc

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 7 (of 11)."
# Contents:  cexp.tab.c
# Wrapped by dclemans at dclemans on Wed Mar 15 14:03:58 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'cexp.tab.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'cexp.tab.c'\"
else
echo shar: Extracting \"'cexp.tab.c'\" \(35689 characters\)
sed "s/^X//" >'cexp.tab.c' <<'END_OF_FILE'
X
X/*  A Bison parser, made from cexp.y  */
X
X#define	INT	258
X#define	CHAR	259
X#define	NAME	260
X#define	ERROR	261
X#define	OR	262
X#define	AND	263
X#define	EQUAL	264
X#define	NOTEQUAL	265
X#define	LEQ	266
X#define	GEQ	267
X#define	LSH	268
X#define	RSH	269
X#define	UNARY	270
X
X#line 125 "cexp.y"
X
X#include "shell.h"
X#include <setjmp.h>
X/* #define YYDEBUG 1 */
X
X  int yylex ();
X  void yyerror ();
X  int expression_value;
X
X  static jmp_buf parse_return_error;
X
X  /* some external tables of character types */
X  extern unsigned char is_idstart[], is_idchar[];
X
X  static int allow_side_effects = 0;
X
X
X#line 143 "cexp.y"
Xtypedef union {
X  long lval;
X  int voidval;
X  char *sval;
X} YYSTYPE;
X
X#ifndef YYLTYPE
Xtypedef
X  struct yyltype
X    {
X      int timestamp;
X      int first_line;
X      int first_column;
X      int last_line;
X      int last_column;
X      char *text;
X   }
X  yyltype;
X
X#define YYLTYPE yyltype
X#endif
X
X#define	YYACCEPT	return(0)
X#define	YYABORT	return(1)
X#define	YYERROR	goto yyerrlab
X#include <stdio.h>
X
X#ifndef __STDC__
X#define const
X#endif
X
X
X
X#define	YYFINAL		61
X#define	YYFLAG		-32768
X#define	YYNTBASE	34
X
X#define YYTRANSLATE(x) ((unsigned)(x) <= 270 ? yytranslate[x] : 37)
X
Xstatic const char yytranslate[] = {     0,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,    29,     2,     2,     2,    27,    14,     2,    31,
X    32,    25,    23,     9,    24,     2,    26,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     8,     2,    17,
X    33,    18,     7,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,    13,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,    12,     2,    30,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
X     2,     2,     2,     2,     2,     1,     2,     3,     4,     5,
X     6,    10,    11,    15,    16,    19,    20,    21,    22,    28
X};
X
Xstatic const short yyrline[] = {     0,
X   172,   177,   178,   183,   185,   187,   189,   191,   206,   208,
X   215,   222,   224,   226,   228,   230,   232,   234,   236,   238,
X   240,   242,   244,   246,   248,   250,   252,   254,   256,   258
X};
X
Xstatic const char * const yytname[] = {     0,
X"error","$illegal.","INT","CHAR","NAME","ERROR","'?'","':'","','","OR",
X"AND","'|'","'^'","'&'","EQUAL","NOTEQUAL","'<'","'>'","LEQ","GEQ",
X"LSH","RSH","'+'","'-'","'*'","'/'","'%'","UNARY","'!'","'~'",
X"'('","')'","'='","start"
X};
X
Xstatic const short yyr1[] = {     0,
X    34,    35,    35,    36,    36,    36,    36,    36,    36,    36,
X    36,    36,    36,    36,    36,    36,    36,    36,    36,    36,
X    36,    36,    36,    36,    36,    36,    36,    36,    36,    36
X};
X
Xstatic const short yyr2[] = {     0,
X     1,     1,     3,     2,     2,     2,     3,     3,     3,     3,
X     3,     3,     3,     3,     3,     3,     3,     3,     3,     3,
X     3,     3,     3,     3,     3,     3,     5,     1,     1,     1
X};
X
Xstatic const short yydefact[] = {     0,
X    28,    29,    30,     0,     0,     0,     0,     1,     2,     0,
X     4,     5,     6,     0,     0,     0,     0,     0,     0,     0,
X     0,     0,     0,     0,     0,     0,     0,     0,     0,     0,
X     0,     0,     0,     0,     8,     7,     3,     0,    26,    25,
X    24,    23,    22,    16,    17,    20,    21,    18,    19,    14,
X    15,    12,    13,     9,    10,    11,     0,    27,     0,     0,
X     0
X};
X
Xstatic const short yydefgoto[] = {    59,
X     8,     9
X};
X
Xstatic const short yypact[] = {    28,
X-32768,-32768,   -23,    28,    28,    28,    28,    26,    76,    28,
X-32768,-32768,-32768,    29,    28,    28,    28,    28,    28,    28,
X    28,    28,    28,    28,    28,    28,    28,    28,    28,    28,
X    28,    28,    28,    28,    26,-32768,    94,    55,   111,   127,
X   142,   156,    24,   167,   167,   -18,   -18,   -18,   -18,   172,
X   172,   175,   175,-32768,-32768,-32768,    28,    76,    36,    54,
X-32768
X};
X
Xstatic const short yypgoto[] = {-32768,
X    27,    -4
X};
X
X
X#define	YYLAST		202
X
X
Xstatic const short yytable[] = {    11,
X    12,    13,    28,    29,    30,    31,    32,    33,    34,    10,
X    37,    38,    39,    40,    41,    42,    43,    44,    45,    46,
X    47,    48,    49,    50,    51,    52,    53,    54,    55,    56,
X     1,     2,     3,    14,    15,    60,    35,    15,    22,    23,
X    24,    25,    26,    27,    28,    29,    30,    31,    32,    33,
X    34,     4,    58,    61,     0,     0,     5,     6,     7,     0,
X    36,    16,    57,     0,    17,    18,    19,    20,    21,    22,
X    23,    24,    25,    26,    27,    28,    29,    30,    31,    32,
X    33,    34,    16,     0,     0,    17,    18,    19,    20,    21,
X    22,    23,    24,    25,    26,    27,    28,    29,    30,    31,
X    32,    33,    34,    17,    18,    19,    20,    21,    22,    23,
X    24,    25,    26,    27,    28,    29,    30,    31,    32,    33,
X    34,    18,    19,    20,    21,    22,    23,    24,    25,    26,
X    27,    28,    29,    30,    31,    32,    33,    34,    19,    20,
X    21,    22,    23,    24,    25,    26,    27,    28,    29,    30,
X    31,    32,    33,    34,    20,    21,    22,    23,    24,    25,
X    26,    27,    28,    29,    30,    31,    32,    33,    34,    21,
X    22,    23,    24,    25,    26,    27,    28,    29,    30,    31,
X    32,    33,    34,    24,    25,    26,    27,    28,    29,    30,
X    31,    32,    33,    34,    30,    31,    32,    33,    34,    32,
X    33,    34
X};
X
Xstatic const short yycheck[] = {     4,
X     5,     6,    21,    22,    23,    24,    25,    26,    27,    33,
X    15,    16,    17,    18,    19,    20,    21,    22,    23,    24,
X    25,    26,    27,    28,    29,    30,    31,    32,    33,    34,
X     3,     4,     5,     7,     9,     0,    10,     9,    15,    16,
X    17,    18,    19,    20,    21,    22,    23,    24,    25,    26,
X    27,    24,    57,     0,    -1,    -1,    29,    30,    31,    -1,
X    32,     7,     8,    -1,    10,    11,    12,    13,    14,    15,
X    16,    17,    18,    19,    20,    21,    22,    23,    24,    25,
X    26,    27,     7,    -1,    -1,    10,    11,    12,    13,    14,
X    15,    16,    17,    18,    19,    20,    21,    22,    23,    24,
X    25,    26,    27,    10,    11,    12,    13,    14,    15,    16,
X    17,    18,    19,    20,    21,    22,    23,    24,    25,    26,
X    27,    11,    12,    13,    14,    15,    16,    17,    18,    19,
X    20,    21,    22,    23,    24,    25,    26,    27,    12,    13,
X    14,    15,    16,    17,    18,    19,    20,    21,    22,    23,
X    24,    25,    26,    27,    13,    14,    15,    16,    17,    18,
X    19,    20,    21,    22,    23,    24,    25,    26,    27,    14,
X    15,    16,    17,    18,    19,    20,    21,    22,    23,    24,
X    25,    26,    27,    17,    18,    19,    20,    21,    22,    23,
X    24,    25,    26,    27,    23,    24,    25,    26,    27,    25,
X    26,    27
X};
X#define YYPURE 1
X
X#line 2 "bison.simple"
X
X/* Skeleton output parser for bison,
X   copyright (C) 1984 Bob Corbett and Richard Stallman
X
X		       NO WARRANTY
X
X  BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
XNO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
XWHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
XRICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
XDEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
XCORRECTION.
X
X IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
XSTALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
XWHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
XLIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
XOTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
XUSE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
XDATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
XA FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
XPROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
XDAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
X
X		GENERAL PUBLIC LICENSE TO COPY
X
X  1. You may copy and distribute verbatim copies of this source file
Xas you receive it, in any medium, provided that you conspicuously and
Xappropriately publish on each copy a valid copyright notice "Copyright
X(C) 1985 Free Software Foundation, Inc."; and include following the
Xcopyright notice a verbatim copy of the above disclaimer of warranty
Xand of this License.  You may charge a distribution fee for the
Xphysical act of transferring a copy.
X
X  2. You may modify your copy or copies of this source file or
Xany portion of it, and copy and distribute such modifications under
Xthe terms of Paragraph 1 above, provided that you also do the following:
X
X    a) cause the modified files to carry prominent notices stating
X    that you changed the files and the date of any change; and
X
X    b) cause the whole of any work that you distribute or publish,
X    that in whole or in part contains or is a derivative of this
X    program or any part thereof, to be licensed at no charge to all
X    third parties on terms identical to those contained in this
X    License Agreement (except that you may choose to grant more extensive
X    warranty protection to some or all third parties, at your option).
X
X    c) You may charge a distribution fee for the physical act of
X    transferring a copy, and you may at your option offer warranty
X    protection in exchange for a fee.
X
XMere aggregation of another unrelated program with this program (or its
Xderivative) on a volume of a storage or distribution medium does not bring
Xthe other program under the scope of these terms.
X
X  3. You may copy and distribute this program (or a portion or derivative
Xof it, under Paragraph 2) in object code or executable form under the terms
Xof Paragraphs 1 and 2 above provided that you also do one of the following:
X
X    a) accompany it with the complete corresponding machine-readable
X    source code, which must be distributed under the terms of
X    Paragraphs 1 and 2 above; or,
X
X    b) accompany it with a written offer, valid for at least three
X    years, to give any third party free (except for a nominal
X    shipping charge) a complete machine-readable copy of the
X    corresponding source code, to be distributed under the terms of
X    Paragraphs 1 and 2 above; or,
X
X    c) accompany it with the information you received as to where the
X    corresponding source code may be obtained.  (This alternative is
X    allowed only for noncommercial distribution and only if you
X    received the program in object code or executable form alone.)
X
XFor an executable file, complete source code means all the source code for
Xall modules it contains; but, as a special exception, it need not include
Xsource code for modules which are standard libraries that accompany the
Xoperating system on which the executable file runs.
X
X  4. You may not copy, sublicense, distribute or transfer this program
Xexcept as expressly provided under this License Agreement.  Any attempt
Xotherwise to copy, sublicense, distribute or transfer this program is void and
Xyour rights to use the program under this License agreement shall be
Xautomatically terminated.  However, parties who have received computer
Xsoftware programs from you with this License Agreement will not have
Xtheir licenses terminated so long as such parties remain in full compliance.
X
X  5. If you wish to incorporate parts of this program into other free
Xprograms whose distribution conditions are different, write to the Free
XSoftware Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
Xworked out a simple rule that can be stated here, but we will often permit
Xthis.  We will be guided by the two goals of preserving the free status of
Xall derivatives of our free software and of promoting the sharing and reuse of
Xsoftware.
X
X
XIn other words, you are welcome to use, share and improve this program.
XYou are forbidden to forbid anyone else to use, share and improve
Xwhat you give them.   Help stamp out software-hoarding!  */
X
X/* This is the parser code that is written into each bison parser
X  when the %semantic_parser declaration is not specified in the grammar.
X  It was written by Richard Stallman by simplifying the hairy parser
X  used when %semantic_parser is specified.  */
X
X/* Note: there must be only one dollar sign in this file.
X   It is replaced by the list of actions, each action
X   as one case of the switch.  */
X
X#define yyerrok		(yyerrstatus = 0)
X#define yyclearin	(yychar = YYEMPTY)
X#define YYEMPTY		-2
X#define YYEOF		0
X#define YYFAIL		goto yyerrlab;
X
X#define YYTERROR	1
X
X#ifndef YYIMPURE
X#define YYLEX		yylex()
X#endif
X
X#ifndef YYPURE
X#define YYLEX		yylex(&yylval, &yylloc)
X#endif
X
X/* If nonreentrant, generate the variables here */
X
X#ifndef YYIMPURE
X
Xint	yychar;			/*  the lookahead symbol		*/
XYYSTYPE	yylval;			/*  the semantic value of the		*/
X				/*  lookahead symbol			*/
X
XYYLTYPE yylloc;			/*  location data for the lookahead	*/
X				/*  symbol				*/
X
Xint yynerr;			/*  number of parse errors so far       */
X
X#ifdef YYDEBUG
Xint yydebug = 0;		/*  nonzero means print parse trace	*/
X#endif
X
X#endif  /* YYIMPURE */
X
X
X/*  YYMAXDEPTH indicates the initial size of the parser's stacks	*/
X
X#ifndef	YYMAXDEPTH
X#define YYMAXDEPTH 200
X#endif
X
X/*  YYMAXLIMIT is the maximum size the stacks can grow to
X    (effective only if the built-in stack extension method is used).  */
X
X#ifndef YYMAXLIMIT
X#define YYMAXLIMIT 10000
X#endif
X
X
X#line 167 "bison.simple"
Xint
Xyyparse()
X{
X  register int yystate;
X  register int yyn;
X  register short *yyssp;
X  register YYSTYPE *yyvsp;
X  YYLTYPE *yylsp;
X  int yyerrstatus;	/*  number of tokens to shift before error messages enabled */
X  int yychar1;		/*  lookahead token as an internal (translated) token number */
X
X  short	yyssa[YYMAXDEPTH];	/*  the state stack			*/
X  YYSTYPE yyvsa[YYMAXDEPTH];	/*  the semantic value stack		*/
X  YYLTYPE yylsa[YYMAXDEPTH];	/*  the location stack			*/
X
X  short *yyss = yyssa;		/*  refer to the stacks thru separate pointers */
X  YYSTYPE *yyvs = yyvsa;	/*  to allow yyoverflow to reallocate them elsewhere */
X  YYLTYPE *yyls = yylsa;
X
X  int yymaxdepth = YYMAXDEPTH;
X
X#ifndef YYPURE
X  int yychar;
X  YYSTYPE yylval;
X  YYLTYPE yylloc;
X#endif
X
X#ifdef YYDEBUG
X  extern int yydebug;
X#endif
X
X
X  YYSTYPE yyval;		/*  the variable used to return		*/
X				/*  semantic values from the action	*/
X				/*  routines				*/
X
X  int yylen;
X
X#ifdef YYDEBUG
X  if (yydebug)
X    fprintf(stderr, "Starting parse\n");
X#endif
X
X  yystate = 0;
X  yyerrstatus = 0;
X  yynerr = 0;
X  yychar = YYEMPTY;		/* Cause a token to be read.  */
X
X  /* Initialize stack pointers.
X     Waste one element of value and location stack
X     so that they stay on the same level as the state stack.  */
X
X  yyssp = yyss - 1;
X  yyvsp = yyvs;
X  yylsp = yyls;
X
X/* Push a new state, which is found in  yystate  .  */
X/* In all cases, when you get here, the value and location stacks
X   have just been pushed. so pushing a state here evens the stacks.  */
Xyynewstate:
X
X  *++yyssp = yystate;
X
X  if (yyssp >= yyss + yymaxdepth - 1)
X    {
X      /* Give user a chance to reallocate the stack */
X      /* Use copies of these so that the &'s don't force the real ones into memory. */
X      YYSTYPE *yyvs1 = yyvs;
X      YYLTYPE *yyls1 = yyls;
X      short *yyss1 = yyss;
X
X      /* Get the current used size of the three stacks, in elements.  */
X      int size = yyssp - yyss + 1;
X
X#ifdef yyoverflow
X      /* Each stack pointer address is followed by the size of
X	 the data in use in that stack, in bytes.  */
X      yyoverflow("parser stack overflow",
X		 &yyss1, size * sizeof (*yyssp),
X		 &yyvs1, size * sizeof (*yyvsp),
X		 &yyls1, size * sizeof (*yylsp),
X		 &yymaxdepth);
X
X      yyss = yyss1; yyvs = yyvs1; yyls = yyls1;
X#else /* no yyoverflow */
X      /* Extend the stack our own way.  */
X      if (yymaxdepth >= YYMAXLIMIT)
X	yyerror("parser stack overflow");
X      yymaxdepth *= 2;
X      if (yymaxdepth > YYMAXLIMIT)
X	yymaxdepth = YYMAXLIMIT;
X      yyss = (short *) alloca (yymaxdepth * sizeof (*yyssp));
X      bcopy ((char *)yyss1, (char *)yyss, size * sizeof (*yyssp));
X      yyvs = (YYSTYPE *) alloca (yymaxdepth * sizeof (*yyvsp));
X      bcopy ((char *)yyvs1, (char *)yyvs, size * sizeof (*yyvsp));
X#ifdef YYLSP_NEEDED
X      yyls = (YYLTYPE *) alloca (yymaxdepth * sizeof (*yylsp));
X      bcopy ((char *)yyls1, (char *)yyls, size * sizeof (*yylsp));
X#endif
X#endif /* no yyoverflow */
X
X      yyssp = yyss + size - 1;
X      yyvsp = yyvs + size - 1;
X#ifdef YYLSP_NEEDED
X      yylsp = yyls + size - 1;
X#endif
X
X#ifdef YYDEBUG
X      if (yydebug)
X	fprintf(stderr, "Stack size increased to %d\n", yymaxdepth);
X#endif
X
X      if (yyssp >= yyss + yymaxdepth - 1)
X	YYABORT;
X    }
X
X#ifdef YYDEBUG
X  if (yydebug)
X    fprintf(stderr, "Entering state %d\n", yystate);
X#endif
X
X/* Do appropriate processing given the current state.  */
X/* Read a lookahead token if we need one and don't already have one.  */
Xyyresume:
X
X  /* First try to decide what to do without reference to lookahead token.  */
X
X  yyn = yypact[yystate];
X  if (yyn == YYFLAG)
X    goto yydefault;
X
X  /* Not known => get a lookahead token if don't already have one.  */
X
X  /* yychar is either YYEMPTY or YYEOF
X     or a valid token in external form.  */
X
X  if (yychar == YYEMPTY)
X    {
X#ifdef YYDEBUG
X      if (yydebug)
X	fprintf(stderr, "Reading a token: ");
X#endif
X      yychar = YYLEX;
X    }
X
X  /* Convert token to internal form (in yychar1) for indexing tables with */
X
X  if (yychar <= 0)		/* This means end of input. */
X    {
X      yychar1 = 0;
X      yychar = YYEOF;		/* Don't call YYLEX any more */
X
X#ifdef YYDEBUG
X      if (yydebug)
X	fprintf(stderr, "Now at end of input.\n");
X#endif
X    }
X  else
X    {
X      yychar1 = YYTRANSLATE(yychar);
X
X#ifdef YYDEBUG
X      if (yydebug)
X	fprintf(stderr, "Next token is %d (%s)\n", yychar, yytname[yychar1]);
X#endif
X    }
X
X  yyn += yychar1;
X  if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
X    goto yydefault;
X
X  yyn = yytable[yyn];
X
X  /* yyn is what to do for this token type in this state.
X     Negative => reduce, -yyn is rule number.
X     Positive => shift, yyn is new state.
X       New state is final state => don't bother to shift,
X       just return success.
X     0, or most negative number => error.  */
X
X  if (yyn < 0)
X    {
X      if (yyn == YYFLAG)
X	goto yyerrlab;
X      yyn = -yyn;
X      goto yyreduce;
X    }
X  else if (yyn == 0)
X    goto yyerrlab;
X
X  if (yyn == YYFINAL)
X    YYACCEPT;
X
X  /* Shift the lookahead token.  */
X
X#ifdef YYDEBUG
X  if (yydebug)
X    fprintf(stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]);
X#endif
X
X  /* Discard the token being shifted unless it is eof.  */
X  if (yychar != YYEOF)
X    yychar = YYEMPTY;
X
X  *++yyvsp = yylval;
X#ifdef YYLSP_NEEDED
X  *++yylsp = yylloc;
X#endif
X
X  /* count tokens shifted since error; after three, turn off error status.  */
X  if (yyerrstatus) yyerrstatus--;
X
X  yystate = yyn;
X  goto yynewstate;
X
X/* Do the default action for the current state.  */
Xyydefault:
X
X  yyn = yydefact[yystate];
X  if (yyn == 0)
X    goto yyerrlab;
X
X/* Do a reduction.  yyn is the number of a rule to reduce with.  */
Xyyreduce:
X  yylen = yyr2[yyn];
X  yyval = yyvsp[1-yylen]; /* implement default value of the action */
X
X#ifdef YYDEBUG
X  if (yydebug)
X    {
X      if (yylen == 1)
X	fprintf (stderr, "Reducing 1 value via line %d, ",
X		 yyrline[yyn]);
X      else
X	fprintf (stderr, "Reducing %d values via line %d, ",
X		 yylen, yyrline[yyn]);
X    }
X#endif
X
X
X  switch (yyn) {
X
Xcase 1:
X#line 173 "cexp.y"
X{ expression_value = yyvsp[0].lval; ;
X    break;}
Xcase 3:
X#line 179 "cexp.y"
X{ yyval.lval = yyvsp[0].lval; ;
X    break;}
Xcase 4:
X#line 184 "cexp.y"
X{ yyval.lval = - yyvsp[0].lval; ;
X    break;}
Xcase 5:
X#line 186 "cexp.y"
X{ yyval.lval = ! yyvsp[0].lval; ;
X    break;}
Xcase 6:
X#line 188 "cexp.y"
X{ yyval.lval = ~ yyvsp[0].lval; ;
X    break;}
Xcase 7:
X#line 190 "cexp.y"
X{ yyval.lval = yyvsp[-1].lval; ;
X    break;}
Xcase 8:
X#line 192 "cexp.y"
X{
X              if (yyvsp[-2].sval == (char *)NULL)
X                yyval.lval = 0;
X              else { char buffer[16];
X                sprintf(buffer,"%d",yyvsp[0].lval);
X                if (allow_side_effects)
X                  var_define0(&yyvsp[-2].sval[1],buffer,0);
X                free(yyvsp[-2].sval);
X                yyval.lval = yyvsp[0].lval;
X              }
X            ;
X    break;}
Xcase 9:
X#line 207 "cexp.y"
X{ yyval.lval = yyvsp[-2].lval * yyvsp[0].lval; ;
X    break;}
Xcase 10:
X#line 209 "cexp.y"
X{ if (yyvsp[0].lval == 0)
X			    {
X			      errmsg (0,LOC("yyparse"),"division by zero");
X			      yyvsp[0].lval = 1;
X			    }
X			  yyval.lval = yyvsp[-2].lval / yyvsp[0].lval; ;
X    break;}
Xcase 11:
X#line 216 "cexp.y"
X{ if (yyvsp[0].lval == 0)
X			    {
X			      errmsg (0,LOC("yparse"),"division by zero");
X			      yyvsp[0].lval = 1;
X			    }
X			  yyval.lval = yyvsp[-2].lval % yyvsp[0].lval; ;
X    break;}
Xcase 12:
X#line 223 "cexp.y"
X{ yyval.lval = yyvsp[-2].lval + yyvsp[0].lval; ;
X    break;}
Xcase 13:
X#line 225 "cexp.y"
X{ yyval.lval = yyvsp[-2].lval - yyvsp[0].lval; ;
X    break;}
Xcase 14:
X#line 227 "cexp.y"
X{ yyval.lval = yyvsp[-2].lval << yyvsp[0].lval; ;
X    break;}
Xcase 15:
X#line 229 "cexp.y"
X{ yyval.lval = yyvsp[-2].lval >> yyvsp[0].lval; ;
X    break;}
Xcase 16:
X#line 231 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval == yyvsp[0].lval); ;
X    break;}
Xcase 17:
X#line 233 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval != yyvsp[0].lval); ;
X    break;}
Xcase 18:
X#line 235 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval <= yyvsp[0].lval); ;
X    break;}
Xcase 19:
X#line 237 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval >= yyvsp[0].lval); ;
X    break;}
Xcase 20:
X#line 239 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval < yyvsp[0].lval); ;
X    break;}
Xcase 21:
X#line 241 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval > yyvsp[0].lval); ;
X    break;}
Xcase 22:
X#line 243 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval & yyvsp[0].lval); ;
X    break;}
Xcase 23:
X#line 245 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval ^ yyvsp[0].lval); ;
X    break;}
Xcase 24:
X#line 247 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval | yyvsp[0].lval); ;
X    break;}
Xcase 25:
X#line 249 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval && yyvsp[0].lval); ;
X    break;}
Xcase 26:
X#line 251 "cexp.y"
X{ yyval.lval = (yyvsp[-2].lval || yyvsp[0].lval); ;
X    break;}
Xcase 27:
X#line 253 "cexp.y"
X{ yyval.lval = yyvsp[-4].lval ? yyvsp[-2].lval : yyvsp[0].lval; ;
X    break;}
Xcase 28:
X#line 255 "cexp.y"
X{ yyval.lval = yylval.lval; ;
X    break;}
Xcase 29:
X#line 257 "cexp.y"
X{ yyval.lval = yylval.lval; ;
X    break;}
Xcase 30:
X#line 259 "cexp.y"
X{
X              if (yylval.sval == (char *)NULL)
X                yyval.lval = 0;
X              else { char *ptr;
X                ptr = var_normal(yylval.sval);
X                if (ptr != (char *)NULL) {
X                  yyval.lval = atoi(ptr);
X                  free(ptr);
X                }
X                else yyval.lval = 0;
X                free(yylval.sval);
X              }
X            ;
X    break;}
X}
X   /* the action file gets copied in in place of this dollarsign */
X#line 408 "bison.simple"
X
X  yyvsp -= yylen;
X  yyssp -= yylen;
X#ifdef YYLSP_NEEDED
X  yylsp -= yylen;
X#endif
X
X#ifdef YYDEBUG
X  if (yydebug)
X    {
X      short *ssp1 = yyss - 1;
X      fprintf (stderr, "state stack now");
X      while (ssp1 != yyssp)
X	fprintf (stderr, " %d", *++ssp1);
X      fprintf (stderr, "\n");
X    }
X#endif
X
X  *++yyvsp = yyval;
X
X#ifdef YYLSP_NEEDED
X  yylsp++;
X  if (yylen == 0)
X    {
X      yylsp->first_line = yylloc.first_line;
X      yylsp->first_column = yylloc.first_column;
X      yylsp->last_line = (yylsp-1)->last_line;
X      yylsp->last_column = (yylsp-1)->last_column;
X      yylsp->text = 0;
X    }
X  else
X    {
X      yylsp->last_line = (yylsp+yylen-1)->last_line;
X      yylsp->last_column = (yylsp+yylen-1)->last_column;
X    }
X#endif
X
X  /* Now "shift" the result of the reduction.
X     Determine what state that goes to,
X     based on the state we popped back to
X     and the rule number reduced by.  */
X
X  yyn = yyr1[yyn];
X
X  yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
X  if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
X    yystate = yytable[yystate];
X  else
X    yystate = yydefgoto[yyn - YYNTBASE];
X
X  goto yynewstate;
X
Xyyerrlab:   /* here on detecting error */
X
X  if (! yyerrstatus)
X    /* If not already recovering from an error, report this error.  */
X    {
X      ++yynerr;
X      yyerror("parse error");
X    }
X
X  if (yyerrstatus == 3)
X    {
X      /* if just tried and failed to reuse lookahead token after an error, discard it.  */
X
X      /* return failure if at end of input */
X      if (yychar == YYEOF)
X	YYABORT;
X
X#ifdef YYDEBUG
X      if (yydebug)
X	fprintf(stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1]);
X#endif
X
X      yychar = YYEMPTY;
X    }
X
X  /* Else will try to reuse lookahead token
X     after shifting the error token.  */
X
X  yyerrstatus = 3;		/* Each real token shifted decrements this */
X
X  goto yyerrhandle;
X
Xyyerrdefault:  /* current state does not do anything special for the error token. */
X
X#if 0
X  /* This is wrong; only states that explicitly want error tokens
X     should shift them.  */
X  yyn = yydefact[yystate];  /* If its default is to accept any token, ok.  Otherwise pop it.*/
X  if (yyn) goto yydefault;
X#endif
X
Xyyerrpop:   /* pop the current state because it cannot handle the error token */
X
X  if (yyssp == yyss) YYABORT;
X  yyvsp--;
X  yystate = *--yyssp;
X#ifdef YYLSP_NEEDED
X  yylsp--;
X#endif
X
X#ifdef YYDEBUG
X  if (yydebug)
X    {
X      short *ssp1 = yyss - 1;
X      fprintf (stderr, "Error: state stack now");
X      while (ssp1 != yyssp)
X	fprintf (stderr, " %d", *++ssp1);
X      fprintf (stderr, "\n");
X    }
X#endif
X
Xyyerrhandle:
X
X  yyn = yypact[yystate];
X  if (yyn == YYFLAG)
X    goto yyerrdefault;
X
X  yyn += YYTERROR;
X  if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
X    goto yyerrdefault;
X
X  yyn = yytable[yyn];
X  if (yyn < 0)
X    {
X      if (yyn == YYFLAG)
X	goto yyerrpop;
X      yyn = -yyn;
X      goto yyreduce;
X    }
X  else if (yyn == 0)
X    goto yyerrpop;
X
X  if (yyn == YYFINAL)
X    YYACCEPT;
X
X#ifdef YYDEBUG
X  if (yydebug)
X    fprintf(stderr, "Shifting error token, ");
X#endif
X
X  *++yyvsp = yylval;
X#ifdef YYLSP_NEEDED
X  *++yylsp = yylloc;
X#endif
X
X  yystate = yyn;
X  goto yynewstate;
X}
X#line 273 "cexp.y"
X
X
X/* During parsing of a C expression, the pointer to the next character
X   is in this variable.  */
X
Xstatic char *lexptr;
X
X/* Take care of parsing a number (anything that starts with a digit).
X   Set yylval and return the token type; update lexptr.
X   LEN is the number of characters in it.  */
X
X/* maybe needs to actually deal with floating point numbers */
X
Xint
Xparse_number (olen)
X     int olen;
X{
X  register char *p = lexptr;
X  register long n = 0;
X  register int c;
X  register int base = 10;
X  register len = olen;
X
X  extern double atof ();
X
X  for (c = 0; c < len; c++)
X    if (p[c] == '.') {
X      /* It's a float since it contains a point.  */
X      yyerror ("floating point numbers not allowed in expressions");
X      return ERROR;
X      
X/* ****************
X	 yylval.dval = atof (p);
X	 lexptr += len;
X	 return FLOAT;
X		 ****************  */
X    }
X  
X  if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
X    p += 2;
X    base = 16;
X    len -= 2;
X  }
X  else if (*p == '0')
X    base = 8;
X  
X  while (len-- > 0) {
X    c = *p++;
X    n *= base;
X    if (c >= '0' && c <= '9')
X      n += c - '0';
X    else {
X      if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
X      if (base == 16 && c >= 'a' && c <= 'f')
X	n += c - 'a' + 10;
X      else if (len == 0 && c == 'l')
X	;
X      else {
X	yyerror ("Invalid number in expression");
X	return ERROR;
X      }
X    }
X  }
X
X  lexptr = p;
X  yylval.lval = n;
X  return INT;
X}
X
Xstruct etoken {
X  char *operator;
X  int token;
X};
X
X#ifndef	NULL
X#define NULL 0
X#endif
X
Xstatic struct etoken tokentab2[] = {
X  {"&&", AND},
X  {"||", OR},
X  {"<<", LSH},
X  {">>", RSH},
X  {"==", EQUAL},
X  {"!=", NOTEQUAL},
X  {"<=", LEQ},
X  {">=", GEQ},
X  {NULL, ERROR}
X};
X
X/* Read one token, getting characters through lexptr.  */
X
Xint
Xyylex ()
X{
X  register int c;
X  register int namelen;
X  register char *tokstart;
X  register struct etoken *toktab;
X  char *name;
X
X retry:
X
X  tokstart = lexptr;
X  c = *tokstart;
X  /* See if it is a special token of length 2.  */
X  for (toktab = tokentab2; toktab->operator != NULL; toktab++)
X    if (c == *toktab->operator && tokstart[1] == toktab->operator[1]) {
X      lexptr += 2;
X      return toktab->token;
X    }
X
X  switch (c) {
X  case 0:
X    return 0;
X    
X  case ' ':
X  case '\t':
X  case '\n':
X    lexptr++;
X    goto retry;
X    
X  case '\'':
X    lexptr++;
X    c = *lexptr++;
X    if (c == '\\')
X      c = parse_escape (&lexptr);
X    yylval.lval = c;
X    c = *lexptr++;
X    if (c != '\'') {
X      yyerror ("Invalid character constant");
X      return ERROR;
X    }
X    
X    return CHAR;
X
X    /* some of these chars are invalid in constant expressions;
X       maybe do something about them later */
X  case '/':
X  case '+':
X  case '-':
X  case '*':
X  case '%':
X  case '|':
X  case '&':
X  case '^':
X  case '~':
X  case '!':
X  case '@':
X  case '<':
X  case '>':
X  case '(':
X  case ')':
X  case '[':
X  case ']':
X  case '.':
X  case '?':
X  case ':':
X  case '=':
X  case '{':
X  case '}':
X  case ',':
X    lexptr++;
X    return c;
X    
X  case '"':
X    yyerror ("double quoted strings not allowed in expressions");
X    return ERROR;
X  }
X  if (c >= '0' && c <= '9') {
X    /* It's a number */
X    for (namelen = 0;
X	 c = tokstart[namelen], is_idchar[c] || c == '.'; 
X	 namelen++)
X      ;
X    return parse_number (namelen);
X  }
X  
X  if (!is_idstart[c]) {
X    yyerror ("Invalid token in expression");
X    return ERROR;
X  }
X  
X  /* It is a name.  See how long it is.  */
X
X  for (namelen = 0; is_idchar[tokstart[namelen]]; namelen++)
X    ;
X  name = new_string(namelen+2);
X  if (name == (char *)NULL)
X    errmsg(SHERR_NOMEM,LOC("yylex"));
X  yylval.sval = name;
X  if (name != (char *)NULL) {
X    name[0] = '$';
X    strncpy(&name[1],tokstart,namelen);
X    name[namelen+1] = '\0';
X  }
X  
X  lexptr += namelen;
X  return NAME;
X}
X
X
X/* Parse a C escape sequence.  STRING_PTR points to a variable
X   containing a pointer to the string to parse.  That pointer
X   is updated past the characters we use.  The value of the
X   escape sequence is returned.
X
X   A negative value means the sequence \ newline was seen,
X   which is supposed to be equivalent to nothing at all.
X
X   If \ is followed by a null character, we return a negative
X   value and leave the string pointer pointing at the null character.
X
X   If \ is followed by 000, we return 0 and leave the string pointer
X   after the zeros.  A value of 0 does not mean end of string.  */
X
Xint
Xparse_escape (string_ptr)
X     char **string_ptr;
X{
X  register int c = *(*string_ptr)++;
X  switch (c)
X    {
X    case 'a':
X      return '\a';
X    case 'b':
X      return '\b';
X    case 'e':
X      return 033;
X    case 'f':
X      return '\f';
X    case 'n':
X      return '\n';
X    case 'r':
X      return '\r';
X    case 't':
X      return '\t';
X    case 'v':
X      return '\v';
X    case '\n':
X      return -2;
X    case 0:
X      (*string_ptr)--;
X      return 0;
X    case '^':
X      c = *(*string_ptr)++;
X      if (c == '\\')
X	c = parse_escape (string_ptr);
X      if (c == '?')
X	return 0177;
X      return (c & 0200) | (c & 037);
X      
X    case '0':
X    case '1':
X    case '2':
X    case '3':
X    case '4':
X    case '5':
X    case '6':
X    case '7':
X      {
X	register int i = c - '0';
X	register int count = 0;
X	while (++count < 3)
X	  {
X	    if ((c = *(*string_ptr)++) >= '0' && c <= '7')
X	      {
X		i *= 8;
X		i += c - '0';
X	      }
X	    else
X	      {
X		(*string_ptr)--;
X		break;
X	      }
X	  }
X	return i;
X      }
X    default:
X      return c;
X    }
X}
X
Xvoid
Xyyerror (s)
X     char *s;
X{
X  errmsg (0,LOC("yyerror"),s);
X  longjmp (parse_return_error, 1);
X}
X
X/* This page contains the entry point to this file.  */
X
X/* Parse STRING as an expression, and complain if this fails
X   to use up all of the contents of STRING.  */
X/* We do not support C comments.  They should be removed before
X   this function is called.  */
X
Xint
Xparse_c_expression (string,side_effects)
X     char *string;
X     int side_effects;
X{
X  static int initialized;
X
X  if (!initialized) {
X    initialize_random_junk();
X    initialized++;
X  }
X
X  lexptr = string;
X  allow_side_effects = side_effects;
X  
X  if (lexptr == 0 || *lexptr == 0) {
X    return 0;
X  }
X
X  /* if there is some sort of scanning error, just return 0 and assume
X     the parsing routine has printed an error message somewhere.
X     there is surely a better thing to do than this.     */
X  if (setjmp(parse_return_error))
X    return 0;
X
X  if (yyparse ())
X    return 0;			/* actually this is never reached
X				   the way things stand. */
X  if (*lexptr)
X    errmsg (0,LOC("parse_c_expression"),"Junk after end of expression.");
X
X  return expression_value;	/* set by yyparse() */
X}
X
X/* table to tell if char can be part of a C identifier. */
Xunsigned char is_idchar[256];
X/* table to tell if char can be first char of a c identifier. */
Xunsigned char is_idstart[256];
X
X/*
X * initialize random junk in the hash table and maybe other places
X */
Xinitialize_random_junk()
X{
X  register int i;
X
X  /*
X   * Set up is_idchar and is_idstart tables.  These should be
X   * faster than saying (is_alpha(c) || c == '_'), etc.
X   * Must do set up these things before calling any routines tthat
X   * refer to them.
X   */
X  for (i = 'a'; i <= 'z'; i++) {
X    ++is_idchar[i - 'a' + 'A'];
X    ++is_idchar[i];
X    ++is_idstart[i - 'a' + 'A'];
X    ++is_idstart[i];
X  }
X  for (i = '0'; i <= '9'; i++)
X    ++is_idchar[i];
X  ++is_idchar['_'];
X  ++is_idstart['_'];
X#ifdef DOLLARS_IN_IDENTIFIERS
X  ++is_idchar['$'];
X  ++is_idstart['$'];
X#endif
X}
X
X
X#ifdef TEST_EXP_READER
X/* main program, for testing purposes. */
Xmain()
X{
X  int n;
X  char buf[1024];
X  extern int yydebug;
X/*
X  yydebug = 1;
X*/
X  initialize_random_junk ();
X
X  for (;;) {
X    printf("enter expression: ");
X    n = 0;
X    while ((buf[n] = getchar()) != '\n')
X      n++;
X    buf[n] = '\0';
X    printf("parser returned %d\n", parse_c_expression(buf));
X  }
X}
X
Xerrmsg (code,f,r,l,msg)
Xint code;
Xchar *f;
Xchar *r;
Xint l;
X{
X  printf("error: %s\n", msg);
X}
X#endif
END_OF_FILE
if test 35689 -ne `wc -c <'cexp.tab.c'`; then
    echo shar: \"'cexp.tab.c'\" unpacked with wrong size!
fi
# end of 'cexp.tab.c'
fi
echo shar: End of archive 7 \(of 11\).
cp /dev/null ark7isdone
MISSING=""
for I in 1 2 3 4 5 6 7 8 9 10 11 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked all 11 archives.
    rm -f ark[1-9]isdone ark[1-9][0-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0



More information about the Alt.sources mailing list