Skip to content

Commit

Permalink
sgramm.y: eliminate easy global variables
Browse files Browse the repository at this point in the history
Eliminate all global variables not needed in sread_terminal() or
sread_rule().

Required for vnmakarov#12.
  • Loading branch information
TheCount committed Mar 1, 2022
1 parent 4271228 commit 775826e
Showing 1 changed file with 78 additions and 82 deletions.
160 changes: 78 additions & 82 deletions src/sgramm.y
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,29 @@ struct srule
/* The following structure contains the parser data. */
struct parser_data
{
/* The following contain all right hand sides and translations arrays.
See members rhs, trans in structure `rule'. */
#ifndef __cplusplus
os_t srhs, strans;
#else
os_t *srhs, *strans;
#endif
/* The following is cost of the last translation which contains an
abstract node. */
int anode_cost;
/* This variable is used in yacc action to process alternatives. */
char *slhs;
/* The following is current input character of the grammar
description. */
const char *curr_ch;
/* The following is current line number of the grammar description. */
int ln;
/* The following contains all representation of the syntax tokens. */
#ifndef __cplusplus
os_t stoks;
#else
os_t *stoks;
#endif
};

/* The following vlos contain all syntax terminal and syntax rule
Expand All @@ -103,17 +123,6 @@ static vlo_t sterms, srules;
static vlo_t *sterms, *srules;
#endif

/* The following contain all right hand sides and translations arrays.
See members rhs, trans in structure `rule'. */
#ifndef __cplusplus
static os_t srhs, strans;
#else
static os_t *srhs, *strans;
#endif

/* This variable is used in yacc action to process alternatives. */
static char *slhs;

/* Forward declarations. */
extern int yyerror (struct parser_data *data, const char *str);
union YYSTYPE;
Expand Down Expand Up @@ -166,7 +175,7 @@ number : {$$ = -1;}
| '=' NUMBER {$$ = $2;}
;

rule : SEM_IDENT {slhs = (char *) $1;} rhs opt_sem
rule : SEM_IDENT {data->slhs = (char *) $1;} rhs opt_sem
;

rhs : rhs '|' alt
Expand All @@ -178,17 +187,17 @@ alt : seq trans
struct srule rule;
int end_marker = -1;

OS_TOP_ADD_MEMORY (strans, &end_marker, sizeof (int));
rule.lhs = slhs;
OS_TOP_ADD_MEMORY (data->strans, &end_marker, sizeof (int));
rule.lhs = data->slhs;
rule.anode = (char *) $2;
rule.anode_cost = (rule.anode == NULL ? 0 : data->anode_cost);
rule.rhs_len = OS_TOP_LENGTH (srhs) / sizeof (char *);
OS_TOP_EXPAND (srhs, sizeof (char *));
rule.rhs = (char **) OS_TOP_BEGIN (srhs);
rule.rhs_len = OS_TOP_LENGTH (data->srhs) / sizeof (char *);
OS_TOP_EXPAND (data->srhs, sizeof (char *));
rule.rhs = (char **) OS_TOP_BEGIN (data->srhs);
rule.rhs [rule.rhs_len] = NULL;
OS_TOP_FINISH (srhs);
rule.trans = (int *) OS_TOP_BEGIN (strans);
OS_TOP_FINISH (strans);
OS_TOP_FINISH (data->srhs);
rule.trans = (int *) OS_TOP_BEGIN (data->strans);
OS_TOP_FINISH (data->strans);
VLO_ADD_MEMORY (srules, &rule, sizeof (rule));
}
;
Expand All @@ -197,7 +206,7 @@ seq : seq IDENT
{
char *repr = (char *) $2;

OS_TOP_ADD_MEMORY (srhs, &repr, sizeof (repr));
OS_TOP_ADD_MEMORY (data->srhs, &repr, sizeof (repr));
}
| seq CHAR
{
Expand All @@ -207,7 +216,7 @@ seq : seq IDENT
term.code = term.repr [1];
term.num = VLO_LENGTH (sterms) / sizeof (term);
VLO_ADD_MEMORY (sterms, &term, sizeof (term));
OS_TOP_ADD_MEMORY (srhs, &term.repr, sizeof (term.repr));
OS_TOP_ADD_MEMORY (data->srhs, &term.repr, sizeof (term.repr));
}
|
;
Expand All @@ -219,14 +228,14 @@ trans : {$$ = NULL;}
int symb_num = $2;

