Reputation: 63
I am trying to build my own mini C language compiler using flex
but there are an error that keeps showing up. Can you please help me to find what is the problem? I searched for a solution but still not found the reason behind this error. Here is the code:
%{
#define KEY_VOID = 258
#define KEY_FLOAT = 260
#define KEY_IF = 261
#define KEY_ELSE = 262
#define KEY_WHILE = 263
#define KEY_FOR = 264
#define KEY_RETURN = 265
#define KEY_BREAK = 266
#define AND_OP = 267
#define OR_OP = 268
#define SMALL_EQ_OP = 269
#define GREAT_EQ_OP = 270
#define EQ_OP = 271
#define SMALL_OP = 272
#define GREAT_OP = 273
#define NOT_EQ_OP = 274
#define ASSIGN_OP = 275
#define OPN_BRACKET = 276
#define CLS_BRACKET = 277
#define CLS_BRACE = 290
#define OPN_BRACE = 291
#define SEMICOLON_SYMBOL = 278
#define COMMA_SYMBOL = 279
#define DOT_SYMBOL = 280
#define ADD_OP = 281
#define SUB_OP = 282
#define ASTERISK_SYMBOL = 283
#define SLASH_SYMBOL = 284
#define START_COMMENT_SYMBOL = 285
#define END_COMMENT_SYMBOL = 286
#define INT_NUM 287
#define FLOAT_NUM 288
#define IDENTIFIER 289
Int yylval;
%}
Letters [a-zA-Z]
Digits [0-9]
Sympols [@#$%&*-+!"':;/?(),~`|^_=×{}<>]
%%
[/*][{Letters}|{Digits}|{Sympols}|\n|\t]*[/*] {/* Ignore Comments */}
[-+]?[{Digits}]+ {yylval = atoi(yytext); return INT_NUM;}
[-+]?[{Digits}]+.[{Digits}]+ {yylval = atoi(yytext);return FLOAT_NUM;}
[{Letters}][{Letters}|{Digits}|_]* {return IDENTIFIER;}
[ \t\n]+ {/* Ignore WhiteSpaces and New lines */}
"void" {return KEY_VOID;}
"float" {return KEY_FLOAT;}
"if" {return KEY_IF;}
"else" {return KEY_ELSE ;}
"while" {return KEY_WHILE;}
"for" {return KEY_FOR;}
"return" {return KEY_RETURN;}
"break" {return KEY_BREAK;}
"&&" {return AND_OP;}
"||" {return OR_OP;}
"<=" {return SMALL_EQ_OP;}
">=" {return GREAT_EQ_OP;}
"==" {return EQ_OP;}
"<" {return SMALL_OP;}
">" {return GREAT_OP;}
"!=" {return NOT_EQ_OP;}
"=" {return ASSIGN_OP;}
"(" {return OPN_BRACKET;}
")" {return CLS_BRACKET;}
";" {return SEMICOLON_SYMBOL;}
"," {return COMMA_SYMBOL;}
"." {return DOT_SYMBOL;}
"+" {return ADD_OP;}
"-" {return SUB_OP;}
"*" {return ASTERISK_SYMBOL;}
"/" {return SLASH_SYMBOL;}
. {yyerror();}
%%
int main(void)
{
yyparse();
return 0;
}
int yywrap(void)
{
return 1;
}
int yyerror(void)
{
printf("Error\n");
exit(1);
}
And this is the error that keeps showing up:
46 C:\GnuWin32\GnuWin32\bin\Project.l syntax error before '=' token
Upvotes: 0
Views: 521
Reputation: 13808
The problem is in your #define
s - they should look just like:
#define KEY_VOID 258
But sometimes you have an extra equals sign there, for example:
#define KEY_VOID = 258
The error message line is misleading, because the error gets reported only where the macro is subsequently used, not where it is defined.
Upvotes: 2