I am using bison+flex to parse file. On error yyerror() is invoked. How can I get the line number or string that is violating the rules, to print it with the error message?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Line number is easy:
yylinenois the line number. Specify%option yylinenoat the top of your .l file.Well, almost easy. Lookahead can sometimes make the line number be off by one. Instead of saying something like “Error occurred at line #xxx” you might want to say that the error occurred near line #xxx.
As far as the rest, it’s up to you. You are going to have to capture the not-quite valid syntax and call the appropriate warning or error handler. See the O’Reilly “flex & bison” book for details; it has an entire chapter on error messages. An entire chapter is a bit too much to reproduce at this Q&A site.