0
votes

For example , if I type prin instead of print , I want it to show an error message undefined variables or some syntax error.

Can this be done in Yacc ? I coudldnt find any helpful resources

3

3 Answers

1
votes

To amplify @EJP's answer: By "type prin instead of print," I infer that you're talking about an input program to the yacc-generated parser, not your .y file that you process with yacc to make a parser.

And in that case, if an identifier is not valid at a particular point in your program, a syntax error will be raised and the yyerror() function (which you can supply) will be called. In that function, you have control over the error message produced -- and access to all the semantic information you have collected about the program. For instance, consider the case where prin is a legitimately defined variable, just out of place syntactically?

1
votes

'Undefined variable' is not a syntax error. Yacc will not produce it. It comes out of the semantic analysis phase, which you have to write from scratch. You can call yyerror() from in there of course.

0
votes

Well, You must have a some way to define variables/functions on your input program for the yacc-generated parser, right?

If so, you must need a symbol table in order to store those definitions and later to check if a specific variable is defined or not and the print the desired error message.

Yacc alone, doesn't have support for that. You must develop you own symbol table, and figure out, what is the best way to do this according to your needs.