Hi everyone,
I need to write a compiler using Yacc and I have encountered a problem. Since some of the code is C i thought I would try here. The problem occurs at one line in the following program. Once running, everything runs fine till this line is hit, then a syntax error is given.

%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;
        }

Never mind, i got it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.