1991-11-23 10:41:43 +00:00
|
|
|
/* Copyright (c) 1991 by the Vrije Universiteit, Amsterdam, the Netherlands.
|
1995-07-31 09:17:14 +00:00
|
|
|
* For full copyright and restrictions on use see the file COPYING in the top
|
1995-07-31 09:10:42 +00:00
|
|
|
* level of the LLgen tree.
|
1985-10-03 10:35:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* L L G E N
|
|
|
|
*
|
|
|
|
* An Extended LL(1) Parser Generator
|
|
|
|
*
|
|
|
|
* Author : Ceriel J.H. Jacobs
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LLgen.g
|
|
|
|
* Defines the grammar of LLgen.
|
1985-11-25 15:47:51 +00:00
|
|
|
* Some routines that build the internal structure are also included
|
1985-10-03 10:35:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
2006-02-04 00:37:19 +00:00
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
1985-10-03 10:35:55 +00:00
|
|
|
# include "types.h"
|
|
|
|
# include "io.h"
|
|
|
|
# include "extern.h"
|
|
|
|
# include "assert.h"
|
1985-11-25 15:47:51 +00:00
|
|
|
# include "cclass.h"
|
1985-10-03 10:35:55 +00:00
|
|
|
|
|
|
|
# ifndef NORCSID
|
1994-06-24 11:31:16 +00:00
|
|
|
static string rcsid = "$Id$";
|
1985-10-03 10:35:55 +00:00
|
|
|
# endif
|
1987-05-12 18:23:09 +00:00
|
|
|
p_mem alloc(), ralloc();
|
1985-10-03 10:35:55 +00:00
|
|
|
string store();
|
1985-11-25 15:47:51 +00:00
|
|
|
p_gram search();
|
1991-11-26 15:38:41 +00:00
|
|
|
long ftell();
|
1985-10-03 10:35:55 +00:00
|
|
|
|
|
|
|
static int acount; /* count #of global actions */
|
1987-05-12 18:23:09 +00:00
|
|
|
static p_term t_list;
|
|
|
|
static int t_cnt;
|
1988-07-05 14:05:24 +00:00
|
|
|
static p_gram alt_table;
|
|
|
|
static int n_alts;
|
|
|
|
static int max_alts;
|
1988-07-05 11:51:47 +00:00
|
|
|
#define ALTINCR 32
|
|
|
|
|
1988-07-05 14:05:24 +00:00
|
|
|
static p_gram rule_table;
|
|
|
|
static int n_rules;
|
|
|
|
static int max_rules;
|
|
|
|
#define RULEINCR 32
|
1985-10-03 10:35:55 +00:00
|
|
|
|
|
|
|
/* Here are defined : */
|
1997-02-21 11:27:57 +00:00
|
|
|
STATIC newnorder();
|
|
|
|
STATIC newtorder();
|
1985-11-25 15:47:51 +00:00
|
|
|
STATIC mkalt();
|
|
|
|
STATIC mkterm();
|
1985-10-03 10:35:55 +00:00
|
|
|
STATIC p_gram copyrule();
|
1985-11-25 15:47:51 +00:00
|
|
|
/* and of course LLparse() */
|
|
|
|
|
1991-11-01 10:37:26 +00:00
|
|
|
STATIC
|
|
|
|
newnorder(index) {
|
|
|
|
static int porder;
|
1985-11-25 15:47:51 +00:00
|
|
|
|
1991-11-01 10:37:26 +00:00
|
|
|
if (norder != -1) {
|
|
|
|
nonterms[porder].n_next = index;
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
1991-11-01 10:37:26 +00:00
|
|
|
else norder = index;
|
|
|
|
porder = index;
|
|
|
|
nonterms[porder].n_next = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC
|
|
|
|
newtorder(index) {
|
|
|
|
static int porder;
|
|
|
|
|
|
|
|
if (torder != -1) {
|
|
|
|
tokens[porder].t_next = index;
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
1991-11-01 10:37:26 +00:00
|
|
|
else torder = index;
|
|
|
|
porder = index;
|
|
|
|
tokens[porder].t_next = -1;
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
1988-07-05 14:05:24 +00:00
|
|
|
|
1988-07-05 11:51:47 +00:00
|
|
|
p_init()
|
|
|
|
{
|
1988-07-05 14:05:24 +00:00
|
|
|
alt_table = (p_gram )alloc(ALTINCR*sizeof(t_gram));
|
|
|
|
n_alts = 0;
|
|
|
|
max_alts = ALTINCR;
|
|
|
|
rule_table = (p_gram )alloc(RULEINCR*sizeof(t_gram));
|
|
|
|
n_rules = 0;
|
|
|
|
max_rules = RULEINCR;
|
1988-07-05 11:51:47 +00:00
|
|
|
}
|
|
|
|
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
%start LLparse, spec;
|
|
|
|
|
1988-07-05 11:51:47 +00:00
|
|
|
spec : { acount = 0; p_init(); }
|
1985-10-03 10:35:55 +00:00
|
|
|
[ %persistent def ]*
|
|
|
|
{ /*
|
|
|
|
* Put an endmarker in temporary file
|
|
|
|
*/
|
1987-03-16 08:56:31 +00:00
|
|
|
putc('\0', fact);
|
|
|
|
putc('\0', fact);
|
1988-07-05 11:51:47 +00:00
|
|
|
free((p_mem) rule_table);
|
|
|
|
free((p_mem) alt_table);
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
def { register string p; }
|
1988-07-05 14:05:24 +00:00
|
|
|
: rule
|
1985-10-03 10:35:55 +00:00
|
|
|
/*
|
|
|
|
* A grammar rule
|
|
|
|
*/
|
|
|
|
| C_TOKEN listel [ ',' listel ]* ';'
|
|
|
|
/*
|
|
|
|
* A token declaration
|
|
|
|
*/
|
|
|
|
| C_START C_IDENT
|
1988-07-05 14:05:24 +00:00
|
|
|
{ p = store(lextoken.t_string); }
|
1997-02-17 15:14:55 +00:00
|
|
|
',' C_IDENT
|
1985-10-03 10:35:55 +00:00
|
|
|
/*
|
|
|
|
* A start symbol declaration
|
|
|
|
*/
|
|
|
|
{ /*
|
|
|
|
* Put the declaration in the list
|
|
|
|
* of start symbols
|
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
register p_gram temp;
|
1985-10-03 10:35:55 +00:00
|
|
|
register p_start ff;
|
|
|
|
|
|
|
|
temp = search(NONTERM,lextoken.t_string,BOTH);
|
|
|
|
ff = (p_start) alloc(sizeof(t_start));
|
1987-05-12 18:23:09 +00:00
|
|
|
ff->ff_nont = g_getcont(temp);
|
1985-10-03 10:35:55 +00:00
|
|
|
ff->ff_name = p;
|
|
|
|
ff->ff_next = start;
|
|
|
|
start = ff;
|
1991-11-01 10:37:26 +00:00
|
|
|
while (ff = ff->ff_next) {
|
|
|
|
if (! strcmp(p, ff->ff_name)) {
|
|
|
|
error(linecount, "\"%s\" already used in a %%start", p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
1997-02-17 15:14:55 +00:00
|
|
|
';'
|
1985-10-03 10:35:55 +00:00
|
|
|
| C_LEXICAL C_IDENT
|
1987-02-16 21:38:52 +00:00
|
|
|
/*
|
|
|
|
* Declaration of a name for the lexical analyser.
|
|
|
|
* May appear only once
|
|
|
|
*/
|
1985-10-03 10:35:55 +00:00
|
|
|
{ if (!lexical) {
|
|
|
|
lexical = store(lextoken.t_string);
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
|
|
|
else error(linecount,"Duplicate %%lexical");
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
';'
|
1990-01-29 13:45:42 +00:00
|
|
|
| C_PREFIX C_IDENT
|
|
|
|
/*
|
|
|
|
* Prefix of external names (default: LL)
|
|
|
|
*/
|
|
|
|
{ if (!prefix) {
|
|
|
|
prefix = store(lextoken.t_string);
|
|
|
|
if (strlen(prefix) > 6) {
|
|
|
|
error(linecount,
|
|
|
|
"%%prefix too long");
|
|
|
|
prefix[6] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else error(linecount,"Duplicate %%prefix");
|
|
|
|
}
|
|
|
|
';'
|
1987-02-16 21:38:52 +00:00
|
|
|
| C_ONERROR C_IDENT
|
1997-02-21 11:27:57 +00:00
|
|
|
{
|
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
if (non_corr) {
|
|
|
|
warning(linecount, "%%onerror conflicts with -n option");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (! onerror) {
|
1987-02-16 21:38:52 +00:00
|
|
|
onerror = store(lextoken.t_string);
|
|
|
|
}
|
|
|
|
else error(linecount,"Duplicate %%onerror");
|
|
|
|
}
|
|
|
|
';'
|
1997-02-21 11:27:57 +00:00
|
|
|
| C_ACTION { acount++; }
|
1985-10-03 10:35:55 +00:00
|
|
|
/*
|
|
|
|
* A global C-declaration
|
|
|
|
*/
|
|
|
|
| firsts
|
|
|
|
/*
|
|
|
|
* declarations for macros
|
|
|
|
*/
|
|
|
|
;
|
|
|
|
|
1991-11-01 10:37:26 +00:00
|
|
|
listel : C_IDENT { p_gram temp = search(TERMINAL,lextoken.t_string,ENTERING);
|
|
|
|
newtorder(g_getcont(temp));
|
|
|
|
tokens[g_getcont(temp)].t_lineno = linecount;
|
|
|
|
}
|
1985-10-03 10:35:55 +00:00
|
|
|
;
|
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
rule { register p_nont p;
|
|
|
|
p_gram rr;
|
|
|
|
register p_gram temp;
|
|
|
|
}
|
1985-10-03 10:35:55 +00:00
|
|
|
: /*
|
|
|
|
* grammar for a production rule
|
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
C_IDENT { temp = search(NONTERM,lextoken.t_string,BOTH);
|
1988-07-05 14:05:24 +00:00
|
|
|
p = &nonterms[g_getcont(temp)];
|
1985-10-03 10:35:55 +00:00
|
|
|
if (p->n_rule) {
|
|
|
|
error(linecount,
|
1987-06-03 19:05:14 +00:00
|
|
|
"Nonterminal %s already defined", lextoken.t_string);
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Remember the order in which the nonterminals
|
|
|
|
* were defined. Code must be generated in that
|
|
|
|
* order to keep track with the actions on the
|
|
|
|
* temporary file
|
|
|
|
*/
|
1991-11-01 10:37:26 +00:00
|
|
|
newnorder(p - nonterms);
|
1985-10-03 10:35:55 +00:00
|
|
|
p->n_count = acount;
|
|
|
|
acount = 0;
|
|
|
|
p->n_lineno = linecount;
|
1991-11-26 15:38:41 +00:00
|
|
|
p->n_off = ftell(fact);
|
1988-07-05 14:05:24 +00:00
|
|
|
}
|
1997-02-21 11:27:57 +00:00
|
|
|
[ C_PARAMS { if (lextoken.t_num > 0) {
|
1993-05-10 14:02:52 +00:00
|
|
|
p->n_flags |= PARAMS;
|
1997-02-21 11:27:57 +00:00
|
|
|
if (lextoken.t_num > 15) {
|
1993-05-10 14:02:52 +00:00
|
|
|
error(linecount,"Too many parameters");
|
|
|
|
}
|
1997-02-21 11:27:57 +00:00
|
|
|
else setntparams(p,lextoken.t_num);
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
]?
|
1997-02-21 11:27:57 +00:00
|
|
|
[ C_ACTION { p->n_flags |= LOCALS; }
|
1985-10-03 10:35:55 +00:00
|
|
|
]?
|
1997-02-21 11:27:57 +00:00
|
|
|
':' { in_production = 1; }
|
|
|
|
productions(&rr) ';'
|
|
|
|
{ in_production = 0; }
|
1985-11-25 15:47:51 +00:00
|
|
|
/*
|
|
|
|
* Do not use p->n_rule now! The nonterms array
|
|
|
|
* might have been re-allocated.
|
|
|
|
*/
|
1988-07-05 14:05:24 +00:00
|
|
|
{ nonterms[g_getcont(temp)].n_rule = rr;}
|
1985-10-03 10:35:55 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
productions(p_gram *p;)
|
|
|
|
/*
|
|
|
|
* One or more alternatives
|
|
|
|
*/
|
|
|
|
{ p_gram prod;
|
|
|
|
int conflres = 0;
|
|
|
|
int t = 0;
|
|
|
|
int haddefault = 0;
|
1988-07-05 14:05:24 +00:00
|
|
|
int altcnt = 0;
|
1985-10-03 10:35:55 +00:00
|
|
|
int o_lc, n_lc;
|
|
|
|
} :
|
1985-11-25 15:47:51 +00:00
|
|
|
{ o_lc = linecount; }
|
1985-10-03 10:35:55 +00:00
|
|
|
simpleproduction(p,&conflres)
|
1985-11-25 15:47:51 +00:00
|
|
|
{ if (conflres & DEF) haddefault = 1; }
|
1988-07-05 14:05:24 +00:00
|
|
|
[
|
1985-11-25 15:47:51 +00:00
|
|
|
[ '|' { n_lc = linecount; }
|
|
|
|
simpleproduction(&prod,&t)
|
1988-07-05 14:05:24 +00:00
|
|
|
{ if (n_alts >= max_alts-2) {
|
|
|
|
alt_table = (p_gram ) ralloc(
|
|
|
|
(p_mem) alt_table,
|
1990-01-29 13:45:42 +00:00
|
|
|
(unsigned)(max_alts+=ALTINCR)*sizeof(t_gram));
|
1988-07-05 14:05:24 +00:00
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
if (t & DEF) {
|
|
|
|
if (haddefault) {
|
|
|
|
error(n_lc,
|
|
|
|
"More than one %%default in alternation");
|
|
|
|
}
|
|
|
|
haddefault = 1;
|
|
|
|
}
|
1988-07-05 11:51:47 +00:00
|
|
|
mkalt(*p,conflres,o_lc,&alt_table[n_alts++]);
|
1988-07-05 14:05:24 +00:00
|
|
|
altcnt++;
|
1985-10-03 10:35:55 +00:00
|
|
|
o_lc = n_lc;
|
|
|
|
conflres = t;
|
|
|
|
t = 0;
|
|
|
|
*p = prod;
|
|
|
|
}
|
1997-02-21 11:27:57 +00:00
|
|
|
]+ { if (conflres & (COND|PREFERING|AVOIDING)) {
|
1985-10-03 10:35:55 +00:00
|
|
|
error(n_lc,
|
1985-11-25 15:47:51 +00:00
|
|
|
"Resolver on last alternative not allowed");
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
1988-07-05 14:05:24 +00:00
|
|
|
mkalt(*p,conflres,n_lc,&alt_table[n_alts++]);
|
|
|
|
altcnt++;
|
|
|
|
g_settype((&alt_table[n_alts]),EORULE);
|
|
|
|
*p = copyrule(&alt_table[n_alts-altcnt],altcnt+1);
|
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
|
|
1997-02-21 11:27:57 +00:00
|
|
|
{ if (conflres & (COND|PREFERING|AVOIDING)) {
|
1985-11-25 15:47:51 +00:00
|
|
|
error(o_lc,
|
|
|
|
"No alternation conflict resolver allowed here");
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
if (conflres & DEF) {
|
|
|
|
error(o_lc,
|
|
|
|
"No %%default allowed here");
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
]
|
1988-07-05 11:51:47 +00:00
|
|
|
{ n_alts -= altcnt; }
|
1985-10-03 10:35:55 +00:00
|
|
|
;
|
|
|
|
{
|
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
STATIC
|
|
|
|
mkalt(prod,condition,lc,res) p_gram prod; register p_gram res; {
|
1985-10-03 10:35:55 +00:00
|
|
|
/*
|
1985-11-25 15:47:51 +00:00
|
|
|
* Create an alternation and initialise it.
|
1985-10-03 10:35:55 +00:00
|
|
|
*/
|
|
|
|
register p_link l;
|
1987-05-12 18:23:09 +00:00
|
|
|
static p_link list;
|
|
|
|
static int cnt;
|
1985-10-03 10:35:55 +00:00
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
if (! cnt) {
|
|
|
|
cnt = 50;
|
|
|
|
list = (p_link) alloc(50 * sizeof(t_link));
|
|
|
|
}
|
|
|
|
cnt--;
|
|
|
|
l = list++;
|
1985-10-03 10:35:55 +00:00
|
|
|
l->l_rule = prod;
|
|
|
|
l->l_flag = condition;
|
1987-05-12 18:23:09 +00:00
|
|
|
g_setlink(res,l);
|
1985-11-25 15:47:51 +00:00
|
|
|
g_settype(res,ALTERNATION);
|
|
|
|
res->g_lineno = lc;
|
1987-05-12 18:23:09 +00:00
|
|
|
nalts++;
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
simpleproduction(p_gram *p; register int *conflres;)
|
1988-07-05 11:51:47 +00:00
|
|
|
{ t_gram elem;
|
|
|
|
int elmcnt = 0;
|
1985-11-25 15:47:51 +00:00
|
|
|
int cnt, kind;
|
1987-05-12 18:23:09 +00:00
|
|
|
int termdeleted = 0;
|
1985-10-03 10:35:55 +00:00
|
|
|
} :
|
1997-02-21 11:27:57 +00:00
|
|
|
[ C_DEFAULT { *conflres |= DEF; }
|
1985-11-25 15:47:51 +00:00
|
|
|
]?
|
1985-10-03 10:35:55 +00:00
|
|
|
[
|
|
|
|
/*
|
|
|
|
* Optional conflict reslover
|
|
|
|
*/
|
1997-02-21 11:27:57 +00:00
|
|
|
C_IF C_EXPR { *conflres |= COND; }
|
1985-10-03 10:35:55 +00:00
|
|
|
| C_PREFER { *conflres |= PREFERING; }
|
|
|
|
| C_AVOID { *conflres |= AVOIDING; }
|
|
|
|
]?
|
1997-02-21 11:27:57 +00:00
|
|
|
[ C_ILLEGAL {
|
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
if (n_rules >= max_rules-2) {
|
|
|
|
rule_table = (p_gram) ralloc(
|
|
|
|
(p_mem) rule_table,
|
|
|
|
(unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
|
|
|
|
}
|
|
|
|
elmcnt++;
|
|
|
|
rule_table[n_rules++] =
|
|
|
|
*search(TERMINAL, "LLILLEGAL", BOTH);
|
|
|
|
if (*conflres & DEF) {
|
|
|
|
error(linecount, "%%illegal not allowed in %%default rule");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
]?
|
1985-11-25 15:47:51 +00:00
|
|
|
[ %persistent elem(&elem)
|
1988-07-05 14:05:24 +00:00
|
|
|
{ if (n_rules >= max_rules-2) {
|
|
|
|
rule_table = (p_gram) ralloc(
|
|
|
|
(p_mem) rule_table,
|
1990-01-29 13:45:42 +00:00
|
|
|
(unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
kind = FIXED;
|
|
|
|
cnt = 0;
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
[ repeats(&kind, &cnt)
|
1985-10-03 10:35:55 +00:00
|
|
|
{ if (g_gettype(&elem) != TERM) {
|
1988-07-05 11:51:47 +00:00
|
|
|
rule_table[n_rules] = elem;
|
1988-07-05 14:05:24 +00:00
|
|
|
g_settype((&rule_table[n_rules+1]),EORULE);
|
|
|
|
mkterm(copyrule(&rule_table[n_rules],2),
|
1985-11-25 15:47:51 +00:00
|
|
|
0,
|
1988-07-05 11:51:47 +00:00
|
|
|
elem.g_lineno,
|
1985-11-25 15:47:51 +00:00
|
|
|
&elem);
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
1987-05-12 18:23:09 +00:00
|
|
|
|
|
|
|
|
{ if (g_gettype(&elem) == TERM) {
|
|
|
|
register p_term q = g_getterm(&elem);
|
|
|
|
|
1988-04-18 11:06:55 +00:00
|
|
|
if (! (q->t_flags & RESOLVER) &&
|
|
|
|
g_gettype(q->t_rule) != ALTERNATION &&
|
1987-05-12 18:23:09 +00:00
|
|
|
g_gettype(q->t_rule) != EORULE) {
|
|
|
|
while (g_gettype(q->t_rule) != EORULE) {
|
1988-07-05 11:51:47 +00:00
|
|
|
rule_table[n_rules++] = *q->t_rule++;
|
1988-07-05 14:05:24 +00:00
|
|
|
elmcnt++;
|
|
|
|
if (n_rules >= max_rules-2) {
|
|
|
|
rule_table = (p_gram) ralloc(
|
|
|
|
(p_mem) rule_table,
|
1990-01-29 13:45:42 +00:00
|
|
|
(unsigned)(max_rules+=RULEINCR)*sizeof(t_gram));
|
1988-07-05 14:05:24 +00:00
|
|
|
}
|
1987-05-12 18:23:09 +00:00
|
|
|
}
|
|
|
|
elem = *--(q->t_rule);
|
1988-07-05 11:51:47 +00:00
|
|
|
n_rules--;
|
|
|
|
elmcnt--;
|
1987-05-12 18:23:09 +00:00
|
|
|
if (q == t_list - 1) {
|
1988-07-05 14:05:24 +00:00
|
|
|
t_list--;
|
|
|
|
nterms--;
|
1987-05-12 18:23:09 +00:00
|
|
|
t_cnt++;
|
|
|
|
}
|
|
|
|
termdeleted = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
] { if (!termdeleted && g_gettype(&elem) == TERM) {
|
1985-10-03 10:35:55 +00:00
|
|
|
register p_term q;
|
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
q = g_getterm(&elem);
|
1985-11-25 15:47:51 +00:00
|
|
|
r_setkind(q,kind);
|
|
|
|
r_setnum(q,cnt);
|
|
|
|
if ((q->t_flags & RESOLVER) &&
|
|
|
|
(kind == PLUS || kind == FIXED)) {
|
1985-10-03 10:35:55 +00:00
|
|
|
error(linecount,
|
1985-11-25 15:47:51 +00:00
|
|
|
"%%while not allowed in this term");
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
/*
|
|
|
|
* A persistent fixed term is the same
|
|
|
|
* as a non-persistent fixed term.
|
|
|
|
* Should we complain?
|
|
|
|
if ((q->t_flags & PERSISTENT) &&
|
|
|
|
kind == FIXED) {
|
1985-10-03 10:35:55 +00:00
|
|
|
error(linecount,
|
1987-06-03 19:05:14 +00:00
|
|
|
"Illegal %%persistent");
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
*/
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
1987-06-03 19:05:14 +00:00
|
|
|
termdeleted = 0;
|
1988-07-05 11:51:47 +00:00
|
|
|
elmcnt++;
|
1988-07-05 14:05:24 +00:00
|
|
|
rule_table[n_rules++] = elem;
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
]* { register p_term q;
|
1988-07-05 14:05:24 +00:00
|
|
|
|
1988-07-05 11:51:47 +00:00
|
|
|
g_settype((&rule_table[n_rules]),EORULE);
|
1988-07-05 14:05:24 +00:00
|
|
|
*p = 0;
|
|
|
|
n_rules -= elmcnt;
|
|
|
|
if (g_gettype(&rule_table[n_rules]) == TERM &&
|
|
|
|
elmcnt == 1) {
|
|
|
|
q = g_getterm(&rule_table[n_rules]);
|
|
|
|
if (r_getkind(q) == FIXED &&
|
|
|
|
r_getnum(q) == 0) {
|
|
|
|
*p = q->t_rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!*p) *p = copyrule(&rule_table[n_rules],
|
|
|
|
elmcnt+1);
|
|
|
|
}
|
1985-10-03 10:35:55 +00:00
|
|
|
;
|
|
|
|
{
|
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
STATIC
|
1987-05-12 18:23:09 +00:00
|
|
|
mkterm(prod,flags,lc,result) p_gram prod; register p_gram result; {
|
1985-10-03 10:35:55 +00:00
|
|
|
/*
|
|
|
|
* Create a term, initialise it and return
|
1985-11-25 15:47:51 +00:00
|
|
|
* a grammar element containing it
|
1985-10-03 10:35:55 +00:00
|
|
|
*/
|
|
|
|
register p_term q;
|
1988-07-05 14:05:24 +00:00
|
|
|
|
1987-05-12 18:23:09 +00:00
|
|
|
if (! t_cnt) {
|
|
|
|
t_cnt = 50;
|
|
|
|
t_list = (p_term) alloc(50 * sizeof(t_term));
|
|
|
|
}
|
|
|
|
t_cnt--;
|
|
|
|
q = t_list++;
|
1985-10-03 10:35:55 +00:00
|
|
|
q->t_rule = prod;
|
|
|
|
q->t_contains = 0;
|
|
|
|
q->t_flags = flags;
|
1985-11-25 15:47:51 +00:00
|
|
|
g_settype(result,TERM);
|
1987-05-12 18:23:09 +00:00
|
|
|
g_setterm(result,q);
|
1985-11-25 15:47:51 +00:00
|
|
|
result->g_lineno = lc;
|
1987-05-12 18:23:09 +00:00
|
|
|
nterms++;
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
elem (register p_gram pres;)
|
1987-05-12 18:23:09 +00:00
|
|
|
{ register int t = 0;
|
1985-10-03 10:35:55 +00:00
|
|
|
p_gram p1;
|
|
|
|
int ln;
|
1985-11-25 15:47:51 +00:00
|
|
|
p_gram pe;
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
int erroneous = 0;
|
|
|
|
#endif
|
1985-10-03 10:35:55 +00:00
|
|
|
} :
|
|
|
|
'[' { ln = linecount; }
|
1997-02-21 11:27:57 +00:00
|
|
|
[ C_WHILE C_EXPR { t |= RESOLVER; }
|
1985-10-03 10:35:55 +00:00
|
|
|
]?
|
|
|
|
[ C_PERSISTENT { t |= PERSISTENT; }
|
|
|
|
]?
|
|
|
|
productions(&p1)
|
1985-11-25 15:47:51 +00:00
|
|
|
']' {
|
1988-07-05 14:05:24 +00:00
|
|
|
mkterm(p1,t,ln,pres);
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
1985-11-25 15:47:51 +00:00
|
|
|
|
|
1997-02-21 11:27:57 +00:00
|
|
|
[ C_ERRONEOUS {
|
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
erroneous = 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
]?
|
|
|
|
|
|
|
|
[
|
1985-11-25 15:47:51 +00:00
|
|
|
C_IDENT { pe = search(UNKNOWN,lextoken.t_string,BOTH);
|
|
|
|
*pres = *pe;
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
if (erroneous) {
|
|
|
|
if (g_gettype(pres) != TERMINAL){
|
|
|
|
warning(linecount,
|
|
|
|
"Erroneous only allowed on terminal");
|
|
|
|
erroneous = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pres->g_erroneous = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1988-07-05 14:05:24 +00:00
|
|
|
}
|
1997-02-21 11:27:57 +00:00
|
|
|
[ C_PARAMS { if (lextoken.t_num > 15) {
|
1985-10-03 10:35:55 +00:00
|
|
|
error(linecount,"Too many parameters");
|
1997-02-21 11:27:57 +00:00
|
|
|
} else g_setnpar(pres,lextoken.t_num);
|
1985-11-25 15:47:51 +00:00
|
|
|
if (g_gettype(pres) == TERMINAL) {
|
1985-10-03 10:35:55 +00:00
|
|
|
error(linecount,
|
|
|
|
"Terminal with parameters");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]?
|
1985-11-25 15:47:51 +00:00
|
|
|
| C_LITERAL { pe = search(LITERAL,lextoken.t_string,BOTH);
|
|
|
|
*pres = *pe;
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
if (erroneous)
|
|
|
|
pres->g_erroneous = 1;
|
|
|
|
#endif
|
1985-11-25 15:47:51 +00:00
|
|
|
}
|
1997-02-21 11:27:57 +00:00
|
|
|
]
|
1985-11-25 15:47:51 +00:00
|
|
|
| { g_settype(pres,ACTION);
|
|
|
|
pres->g_lineno = linecount;
|
1997-02-21 11:27:57 +00:00
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
g_setsubparse(pres, (p_start) 0);
|
|
|
|
#endif
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
|
1997-02-21 11:27:57 +00:00
|
|
|
[ C_SUBSTART
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
nsubstarts++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
C_IDENT
|
|
|
|
{
|
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
register p_gram temp;
|
|
|
|
register p_start subp;
|
|
|
|
|
|
|
|
temp = search(NONTERM,lextoken.t_string,BOTH);
|
|
|
|
subp = (p_start) alloc (sizeof(t_start));
|
|
|
|
|
|
|
|
subp->ff_nont = g_getcont(temp);
|
|
|
|
subp->ff_name = (string) 0;
|
|
|
|
subp->ff_next = (p_start) 0;
|
|
|
|
g_setsubparse(pres, subp);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
[ ',' C_IDENT
|
|
|
|
{
|
|
|
|
#ifdef NON_CORRECTING
|
|
|
|
register p_gram temp;
|
|
|
|
register p_start ff;
|
|
|
|
|
|
|
|
temp = search(NONTERM,lextoken.t_string,BOTH);
|
|
|
|
|
|
|
|
ff = g_getsubparse(pres);
|
|
|
|
while (ff) {
|
|
|
|
if (ff->ff_nont == g_getcont(temp)) {
|
|
|
|
warning(linecount, "\"%s\" used twice in %%substart", lextoken.t_string);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ff = ff->ff_next;
|
|
|
|
|
1993-05-10 14:02:52 +00:00
|
|
|
}
|
1997-02-21 11:27:57 +00:00
|
|
|
|
|
|
|
ff = (p_start) alloc(sizeof(t_start));
|
|
|
|
ff->ff_nont = g_getcont(temp);
|
|
|
|
ff->ff_name = (string) 0;
|
|
|
|
ff->ff_next = g_getsubparse(pres);
|
|
|
|
g_setsubparse(pres, ff);
|
|
|
|
#endif
|
1993-05-10 14:02:52 +00:00
|
|
|
}
|
1985-10-03 10:35:55 +00:00
|
|
|
|
1997-02-21 11:27:57 +00:00
|
|
|
]* ';'
|
|
|
|
]?
|
|
|
|
|
|
|
|
C_ACTION
|
1985-10-03 10:35:55 +00:00
|
|
|
;
|
|
|
|
|
1991-12-02 09:15:04 +00:00
|
|
|
repeats(int *kind; int *cnt;) { int t1 = 0; } :
|
1985-10-03 10:35:55 +00:00
|
|
|
[
|
1985-11-25 15:47:51 +00:00
|
|
|
'?' { *kind = OPT; }
|
|
|
|
| [ '*' { *kind = STAR; }
|
|
|
|
| '+' { *kind = PLUS; }
|
1985-10-03 10:35:55 +00:00
|
|
|
]
|
|
|
|
number(&t1)?
|
1985-11-25 15:47:51 +00:00
|
|
|
{ if (t1 == 1) {
|
|
|
|
t1 = 0;
|
|
|
|
if (*kind == STAR) *kind = OPT;
|
|
|
|
if (*kind == PLUS) *kind = FIXED;
|
1985-10-03 10:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| number(&t1)
|
1985-11-25 15:47:51 +00:00
|
|
|
] { *cnt = t1; }
|
1985-10-03 10:35:55 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
number(int *t;)
|
|
|
|
: C_NUMBER
|
|
|
|
{ *t = lextoken.t_num;
|
|
|
|
if (*t <= 0 || *t >= 8192) {
|
|
|
|
error(linecount,"Illegal number");
|
|
|
|
}
|
1988-07-05 14:05:24 +00:00
|
|
|
}
|
1985-10-03 10:35:55 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
firsts { register string p; }
|
|
|
|
: C_FIRST C_IDENT
|
1988-07-05 14:05:24 +00:00
|
|
|
{ p = store(lextoken.t_string); }
|
1985-10-03 10:35:55 +00:00
|
|
|
',' C_IDENT ';'
|
|
|
|
{ /*
|
|
|
|
* Store this %first in the list belonging
|
|
|
|
* to this input file
|
|
|
|
*/
|
1985-11-25 15:47:51 +00:00
|
|
|
p_gram temp;
|
1985-10-03 10:35:55 +00:00
|
|
|
register p_first ff;
|
|
|
|
|
|
|
|
temp = search(NONTERM,lextoken.t_string,BOTH);
|
|
|
|
ff = (p_first) alloc(sizeof(t_first));
|
1987-05-12 18:23:09 +00:00
|
|
|
ff->ff_nont = g_getcont(temp);
|
1985-10-03 10:35:55 +00:00
|
|
|
ff->ff_name = p;
|
|
|
|
ff->ff_next = pfile->f_firsts;
|
|
|
|
pfile->f_firsts = ff;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
{
|
|
|
|
|
|
|
|
STATIC p_gram
|
1985-11-25 15:47:51 +00:00
|
|
|
copyrule(p,length) register p_gram p; {
|
1985-10-03 10:35:55 +00:00
|
|
|
/*
|
|
|
|
* Returns a pointer to a grammar rule that was created in
|
|
|
|
* p. The space pointed to by p can now be reused
|
|
|
|
*/
|
|
|
|
register p_gram t;
|
|
|
|
p_gram rule;
|
|
|
|
|
|
|
|
t = (p_gram) alloc((unsigned) length * sizeof(t_gram));
|
|
|
|
rule = t;
|
|
|
|
while (length--) {
|
|
|
|
*t++ = *p++;
|
|
|
|
}
|
|
|
|
return rule;
|
|
|
|
}
|
|
|
|
}
|