1987-03-10 17:51:10 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
1986-03-10 13:07:55 +00:00
|
|
|
/* $Header$ */
|
|
|
|
/* PROGRAM PARSER */
|
|
|
|
|
|
|
|
/* The presence of typedef declarations renders it impossible to
|
|
|
|
make a context-free grammar of C. Consequently we need
|
|
|
|
context-sensitive parsing techniques, the simplest one being
|
|
|
|
a subtle cooperation between the parser and the lexical scanner.
|
|
|
|
The lexical scanner has to know whether to return IDENTIFIER
|
|
|
|
or TYPE_IDENTIFIER for a given tag, and it obtains this information
|
|
|
|
from the definition list, as constructed by the parser.
|
|
|
|
The present grammar is essentially LL(2), and is processed by
|
|
|
|
a parser generator which accepts LL(1) with tie breaking rules
|
|
|
|
in C, of the form %if(cond) and %while(cond). To solve the LL(1)
|
|
|
|
ambiguities, the lexical scanner does a one symbol look-ahead.
|
|
|
|
This symbol, however, cannot always be correctly assessed, since
|
|
|
|
the present symbol may cause a change in the definition list
|
|
|
|
which causes the identification of the look-ahead symbol to be
|
|
|
|
invalidated.
|
|
|
|
The lexical scanner relies on the parser (or its routines) to
|
|
|
|
detect this situation and then update the look-ahead symbol.
|
|
|
|
An alternative approach would be to reassess the look-ahead symbol
|
|
|
|
in the lexical scanner when it is promoted to dot symbol. This
|
|
|
|
would be more beautiful but less correct, since then for a short
|
|
|
|
while there would be a discrepancy between the look-ahead symbol
|
|
|
|
and the definition list; I think it would nevertheless work in
|
|
|
|
correct programs.
|
|
|
|
A third solution would be to enter the identifier as soon as it
|
|
|
|
is found; its storage class is then known, although its full type
|
|
|
|
isn't. We would have to fill that in afterwards.
|
|
|
|
|
|
|
|
At block exit the situation is even worse. Upon reading the
|
|
|
|
closing brace, the names declared inside the function are cleared
|
|
|
|
from the name list. This action may expose a type identifier that
|
|
|
|
is the same as the identifier in the look-ahead symbol. This
|
|
|
|
situation certainly invalidates the third solution, and casts
|
|
|
|
doubts upon the second.
|
|
|
|
*/
|
|
|
|
|
|
|
|
%lexical LLlex;
|
|
|
|
%start C_program, program;
|
|
|
|
%start If_expr, control_if_expression;
|
|
|
|
|
|
|
|
{
|
1988-09-20 16:44:27 +00:00
|
|
|
#include "lint.h"
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "nopp.h"
|
|
|
|
#include "arith.h"
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "idf.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "type.h"
|
1986-03-10 15:10:56 +00:00
|
|
|
#include "declar.h"
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "decspecs.h"
|
|
|
|
#include "code.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "def.h"
|
1988-09-20 16:44:27 +00:00
|
|
|
#ifdef LINT
|
1989-03-06 15:17:39 +00:00
|
|
|
#include "l_lint.h"
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* LINT */
|
1988-09-20 16:44:27 +00:00
|
|
|
|
1986-03-10 13:07:55 +00:00
|
|
|
#ifndef NOPP
|
|
|
|
extern arith ifval;
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* NOPP */
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
extern error();
|
|
|
|
}
|
|
|
|
|
|
|
|
control_if_expression
|
|
|
|
{
|
1987-02-09 23:19:42 +00:00
|
|
|
struct expr *exprX;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
:
|
1987-02-09 23:19:42 +00:00
|
|
|
constant_expression(&exprX)
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
|
|
|
#ifndef NOPP
|
1987-02-09 23:19:42 +00:00
|
|
|
register struct expr *expr = exprX;
|
1986-03-10 13:07:55 +00:00
|
|
|
if (expr->ex_flags & EX_SIZEOF)
|
1986-03-27 18:17:48 +00:00
|
|
|
expr_error(expr,
|
|
|
|
"sizeof not allowed in preprocessor");
|
1986-03-10 13:07:55 +00:00
|
|
|
ifval = expr->VL_VALUE;
|
|
|
|
free_expression(expr);
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* NOPP */
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
/* 10 */
|
|
|
|
program:
|
|
|
|
[%persistent external_definition]*
|
|
|
|
{unstack_world();}
|
|
|
|
;
|
|
|
|
|
|
|
|
/* A C identifier definition is remarkable in that it formulates
|
|
|
|
the declaration in a way different from most other languages:
|
|
|
|
e.g., rather than defining x as a pointer-to-integer, it defines
|
|
|
|
*x as an integer and lets the compiler deduce that x is actually
|
1986-12-13 20:57:40 +00:00
|
|
|
pointer-to-integer. This has profound consequences, both for the
|
1986-03-10 13:07:55 +00:00
|
|
|
structure of an identifier definition and for the compiler.
|
|
|
|
|
|
|
|
A definition starts with a decl_specifiers, which contains things
|
|
|
|
like
|
|
|
|
typedef int
|
|
|
|
which is implicitly repeated for every definition in the list, and
|
|
|
|
then for each identifier a declarator is given, of the form
|
|
|
|
*a()
|
|
|
|
or so. The decl_specifiers is kept in a struct decspecs, to be
|
|
|
|
used again and again, while the declarator is stored in a struct
|
|
|
|
declarator, only to be passed to declare_idf together with the
|
|
|
|
struct decspecs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
external_definition
|
|
|
|
{
|
|
|
|
struct decspecs Ds;
|
|
|
|
struct declarator Dc;
|
|
|
|
}
|
|
|
|
:
|
|
|
|
{
|
|
|
|
Ds = null_decspecs;
|
|
|
|
Dc = null_declarator;
|
|
|
|
}
|
|
|
|
ext_decl_specifiers(&Ds)
|
|
|
|
[
|
|
|
|
declarator(&Dc)
|
1988-08-19 13:55:22 +00:00
|
|
|
{
|
|
|
|
declare_idf(&Ds, &Dc, level);
|
1988-09-20 16:44:27 +00:00
|
|
|
#ifdef LINT
|
|
|
|
lint_ext_def(Dc.dc_idf, Ds.ds_sc);
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* LINT */
|
1988-08-19 13:55:22 +00:00
|
|
|
}
|
1989-10-30 16:19:35 +00:00
|
|
|
[
|
1988-08-29 15:51:48 +00:00
|
|
|
function(&Ds, &Dc)
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
|
non_function(&Ds, &Dc)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
';'
|
|
|
|
]
|
|
|
|
{remove_declarator(&Dc);}
|
|
|
|
|
|
|
|
|
asm_statement /* top level, would you believe */
|
|
|
|
;
|
|
|
|
|
|
|
|
ext_decl_specifiers(struct decspecs *ds;) :
|
1987-08-18 10:05:18 +00:00
|
|
|
%if (DOT != IDENTIFIER || AHEAD == IDENTIFIER) /* the thin ice in R.M. 11.1 */
|
1986-03-10 13:07:55 +00:00
|
|
|
decl_specifiers(ds)
|
|
|
|
|
|
|
|
|
empty
|
|
|
|
{do_decspecs(ds);}
|
|
|
|
;
|
|
|
|
|
1987-02-09 23:19:42 +00:00
|
|
|
non_function(register struct decspecs *ds; register struct declarator *dc;)
|
1986-03-10 13:07:55 +00:00
|
|
|
:
|
|
|
|
{reject_params(dc);}
|
1987-03-25 23:14:43 +00:00
|
|
|
[
|
|
|
|
initializer(dc->dc_idf, ds->ds_sc)
|
|
|
|
|
|
1987-03-27 16:39:57 +00:00
|
|
|
{ code_declaration(dc->dc_idf, (struct expr *) 0, level, ds->ds_sc); }
|
1987-03-25 23:14:43 +00:00
|
|
|
]
|
1988-08-19 13:55:22 +00:00
|
|
|
{
|
1988-09-20 16:44:27 +00:00
|
|
|
#ifdef LINT
|
1990-12-06 14:56:42 +00:00
|
|
|
lint_non_function_decl(ds, dc);
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* LINT */
|
1988-08-19 13:55:22 +00:00
|
|
|
}
|
1986-03-10 13:07:55 +00:00
|
|
|
[
|
|
|
|
','
|
|
|
|
init_declarator(ds)
|
|
|
|
]*
|
|
|
|
';'
|
|
|
|
;
|
|
|
|
|
|
|
|
/* 10.1 */
|
1988-08-29 15:51:48 +00:00
|
|
|
function(struct decspecs *ds; struct declarator *dc;)
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
1988-08-23 12:45:11 +00:00
|
|
|
arith fbytes;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
:
|
1987-02-09 23:19:42 +00:00
|
|
|
{ register struct idf *idf = dc->dc_idf;
|
1988-09-20 16:44:27 +00:00
|
|
|
#ifdef LINT
|
|
|
|
lint_start_function();
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* LINT */
|
1986-03-10 13:07:55 +00:00
|
|
|
init_idf(idf);
|
|
|
|
stack_level(); /* L_FORMAL1 declarations */
|
|
|
|
declare_params(dc);
|
1988-08-29 15:51:48 +00:00
|
|
|
begin_proc(ds, idf); /* sets global function info */
|
1986-03-10 13:07:55 +00:00
|
|
|
stack_level(); /* L_FORMAL2 declarations */
|
|
|
|
}
|
|
|
|
declaration*
|
|
|
|
{
|
|
|
|
declare_formals(&fbytes);
|
1988-09-20 16:44:27 +00:00
|
|
|
#ifdef LINT
|
|
|
|
lint_formals();
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* LINT */
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
1987-07-16 13:27:37 +00:00
|
|
|
compound_statement
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
1987-07-16 13:27:37 +00:00
|
|
|
end_proc(fbytes);
|
1988-09-20 16:44:27 +00:00
|
|
|
#ifdef LINT
|
1989-03-06 15:17:39 +00:00
|
|
|
lint_implicit_return();
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* LINT */
|
1986-03-10 13:07:55 +00:00
|
|
|
unstack_level(); /* L_FORMAL2 declarations */
|
1988-09-20 16:44:27 +00:00
|
|
|
#ifdef LINT
|
1989-03-06 15:17:39 +00:00
|
|
|
lint_end_formals();
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* LINT */
|
1986-03-10 13:07:55 +00:00
|
|
|
unstack_level(); /* L_FORMAL1 declarations */
|
1988-09-20 16:44:27 +00:00
|
|
|
#ifdef LINT
|
|
|
|
lint_end_function();
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* LINT */
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
;
|