cpython/Parser/parser.c

431 lines
9.5 KiB
C
Raw Normal View History

1991-02-19 12:39:46 +00:00
/***********************************************************
1995-01-04 19:08:09 +00:00
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
1991-02-19 12:39:46 +00:00
All Rights Reserved
1996-10-25 14:44:06 +00:00
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
1991-02-19 12:39:46 +00:00
provided that the above copyright notice appear in all copies and that
1996-10-25 14:44:06 +00:00
both that copyright notice and this permission notice appear in
1991-02-19 12:39:46 +00:00
supporting documentation, and that the names of Stichting Mathematisch
1996-10-25 14:44:06 +00:00
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
1991-02-19 12:39:46 +00:00
1996-10-25 14:44:06 +00:00
While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
1991-02-19 12:39:46 +00:00
******************************************************************/
1990-10-14 12:07:46 +00:00
/* Parser implementation */
/* For a description, see the comments at end of this file */
/* XXX To do: error recovery */
1990-12-20 15:06:42 +00:00
#include "pgenheaders.h"
1990-10-14 12:07:46 +00:00
#include "assert.h"
#include "token.h"
#include "grammar.h"
#include "node.h"
#include "parser.h"
#include "errcode.h"
1996-12-30 16:17:54 +00:00
#ifdef Py_DEBUG
1997-04-29 21:03:06 +00:00
extern int Py_DebugFlag;
#define D(x) if (!Py_DebugFlag); else x
1990-10-14 12:07:46 +00:00
#else
#define D(x)
#endif
/* STACK DATA TYPE */
1997-04-29 21:03:06 +00:00
static void s_reset Py_PROTO((stack *));
1990-10-14 12:07:46 +00:00
static void
s_reset(s)
stack *s;
{
s->s_top = &s->s_base[MAXSTACK];
}
#define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK])
1997-04-29 21:03:06 +00:00
static int s_push Py_PROTO((stack *, dfa *, node *));
1990-10-14 12:07:46 +00:00
static int
s_push(s, d, parent)
register stack *s;
dfa *d;
node *parent;
{
register stackentry *top;
if (s->s_top == s->s_base) {
fprintf(stderr, "s_push: parser stack overflow\n");
return -1;
}
top = --s->s_top;
top->s_dfa = d;
top->s_parent = parent;
top->s_state = 0;
return 0;
}
1996-12-30 16:17:54 +00:00
#ifdef Py_DEBUG
1990-10-14 12:07:46 +00:00
1997-04-29 21:03:06 +00:00
static void s_pop Py_PROTO((stack *));
1990-10-14 12:07:46 +00:00
static void
s_pop(s)
register stack *s;
{
if (s_empty(s))
1997-04-29 21:03:06 +00:00
Py_FatalError("s_pop: parser stack underflow -- FATAL");
1990-10-14 12:07:46 +00:00
s->s_top++;
}
1996-12-30 16:17:54 +00:00
#else /* !Py_DEBUG */
1990-10-14 12:07:46 +00:00
#define s_pop(s) (s)->s_top++
#endif
/* PARSER CREATION */
parser_state *
1997-04-29 21:03:06 +00:00
PyParser_New(g, start)
1990-10-14 12:07:46 +00:00
grammar *g;
int start;
{
parser_state *ps;
if (!g->g_accel)
1997-04-29 21:03:06 +00:00
PyGrammar_AddAccelerators(g);
ps = PyMem_NEW(parser_state, 1);
1990-10-14 12:07:46 +00:00
if (ps == NULL)
return NULL;
ps->p_grammar = g;
1997-04-29 21:03:06 +00:00
ps->p_tree = PyNode_New(start);
1990-10-14 12:07:46 +00:00
if (ps->p_tree == NULL) {
1997-04-29 21:03:06 +00:00
PyMem_DEL(ps);
1990-10-14 12:07:46 +00:00
return NULL;
}
s_reset(&ps->p_stack);
1997-04-29 21:03:06 +00:00
(void) s_push(&ps->p_stack, PyGrammar_FindDFA(g, start), ps->p_tree);
1990-10-14 12:07:46 +00:00
return ps;
}
void
1997-04-29 21:03:06 +00:00
PyParser_Delete(ps)
1990-10-14 12:07:46 +00:00
parser_state *ps;
{
1990-11-18 17:38:42 +00:00
/* NB If you want to save the parse tree,
you must set p_tree to NULL before calling delparser! */
1997-04-29 21:03:06 +00:00
PyNode_Free(ps->p_tree);
PyMem_DEL(ps);
1990-10-14 12:07:46 +00:00
}
/* PARSER STACK OPERATIONS */
1997-04-29 21:03:06 +00:00
static int shift Py_PROTO((stack *, int, char *, int, int));
1990-10-14 12:07:46 +00:00
static int
1990-12-20 15:06:42 +00:00
shift(s, type, str, newstate, lineno)
1990-10-14 12:07:46 +00:00
register stack *s;
int type;
char *str;
int newstate;
1990-12-20 15:06:42 +00:00
int lineno;
1990-10-14 12:07:46 +00:00
{
int err;
1990-10-14 12:07:46 +00:00
assert(!s_empty(s));
err = PyNode_AddChild(s->s_top->s_parent, type, str, lineno);
if (err)
return err;
1990-10-14 12:07:46 +00:00
s->s_top->s_state = newstate;
return 0;
}
1997-04-29 21:03:06 +00:00
static int push Py_PROTO((stack *, int, dfa *, int, int));
1990-10-14 12:07:46 +00:00
static int
1990-12-20 15:06:42 +00:00
push(s, type, d, newstate, lineno)
1990-10-14 12:07:46 +00:00
register stack *s;
int type;
dfa *d;
int newstate;
1990-12-20 15:06:42 +00:00
int lineno;
1990-10-14 12:07:46 +00:00
{
int err;
1990-10-14 12:07:46 +00:00
register node *n;
n = s->s_top->s_parent;
assert(!s_empty(s));
err = PyNode_AddChild(n, type, (char *)NULL, lineno);
if (err)
return err;
1990-10-14 12:07:46 +00:00
s->s_top->s_state = newstate;
return s_push(s, d, CHILD(n, NCH(n)-1));
}
/* PARSER PROPER */
1997-04-29 21:03:06 +00:00
static int classify Py_PROTO((grammar *, int, char *));
1990-10-14 12:07:46 +00:00
static int
classify(g, type, str)
grammar *g;
register int type;
char *str;
{
register int n = g->g_ll.ll_nlabels;
if (type == NAME) {
register char *s = str;
register label *l = g->g_ll.ll_label;
register int i;
for (i = n; i > 0; i--, l++) {
if (l->lb_type == NAME && l->lb_str != NULL &&
l->lb_str[0] == s[0] &&
strcmp(l->lb_str, s) == 0) {
D(printf("It's a keyword\n"));
return n - i;
}
}
}
{
register label *l = g->g_ll.ll_label;
register int i;
for (i = n; i > 0; i--, l++) {
if (l->lb_type == type && l->lb_str == NULL) {
D(printf("It's a token we know\n"));
return n - i;
}
}
}
D(printf("Illegal token\n"));
return -1;
}
int
1997-04-29 21:03:06 +00:00
PyParser_AddToken(ps, type, str, lineno)
1990-10-14 12:07:46 +00:00
register parser_state *ps;
register int type;
char *str;
1990-12-20 15:06:42 +00:00
int lineno;
1990-10-14 12:07:46 +00:00
{
register int ilabel;
int err;
1990-10-14 12:07:46 +00:00
1997-04-29 21:03:06 +00:00
D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));
1990-10-14 12:07:46 +00:00
/* Find out which label this token is */
ilabel = classify(ps->p_grammar, type, str);
if (ilabel < 0)
return E_SYNTAX;
/* Loop until the token is shifted or an error occurred */
for (;;) {
/* Fetch the current dfa and state */
register dfa *d = ps->p_stack.s_top->s_dfa;
register state *s = &d->d_state[ps->p_stack.s_top->s_state];
D(printf(" DFA '%s', state %d:",
d->d_name, ps->p_stack.s_top->s_state));
/* Check accelerator */
if (s->s_lower <= ilabel && ilabel < s->s_upper) {
register int x = s->s_accel[ilabel - s->s_lower];
if (x != -1) {
if (x & (1<<7)) {
/* Push non-terminal */
int nt = (x >> 8) + NT_OFFSET;
int arrow = x & ((1<<7)-1);
1997-04-29 21:03:06 +00:00
dfa *d1 = PyGrammar_FindDFA(
ps->p_grammar, nt);
if ((err = push(&ps->p_stack, nt, d1,
arrow, lineno)) > 0) {
1997-04-29 21:03:06 +00:00
D(printf(" MemError: push\n"));
return err;
1990-10-14 12:07:46 +00:00
}
D(printf(" Push ...\n"));
continue;
}
/* Shift the token */
if ((err = shift(&ps->p_stack, type, str,
x, lineno)) > 0) {
1990-10-14 12:07:46 +00:00
D(printf(" MemError: shift.\n"));
return err;
1990-10-14 12:07:46 +00:00
}
D(printf(" Shift.\n"));
/* Pop while we are in an accept-only state */
while (s = &d->d_state
[ps->p_stack.s_top->s_state],
s->s_accept && s->s_narcs == 1) {
D(printf(" Direct pop.\n"));
s_pop(&ps->p_stack);
if (s_empty(&ps->p_stack)) {
D(printf(" ACCEPT.\n"));
return E_DONE;
}
d = ps->p_stack.s_top->s_dfa;
}
return E_OK;
}
}
if (s->s_accept) {
/* Pop this dfa and try again */
s_pop(&ps->p_stack);
D(printf(" Pop ...\n"));
if (s_empty(&ps->p_stack)) {
D(printf(" Error: bottom of stack.\n"));
return E_SYNTAX;
}
continue;
}
/* Stuck, report syntax error */
D(printf(" Error.\n"));
return E_SYNTAX;
}
}
1996-12-30 16:17:54 +00:00
#ifdef Py_DEBUG
1990-10-14 12:07:46 +00:00
/* DEBUG OUTPUT */
void
dumptree(g, n)
grammar *g;
node *n;
{
int i;
if (n == NULL)
printf("NIL");
else {
label l;
l.lb_type = TYPE(n);
1992-09-03 20:46:37 +00:00
l.lb_str = STR(n);
1997-04-29 21:03:06 +00:00
printf("%s", PyGrammar_LabelRepr(&l));
1990-10-14 12:07:46 +00:00
if (ISNONTERMINAL(TYPE(n))) {
printf("(");
for (i = 0; i < NCH(n); i++) {
if (i > 0)
printf(",");
dumptree(g, CHILD(n, i));
}
printf(")");
}
}
}
void
showtree(g, n)
grammar *g;
node *n;
{
int i;
if (n == NULL)
return;
if (ISNONTERMINAL(TYPE(n))) {
for (i = 0; i < NCH(n); i++)
showtree(g, CHILD(n, i));
}
else if (ISTERMINAL(TYPE(n))) {
1997-04-29 21:03:06 +00:00
printf("%s", _PyParser_TokenNames[TYPE(n)]);
1990-10-14 12:07:46 +00:00
if (TYPE(n) == NUMBER || TYPE(n) == NAME)
printf("(%s)", STR(n));
printf(" ");
}
else
printf("? ");
}
void
printtree(ps)
parser_state *ps;
{
1997-04-29 21:03:06 +00:00
if (Py_DebugFlag) {
1990-10-14 12:07:46 +00:00
printf("Parse tree:\n");
dumptree(ps->p_grammar, ps->p_tree);
printf("\n");
printf("Tokens:\n");
showtree(ps->p_grammar, ps->p_tree);
printf("\n");
}
printf("Listing:\n");
1997-04-29 21:03:06 +00:00
PyNode_ListTree(ps->p_tree);
1990-10-14 12:07:46 +00:00
printf("\n");
}
1996-12-30 16:17:54 +00:00
#endif /* Py_DEBUG */
1990-10-14 12:07:46 +00:00
/*
Description
-----------
The parser's interface is different than usual: the function addtoken()
must be called for each token in the input. This makes it possible to
turn it into an incremental parsing system later. The parsing system
constructs a parse tree as it goes.
A parsing rule is represented as a Deterministic Finite-state Automaton
(DFA). A node in a DFA represents a state of the parser; an arc represents
a transition. Transitions are either labeled with terminal symbols or
with non-terminals. When the parser decides to follow an arc labeled
with a non-terminal, it is invoked recursively with the DFA representing
the parsing rule for that as its initial state; when that DFA accepts,
the parser that invoked it continues. The parse tree constructed by the
recursively called parser is inserted as a child in the current parse tree.
The DFA's can be constructed automatically from a more conventional
language description. An extended LL(1) grammar (ELL(1)) is suitable.
Certain restrictions make the parser's life easier: rules that can produce
the empty string should be outlawed (there are other ways to put loops
or optional parts in the language). To avoid the need to construct
FIRST sets, we can require that all but the last alternative of a rule
(really: arc going out of a DFA's state) must begin with a terminal
symbol.
As an example, consider this grammar:
expr: term (OP term)*
term: CONSTANT | '(' expr ')'
The DFA corresponding to the rule for expr is:
------->.---term-->.------->
^ |
| |
\----OP----/
The parse tree generated for the input a+b is:
(expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))
*/