I am new to C and even more new to Flex and Bison. I am writing a simple parser, for a project. I am trying to be able to make a load command so I can load code from an external file and have the parser run it. Here is the code I have:
Bison (spire.y):
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "helper.h"
/* prototypes */
nodeType *opr(int oper, int nops, ...);
nodeType *id(int i);
nodeType *con(int value);
void freeNode(nodeType *p);
int ex(nodeType *p);
int yylex(void);
void yyerror(char *s);
int sym[26]; /* symbol table */
%}
%union {
int iValue; /* integer value */
char sIndex; /* symbol table index */
char *var;
char *str;
nodeType *nPtr; /* node pointer */
};
%token <iValue> INTEGER
%token <sIndex> VARIABLE
%token <var> VARNAME
%token <str> STRING
%token WHILE IF TELL EXITCOMM LOAD
%nonassoc IFX
%nonassoc ELSE
%left GE LE EQ NE '>' '<' BLKND
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%type <nPtr> stmt expr stmt_list
%%
spire:
code { exit(0); }
;
code:
code stmt { ex($2); freeNode($2); }
| /* NULL */
;
stmt:
';' { $$ = opr(';', 2, NULL, NULL); }
| expr ';' { $$ = $1; }
| EXITCOMM {exit(EXIT_SUCCESS); }
| LOAD STRING ';' { char *f = $2; f++[strlen(f)-1] = 0; $$ = getCode(f); }
...
%%
...
const char * getCode(char *fileName){
char ch;
char *code;
FILE *fp;
fp = fopen(fileName, "r");
if( fp == NULL )
{
printf("Error while opening the file %s.\n", fileName);
return ';';
}else{
while((ch = fgetc(fp))!=EOF)
code = strcat(code, ch);
fclose(fp);
return code;
}
Flex(spire.l):
%{
#include <stdlib.h>
#include "helper.h"
#include "spire.tab.h"
void yyerror(char *);
%}
%%
[a-z] {
yylval.sIndex = *yytext - 'a';
return VARIABLE;
}
0 {
yylval.iValue = atoi(yytext);
return INTEGER;
}
[1-9][0-9]* {
yylval.iValue = atoi(yytext);
return INTEGER;
}
[-()<>^=+*/;{}~."] {
return *yytext;
}
'~' return EXITCOMM;
"^^" return BLKND;
">=" return GE;
"<=" return LE;
"==" return EQ;
"!=" return NE;
"while" return WHILE;
"if" return IF;
"else" return ELSE;
"tell" return TELL;
"load" return LOAD;
[a-z][a-zA-Z0-9_]* {
yylval.var = strdup(yytext);
return VARNAME;
}
\"[^"\n]*["\n] {
yylval.str = strdup(yytext);
return STRING;
}
[ \t\n]+ ; /* ignore whitespace */
. yyerror("Unknown character");
%%
int yywrap(void) {
return 1;
}
Here are the load of Errors I get, Every time I attempt to fix one I just get more.
Errors:
spire.y: In function 'yyparse':
spire.y:56: warning: assignment makes pointer from integer without a cast
spire.y: At top level:
spire.y:112: error: conflicting types for 'getCode'
spire.y:56: note: previous implicit declaration of 'getCode' was here
spire.y: In function 'getCode':
spire.y:122: warning: return makes pointer from integer without a cast
spire.y:125: warning: passing argument 2 of 'strcat' makes pointer from integer without a cast
/usr/include/string.h:136: note: expected 'const char * __restrict__' but argument is of type 'char'
spire.y:208: error: expected declaration or statement at end of input
Let me know if you want the entire Bison file