Sign in to follow this  
Plasmarobo

Bison recognition

Recommended Posts

I have a bison file, and I believe that my ordering of the rules is wrong or something. Here is the bison file (It may have something to do with type checking or the symbol table, but I think its in the parser).
 /*
 *   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 floatconst;
	char *stringconst;
 
 
 };
 
 %token tMAIN tFUNCTION
 %token <identifier> tIDENTIFIER
 %token <numconst> tNUMCONST 
 %token <floatconst> 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> items neitems
 %type <function> 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
 
  %left ')'
  %left ']'
  %left tELSE
  %right '='
  %left tOR
  %left tAND
  %left EQ NE
  %left '<' '>' GE LE
  %left '+' '-'
  %left '*' '/' '%'
  %left '^'
 
 %%

 script: items
	{sc = makeSCRIPT($1);}
;
items :
	{$$ = NULL;}
	| neitems items
	{$$ = $1;}
;

neitems: function
	{$$ = makeITEMfunction($1);}
	| main
	{$$ = makeITEMmain($1);}
    | function neitems
	{$$ = makeITEMfunction($1); $$->next =$2;}
	| main neitems
	{$$ = makeITEMmain($1);} /*MUST be terminated */
;

type : tNUM
	{$$ = makeTYPEnum();}
    | tFLOAT
	{$$ = makeTYPEdeci();}
    | tSTRING
	{$$ = makeTYPEstring();}
    | tBOOL
	{$$ = makeTYPEbool();}
;
 decl : type identifiers initialization tENDLINE
	{$$ = makeDECLvar($1, $2, $3);}
;

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

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

function : type tFUNCTION tIDENTIFIER '[' args ']' compstm
	{$$ = makeFUNCTION($3, $1, $5, $7);}
;

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
	{$$ = makeSTMsequence($1, $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 tENDLINE
	{ $$ = 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);}
;

Alright, for anyone who understands this somewhat, the rules in question are near the end. My function calls are being recognized as Lvalues rather than invocations. I'll post any compiler source or the script in anyone would like to see it.

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