📄 output.c
字号:
for (i = 0; i < nvectors; i++)
base[i] = MINSHORT;
for (i = 0; i < MAXTABLE; i++)
check[i] = -1;
for (i = 0; i < nentries; i++)
{
state = matching_state(i);
if (state < 0)
place = pack_vector(i);
else
place = base[state];
pos[i] = place;
base[order[i]] = place;
}
for (i = 0; i < nvectors; i++)
{
if (froms[i])
FREE(froms[i]);
if (tos[i])
FREE(tos[i]);
}
FREE(froms);
FREE(tos);
FREE(pos);
}
int
matching_state(vector)
int vector;
{
register int i;
register int j;
register int k;
register int t;
register int w;
register int match;
register int prev;
i = order[vector];
if (i >= nstates)
return (-1);
t = tally[i];
w = width[i];
for (prev = vector - 1; prev >= 0; prev--)
{
j = order[prev];
if (width[j] != w || tally[j] != t)
return (-1);
match = 1;
for (k = 0; match && k < t; k++)
{
if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
match = 0;
}
if (match)
return (j);
}
return (-1);
}
int
pack_vector(vector)
int vector;
{
register int i;
register int j;
register int k;
register int t;
register int loc;
register int ok;
register short *from;
register short *to;
i = order[vector];
t = tally[i];
if (t == 0)
berror("pack_vector");
from = froms[i];
to = tos[i];
for (j = lowzero - from[0]; j < MAXTABLE; j++)
{
ok = 1;
for (k = 0; ok && k < t; k++)
{
loc = j + from[k];
if (loc > MAXTABLE)
fatals("maximum table size (%d) exceeded",MAXTABLE);
if (table[loc] != 0)
ok = 0;
}
for (k = 0; ok && k < vector; k++)
{
if (pos[k] == j)
ok = 0;
}
if (ok)
{
for (k = 0; k < t; k++)
{
loc = j + from[k];
table[loc] = to[k];
check[loc] = from[k];
}
while (table[lowzero] != 0)
lowzero++;
if (loc > high)
high = loc;
return (j);
}
}
berror("pack_vector");
return 0; /* JF keep lint happy */
}
/* the following functions output yytable, yycheck
and the vectors whose elements index the portion starts */
void
output_base()
{
register int i;
register int j;
fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);
j = 10;
for (i = 1; i < nstates; i++)
{
putc(',', ftable);
if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}
fprintf(ftable, "%6d", base[i]);
}
fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);
j = 10;
for (i = nstates + 1; i < nvectors; i++)
{
putc(',', ftable);
if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}
fprintf(ftable, "%6d", base[i]);
}
fprintf(ftable, "\n};\n");
FREE(base);
}
void
output_table()
{
register int i;
register int j;
fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]);
j = 10;
for (i = 1; i <= high; i++)
{
putc(',', ftable);
if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}
fprintf(ftable, "%6d", table[i]);
}
fprintf(ftable, "\n};\n");
FREE(table);
}
void
output_check()
{
register int i;
register int j;
fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
j = 10;
for (i = 1; i <= high; i++)
{
putc(',', ftable);
if (j >= 10)
{
putc('\n', ftable);
j = 1;
}
else
{
j++;
}
fprintf(ftable, "%6d", check[i]);
}
fprintf(ftable, "\n};\n");
FREE(check);
}
/* copy the parser code into the ftable file at the end. */
void
output_parser()
{
register int c;
output_section(fparser,ftable);
rewind(faction);
for(c=getc(faction);c!=EOF;c=getc(faction))
putc(c,ftable);
output_section(fparser,ftable);
}
void
output_section(fin,fout)
FILE *fin,*fout;
{
register int c;
int dummy;
int *pcounter=&dummy;
char *fil_name;
fil_name="?";
if(fin==fparser)
{pcounter=&line_fparser;fil_name=parser_fname;}
else if(fin==fhskel)
{pcounter=&line_fhskel;fil_name=hskel_fname;}
/* Loop over lines in the standard parser file. */
if (!nolinesflag)
fprintf(fout, "\n#line %d \"%s\"\n", (*pcounter), quoted_filename(fil_name));
while (1)
{
/* now write out the line... */
for ( c = getc(fin); c != '\n' && c != EOF; c = getc(fin))
{if (c == '$')
{
if (!nolinesflag)
{fprintf(fout,
"\n/* #line %d \"%s\" */\n#line @\n",
(*pcounter), quoted_filename(fil_name));
}
return;
}
else if(c=='@')
{fprintf(fout,"%s",parser_name);
}
else
putc(c, fout);
}
if (c == EOF)
break;
else if(c=='\n') (*pcounter)++;
putc(c, fout);
}
}
void
output_program()
{
register int c;
extern int lineno;
int is_escaped=0,is_commented=0;
char quoted='\0',last='\0';
int len_match=0,i;
char *match_open="%header{";
char *match_close="%}";
char *match_wait=match_open;
if (!nolinesflag)
fprintf(ftable, "#line %d \"%s\"\n", lineno, quoted_filename(infile));
for (c = getc(finput);c != EOF;last=c,c = getc(finput))
{
if(!match_wait[len_match])
{if(match_wait==match_open)
{match_wait=match_close;
if (!nolinesflag && definesflag)
fprintf(fdefines, "\n#line %d \"%s\"\n", lineno, quoted_filename(infile));
}
else
{match_wait=match_open;}
len_match=0;
}
else if(c!=match_wait[len_match] || is_escaped || is_commented || quoted)
{for(i=0;i<len_match;i++)
{if(match_wait==match_close && definesflag)
putc(match_wait[i],fdefines);
putc(match_wait[i],ftable);}
len_match=0;
};
if(c==match_wait[len_match] && !is_escaped && !is_commented && !quoted)
{len_match++;}
else
{if(match_wait==match_close && definesflag)
putc(c,fdefines);
putc(c,ftable);
}
if(c=='\n') lineno++;
if(is_escaped)
{is_escaped=0;}
else if(c=='\\')
{is_escaped=1;}
else if(is_commented==1)
{if(last=='*' && c=='/')
is_commented=0;}
else if(is_commented==2)
{if(c=='\n')
is_commented=0;}
else if((c=='"'|| c== '\''))
{if(!quoted) quoted=c;
else if(quoted==c) quoted='\0';
}
else if(quoted) {}
else if(last=='/' && c=='*') is_commented=1;
else if(last=='/' && c=='/') is_commented=2;
}
}
void
free_itemsets()
{
register core *cp,*cptmp;
FREE(state_table);
for (cp = first_state; cp; cp = cptmp) {
cptmp=cp->next;
FREE(cp);
}
}
void
free_shifts()
{
register shifts *sp,*sptmp;/* JF derefrenced freed ptr */
FREE(shift_table);
for (sp = first_shift; sp; sp = sptmp) {
sptmp=sp->next;
FREE(sp);
}
}
void
free_reductions()
{
register reductions *rp,*rptmp;/* JF fixed freed ptr */
FREE(reduction_table);
for (rp = first_reduction; rp; rp = rptmp) {
rptmp=rp->next;
FREE(rp);
}
}
void output_token_defines();
void output_token_const_def();
void output_token_const_decl();
void output_about_token()
{ register int i;
output_section(fparser,ftable);
output_token_defines(ftable);
output_section(fparser,ftable);
output_token_const_decl(ftable);
output_section(fparser,ftable); /* new section */
output_token_enum(ftable); /* enum */
output_section(fparser,ftable);
output_token_const_def(ftable);
output_section(fparser,ftable);
if (definesflag)
{
output_section(fhskel,fdefines);
output_token_defines(fdefines);
output_section(fhskel,fdefines);
output_token_const_decl(fdefines);
output_section(fhskel,fdefines); /* new section */
output_token_enum(fdefines); /* enum */
output_section(fhskel,fdefines);
if (semantic_parser)
for (i = ntokens; i < nsyms; i++)
{
/* don't make these for dummy nonterminals made by gensym. */
if (*tags[i] != '@')
fprintf(fdefines, "#define\tNT%s\t%d\n", tags[i], i);
}
}
}
void output_token_defines(file)
FILE *file;
{output_token_defines_fmt(file,"#define\t%s\t%d\n",0);
if (semantic_parser)
output_token_defines_fmt(file,"#define\tT%s\t%d\n",1);
}
void output_token_const_def(file)
FILE *file;
{char line[256];
sprintf(line,"const int YY_%s_CLASS::%%s=%%d;\n",parser_name);
output_token_defines_fmt(file,line,0);
sprintf(line,"const int YY_%s_CLASS::T%%s=%%d;\n",parser_name);
if (semantic_parser)
output_token_defines_fmt(file,line,1);
}
void output_token_const_decl(file)
FILE *file;
{char line[256];
output_token_defines_fmt(file,"static const int %s;\n",0);
if (semantic_parser)
output_token_defines_fmt(file,"static const int T%s;\n",1);
}
/* create a list like
,FIRST_TOKEN=256
,SECOND_TOKEN=257
*/
void output_token_enum(file)
FILE *file;
{
output_token_defines_fmt(file,"\t,%s=%d\n",0);
if (semantic_parser) /* just for compatibility with semantic parser */
output_token_defines_fmt(file,"\t,T%s=%d\n",1);
}
void
output_token_defines_fmt(file,fmt,notrans)
FILE *file;
char *fmt;
int notrans;
{
bucket *bp;
for (bp = firstsymbol; bp; bp = bp->next)
{
if (bp->value >= ntokens) continue;
/* For named tokens, but not literal ones, define the name. */
/* The value is the user token number. */
if ('\'' != *tags[bp->value] && bp != errtoken)
{
register char *cp = tags[bp->value];
register char c;
/* Don't #define nonliteral tokens whose names contain periods. */
while ((c = *cp++) && c != '.');
if (!c)
{
fprintf(file, fmt, tags[bp->value],
(translations && !notrans ? bp->user_token_number : bp->value));
}
}
}
putc('\n', file);
}
char *quoted_filename(f)
char *f;
{
static char *buffer=NULL;
static int buff_size=0;
char *p;
if(buff_size<strlen(f)*2+1)
{
if(buffer !=NULL ) free(buffer);
buffer=xmalloc(strlen(f)*2+1);
}
for(p=buffer;*f;f++)
{if(*f=='\\')
*p++ ='\\';
*p++ =*f;
}
*p++='\0';
return buffer;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -