📄 reader.c
字号:
/* Input parser for bison
Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
Bison is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Bison is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* read in the grammar specification and record it in the format described in gram.h.
All guards are copied into the fguard file and all actions into faction,
in each case forming the body of a C function (yyguard or yyaction)
which contains a switch statement to decide which guard or action to execute.
The entry point is reader(). */
#include <stdio.h>
#include <ctype.h>
#include "system.h"
#include "files.h"
#include "new.h"
#include "symtab.h"
#include "lex.h"
#include "gram.h"
#include "machine.h"
#define LTYPESTR "\n#ifndef YYLTYPE\ntypedef\n struct yyltype\n\
{\n int timestamp;\n int first_line;\n int first_column;\
\n int last_line;\n int last_column;\n char *text;\n }\n\
yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
/* Number of slots allocated (but not necessarily used yet) in `rline' */
int rline_allocated;
extern char *program_name;
extern int definesflag;
extern int nolinesflag;
extern bucket *symval;
extern int numval;
extern int failure;
extern int expected_conflicts;
extern char *token_buffer;
extern void init_lex();
extern void tabinit();
extern void output_headers();
extern void output_trailers();
extern void free_symtab();
extern void open_extra_files();
extern void fatal();
extern void fatals();
extern void unlex();
extern void done();
extern int skip_white_space();
extern int parse_percent_token();
extern int lex();
void read_declarations();
void copy_definition();
void parse_token_decl();
void parse_start_decl();
void parse_type_decl();
void parse_assoc_decl();
void parse_union_decl();
void parse_expect_decl();
void copy_action();
void readgram();
void record_rule_line();
void packsymbols();
void output_token_defines();
void packgram();
int read_signed_integer();
int get_type();
typedef
struct symbol_list
{
struct symbol_list *next;
bucket *sym;
bucket *ruleprec;
}
symbol_list;
int lineno;
symbol_list *grammar;
int start_flag;
bucket *startval;
char **tags;
/* Nonzero if components of semantic values are used, implying
they must be unions. */
static int value_components_used;
static int typed; /* nonzero if %union has been seen. */
static int lastprec; /* incremented for each %left, %right or %nonassoc seen */
static int gensym_count; /* incremented for each generated symbol */
static bucket *errtoken;
/* Nonzero if any action or guard uses the @n construct. */
static int yylsp_needed;
extern char *version_string;
void
reader()
{
start_flag = 0;
startval = NULL; /* start symbol not specified yet. */
#if 0
translations = 0; /* initially assume token number translation not needed. */
#endif
/* Nowadays translations is always set to 1,
since we give `error' a user-token-number
to satisfy the Posix demand for YYERRCODE==256. */
translations = 1;
nsyms = 1;
nvars = 0;
nrules = 0;
nitems = 0;
rline_allocated = 10;
rline = NEW2(rline_allocated, short);
typed = 0;
lastprec = 0;
gensym_count = 0;
semantic_parser = 0;
pure_parser = 0;
yylsp_needed = 0;
grammar = NULL;
init_lex();
lineno = 1;
/* initialize the symbol table. */
tabinit();
/* construct the error token */
errtoken = getsym("error");
errtoken->class = STOKEN;
errtoken->user_token_number = 256; /* Value specified by posix. */
/* construct a token that represents all undefined literal tokens. */
/* it is always token number 2. */
getsym("$undefined.")->class = STOKEN;
/* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
Also notice any %token, %left, etc. found there. */
fprintf(ftable, "\n/* A Bison parser, made from %s", infile);
fprintf(ftable, " with Bison version %s */\n\n", version_string);
fprintf(ftable, "#define YYBISON 1 /* Identify Bison output. */\n\n");
read_declarations();
/* output the definition of YYLTYPE into the fattrs and fdefines files. */
/* fattrs winds up in the .tab.c file, before bison.simple. */
fprintf(fattrs, LTYPESTR);
/* start writing the guard and action files, if they are needed. */
output_headers();
/* read in the grammar, build grammar in list form. write out guards and actions. */
readgram();
/* Now we know whether we need the line-number stack.
If we do, write its type into the .tab.h file. */
if (yylsp_needed)
{
if (fdefines)
fprintf(fdefines, LTYPESTR);
}
/* write closing delimiters for actions and guards. */
output_trailers();
if (yylsp_needed)
fprintf(ftable, "#define YYLSP_NEEDED\n\n");
/* assign the symbols their symbol numbers.
Write #defines for the token symbols into fdefines if requested. */
packsymbols();
/* convert the grammar into the format described in gram.h. */
packgram();
/* free the symbol table data structure
since symbols are now all referred to by symbol number. */
free_symtab();
}
/* read from finput until %% is seen. Discard the %%.
Handle any % declarations,
and copy the contents of any %{ ... %} groups to fattrs. */
void
read_declarations ()
{
register int c;
register int tok;
for (;;)
{
c = skip_white_space();
if (c == '%')
{
tok = parse_percent_token();
switch (tok)
{
case TWO_PERCENTS:
return;
case PERCENT_LEFT_CURLY:
copy_definition();
break;
case TOKEN:
parse_token_decl (STOKEN, SNTERM);
break;
case NTERM:
parse_token_decl (SNTERM, STOKEN);
break;
case TYPE:
parse_type_decl();
break;
case START:
parse_start_decl();
break;
case UNION:
parse_union_decl();
break;
case EXPECT:
parse_expect_decl();
break;
case LEFT:
parse_assoc_decl(LEFT_ASSOC);
break;
case RIGHT:
parse_assoc_decl(RIGHT_ASSOC);
break;
case NONASSOC:
parse_assoc_decl(NON_ASSOC);
break;
case SEMANTIC_PARSER:
if (semantic_parser == 0)
{
semantic_parser = 1;
open_extra_files();
}
break;
case PURE_PARSER:
pure_parser = 1;
break;
default:
fatal("junk after `%%' in definition section");
}
}
else if (c == EOF)
fatal("no input grammar");
else if (c >= 040 && c <= 0177)
fatals ("unknown character `%c' in declaration section", c);
else
fatals ("unknown character with code 0x%x in declaration section", c);
}
}
/* copy the contents of a %{ ... %} into the definitions file.
The %{ has already been read. Return after reading the %}. */
void
copy_definition ()
{
register int c;
register int match;
register int ended;
register int after_percent; /* -1 while reading a character if prev char was % */
int cplus_comment;
if (!nolinesflag)
fprintf(fattrs, "#line %d \"%s\"\n", lineno, infile);
after_percent = 0;
c = getc(finput);
for (;;)
{
switch (c)
{
case '\n':
putc(c, fattrs);
lineno++;
break;
case '%':
after_percent = -1;
break;
case '\'':
case '"':
match = c;
putc(c, fattrs);
c = getc(finput);
while (c != match)
{
if (c == EOF || c == '\n')
fatal("unterminated string");
putc(c, fattrs);
if (c == '\\')
{
c = getc(finput);
if (c == EOF)
fatal("unterminated string");
putc(c, fattrs);
if (c == '\n')
lineno++;
}
c = getc(finput);
}
putc(c, fattrs);
break;
case '/':
putc(c, fattrs);
c = getc(finput);
if (c != '*' && c != '/')
continue;
cplus_comment = (c == '/');
putc(c, fattrs);
c = getc(finput);
ended = 0;
while (!ended)
{
if (!cplus_comment && c == '*')
{
while (c == '*')
{
putc(c, fattrs);
c = getc(finput);
}
if (c == '/')
{
putc(c, fattrs);
ended = 1;
}
}
else if (c == '\n')
{
lineno++;
putc(c, fattrs);
if (cplus_comment)
ended = 1;
else
c = getc(finput);
}
else if (c == EOF)
fatal("unterminated comment in `%{' definition");
else
{
putc(c, fattrs);
c = getc(finput);
}
}
break;
case EOF:
fatal("unterminated `%{' definition");
default:
putc(c, fattrs);
}
c = getc(finput);
if (after_percent)
{
if (c == '}')
return;
putc('%', fattrs);
}
after_percent = 0;
}
}
/* parse what comes after %token or %nterm.
For %token, what_is is STOKEN and what_is_not is SNTERM.
For %nterm, the arguments are reversed. */
void
parse_token_decl (what_is, what_is_not)
int what_is, what_is_not;
{
/* register int start_lineno; JF */
register int token = 0;
register int prev;
register char *typename = 0;
int k;
/* start_lineno = lineno; JF */
for (;;)
{
if(ungetc(skip_white_space(), finput) == '%')
return;
/* if (lineno != start_lineno)
return; JF */
/* we have not passed a newline, so the token now starting is in this declaration */
prev = token;
token = lex();
if (token == COMMA)
continue;
if (token == TYPENAME)
{
k = strlen(token_buffer);
typename = NEW2(k + 1, char);
strcpy(typename, token_buffer);
value_components_used = 1;
}
else if (token == IDENTIFIER)
{
int oldclass = symval->class;
if (symval->class == what_is_not)
fatals("symbol %s redefined", symval->tag);
symval->class = what_is;
if (what_is == SNTERM && oldclass != SNTERM)
symval->value = nvars++;
if (typename)
{
if (symval->type_name == NULL)
symval->type_name = typename;
else
fatals("type redeclaration for %s", symval->tag);
}
}
else if (prev == IDENTIFIER && token == NUMBER)
{
symval->user_token_number = numval;
translations = 1;
}
else
fatal("invalid text in %token or %nterm declaration");
}
}
/* parse what comes after %start */
void
parse_start_decl ()
{
if (start_flag)
fatal("multiple %start declarations");
start_flag = 1;
if (lex() != IDENTIFIER)
fatal("invalid %start declaration");
startval = symval;
}
/* read in a %type declaration and record its information for get_type_name to access */
void
parse_type_decl ()
{
register int k;
register char *name;
/* register int start_lineno; JF */
if (lex() != TYPENAME)
fatal("ill-formed %type declaration");
k = strlen(token_buffer);
name = NEW2(k + 1, char);
strcpy(name, token_buffer);
/* start_lineno = lineno; */
for (;;)
{
register int t;
if(ungetc(skip_white_space(), finput) == '%')
return;
/* if (lineno != start_lineno)
return; JF */
/* we have not passed a newline, so the token now starting is in this declaration */
t = lex();
switch (t)
{
case COMMA:
case SEMICOLON:
break;
case IDENTIFIER:
if (symval->type_name == NULL)
symval->type_name = name;
else
fatals("type redeclaration for %s", symval->tag);
break;
default:
fatal("invalid %type declaration");
}
}
}
/* read in a %left, %right or %nonassoc declaration and record its information. */
/* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
void
parse_assoc_decl (assoc)
int assoc;
{
register int k;
register char *name = NULL;
/* register int start_lineno; JF */
register int prev = 0; /* JF added = 0 to keep lint happy */
lastprec++; /* Assign a new precedence level, never 0. */
/* start_lineno = lineno; */
for (;;)
{
register int t;
if(ungetc(skip_white_space(), finput) == '%')
return;
/* if (lineno != start_lineno)
return; JF */
/* we have not passed a newline, so the token now starting is in this declaration */
t = lex();
switch (t)
{
case TYPENAME:
k = strlen(token_buffer);
name = NEW2(k + 1, char);
strcpy(name, token_buffer);
break;
case COMMA:
break;
case IDENTIFIER:
if (symval->prec != 0)
fatals("redefining precedence of %s", symval->tag);
symval->prec = lastprec;
symval->assoc = assoc;
if (symval->class == SNTERM)
fatals("symbol %s redefined", symval->tag);
symval->class = STOKEN;
if (name)
{ /* record the type, if one is specified */
if (symval->type_name == NULL)
symval->type_name = name;
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -