Sign in to follow this  
Plasmarobo

Integrating Flex, Bison, and Visual Studio 2008

Recommended Posts

I'm building a compiler for my comp-sci project but I can't seem to get the thing to compile. All of the compiler components work, but the Lexical and Symantic Analysis functions won't compile. Also, it seems to be both using my custom command line instructions for building the *.y and *.l files, but it's also attempting to compile them as C++ source code. I could use some help on negotiating these into compliance, here is my build log:
------ Build started: Project: CompSciComp, Configuration: Debug Win32 ------
Parsing
Building lex File
Compiling...
language.tab.c
language.y(91) : warning C4133: '=' : incompatible types - from 'ITEM *' to 'SCRIPT *'
language.y(94) : warning C4133: '=' : incompatible types - from 'MAIN *' to 'ITEM *'
language.y(105) : error C2039: 'fuction' : is not a member of 'YYSTYPE'
        language.y(22) : see declaration of 'YYSTYPE'
language.y(107) : error C2039: 'fuction' : is not a member of 'YYSTYPE'
        language.y(22) : see declaration of 'YYSTYPE'
language.y(112) : warning C4013: 'makeTYPEfloat' undefined; assuming extern returning int
language.y(112) : warning C4047: '=' : 'TYPE *' differs in levels of indirection from 'int'
language.y(119) : warning C4133: 'function' : incompatible types - from 'IDENTIFIER *' to 'EXP *'
language.y(138) : error C2039: 'fuction' : is not a member of 'YYSTYPE'
        language.y(22) : see declaration of 'YYSTYPE'
language.y(138) : warning C4133: 'function' : incompatible types - from 'DECL *' to 'TYPE *'
language.y(138) : warning C4133: 'function' : incompatible types - from 'STM *' to 'DECL *'
language.y(138) : error C2198: 'makeFUNCTION' : too few arguments for call
language.y(166) : error C2039: 'next' : is not a member of 'STM'
        c:\documents and settings\austen\my documents\visual studio 2008\projects\compscicomp\compscicomp\nodetree.h(99) : see declaration of 'STM'
lex.yy.c
language.l(5) : error C2449: found '{' at file scope (missing function header?)
language.l(12) : error C2059: syntax error : '}'
language.l(26) : error C2143: syntax error : missing ';' before '<'
language.l(27) : error C2017: illegal escape sequence
language.l(28) : error C2143: syntax error : missing ';' before '{'
language.l(28) : error C2065: 'commentdepth' : undeclared identifier
language.l(28) : error C2065: 'commentdept' : undeclared identifier
language.l(29) : error C2146: syntax error : missing ';' before identifier 'commentdepth'
language.l(29) : error C2065: 'commentdepth' : undeclared identifier
language.l(30) : error C2059: syntax error : '^'
language.l(30) : error C2017: illegal escape sequence
language.l(31) : error C2143: syntax error : missing ';' before '<<'
language.l(32) : error C2143: syntax error : missing ';' before '['
language.l(32) : warning C4138: '*/' found outside of comment
language.l(32) : error C2017: illegal escape sequence
language.l(33) : error C2143: syntax error : missing ';' before '.'
lex.yy.c(640) : error C2059: syntax error : 'if'
lex.yy.c(664) : error C2059: syntax error : 'while'
lex.yy.c(1095) : error C2059: syntax error : '}'
lex.yy.c(1391) : warning C4013: 'yywrap' undefined; assuming extern returning int
lex.yy.c(1749) : warning C4013: 'exit' undefined; assuming extern returning int
lex.yy.c(1812) : warning C4013: 'malloc' undefined; assuming extern returning int
lex.yy.c(1830) : warning C4013: 'realloc' undefined; assuming extern returning int
lex.yy.c(1840) : warning C4013: 'free' undefined; assuming extern returning int
Generating Code...
Build log was saved at "file://c:\Documents and Settings\Austen\My Documents\Visual Studio 2008\Projects\CompSciComp\CompSciComp\Debug\BuildLog.htm"
CompSciComp - 23 error(s), 13 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Here is the Flex file:
  
  /*
  * ASL Austens Symbolic Language
  */


 {%

 #include "language.tab.h"
 #include <string.h>
 #include "memory.h"
 #include "error.h"
 

 extern int lineno;
 int commentdepth=0;

 %}

%x coms



%%

 <coms>{
 \n lineno++;
 "*/" {commentdepth--; if(commentdept == 0) BEGIN(INITIAL);}
 "/*" commentdepth++;
 "//" [^\n]* /* ignore single line comments */;
 <<EOF>> yyerror("Unclosed Comment");
 [^*/\n]* /* ignore non-newline characters to speed process */;
 . /* ignore everything else */;
 }

 /* Initial condition */
[ \t]+ ;

