mrk
mrk

Reputation: 3189

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?

Upvotes: 3

Views: 1070

Answers (1)

max
max

Reputation: 4348

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.

Upvotes: 2

Related Questions