I have run into a problem while writing a parser. The following functions are all calling the GetSymbol() function. Also the functions call eachother. Body() would be calling Statement(), Statement() would call Expression() and so on.
Problem is, in any function the list of symbols could be empty. I thought it was unnecessary to add extra code to each function. Instead I added an try catch to the program.
Isn't it wrong to use an exception this way because if GetSymbol runs out of symbols it is expected behaviour.
Should I avoid throwing the exception?
List<Symbol> symbols;
private void Term()
{...}
private void Expression()
{...}
private void Statement()
{...}
private void Body()
{...}
private Symbol GetSymbol()
{
if (symbols.Count > 0)
else
throw new OutOfTokenException();
}
public void Program()
{
try
{
while (Accept(Symbol.LBRACE))
Body();
}
catch (OutOfTokenException ote)
{
Output("Unexpected end of file");
}
}
}