lemon refactor iterate

This commit is contained in:
John Kerl 2016-07-13 21:22:54 -04:00
parent 034698577a
commit bc73e25a54
8 changed files with 130 additions and 123 deletions

View file

@ -3,6 +3,10 @@ AM_CFLAGS= -std=gnu99
noinst_PROGRAMS= lemon
lemon_SOURCES= lemon.c \
lemon_assert.c \
lemon_assert.h \
lemon_error.c \
lemon_error.h \
lemon_option.c \
lemon_option.h

View file

@ -105,8 +105,8 @@ ex2_lexer.c ex2_lexer.h: ex2_lexer.l ./ex_ast.h
flex --prefix=ex2_lexer_ --outfile=ex2_lexer.c --header-file=ex2_lexer.h ex2_lexer.l
# ----------------------------------------------------------------
lemon: lemon.c lemon_option.c
$(DSLCC) -o lemon lemon.c lemon_option.c
lemon: lemon.c lemon_assert.c lemon_error.c lemon_option.c
$(DSLCC) -o lemon lemon.c lemon_assert.c lemon_error.c lemon_option.c
# ----------------------------------------------------------------
clean:

View file

@ -13,6 +13,8 @@
#include <stdlib.h>
#include <unistd.h>
#include "lemon_assert.h"
#include "lemon_error.h"
#include "lemon_option.h"
#ifndef __WIN32__
@ -37,14 +39,6 @@ extern void *malloc();
struct action *Action_new();
struct action *Action_sort();
/********* From the file "assert.h" ************************************/
void myassert();
#ifndef NDEBUG
# define assert(X) if(!(X))myassert(__FILE__,__LINE__)
#else
# define assert(X)
#endif
/********** From the file "build.h" ************************************/
void FindRulePrecedences();
void FindFirstSets();
@ -65,9 +59,6 @@ struct config *Configlist_basis(/* void */);
void Configlist_eat(/* struct config * */);
void Configlist_reset(/* void */);
/********* From the file "error.h" ***************************************/
void ErrorMsg(const char *, int,const char *, ...);
/******** From the file "parse.h" *****************************************/
int Parse(/* struct lemon *lemp */);
@ -538,16 +529,6 @@ int acttab_insert(acttab *p) {
return i - p->mnLookahead;
}
/********************** From the file "assert.c" ****************************/
/*
** A more efficient way of handling assertions.
*/
void myassert(char *file, int line)
{
fprintf(stderr,"Assertion failed on line %d of file \"%s\"\n",line,file);
exit(1);
}
/********************** From the file "build.c" *****************************/
/*
** Routines to construction the finite state machine for the LEMON
@ -1203,76 +1184,6 @@ void Configlist_eat(struct config *cfp)
return;
}
/***************** From the file "error.c" *********************************/
/*
** Code for printing error message.
*/
/* 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)
{
int i,spot;
char c;
for(i=spot=min; i<=max; i++){
c = msg[i];
if (c=='\t') msg[i] = ' ';
if (c=='\n') { msg[i] = ' '; spot = i; break; }
if (c==0) { spot = i; break; }
if (c=='-' && i<max-1) spot = i+1;
if (c==' ') spot = i;
}
return spot;
}
/*
** The error message is split across multiple lines if necessary. The
** splits occur at a space, if there is a space available near the end
** of the line.
*/
#define ERRMSGSIZE 10000 /* Hope this is big enough. No way to error check */
#define LINEWIDTH 79 /* Max width of any output line */
#define PREFIXLIMIT 30 /* Max width of the prefix on each line */
void ErrorMsg(const char *filename, int lineno, const char *format, ...) {
char errmsg[ERRMSGSIZE];
char prefix[PREFIXLIMIT+10];
int errmsgsize;
int prefixsize;
int availablewidth;
va_list ap;
int end, restart, base;
va_start(ap, format);
/* Prepare a prefix to be prepended to every output line */
if (lineno>0) {
sprintf(prefix,"%.*s:%d: ",PREFIXLIMIT-10,filename,lineno);
} else {
sprintf(prefix,"%.*s: ",PREFIXLIMIT-10,filename);
}
prefixsize = strlen(prefix);
availablewidth = LINEWIDTH - prefixsize;
/* Generate the error message */
vsprintf(errmsg,format,ap);
va_end(ap);
errmsgsize = strlen(errmsg);
/* Remove trailing '\n's from the error message. */
while (errmsgsize>0 && errmsg[errmsgsize-1]=='\n') {
errmsg[--errmsgsize] = 0;
}
/* Print the error message */
base = 0;
while (errmsg[base]!=0) {
end = restart = findbreak(&errmsg[base],0,availablewidth);
restart += base;
while (errmsg[restart]==' ') restart++;
fprintf(stdout,"%s%.*s\n",prefix,end,&errmsg[base]);
base = restart;
}
}
/**************** From the file "main.c" ************************************/
/*
** Main program file for the LEMON parser generator.

8
c/dsls/lemon_assert.c Normal file
View file

@ -0,0 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "lemon_assert.h"
void lemon_assert(char *file, int line) {
fprintf(stderr,"Assertion failed on line %d of file \"%s\"\n", line, file);
exit(1);
}

11
c/dsls/lemon_assert.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef LEMON_ASSERT_H
#define LEMON_ASSERT_H
void lemon_assert();
#ifndef NDEBUG
# define assert(X) if(!(X))lemon_assert(__FILE__,__LINE__)
#else
# define assert(X)
#endif
#endif // LEMON_ASSERT_H

73
c/dsls/lemon_error.c Normal file
View file

@ -0,0 +1,73 @@
#include <stdio.h>
#include <string.h>
#include "lemon_error.h"
/***************** From the file "error.c" *********************************/
/*
** Code for printing error message.
*/
/* 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)
{
int i,spot;
char c;
for(i=spot=min; i<=max; i++){
c = msg[i];
if (c=='\t') msg[i] = ' ';
if (c=='\n') { msg[i] = ' '; spot = i; break; }
if (c==0) { spot = i; break; }
if (c=='-' && i<max-1) spot = i+1;
if (c==' ') spot = i;
}
return spot;
}
/*
** The error message is split across multiple lines if necessary. The
** splits occur at a space, if there is a space available near the end
** of the line.
*/
#define ERRMSGSIZE 10000 /* Hope this is big enough. No way to error check */
#define LINEWIDTH 79 /* Max width of any output line */
#define PREFIXLIMIT 30 /* Max width of the prefix on each line */
void ErrorMsg(const char *filename, int lineno, const char *format, ...) {
char errmsg[ERRMSGSIZE];
char prefix[PREFIXLIMIT+10];
int errmsgsize;
int prefixsize;
int availablewidth;
va_list ap;
int end, restart, base;
va_start(ap, format);
/* Prepare a prefix to be prepended to every output line */
if (lineno>0) {
sprintf(prefix,"%.*s:%d: ",PREFIXLIMIT-10,filename,lineno);
} else {
sprintf(prefix,"%.*s: ",PREFIXLIMIT-10,filename);
}
prefixsize = strlen(prefix);
availablewidth = LINEWIDTH - prefixsize;
/* Generate the error message */
vsprintf(errmsg,format,ap);
va_end(ap);
errmsgsize = strlen(errmsg);
/* Remove trailing '\n's from the error message. */
while (errmsgsize>0 && errmsg[errmsgsize-1]=='\n') {
errmsg[--errmsgsize] = 0;
}
/* Print the error message */
base = 0;
while (errmsg[base]!=0) {
end = restart = findbreak(&errmsg[base],0,availablewidth);
restart += base;
while (errmsg[restart]==' ') restart++;
fprintf(stdout,"%s%.*s\n",prefix,end,&errmsg[base]);
base = restart;
}
}

