0
votes

I'm trying to parse statements to see whether they are valid or not according to these rules:

assignment: id = exp ;

expression: id op id {op id}

id is combination of digits and char, the first position contain a char.

I'm getting syntax error when I have something like this in my in.txt file: hellow = three3

but that shouldn't be a syntax error, and then when i put something like: hellow = =

that does not display a syntax error but it should. What am i doing wrong?

Lex:

%{
#include "y.tab.h"
#include <stdio.h>
%}
%%
[ \t\n]+                ;
[a-zA-Z][a-zA-Z0-9]*    {
                        ECHO;
                        return ID;
                    }
%%

YACC:

%{
#include <stdio.h>
extern FILE * yyin;
%}
%token ID

%left '+' '-'
%left '*' '/' '%'
%right ';'

%%
assignment: expression
        |
        ID '=' expression
        ;

expression: ID
        |
        expression '*' expression
        |
        expression '/' expression
        |
        expression '%' expression
        |
        expression '+' expression
        |
        expression '-' expression
        |
        '(' expression ')'
        ;
%%
int main(void) {
yyin = fopen("in.txt", "r");
yyparse();
fclose(yyin);
return 0;
}
1

1 Answers

2
votes

I can't say from just looking at this, but a good way to find out is running your parser when the environment variable YYDEBUG is 1, like in (unix):

YYDEBUG=1 ./a.out 

You should get a detailed list of the steps the parser takes and the tokens it receives from the lexer (where the error is in most cases, like in your example: how does the parser ever get the '=' sign and the operators?). Compare this with the y.output file you get when you run yacc -v