"/*" {commentdepth++; BEGIN(coms);}

\n lineno++;
"//"[^\n]* /*Ignore a line */ ;

bool return tBOOL;
num return tNUM;
decimal return tFLOAT;
# return tNUM;
string return tSTRING;
"$" return tSTRING;
else return tELSE;
if return tIF;
main return tMAIN;
function return tFUNCTION;
while return tWHILE;
break return tBREAK;
continue return tCONTINUE;
return return tRETURN;
print return tPRINT;
";" return tENDLINE;
"[" return '[';
"]" return ']';
"(" return '(';
")" return ')';
SECTION return tSECTION;
ENDSECTION return tENDSECTION;
"+" return '+';
"-" return '-';
NEG return tNEG;
"*" return '*';
"/" return '/';
AND return tAND;
OR return tOR;
"!" return '!';
"=" return '=';
"==" return EQ;
">=" return GE;
"<=" return LE;
"!=" return NE;
"^" return '^';





0|([1-9][0-9]*) { yyval.numconst = atoi(yytext); return tNUMCONST;}
0"."0|([1-9][0-9]*"."[0-9][0-9]*) {yyval.floatconst = atof(yytext); return tFLOATCONST;}
true {yyval.boolconst = 1; return tBOOLCONST;}
false {yyval.boolconst = 0; return tBOOLCONST;}
\"[^\"\n]*\"           { yytext[strlen(yytext)-1]='\0'; /* remove "'s from yytext */
                         yylval.stringconst = strdup(yytext+1);
                          return tSTRINGCONST; }
[A-Za-z_][A-za-z0-9]* { yyval.identifier = strdup(yytext); return tIDENTIFIER;}
. yyerror("Unknown characters '%s'.", yytext);


%%

and finally the Bison file
/*
 *   yacc grammar
 */
 
 %{
 #include <stdio.h>
 #include <string.h>
 #include <malloc.h>
 #include "error.h"
 #include "nodetree.h"
 
 
 int yylex();

 extern SCRIPT *sc;
 

 %}

 %start script
 
 %union {
	struct SCRIPT *script;
	struct ITEM *item;
	struct FUNCTION *function;
	struct MAIN *main;
	struct TYPE *type;
	struct IDENTIFIER *identifiers;
	struct EXP *exp;
	struct DECL *decl;
	struct STM *stm;
	struct LVALUE *lvalue;
	char *identifier;
	int numconst;
	int boolconst;
	float deciconst;
	char *stringconst;
 
 
 };
 
 %token tMAIN tFUNCTION
 %token <identifier> tIDENTIFIER
 %token <numconst> tNUMCONST 
 %token <deciconst> tFLOATCONST 
 %token <boolconst> tBOOLCONST 
 %token <stringconst> tSTRINGCONST
 %token tFLOAT tNUM tBOOL tSTRING
 %token tIF tELSE tWHILE tBREAK tCONTINUE tRETURN tPRINT
 %token tENDLINE '[' ']' '(' ')' tSCOPE tENDSCOPE
 %token '+' '-'
 %token '*' '/' '%'
 %token '='
 %token GE LE EQ NE
 %token '^' 
 %token tAND tOR '!' tNEG
 %token ','
 
 %type <script> script
 %type <item> ne items neitems
 %type <fuction> function 
 %type <main> main
 %type <type> type
 %type <identifiers> identifiers
 %type <exp> initialization exp unary unarypostfix exps nexps
 %type <decl> decl arg args neargs
 %type <stm> nestm stm compstm
 %type <lvalue> lvalue
 

 /*
  * precedence
  */
 
  %left ')'
  %left tELSE
  %right '='
  %left tOR
  %left tAND
  %left EQ NE
  %left '<' '>' GE LE
  %left '+' '-'
  %left '*' '/' '%'
  %left '^'
 
 %%

 script : /* empty */
	{$$ = NULL;}
    | ne
	{$$ = $1;}
;
ne : main items
	{$$ = $1; $$->next = $2;}
;


items : /* empty */
	{$$ = NULL;}
	| neitems
	{$$ = $1;}
;

neitems: function
	{$$ = $1;}
    | function neitems
	{$$ = $1; $$->next =$2;}
;
type : tNUM
	{$$ = makeTYPEnum();}
    | tFLOAT
	{$$ = makeTYPEfloat();}
    | tSTRING
	{$$ = makeTYPEstring();}
    | tBOOL
	{$$ = makeTYPEbool();}
;
 decl : type identifiers initialization tENDLINE
	{$$ = makeDECLvar($1, $2, $2);}
;

identifiers : tIDENTIFIER
	{$$ = makeIDENTIFIER($1);}
    | tIDENTIFIER ',' identifiers
	{$$ = makeIDENTIFIER($1); $$->next = $3;}