7
c/dsls/lemon_error.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef LEMON_ERROR_H
#define LEMON_ERROR_H
#include "stdarg.h"
void ErrorMsg(const char *, int,const char *, ...);
#endif // LEMON_ERROR_H

View file

@ -10,12 +10,10 @@ static FILE *errstream;
#define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
/*
** Print the command line with a carrot pointing to the k-th character
** of the n-th field.
*/
static void errline(int n, int k, FILE *err)
{
// ----------------------------------------------------------------
// Print the command line with a caret pointing to the k-th character
// of the nth field.
static void errline(int n, int k, FILE *err) {
int spcnt, i;
spcnt = 0;
if (argv[0]) fprintf(err,"%s",argv[0]);
@ -33,12 +31,10 @@ static void errline(int n, int k, FILE *err)
}
}
/*
** Return the index of the N-th non-switch argument. Return -1
** if N is out of range.
*/
static int argindex(int n)
{
// ----------------------------------------------------------------
// Return the index of the N-th non-switch argument. Return -1
// if N is out of range.
static int argindex(int n) {
int i;
int dashdash = 0;
if (argv!=0 && *argv!=0) {
@ -55,11 +51,9 @@ static int argindex(int n)
static char emsg[] = "Command line syntax error: ";
/*
** Process a flag command line argument.
*/
static int handleflags(int i, FILE *err)
{
// ----------------------------------------------------------------
// Process a flag command-line argument.
static int handleflags(int i, FILE *err) {
int v;
int errcnt = 0;
int j;
@ -89,11 +83,9 @@ static int handleflags(int i, FILE *err)
return errcnt;
}
/*
** Process a command line switch which has an argument.
*/
static int handleswitch(int i, FILE *err)
{
// ----------------------------------------------------------------
// Process a command-line switch which has an argument.
static int handleswitch(int i, FILE *err) {
int lv = 0;
double dv = 0.0;
char *sv = 0, *end;
@ -177,8 +169,8 @@ static int handleswitch(int i, FILE *err)
return errcnt;
}
int OptInit(char **a, struct s_options *o, FILE *err)
{
// ----------------------------------------------------------------
int OptInit(char **a, struct s_options *o, FILE *err) {
int errcnt = 0;
argv = a;
op = o;
@ -201,6 +193,7 @@ int OptInit(char **a, struct s_options *o, FILE *err)
return 0;
}
// ----------------------------------------------------------------
int OptNArgs() {
int cnt = 0;
int dashdash = 0;
@ -214,21 +207,22 @@ int OptNArgs() {
return cnt;
}
char *OptArg(int n)
{
// ----------------------------------------------------------------
char *OptArg(int n) {
int i;
i = argindex(n);
return i>=0 ? argv[i] : 0;
}
void OptErr(int n)
{
// ----------------------------------------------------------------
void OptErr(int n) {
int i;
i = argindex(n);
if (i>=0) errline(i,0,errstream);
}
void OptPrint(){
// ----------------------------------------------------------------
void OptPrint() {
int i;
int max, len;
max = 0;
@ -277,4 +271,3 @@ void OptPrint(){
}
}
}