home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 1: Amiga
/
FrozenFish-Apr94.iso
/
bbs
/
alib
/
d1xx
/
d105
/
bison.lha
/
Bison
/
calc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1987-10-25
|
14KB
|
526 lines
/* A Bison parser, made from calc.y */
#line 1 "calc.y"
#include <ctype.h>
int regs[26];
int base;
int val;
int printflag;
#ifndef YYLTYPE
typedef
struct yyltype
{
int timestamp;
int first_line;
int first_column;
int last_line;
int last_column;
char *text;
}
yyltype;
#define YYLTYPE yyltype
#endif
#define YYACCEPT return(0)
#define YYERROR return(1)
#include <stdio.h>
typedef int YYSTYPE;
#define DIGIT 258
#define LETTER 259
#define EXIT 260
#define UMINUS 261
#define YYFINAL 31
#define YYFLAG -32768
#define YYNTBASE 17
#define YYTRANSLATE(x) (yytranslate[x])
static char yytranslate[] = { 0,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 12, 7, 2, 15,
16, 10, 8, 2, 9, 2, 11, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
14, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 6, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 1, 2, 3, 4, 5,
13
};
static short yyr1[] = { 0,
17, 17, 17, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 19, 19
};
static short yyr2[] = { 0,
1, 3, 1, 3, 3, 3, 3, 3, 3, 3,
3, 2, 1, 1, 1, 2
};
static short yydefact[] = { 0,
15, 13, 3, 0, 0, 1, 14, 0, 13, 12,
0, 0, 0, 0, 0, 0, 0, 0, 16, 2,
4, 10, 11, 5, 6, 7, 8, 9, 0, 0,
0
};
static short yydefgoto[] = { 29,
6, 7
};
static short yypact[] = { 12,
-32768, -12,-32768, 15, 15, 36, 0, 15,-32768,-32768,
25, 15, 15, 15, 15, 15, 15, 15,-32768, 36,
-32768, 42, 47, -5, -5,-32768,-32768,-32768, 20, 22,
-32768
};
static short yypgoto[] = {-32768,
-4,-32768
};
#define YYLAST 59
static short yytable[] = { 10,
11, 8, 19, 20, 16, 17, 18, 22, 23, 24,
25, 26, 27, 28, 1, 2, 3, 1, 9, 30,
4, 31, 0, 4, 0, 0, 5, 0, 0, 5,
12, 13, 14, 15, 16, 17, 18, 0, 0, 0,
21, 12, 13, 14, 15, 16, 17, 18, 13, 14,
15, 16, 17, 18, 14, 15, 16, 17, 18
};
static short yycheck[] = { 4,
5, 14, 3, 8, 10, 11, 12, 12, 13, 14,
15, 16, 17, 18, 3, 4, 5, 3, 4, 0,
9, 0, -1, 9, -1, -1, 15, -1, -1, 15,
6, 7, 8, 9, 10, 11, 12, -1, -1, -1,
16, 6, 7, 8, 9, 10, 11, 12, 7, 8,
9, 10, 11, 12, 8, 9, 10, 11, 12
};
#define YYPURE 1
#line 2 "simparser.c"
/* Skeleton output parser for bison,
copyright (C) 1984 Bob Corbett and Richard Stallman
Permission is granted to anyone to make or distribute verbatim copies of this program
provided that the copyright notice and this permission notice are preserved;
and provided that the recipient is not asked to waive or limit his right to
redistribute copies as permitted by this permission notice;
and provided that anyone possessing an executable copy
is granted access to copy the source code, in machine-readable form,
in some reasonable manner.
Permission is granted to distribute derived works or enhanced versions of
this program under the above conditions with the additional condition
that the entire derivative or enhanced work
must be covered by a permission notice identical to this one.
Anything distributed as part of a package containing portions derived
from this program, which cannot in current practice perform its function usefully
in the absense of what was derived directly from this program,
is to be considered as forming, together with the latter,
a single work derived from this program,
which must be entirely covered by a permission notice identical to this one
in order for distribution of the package to be permitted.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding! */
/* This is the parser code that is written into each bison parser
when the %semantic_parser declaration is not specified in the grammar.
It was written by Richard Stallman by simplifying the hairy parser
used when %semantic_parser is specified. */
/* Note: there must be only one dollar sign in this file.
It is changed into a #include that includes the action file. */
#define yyerrok (yyerrstatus = 0)
#define yyclearin (yychar1 = -2)
#define YYTERROR 1
#ifndef YYIMPURE
#define YYLEX yylex()
#endif
#ifndef YYPURE
#define YYLEX yylex(&yylval, &yylloc)
#endif
/* If nonreentrant, generate the variables here */
#ifndef YYIMPURE
int yychar; /* the lookahead symbol */
YYSTYPE yylval; /* the semantic value of the */
/* lookahead symbol */
YYLTYPE yylloc; /* location data for the lookahead */
/* symbol */
int yydebug = 0; /* nonzero means print parse trace */
#endif /* YYIMPURE */
/* YYMAXDEPTH indicates the size of the parser's state and value */
/* stacks. */
#ifndef YYMAXDEPTH
#define YYMAXDEPTH 250
#endif
int
yyparse()
{
register int yystate;
register int yyn;
register short *yyssp;
register YYSTYPE *yyvsp;
YYLTYPE *yylsp;
int yyerrstatus; /* number of tokens to shift before error messages enabled */
int yychar1; /* lookahead token as an internal (translated) token number */
short yyssa[YYMAXDEPTH]; /* the state stack */
YYSTYPE yyvsa[YYMAXDEPTH]; /* the semantic value stack */
YYLTYPE yylsa[YYMAXDEPTH]; /* the location stack */
short *yyss = yyssa; /* refer to the stacks thru separate pointers */
YYSTYPE *yyvs = yyvsa; /* to allow yyoverflow to reallocate them elsewhere */
YYLTYPE *yyls = yylsa;
int yymaxdepth = YYMAXDEPTH;
#ifndef YYPURE
int yychar;
YYSTYPE yylval;
YYLTYPE yylloc;
extern int yydebug;
#endif
YYSTYPE yyval; /* the variable used to return */
/* semantic values from the action */
/* routines */
int yylen;
yystate = 0;
yyerrstatus = 0;
yychar1 = 1; /* just to cause a proper refill */
yyssp = yyss - 1;
yyvsp = yyvs - 1;
yylsp = yyls - 1;
yyrefill:
/* at end of file, yychar is <= 0 and yychar1 is 0.
After shifting error token, if should reuse the lookahead token,
yychar is that token in external form and yychar1 is -1.
After shifting error token, if should discard the lookahead token,
yychar is the token to be discarded and yychar1 is 100000.
After yyclearin has been done, yychar1 is -2.
After normal token processing, yychar1 is positive. */
if (yychar1 > 0) yychar = YYLEX;
if (yychar <= 0)
yychar1 = 0;
else yychar1 = YYTRANSLATE(yychar);
if (yydebug)
fprintf(stderr, "Next token is type %d\n", yychar);
yynewstate:
*++yyssp = yystate;
if (yyssp >= yyss + yymaxdepth - 1)
{
#ifdef yyoverflow
/* Give user a chance to reallocate the stack */
int yyssize = yyssp-yyss, yylsize = yylsp-yyls, yyvsize = yyvsp-yyvs;
/* Use copies of these so that the &'s don't force the real ones into memory. */
YYSTYPE *yyvs1 = yyvs;
YYLTYPE *yyls1 = yyls;
short *yyss1 = yyss;
/* Each stack pointer address is followed by the size of
the data in use in that stack, in bytes. */
yyoverflow("parser stack overflow",
&yyss1, (yyssize+1)*sizeof(*yyssp), &yyvs1, (yyvsize+1)*sizeof(*yyvsp),
&yyls1, (yylsize+1)*sizeof(*yylsp), &yymaxdepth);
yyss = yyss1; yyvs = yyvs1; yyls = yyls1;
yyssp = yyss+yyssize;
yylsp = yyls+yylsize;
yyvsp = yyvs+yyvsize;
if (yyssp >= yyss + yymaxdepth - 1)
YYERROR;
#endif
#ifndef yyoverflow
yyerror("parser stack overflow");
#endif
}
if (yydebug)
fprintf(stderr, "Entering state %d\n", yystate);
yyresume:
yyn = yypact[yystate];
if (yyn == YYFLAG)
goto yydefault;
yyn += yychar1;
if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
goto yydefault;
yyn = yytable[yyn];
if (yyn < 0)
{
yyn = -yyn;
goto yyreduce;
}
else if (yyn == 0)
goto yyerrlab;
if (yyn == YYFINAL)
YYACCEPT;
if (yydebug)
fprintf(stderr, "Shifting token %d, ", yychar);
*++yyvsp = yylval;
*++yylsp = yylloc;
/* count tokens shifted since error; after three, turn off error status. */
if (yyerrstatus) yyerrstatus--;
yystate = yyn;
goto yyrefill;
yydefault:
yyn = yydefact[yystate];
if (yyn == 0)
goto yyerrlab;
yyreduce:
/* yyn is the number of a rule to reduce with. */
if (yydebug)
fprintf(stderr, "Reducing via rule %d, ", yyn);
yylen = yyr2[yyn];
yyval = yyvsp[1-yylen]; /* implement default value of the action */
#include "calc.action.c"
/* this dollarsign gets turned into a #include of the action file */
yyvsp -= yylen;
yylsp -= yylen;
yyssp -= yylen;
if (yydebug)
fprintf(stderr, "popping to level %d\n", yyssp-yyss);
*++yyvsp = yyval;
yylsp++;
if (yylen == 0)
{
yylsp->first_line = yylloc.first_line;
yylsp->first_column = yylloc.first_column;
yylsp->last_line = (yylsp-1)->last_line;
yylsp->last_column = (yylsp-1)->last_column;
yylsp->text = 0;
}
else
{
yylsp->last_line = (yylsp+yylen-1)->last_line;
yylsp->last_column = (yylsp+yylen-1)->last_column;
}
yyn = yyr1[yyn];
yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
yystate = yytable[yystate];
else
yystate = yydefgoto[yyn - YYNTBASE];
if (yychar1 == -2) /* detect use of yyclearin */
{ yychar1 = 1; goto yyrefill; }
/* want to reuse or discard token after error => do so. */
if (yychar1 < 0 || yychar1 == 100000)
goto yyrefill;
goto yynewstate;
yyerrlab: /* here on detecting error */
if (! yyerrstatus)
/* If not already recovering from an error, report this error. */
{
yyerror("parse error");
}
if (yyerrstatus == 3)
{
/* if just tried and failed to reuse lookahead token after an error, discard it. */
if (yydebug)
fprintf(stderr, "Discarding token %d.\n", yychar);
yychar1 = 100000; /* Make sure it really gets discarded. */
/* return failure if at end of input */
if (yychar <= 0)
YYERROR;
}
else
{
/* Else try to reuse it after shifting the error token. */
yychar1 = -1;
}
yyerrstatus = 3;
goto yyerrhandle;
yyerrdefault: /* current state does not do anything special for the error token. */
yyn = yydefact[yystate]; /* If its default is to accept any token, ok. Otherwise pop it.*/
if (yyn) goto yydefault;
yyerrpop: /* pop the current state because it cannot handle the error token */
if (yyssp == yyss) YYERROR;
yyvsp--;
yylsp--;
yystate = *--yyssp;
if (yydebug)
fprintf(stderr, "Error: Popping to level %d and state %d.\n", yyssp-yyss, yystate);
yyerrhandle:
yyn = yypact[yystate];
if (yyn == YYFLAG)
goto yyerrdefault;
yyn += YYTERROR;
if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
goto yyerrdefault;
yyn = yytable[yyn];
if (yyn < 0)
{
yyn = -yyn;
goto yyreduce;
}
else if (yyn == 0)
goto yyerrpop;
if (yyn == YYFINAL)
YYACCEPT;
if (yydebug)
fprintf(stderr, "Shifting error token, ");
*++yyvsp = yylval;
*++yylsp = yylloc;
yystate = yyn;
goto yyrefill;
}
#line 69 "calc.y"
static int eol;
int
yylex()
{
int c;
while ( (c=getchar()) == ' ') {}
if (c == '\n')
{ eol = 1;
return 0; }
if (c == 'Q')
return(EXIT);
if (islower(c))
{
yylval = c - 'a';
return (LETTER);
}
if (isdigit(c))
{
yylval = c - '0';
return (DIGIT);
}
return (c);
}
yyerror(s)
char *s;
{
printf("%s\n", s);
}
main()
{
for (;;)
{
eol = 0;
printflag = 0;
if (yyparse()) printflag = 0;
if (printflag) printf("%d\n", val);
while (!eol) yylex();
}
}