$$ = NULL;
OS_TOP_ADD_MEMORY (strans, &symb_num, sizeof (int));
OS_TOP_ADD_MEMORY (data->strans, &symb_num, sizeof (int));
}
| '#' '-'
{
int symb_num = YAEP_NIL_TRANSLATION_NUMBER;

$$ = NULL;
OS_TOP_ADD_MEMORY (strans, &symb_num, sizeof (int));
OS_TOP_ADD_MEMORY (data->strans, &symb_num, sizeof (int));
}
| '#' IDENT cost '(' numbers ')'
{
Expand All @@ -243,34 +252,20 @@ numbers :
{
int symb_num = $2;

OS_TOP_ADD_MEMORY (strans, &symb_num, sizeof (int));
OS_TOP_ADD_MEMORY (data->strans, &symb_num, sizeof (int));
}
| numbers '-'
{
int symb_num = YAEP_NIL_TRANSLATION_NUMBER;

OS_TOP_ADD_MEMORY (strans, &symb_num, sizeof (int));
OS_TOP_ADD_MEMORY (data->strans, &symb_num, sizeof (int));
}
;

cost : { data->anode_cost = 1;}
| NUMBER { data->anode_cost = $1; }
;
%%
/* The following is current input character of the grammar
description. */
static const char *curr_ch;

/* The following is current line number of the grammar description. */
static int ln;

/* The following contains all representation of the syntax tokens. */
#ifndef __cplusplus
static os_t stoks;
#else
static os_t *stoks;
#endif

/* The following is number of syntax terminal and syntax rules being
read. */
static int nsterm, nsrule;
Expand All @@ -284,37 +279,37 @@ yylex (YYSTYPE *lvalp, struct parser_data *data)

for (;;)
{
c = *curr_ch++;
c = *data->curr_ch++;
switch (c)
{
case '\0':
return 0;
case '\n':
ln++;
data->ln++;
case '\t':
case ' ':
break;
case '/':
c = *curr_ch++;
c = *data->curr_ch++;
if (c != '*' && n_errs == 0)
{
n_errs++;
curr_ch--;
data->curr_ch--;
yyerror (data, "invalid input character /");
}
for (;;)
{
c = *curr_ch++;
c = *data->curr_ch++;
if (c == '\0')
yyerror (data, "unfinished comment");
if (c == '\n')
ln++;
data->ln++;
if (c == '*')
{
c = *curr_ch++;
c = *data->curr_ch++;
if (c == '/')
break;
curr_ch--;
data->curr_ch--;
}
}
break;
Expand All @@ -327,46 +322,47 @@ yylex (YYSTYPE *lvalp, struct parser_data *data)
case ')':
return c;
case '\'':
OS_TOP_ADD_BYTE (stoks, '\'');
lvalp->num = *curr_ch++;
OS_TOP_ADD_BYTE (stoks, lvalp->num);
if (*curr_ch++ != '\'')
OS_TOP_ADD_BYTE (data->stoks, '\'');
lvalp->num = *data->curr_ch++;
OS_TOP_ADD_BYTE (data->stoks, lvalp->num);
if (*data->curr_ch++ != '\'')
yyerror (data, "invalid character");
OS_TOP_ADD_BYTE (stoks, '\'');
OS_TOP_ADD_BYTE (stoks, '\0');
lvalp->ref = OS_TOP_BEGIN (stoks);
OS_TOP_FINISH (stoks);
OS_TOP_ADD_BYTE (data->stoks, '\'');
OS_TOP_ADD_BYTE (data->stoks, '\0');
lvalp->ref = OS_TOP_BEGIN (data->stoks);
OS_TOP_FINISH (data->stoks);
return CHAR;
default:
if (isalpha (c) || c == '_')
{
OS_TOP_ADD_BYTE (stoks, c);
while ((c = *curr_ch++) != '\0' && (isalnum (c) || c == '_'))
OS_TOP_ADD_BYTE (stoks, c);
curr_ch--;
OS_TOP_ADD_BYTE (stoks, '\0');
lvalp->ref = OS_TOP_BEGIN (stoks);
OS_TOP_ADD_BYTE (data->stoks, c);
while ((c = *data->curr_ch++) != '\0' && (isalnum (c)
|| c == '_'))
OS_TOP_ADD_BYTE (data->stoks, c);
data->curr_ch--;
OS_TOP_ADD_BYTE (data->stoks, '\0');
lvalp->ref = OS_TOP_BEGIN (data->stoks);
if (strcmp ((char *) lvalp->ref, "TERM") == 0)
{
OS_TOP_NULLIFY (stoks);
OS_TOP_NULLIFY (data->stoks);
return TERM;
}
OS_TOP_FINISH (stoks);
while ((c = *curr_ch++) != '\0')
OS_TOP_FINISH (data->stoks);
while ((c = *data->curr_ch++) != '\0')
if (c == '\n')
ln++;
data->ln++;
else if (c != '\t' && c != ' ')
break;
if (c != ':')
curr_ch--;
data->curr_ch--;
return (c == ':' ? SEM_IDENT : IDENT);
}
else if (isdigit (c))
{
lvalp->num = c - '0';
while ((c = *curr_ch++) != '\0' && isdigit (c))
while ((c = *data->curr_ch++) != '\0' && isdigit (c))
lvalp->num = lvalp->num * 10 + (c - '0');
curr_ch--;
data->curr_ch--;
return NUMBER;
}
else
Expand Down Expand Up @@ -396,7 +392,7 @@ int
yyerror (struct parser_data *data, const char *str)
{
yaep_error (YAEP_DESCRIPTION_SYNTAX_ERROR_CODE,
"description syntax error on ln %d", ln);
"description syntax error on ln %d", data->ln);
return 0;
}

Expand All @@ -416,7 +412,7 @@ sterm_num_cmp (const void *t1, const void *t2)
return ((struct sterm *) t1)->num - ((struct sterm *) t2)->num;
}

static void free_sgrammar (void);
static void free_sgrammar (struct parser_data *data);

/* The following is major function which parses the description and
transforms it into IR. */
Expand All @@ -427,18 +423,18 @@ set_sgrammar (struct grammar *g, const char *grammar, struct parser_data *data)
struct sterm *term, *prev, *arr;
int code = 256;

ln = 1;
data->ln = 1;
if ((code = setjmp (error_longjump_buff)) != 0)
{
free_sgrammar ();
free_sgrammar (data);
return code;
}
OS_CREATE (stoks, g->alloc, 0);
OS_CREATE (data->stoks, g->alloc, 0);
VLO_CREATE (sterms, g->alloc, 0);
VLO_CREATE (srules, g->alloc, 0);
OS_CREATE (srhs, g->alloc, 0);
OS_CREATE (strans, g->alloc, 0);
curr_ch = grammar;
OS_CREATE (data->srhs, g->alloc, 0);
OS_CREATE (data->strans, g->alloc, 0);
data->curr_ch = grammar;
yyparse (data);
/* sort array of syntax terminals by names. */
num = VLO_LENGTH (sterms) / sizeof (struct sterm);
Expand Down Expand Up @@ -485,13 +481,13 @@ set_sgrammar (struct grammar *g, const char *grammar, struct parser_data *data)

/* The following frees IR. */
static void
free_sgrammar (void)
free_sgrammar (struct parser_data *data)
{
OS_DELETE (strans);
OS_DELETE (srhs);
OS_DELETE (data->strans);
OS_DELETE (data->srhs);
VLO_DELETE (srules);
VLO_DELETE (sterms);
OS_DELETE (stoks);
OS_DELETE (data->stoks);
}

/* The following two functions implements functions used by YAEP. */
Expand Down Expand Up @@ -543,6 +539,6 @@ yaep_parse_grammar (struct grammar *g, int strict_p, const char *description)
if ((code = set_sgrammar (g, description, &data)) != 0)
return code;
code = yaep_read_grammar (g, strict_p, sread_terminal, sread_rule);
free_sgrammar ();
free_sgrammar (&data);
return code;
}

0 comments on commit 775826e

Please sign in to comment.