I'm writing a lexical analyzer and I would like it to output meaningful and accurate error messages. Like the exact line and column where errors occurred. What's the appropriate way of handling errors when writing a lexical analyzer?
What tools are you using for lexical analysis? I know a little Flex, I'll just write it down what I know, maybe its helpful for you or someone else.
Flex is a tool used commonly for generating lexical analyzers. Instead of writing one from scratch, you only need to tell it how your tokens are (using regular expressions), and it will generate a C program which gives a stream of tokens from the input character stream.
Its easy to learn, here's a good tutorial. After learning it, you will be able to report errors easily.
Regarding line numbers, Flex defines a variable called yylineno which holds the current line number. For column numbers, this might help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With