Extracted emit_error function
Added error in case of an unexpected end of token stream
Added paramater for end