How to get a complete address from database

I’d like to implement reverse geocoding from OSM database.

  1. From lat,lon → node
  2. Node → way (Current_way_nodes table)
  3. From Way Id, I can get some tags from current_way_tags.

My questions is how can I composite these tags into a complete address.
May be the format are like ‘door number, street, city, state(province), country’.

The current_way_nodes have ‘name’ tag, some time the name is a street name, other time it will be a city name. How do I know them exactly.

Thanks a lot!