I am reading in a file and for some reason i get a syntax error when i try an expression
like 5+5 however, if i do this 5 + 5 it works good. I am baffled why it would do this?
Here is my lex file( i will leave out main that reads in a file):
%{
#include "y.tab.h"
#include "stdio.h"
#include <stdlib.h>
%}
%%
(\/\*([^*]|(\*+([^*/]|[\r\n])))*\*+\/)+ {}
\/\/[^\n]*\n {}
fd { return FD; }
[\r\t\n]+ {}
[ ]* {}
bk { return BK;}
setc {return SETC;}
[-+]?[0-9]+ { yylval = atoi(yytext); return NUMBER;}
fd[0-9]+ { }
rt {return RT;}
pink {return COLOR_TYPE;}
magenta {return COLOR_TYPE; }
if {return IF; }
ifelse {return IFELSE; }
\[ {return LBRACKET; }
\] {return RBRACKET; }
\< {return LESS_THAN; }
\> {return GREATER_THAN; }
\+ {return PLUS; }
\- {return MINUS; }
\/ {return DIVIDE; }
\* {return MULT; }
\( {return LPAREN;}
\) {return RPAREN;}
\= {return EQ;}
%%
Here is part of my yacc file that deals with the expression:
expr : NUMBER { printf("EXPR-->NUMBER: %d\n", $1);}
|expr PLUS expr {$$ = $1 + $3; printf("EXPR-->expression PLUS expression: %d\n", $$);}
|expr DIVIDE expr {$$ = $1 / $3; printf("EXPR-->expression DIVIDE expression %d\n", $$);}
|expr MULT expr {$$ = $1 * $3; printf("EXPR-->expression MULTIPLY expression %d\n", $$);}
|expr MINUS expr {$$ = $1 - $3; printf("EXPR-->expression MINUS expression %d\n", $$);}
|COLOR_TYPE {printf("EXPR-->COLOR\n");}
;
would the problem be in the lex file?
The tokenizer (lexer) returns these two to the parser:
5and+5. Which by your grammar (and logically) is invalid.I think, you’d be better off, to alter your lexer and move the rules for operators up. (That means at least above the rule returning
NUMBER).EDIT: After some thinking (EDIT #2: and a more than useful comment by Jerry Coffin), I suggest changing the lexical rule for
NUMBERto[0-9]+. In order for the parser to still accept input like “+123” or “-123”, you should add this to your grammar:This will allow for an unary
+or-before any number while still giving the operators+and-a higher precedence.