0

I have written a yacc file. currently error function looks like this :

  void yyerror(char *s) {
       fprintf(stderr, "line %d: %s\n", yylineno, s);
   }

Now while giving input to it, if there is a left parenthesis is missing it gives simple 'syntax' error.

  1. Can you please suggest, how can I make my custom error messages for these type of errors ?

  2. And how I can proceed further even after getting this error ?

Thank you so much for your advices..

Brian
  • 3,850
  • 3
  • 21
  • 37
ronny
  • 11
  • You sure error is shown in these lines ? – ameyCU Sep 17 '15 at 13:56
  • Are 'stderr' and 'yylineno' global variables? – pistachiobk Sep 17 '15 at 14:21
  • @ameyCU : This is error function which I am using in my 'yacc code'. – ronny Sep 17 '15 at 15:12
  • @ameyCU : and for example if I give input as : A = ((B),(C)) this is correct input. But when I give A = (B),(C)) this is wrong , and my code says syntax error which is correct . But I want to custom that message to lets say 'Left parenthesis is missing' . How to do that stuff in 'yacc' , can you please enlighten me . thanks a lot for your replies. – ronny Sep 17 '15 at 15:13
  • @pistachiobk : 'stderr' is global variable and 'yylineno' is current line number where the error will occur after giving input to 'yacc' code. – ronny Sep 17 '15 at 15:16

1 Answers1

0

You can't. At least not simply. A syntax error arises when the current state and the next token do not yield a legal continuation of the parse. It might be as simple 'as the next token must be a '('', but in that case your language would probably be poorly defined, as the '(' would probably be redundant. The more usual case is that one of a number of tokens is a legal continuation: and, due to the state-compressing action of LALR(1) parser-generators, it isn't always possibly to actually enumerate those tokens once you get to the point in question.

user207421
  • 305,947
  • 44
  • 307
  • 483