Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling errors in lexical analysis

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?

like image 278
mrk Avatar asked May 21 '26 09:05

mrk


1 Answers

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.

like image 132
max Avatar answered May 25 '26 20:05

max



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!