;

initialization : /* empty */
	{$$ = NULL;}
    | '=' exp
	{$$ = $2;}
;
main : tMAIN compstm
	{$$ = makeMAIN($2);}
;

function : tFUNCTION tIDENTIFIER '[' args ']' compstm
	{$$ = makeFUNCTION($2, $4, $6);}
;

args : /* empty */
	{$$ = NULL;}
    | neargs
	{$$ = $1;}
;

neargs : arg
	{$$ = $1;}
	| arg ',' neargs
	{$$ = $1; $$->next = $3;}
;

arg : type tIDENTIFIER
	{$$ = makeDECLarg($1, $2);}
;

compstm : tSCOPE tENDSCOPE
	{ $$ = NULL;}
    | tSCOPE nestm tENDSCOPE
	{$$ = $2;}
;

nestm : stm
	{$$ = $1;}
    | stm nestm
	{$$ = $1; $$->next = $2;}
;

stm : tENDLINE
	{$$ = makeSTMskip();}
    | tRETURN tENDLINE
	{$$ = makeSTMreturn(NULL);}
    | tRETURN exp tENDLINE
	{$$ = makeSTMreturn($2);}
    | tIF '(' exp ')' stm
	{$$ = makeSTMif($3, $5); }
    | tIF '(' exp ')' stm tELSE stm
	{$$ = makeSTMifelse($3, $5, $7); }
    | tWHILE '(' exp ')' stm
	{$$ = makeSTMwhile($3, $5); }
    | tBREAK tENDLINE
	{$$ = makeSTMbreak(); }
    | tCONTINUE tENDLINE
	{$$ = makeSTMcontinue(); }
    | compstm
	{$$ = makeSTMscope($1);}
    | decl
	{ $$ = makeSTMdecl($1);}
    | exp
	{ $$ = makeSTMexp($1);}
    | tPRINT '(' exp ')'
	{ $$ = makeSTMprint($3);}
;

exp : lvalue '=' exp
	{ $$ = makeEXPassignment($1, $3);}
    | exp EQ exp
	{ $$ = makeEXPequals($1, $3); }
    | exp NE exp
	{ $$ = makeEXPnequals($1, $3);}
    | exp '<' exp
	{ $$ = makeEXPless($1, $3); }
    | exp '>' exp
	{ $$ = makeEXPgreater($1, $3); }
    | exp GE exp
	{ $$ = makeEXPgequals($1, $3); }
    | exp LE exp
	{ $$ = makeEXPlequals($1, $3); }
    | exp '+' exp
	{ $$ = makeEXPplus($1, $3);}
    | exp '-' exp
	{ $$ = makeEXPminus($1, $3);}
    | exp '*' exp
	{ $$ = makeEXPmult($1, $3);}
    | exp '/' exp
	{ $$ = makeEXPdiv($1, $3);}
    | exp '%' exp
	{$$ = makeEXPmodulo($1, $3);}
    | exp '^' exp
	{$$ = makeEXPpow($1, $3);}
    | exp tOR exp
	{$$ = makeEXPor($1, $3);}
    | exp tAND exp
	{$$ = makeEXPand($1, $3);}
    | unary
	{$$ = $1;}
;
unary : tNEG unary
	{$$ = makeEXPneg($2);}
     | '!' unary
	 { $$ = makeEXPnot($2);}
     | '(' type ')' unary
	 { $$ = makeEXPcast($2, $4);}
     | unarypostfix
	 { $$ = $1;}
;

unarypostfix : tNUMCONST
	{$$ = makeEXPnumconst($1);}
    | tBOOLCONST
	{$$ = makeEXPboolconst($1);}
    | tFLOATCONST
	{$$ = makeEXPfloatconst($1);}
    | tSTRINGCONST
	{$$ = makeEXPstringconst($1);}
    | lvalue
	{$$ = makeEXPlvalue($1);}
    | '(' exp ')'
	{$$ = $2;}
    | tIDENTIFIER '[' exps ']' /* Invoke */
	{$$ = makeEXPcall($1, $3); }
;
exps : /* empty */
	{$$ = NULL;}
    | nexps
	{$$ = $1;}
;

nexps : exp
	{$$ = NULL;}
    | exp ',' nexps
	{$$ = $1, $$->next = $3;}
;

lvalue : tIDENTIFIER
	{$$ = makeLVALUEid($1);}
;


The language is based on the PxdScript Tutorials, which I am using as a guide. I've also used a bit of reference from a Unix compiler design book (the name of which escapes me at the moment). Am I using flex and bison incorrectly? These are my command line instructions (via Visual C++ 2008) flex language.l bison -d language.y -o language.tab.c I remember that I did construct a working compiler about a year ago, but it barely deviated from the tutorial (I added a float data member). It worked well actually.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this