This commit is contained in:
John Kerl 2016-07-15 09:21:46 -04:00
parent cdd82c7069
commit b2d88cd69c
5 changed files with 40 additions and 53 deletions

View file

@ -1,12 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lemon_assert.h"
#include "lemon_action.h"
// xxx move:
char *msort(char *list, char **next, int (*cmp)());
#include "lemon_assert.h"
#include "lemon_msort.h"
/*
** Routines processing parser actions in the LEMON parser generator.
@ -25,7 +24,8 @@ struct action *Action_new() {
fprintf(stderr,"Unable to allocate memory for a new parser action.");
exit(1);
}
for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
for (i=0; i<amt-1; i++)
freelist[i].next = &freelist[i+1];
freelist[amt-1].next = 0;
}
new = freelist;
@ -40,16 +40,15 @@ static int actioncmp(struct action *ap1, struct action *ap2)
rc = ap1->sp->index - ap2->sp->index;
if (rc==0) rc = (int)ap1->type - (int)ap2->type;
if (rc==0) {
assert (ap1->type==REDUCE || ap1->type==RD_RESOLVED || ap1->type==CONFLICT);
assert (ap2->type==REDUCE || ap2->type==RD_RESOLVED || ap2->type==CONFLICT);
assert(ap1->type==REDUCE || ap1->type==RD_RESOLVED || ap1->type==CONFLICT);
assert(ap2->type==REDUCE || ap2->type==RD_RESOLVED || ap2->type==CONFLICT);
rc = ap1->x.rp->index - ap2->x.rp->index;
}
return rc;
}
/* Sort parser actions */
struct action *Action_sort(struct action *ap)
{
struct action *Action_sort(struct action *ap) {
ap = (struct action *)msort((char *)ap,(char **)&ap->next,actioncmp);
return ap;
}

View file

@ -28,11 +28,11 @@ struct config *b;
}
// ----------------------------------------------------------------
static struct config *freelist = 0; /* List of free configurations */
static struct config *current = 0; /* Top of list of configurations */
static struct config **currentend = 0; /* Last on list of configs */
static struct config *basis = 0; /* Top of list of basis configs */
static struct config **basisend = 0; /* End of list of basis configs */
static struct config *freelist = 0; /* List of free configurations */
static struct config *current = 0; /* Top of list of configurations */
static struct config **currentend = 0; /* Last on list of configs */
static struct config *basis = 0; /* Top of list of basis configs */
static struct config **basisend = 0; /* End of list of basis configs */
/* Return a pointer to a new configuration */
static struct config *newconfig() {
@ -54,14 +54,13 @@ static struct config *newconfig() {
}
/* The configuration "old" is no longer used */
static void deleteconfig(struct config *old)
{
static void deleteconfig(struct config *old) {
old->next = freelist;
freelist = old;
}
/* Initialized the configuration list builder */
void Configlist_init(){
void Configlist_init() {
current = 0;
currentend = &current;
basis = 0;
@ -71,7 +70,7 @@ void Configlist_init(){
}
/* Initialized the configuration list builder */
void Configlist_reset(){
void Configlist_reset() {
current = 0;
currentend = &current;
basis = 0;
@ -108,8 +107,7 @@ struct config *Configlist_add(
}
/* Add a basis configuration to the configuration list */
struct config *Configlist_addbasis(struct rule *rp, int dot)
{
struct config *Configlist_addbasis(struct rule *rp, int dot) {
struct config *cfp, model;
assert (basisend!=0);
@ -136,8 +134,7 @@ struct config *Configlist_addbasis(struct rule *rp, int dot)
}
/* Compute the closure of the configuration list */
void Configlist_closure(struct lemon *lemp)
{
void Configlist_closure(struct lemon *lemp) {
struct config *cfp, *newcfp;
struct rule *rp, *newrp;
struct symbol *sp, *xsp;
@ -224,9 +221,7 @@ void Configlist_eat(struct config *cfp)
// ================================================================
/* Hash a configuration */
static int confighash(a)
struct config *a;
{
static int confighash(struct config *a) {
int h=0;
h = h*571 + a->rp->index*37 + a->dot;
return h;
@ -331,8 +326,7 @@ int Configtable_insert(struct config *data) {
/* Return a pointer to data assigned to the given key. Return NULL
** if no such key. */
struct config *Configtable_find(struct config *key)
{
struct config *Configtable_find(struct config *key) {
int h;
x4node *np;
@ -348,8 +342,7 @@ struct config *Configtable_find(struct config *key)
/* Remove all data from the table. Pass each data to the function "f"
** as it is removed. ("f" may be null to avoid this step.) */
void Configtable_clear(int(*f)(/* struct config * */))
{
void Configtable_clear(int(*f)(struct config *)) {
int i;
if (x4a==0 || x4a->count==0) return;
if (f) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);

View file

@ -10,8 +10,7 @@
/* Find a good place to break "msg" so that its length is at least "min"
** but no more than "max". Make the point as close to max as possible.
*/
static int findbreak(char *msg, int min, int max)
{
static int findbreak(char *msg, int min, int max) {
int i,spot;
char c;
for(i=spot=min; i<=max; i++){

View file

@ -37,8 +37,7 @@
** are not RHS symbols with a defined precedence, the precedence
** symbol field is left blank.
*/
void FindRulePrecedences(struct lemon *xp)
{
void FindRulePrecedences(struct lemon *xp) {
struct rule *rp;
for(rp=xp->rule; rp; rp=rp->next){
if (rp->precsym==0) {
@ -59,8 +58,7 @@ void FindRulePrecedences(struct lemon *xp)
** The first set is the set of all terminal symbols which can begin
** a string generated by that nonterminal.
*/
void FindFirstSets(struct lemon *lemp)
{
void FindFirstSets(struct lemon *lemp) {
int i;
struct rule *rp;
int progress;
@ -116,8 +114,7 @@ void FindFirstSets(struct lemon *lemp)
*/
static struct state *getstate(/* struct lemon * */); /* forward reference */
void FindStates(struct lemon *lemp)
{
void FindStates(struct lemon *lemp) {
struct symbol *sp;
struct rule *rp;
@ -173,10 +170,9 @@ void FindStates(struct lemon *lemp)
/* Return a pointer to a state which is described by the configuration
** list which has been built from calls to Configlist_add.
*/
static void buildshifts(/* struct lemon *, struct state * */); /* Forward ref */
static void buildshifts(struct lemon *, struct state *); /* Forward ref */
static struct state *getstate(struct lemon *lemp)
{
static struct state *getstate(struct lemon *lemp) {
struct config *cfp, *bp;
struct state *stp;
@ -267,8 +263,7 @@ static void buildshifts(
/*
** Construct the propagation links
*/
void FindLinks(struct lemon *lemp)
{
void FindLinks(struct lemon *lemp) {
int i;
struct config *cfp, *other;
struct state *stp;
@ -302,8 +297,7 @@ void FindLinks(struct lemon *lemp)
** A followset is the set of all symbols which can come immediately
** after a configuration.
*/
void FindFollowSets(struct lemon *lemp)
{
void FindFollowSets(struct lemon *lemp) {
int i;
struct config *cfp;
struct plink *plp;
@ -338,8 +332,7 @@ static int resolve_conflict();
/* Compute the reduce actions, and resolve conflicts.
*/
void FindActions(struct lemon *lemp)
{
void FindActions(struct lemon *lemp) {
int i,j;
struct config *cfp;
struct state *stp;

View file

@ -31,14 +31,14 @@ static void handle_D_option(char *z) {
char **paz;
nDefine++;
azDefine = realloc(azDefine, sizeof(azDefine[0])*nDefine);
if (azDefine==0) {
if (azDefine == 0) {
fprintf(stderr,"out of memory\n");
exit(1);
}
paz = &azDefine[nDefine-1];
*paz = malloc (strlen(z)+1) ;
if (*paz==0) {
fprintf(stderr,"out of memory\n");
if (*paz == 0) {
fprintf(stderr, "out of memory\n");
exit(1);
}
strcpy(*paz, z);
@ -150,10 +150,12 @@ int main(int argc, char **argv) {
FindActions(&lem);
/* Compress the action tables */
if (compress==0) CompressTables(&lem);
if (compress==0)
CompressTables(&lem);
/* Generate a report of the parser generated. (the "y.output" file) */
if (!quiet) ReportOutput(&lem);
/* Generate a report of the parser generated (the "y.output" file) */
if (!quiet)
ReportOutput(&lem);
/* Generate the source code for the parser */
ReportTable(&lem, mhflag);
@ -161,7 +163,8 @@ int main(int argc, char **argv) {
/* Produce a header file for use by the scanner. (This step is
** omitted if the "-m" option is used because makeheaders will
** generate the file for us.) */
if (!mhflag) ReportHeader(&lem);
if (!mhflag)
ReportHeader(&lem);
}
if (statistics) {
printf("Parser statistics: %d terminals, %d nonterminals, %d rules\n",