Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

parser with symbol table programme... not working in linux

[b] KINDLY HELP ME TO SOLVE THIS... PLEASE


sample/output:

parameter (1) float float1;
local (1) int i;
parameter (1) int int1;
local (1) int j;
local (1) int k;
global (0) int a;
global (0) int b;
function (0) int my_function
param list int, float,;
function (0) int print
param list float,;
global (0) float x;
global (0) float y;
sample/warnings:
Warning : Expected ')' at line 23 [/B]

using c or c++ or linux(unix)

lex.h:
#ifndef LEX_H
#define LEX_H
#include
#include
/* lex.h
This file contains structure definitions, prototypes, and constants for
used in the lex project.
*/
#define FALSE 0
#define TRUE 1
#define MAXSTRING 81 /* Maximum identifier size */
#define PUNCTUATION 0 /* Token Type for ",", ";", "{", "}",
"[", "]", "(", ")" */
#define OPS 1 /* Token Type for "=", "+", "-", "*", "/" */
#define RELS 2 /* Token Type for "<", ">", "<=",
">=", ">", "==", "<>" */
#define INTEGER 3 /* Token Type for integer constants */
#define FLOAT 4 /* Token Type for floating point constants */
#define IDENTIFIER 5 /* Token Type for identifiers */
#define COMMENT 6 /* Token Type for comments */
#define ENDTOKEN 7 /* Token Type for EOF marker */
#define KINT 8 /* Token Type for "int" */
#define KFLOAT 9 /* Token Type for "float" */
#define KIF 10 /* Token Type for "if" */
#define KELSE 11 /* Token Type for "else" */
#define KWHILE 12 /* Token Type for "while" */
#define KRETURN 13 /* Token Type for "return" */
#define ERROR 14 /* Token Type for invalid items */
/* Token structure definition */
typedef struct { int type; /* Token Type */
char name[MAXSTRING]; /* Token Value */
} tokentype;
/* function lex prototype definition */
tokentype lex(FILE *fp);
#endif



sym.h:
/* sym.h
This file contains structure definitions, prototypes,
constants for use in the symbol project.
*/
#ifndef SYM_H
#define SYM_H
#include
#include
#include "lex.h"
#define FATAL 0
#define WARN 1
#define MAXSYMBOL 512 /* Maximum symbol table size */
/* Symbol class definitions */
#define PARAMETER 0 /* Function parameter */
#define FUNCTION 1 /* Function name */
#define VARIABLE 2 /* Variable name */
/* A linked list of parameter types for function entries */
typedef struct typ {
int type;
struct typ *next;
} paramtype;
typedef struct sym {
int type; /* INTEGER or FLOAT from lex.h */
int class; /* See symbol class definitions above */
int size; /* Used for variables, number of
subparts */
int scope; /* Scope of definition */
int offset; /* Used later, set to 0 for now */
char name[MAXSTRING]; /* Name string of entry */
struct sym *next; /* To link table entries */
paramtype *next_parm; /* Used only for functions */
} symbol;
/* Standardized function prototype definitions */
/* Make a new entry */
symbol *enter(char *name, int type, int class, int size);
/* Look up symbol entry matching string s */
symbol *lookup(char *s);
/* Initialize the symbol table: You must enter the function print */
/* in the table at scope level 0 with 1 parameter, a float. */
void symtab_init();
#endif

parse.h:
/*
parse.h
This file contains prototypes used in the parser.
*/
#ifndef PARSE_H
#define PARSE_H
#include "lex.h"
#include "sym.h"
#include "lex.h"
#include "sym.h"
/* Standardized function prototype definitions */
int prog(tokentype t, FILE *ifp, FILE *ofp, FILE *efp);
/* Add all your prototype definitions for functions in
parsec.c here. Hand this in with your code.
*/
#endif

main.c:
#include "sym.h"
#include "lex.h"
#include "parse.h"
/* Main driver routine for symbol table project. It initializes
symbol table, gets the first token, and calls the top level
parse routine() */
static char *infile;
static FILE *efp,*ofp;
int STATUS = 0;
main(int argc,char *argv[])
{
tokentype token;
char *outfile,*errfile;
FILE *ifp,*fopen();
/* Get input file */
if(argc<3) {
infile = argv[1];
outfile = "";
errfile = "";
}
else if(argc<4) {
infile = argv[1];
outfile = argv[2];
errfile = "";
}
else {
infile = argv[1];
outfile = argv[2];
errfile = argv[3];
}
if((ifp=fopen(infile,"r"))==NULL) {
printf("Error: can not open file %s
",infile);
return;
}
if((ofp=fopen(outfile,"w"))==NULL)
ofp = stdout;
if((efp=fopen(errfile,"w"))==NULL)
efp = stderr;
/* Initialize symbol table */
symtab_init();
/* Get first input token */
token = lex(ifp);
/* Begin recursive descent parsing and code generation */
prog(token,ifp,ofp,efp);
fclose(ifp);
fclose(ofp);
fclose(efp);
exit(STATUS);
}
/* Print out symbol table information */
void psymbol (symbol *s)
{
paramtype *pp;
/* print symbol type */
switch (s->class) {
case FUNCTION:
fprintf(ofp,"function ");
fprintf(ofp,"(%d) ",s->scope);
break;
case PARAMETER:
fprintf(ofp,"parameter ");
fprintf(ofp,"(%d) ",s->scope);
break;
case VARIABLE:
fprintf(ofp,s->scope ? "local " : "global ");
fprintf(ofp,"(%d) ",s->scope);
break;
}
if(s->type==INTEGER)
fprintf(ofp," int ");
else
fprintf(ofp," float ");
fprintf(ofp," %s", s->name);
if(s->class==FUNCTION) {
fprintf(ofp,"
param list ");
for(pp=s->next_parm;pp!=NULL;pp=pp->next)
if(pp->type==INTEGER)
fprintf(ofp," int,");
else
fprintf(ofp," float,");
}
fprintf(ofp,";
");
}

[b]here only i have a problem how to build it in linux...[/b]
------------------------------------------------------
Makefile -- here's how to build the parser.
------------------------------------------------------
---------
macros
---------
XCC=cc
CFLAGS =
CPPFLAGS =
LIBS =
OBJFILES = main.o lex.o parse.o sym.o
-------------------------------------------------------------
This links the object files into an executable
-------------------------------------------------------------
parse : $(OBJFILES)
$(XCC) $(CPPFLAGS) $(CFLAGS) -o parse $(OBJFILES) $(LIBS)
--------------------------------------------------------------
This builds the object files for the various source files
--------------------------------------------------------------
main.o : main.c
$(XCC) $(CPPFLAGS) $(CFLAGS) -c main.c
parse.o : parse.c parse.h
$(XCC) $(CPPFLAGS) $(CFLAGS) -c parse.c
lex.o : lex.c lex.h
$(XCC) $(CPPFLAGS) $(CFLAGS) -c lex.c
sym.o : sym.c sym.h
$(XCC) $(CPPFLAGS) $(CFLAGS) -c sym.c
-------------------------------------
forces recompilation of everything.
-------------------------------------
scratch : clean parse
---------------------------------------------
deletes all object files and the executable
---------------------------------------------
clean :
-rm *.o parse
sample/input:
int a,b;
float x,y;
int my_function(int int1, float float1){
int i,j,k;
i=5;
j=4;
/* the next line is missing a ) so that you can see a sample warning
if (j0){
print(k);
k=k-1;
}
}

Sign In or Register to comment.