1989-02-07 11:04:05 +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".
|
|
|
|
*/
|
|
|
|
/* $Header$ */
|
|
|
|
/* EXPRESSION SYNTAX PARSER */
|
|
|
|
|
|
|
|
{
|
|
|
|
#include <alloc.h>
|
|
|
|
#include "lint.h"
|
1989-09-19 16:13:23 +00:00
|
|
|
#include <flt_arith.h>
|
1989-02-07 11:04:05 +00:00
|
|
|
#include "arith.h"
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "idf.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "code.h"
|
|
|
|
|
|
|
|
extern struct expr *intexpr();
|
1989-09-19 16:13:23 +00:00
|
|
|
int InSizeof = 0; /* inside a sizeof- expression */
|
|
|
|
int ResultKnown = 0; /* result of the expression is already known */
|
|
|
|
|
|
|
|
/* Since the grammar in the standard is not LL(n), it is modified so that
|
1989-09-29 16:20:38 +00:00
|
|
|
* it accepts basically the same grammar. This means that there is no 1-1
|
1989-09-19 16:13:23 +00:00
|
|
|
* mapping from the grammar in the standard to the grammar given here.
|
|
|
|
* Such is life.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* 3.3.1 */
|
1989-02-07 11:04:05 +00:00
|
|
|
primary(register struct expr **expp;) :
|
|
|
|
IDENTIFIER
|
|
|
|
{dot2expr(expp);}
|
|
|
|
|
|
|
|
|
constant(expp)
|
|
|
|
|
|
|
|
|
string(expp)
|
|
|
|
|
|
|
|
|
'(' expression(expp) ')'
|
1989-09-19 16:13:23 +00:00
|
|
|
{ (*expp)->ex_flags |= EX_PARENS; }
|
1989-02-07 11:04:05 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* Character string literals that are adjacent tokens
|
|
|
|
* are concatenated into a single character string
|
|
|
|
* literal.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
string(register struct expr **expp;)
|
|
|
|
{ register int i, len;
|
|
|
|
register char *str;
|
|
|
|
register int fund;
|
|
|
|
}
|
|
|
|
:
|
|
|
|
STRING
|
|
|
|
{ str = dot.tk_bts;
|
|
|
|
len = dot.tk_len;
|
|
|
|
fund = dot.tk_fund;
|
|
|
|
}
|
|
|
|
[
|
|
|
|
STRING
|
1989-09-19 16:13:23 +00:00
|
|
|
{ /* A pasted string keeps the type of the first
|
|
|
|
* string literal.
|
|
|
|
* The pasting of normal strings and wide
|
|
|
|
* character strings are stated as having an
|
|
|
|
* undefined behaviour.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
if (dot.tk_fund != fund)
|
|
|
|
warning("illegal pasting of string literals");
|
1989-10-19 14:53:25 +00:00
|
|
|
str = Realloc(str, (unsigned) (--len + dot.tk_len));
|
1989-02-07 11:04:05 +00:00
|
|
|
for (i = 0; i < dot.tk_len; i++)
|
|
|
|
str[len++] = dot.tk_bts[i];
|
|
|
|
}
|
|
|
|
]*
|
1989-09-19 16:13:23 +00:00
|
|
|
{ string2expr(expp, str, len); }
|
1989-02-07 11:04:05 +00:00
|
|
|
;
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* 3.3.2 */
|
|
|
|
postfix_expression(register struct expr **expp;)
|
|
|
|
{ int oper;
|
|
|
|
struct expr *e1 = 0;
|
|
|
|
struct idf *idf;
|
|
|
|
}
|
|
|
|
:
|
1989-02-07 11:04:05 +00:00
|
|
|
primary(expp)
|
|
|
|
[
|
1989-09-19 16:13:23 +00:00
|
|
|
'[' expression(&e1) ']'
|
1989-09-25 14:28:10 +00:00
|
|
|
{ ch3bin(expp, '[', e1); e1 = 0; }
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
'(' parameter_list(&e1)? ')'
|
1989-09-25 14:28:10 +00:00
|
|
|
{ ch3bin(expp, '(', e1); call_proto(expp); e1 = 0; }
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
[ '.' | ARROW ] { oper = DOT; }
|
1989-09-25 14:28:10 +00:00
|
|
|
identifier(&idf) { ch3sel(expp, oper, idf); }
|
1989-02-07 11:04:05 +00:00
|
|
|
]*
|
1989-09-19 16:13:23 +00:00
|
|
|
[
|
|
|
|
[
|
|
|
|
PLUSPLUS { oper = POSTINCR; }
|
|
|
|
|
|
|
|
|
MINMIN { oper = POSTDECR; }
|
|
|
|
]
|
1989-09-25 14:28:10 +00:00
|
|
|
{ ch3incr(expp, oper); }
|
1989-09-19 16:13:23 +00:00
|
|
|
]?
|
1989-02-07 11:04:05 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
parameter_list(struct expr **expp;)
|
|
|
|
{struct expr *e1 = 0;}
|
|
|
|
:
|
|
|
|
assignment_expression(expp)
|
|
|
|
{any2opnd(expp, PARCOMMA);}
|
1989-09-19 16:13:23 +00:00
|
|
|
[ %persistent
|
|
|
|
','
|
1989-02-07 11:04:05 +00:00
|
|
|
assignment_expression(&e1)
|
|
|
|
{any2opnd(&e1, PARCOMMA);}
|
1989-09-25 14:28:10 +00:00
|
|
|
{ch3bin(expp, PARCOMMA, e1);}
|
1989-02-07 11:04:05 +00:00
|
|
|
]*
|
|
|
|
;
|
|
|
|
|
|
|
|
%first first_of_type_specifier, type_specifier;
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* 3.3.3 & 3.3.4 */
|
1989-02-07 11:04:05 +00:00
|
|
|
unary(register struct expr **expp;)
|
|
|
|
{struct type *tp; int oper;}
|
|
|
|
:
|
|
|
|
%if (first_of_type_specifier(AHEAD) && AHEAD != IDENTIFIER)
|
|
|
|
cast(&tp) unary(expp)
|
1989-09-25 14:28:10 +00:00
|
|
|
{ ch3cast(expp, CAST, tp);
|
1989-02-07 11:04:05 +00:00
|
|
|
(*expp)->ex_flags |= EX_CAST;
|
|
|
|
}
|
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
postfix_expression(expp)
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
|
unop(&oper) unary(expp)
|
1989-09-25 14:28:10 +00:00
|
|
|
{ch3mon(oper, expp);}
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
|
size_of(expp)
|
|
|
|
;
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* When an identifier is used in a sizeof()-expression, we must stil not
|
|
|
|
* mark it as used.
|
|
|
|
* extern int i; .... sizeof(i) .... need not have a definition for i
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
size_of(register struct expr **expp;)
|
|
|
|
{struct type *tp;}
|
|
|
|
:
|
1989-09-19 16:13:23 +00:00
|
|
|
SIZEOF { InSizeof++; } /* handle (sizeof(sizeof(int))) too */
|
1989-02-07 11:04:05 +00:00
|
|
|
[%if (first_of_type_specifier(AHEAD) && AHEAD != IDENTIFIER)
|
|
|
|
cast(&tp)
|
|
|
|
{
|
|
|
|
*expp = intexpr(size_of_type(tp, "type"), INT);
|
|
|
|
(*expp)->ex_flags |= EX_SIZEOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
unary(expp)
|
1989-09-25 14:28:10 +00:00
|
|
|
{ch3mon(SIZEOF, expp);}
|
1989-02-07 11:04:05 +00:00
|
|
|
]
|
1989-09-19 16:13:23 +00:00
|
|
|
{ InSizeof--; }
|
1989-02-07 11:04:05 +00:00
|
|
|
;
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* 3.3.5-3.3.17 */
|
1989-02-07 11:04:05 +00:00
|
|
|
/* The set of operators in C is stratified in 15 levels, with level
|
1989-09-19 16:13:23 +00:00
|
|
|
N being treated in RM 7.N (although this is not the standard
|
|
|
|
anymore). The standard describes this in phrase-structure-grammar,
|
|
|
|
which we are unable to parse. The description that follows comes
|
|
|
|
from the old C-compiler.
|
|
|
|
|
|
|
|
In principle each operator is assigned a rank, ranging
|
|
|
|
from 1 to 15. Such an expression can be parsed by a construct
|
|
|
|
like:
|
1989-02-07 11:04:05 +00:00
|
|
|
binary_expression(int maxrank;)
|
|
|
|
{int oper;}
|
|
|
|
:
|
|
|
|
binary_expression(maxrank - 1)
|
|
|
|
[%if (rank_of(DOT) <= maxrank)
|
|
|
|
binop(&oper)
|
|
|
|
binary_expression(rank_of(oper)-1)
|
|
|
|
]?
|
|
|
|
;
|
|
|
|
except that some call of 'unary' is necessary, depending on the
|
|
|
|
grammar.
|
|
|
|
|
|
|
|
This simple view is marred by three complications:
|
|
|
|
1. Level 15 (comma operator) is not allowed in many
|
|
|
|
contexts and is different.
|
|
|
|
2. Level 13 (conditional operator) is a ternary operator,
|
|
|
|
which does not fit this scheme at all.
|
|
|
|
3. Level 14 (assignment operators) group right-to-left, as
|
|
|
|
opposed to 2-12, which group left-to-right (or are
|
|
|
|
immaterial).
|
|
|
|
4. The operators in level 14 start with operators in levels
|
|
|
|
2-13 (RM 7.14: The two parts of a compound assignment
|
|
|
|
operator are separate tokens.) This causes LL1 problems.
|
|
|
|
This forces us to have four rules:
|
|
|
|
binary_expression for level 2-12
|
|
|
|
conditional_expression for level 13
|
|
|
|
assignment_expression for level 14 and
|
|
|
|
expression for the most general expression
|
|
|
|
*/
|
|
|
|
|
|
|
|
binary_expression(int maxrank; struct expr **expp;)
|
1989-09-19 16:13:23 +00:00
|
|
|
{int oper, OldResultKnown; struct expr *e1;}
|
1989-02-07 11:04:05 +00:00
|
|
|
:
|
|
|
|
unary(expp)
|
1989-09-19 16:13:23 +00:00
|
|
|
[%while (rank_of(DOT) <= maxrank )
|
|
|
|
/* '?', '=', and ',' are no binops
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
binop(&oper)
|
1989-09-19 16:13:23 +00:00
|
|
|
{ OldResultKnown = ResultKnown;
|
|
|
|
if (oper == OR || oper == AND) {
|
|
|
|
if (is_cp_cst(*expp) || is_fp_cst(*expp)) {
|
|
|
|
if (is_zero_cst(*expp)) {
|
|
|
|
if (oper == AND) ResultKnown++;
|
|
|
|
} else if (oper == OR) ResultKnown++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
binary_expression(rank_of(oper)-1, &e1)
|
|
|
|
{
|
1989-09-25 14:28:10 +00:00
|
|
|
ch3bin(expp, oper, e1);
|
1989-09-19 16:13:23 +00:00
|
|
|
ResultKnown = OldResultKnown;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
]*
|
|
|
|
;
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* 3.3.15 */
|
1989-02-07 11:04:05 +00:00
|
|
|
conditional_expression(struct expr **expp;)
|
1989-09-19 16:13:23 +00:00
|
|
|
{struct expr *e1 = 0, *e2 = 0; int OldResultKnown, ConstExpr=0;}
|
1989-02-07 11:04:05 +00:00
|
|
|
:
|
|
|
|
/* allow all binary operators */
|
|
|
|
binary_expression(rank_of('?') - 1, expp)
|
|
|
|
[ '?'
|
1989-09-19 16:13:23 +00:00
|
|
|
{ OldResultKnown = ResultKnown;
|
|
|
|
if (is_cp_cst(*expp) || is_fp_cst(*expp)) {
|
|
|
|
ConstExpr++;
|
|
|
|
if (is_zero_cst(*expp)) ResultKnown++;
|
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
1989-09-19 16:13:23 +00:00
|
|
|
expression(&e1)
|
1989-02-07 11:04:05 +00:00
|
|
|
':'
|
1989-09-19 16:13:23 +00:00
|
|
|
{ if (ConstExpr) {
|
|
|
|
if (OldResultKnown == ResultKnown) ResultKnown++;
|
|
|
|
else ResultKnown = OldResultKnown;
|
|
|
|
}
|
|
|
|
}
|
1989-10-13 11:04:42 +00:00
|
|
|
conditional_expression(&e2)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
1989-09-19 16:13:23 +00:00
|
|
|
ResultKnown = OldResultKnown;
|
1989-09-25 14:28:10 +00:00
|
|
|
ch3bin(&e1, ':', e2);
|
1989-02-07 11:04:05 +00:00
|
|
|
opnd2test(expp, '?');
|
1989-09-25 14:28:10 +00:00
|
|
|
ch3bin(expp, '?', e1);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
]?
|
|
|
|
;
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* 3.3.16 */
|
1989-02-07 11:04:05 +00:00
|
|
|
assignment_expression(struct expr **expp;)
|
1989-09-19 16:13:23 +00:00
|
|
|
{ int oper;
|
|
|
|
struct expr *e1 = 0;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
:
|
|
|
|
conditional_expression(expp)
|
1989-10-13 11:04:42 +00:00
|
|
|
[
|
1989-02-07 11:04:05 +00:00
|
|
|
asgnop(&oper)
|
|
|
|
assignment_expression(&e1)
|
1989-09-25 14:28:10 +00:00
|
|
|
{ch3asgn(expp, oper, e1);}
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
|
empty /* LLgen artefact ??? */
|
|
|
|
]
|
|
|
|
;
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* 3.3.17 */
|
1989-02-07 11:04:05 +00:00
|
|
|
expression(struct expr **expp;)
|
|
|
|
{struct expr *e1;}
|
|
|
|
:
|
|
|
|
assignment_expression(expp)
|
|
|
|
[ ','
|
|
|
|
assignment_expression(&e1)
|
|
|
|
{
|
1989-09-25 14:28:10 +00:00
|
|
|
ch3bin(expp, ',', e1);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
]*
|
|
|
|
;
|
|
|
|
|
|
|
|
unop(int *oper;) :
|
|
|
|
['*' | '&' | '-' | '+' | '!' | '~' | PLUSPLUS | MINMIN]
|
|
|
|
{*oper = DOT;}
|
|
|
|
;
|
|
|
|
|
|
|
|
multop:
|
|
|
|
'*' | '/' | '%'
|
|
|
|
;
|
|
|
|
|
|
|
|
addop:
|
|
|
|
'+' | '-'
|
|
|
|
;
|
|
|
|
|
|
|
|
shiftop:
|
|
|
|
LEFT | RIGHT
|
|
|
|
;
|
|
|
|
|
|
|
|
relop:
|
|
|
|
'<' | '>' | LESSEQ | GREATEREQ
|
|
|
|
;
|
|
|
|
|
|
|
|
eqop:
|
|
|
|
EQUAL | NOTEQUAL
|
|
|
|
;
|
|
|
|
|
|
|
|
arithop:
|
|
|
|
multop | addop | shiftop
|
|
|
|
|
|
|
|
|
'&' | '^' | '|'
|
|
|
|
;
|
|
|
|
|
|
|
|
binop(int *oper;) :
|
|
|
|
[ arithop | relop | eqop | AND | OR ]
|
|
|
|
{*oper = DOT;}
|
|
|
|
;
|
|
|
|
|
|
|
|
asgnop(register int *oper;):
|
1989-09-19 16:13:23 +00:00
|
|
|
[ '=' | PLUSAB | MINAB | TIMESAB | DIVAB | MODAB
|
|
|
|
| LEFTAB | RIGHTAB | ANDAB | XORAB | ORAB ]
|
1989-02-07 11:04:05 +00:00
|
|
|
{ *oper = DOT; }
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
constant(struct expr **expp;) :
|
|
|
|
[
|
|
|
|
INTEGER
|
|
|
|
|
|
|
|
|
FLOATING
|
|
|
|
] {dot2expr(expp);}
|
|
|
|
;
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* 3.4 */
|
1989-02-07 11:04:05 +00:00
|
|
|
constant_expression (struct expr **expp;) :
|
1989-09-19 16:13:23 +00:00
|
|
|
conditional_expression(expp)
|
|
|
|
{ chk_cst_expr(expp); }
|
1989-02-07 11:04:05 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
identifier(struct idf **idfp;) :
|
1989-09-19 16:13:23 +00:00
|
|
|
[ IDENTIFIER
|
|
|
|
| TYPE_IDENTIFIER
|
1989-02-07 11:04:05 +00:00
|
|
|
]
|
1989-09-19 16:13:23 +00:00
|
|
|
{ *idfp = dot.tk_idf; }
|
1989-02-07 11:04:05 +00:00
|
|
|
;
|