I am writing a recursive descent parser for config files. These are mostly similar to ini files. Here is the language in some sort of EBNF-like form:
document ::= { category }
category ::= title {entry}
title ::= "[" <name> "]"
entry ::= <key> ":" <value>
Here is an example of a file that should give a parsing error at the end:
[Category1]
key1:val1
key2 :val2
key3 : val3
[Category2]
key4: val4
this line right here should produce an error
All of the examples I could find online would parse the input until an invalid symbol is reached, then quit without printing a useful error message. I have a working parser that follows this behavior, but I am not sure how to implement useful error reporting.
For example, a document
consists of zero or more categories. What do I do when the first two categories are parsed without error but the third contains a syntax error? What if the input ends after the second category and I am unable to parse a third category because there are no tokens left (this should not produce an error message)? How do I differentiate between these situations? The invalid line could be made valid in two ways: becoming an entry or becoming a title. This confuses me.
I would like my program to print something like line 9: expected entry or title
when it reaches the last line of the above input. How do people normally implement error messages in recursive descent parsers?