Hi everyone,
I need to write a compiler with Yacc that will basically perform minor algebraic commands. Here is the code so far for the Yacc part.
%token INTEGER VARIABLE READ WRITE
%left '+' '-'
%left '*' '/'
%{
#include <stdio.h>
void yyerror(char *);
int yylex(void);
int sym[26];
int x;
%}
%%
program:
stmt_list '\n'
|
;
stmt_list:
stmt_list stmt
| stmt
;
stmt:
VARIABLE '=' expr ';' {sym[$1] = $3;}
| READ VARIABLE ';' {printf("Please enter an Integer");
scanf("%d", x);
sym[$2] = x;} // HERE IS THE PROBLEM LINE
| WRITE expr ';' {printf("The result is: %d", $2);}
;
expr:
term
| expr '+' term {$$ = $1 + $3; }
| expr '-' term {$$ = $1 - $3; }
;
term:
factor
| term '*' factor {$$ = $1 * $3; }
| term '/' factor {$$ = $1 / $3; }
;
factor:
'('expr')'
| VARIABLE {$$ = sym[$1];}
| INTEGER
;
%%
void yyerror (char *s)
{ printf("%s\n", s); }
int main(void) {
yyparse();
return 0;
}
I have indicated where the problem occurs. I get a basic syntax error but I cannot figure it out. Any help would be greatly appreciated.