سؤال

This is my bison code. The problem is, when I input for example 4+3, program detects a syntax error. But when I input 4 + 3, that is the numbers are separated by a space, it computes and shows the result perfectly. Kindly hint where it might have gone wrong.


input:
 | input line
 ;

line: NEWLINE
 | expr NEWLINE     {cout << $1 << endl; $$ = $1;}
 ;
expr: term
 | expr PLUS term    {$$ = $1 + $3;}
 | expr MINUS term  {$$ = $1 - $3;}
 | expr LSHIFT term {checkedShifting($1, $3, &$$, true);}
 | expr RSHIFT term {checkedShifting($1, $3, &$$, false);}
 ;
term: factor
 | term MUL factor  {$$ = $1 * $3;}
 | term DIV factor  {checkedDivision($1, $3, &$$);}
 | term MOD factor  {checkedMod($1, $3, &$$);}
 ;
factor: NUMBER
   | LBRACE expr RBRACE     {$$ = $2;}
   | factor EXPONENT factor {$$ = pow($1, $3);}
   ;

Edit : This is my lex code: http://paste.ubuntu.com/6959725/

And the bison code: http://paste.ubuntu.com/6959727/

هل كانت مفيدة؟

المحلول

At a glance, seems that 4+3 should be tokenized as {number}{signednumber}, not {number}{PLUS}{number}.

Also I think you shouldn't have a special token for signed number. Parser should figure out if this is an unary or binary plus/minus.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top