- Is this legal?
Yes, as long as you follow the Stack Exchange Network Terms of Service and APIs Terms of Use
- Does Stack Overflow provides any such APIs to fetch data programmatically?
Yes, please visit: http://api.stackexchange.com/. Please note that there is a 'Rate Limit' set in order to prevent abuse the Stack Exchange API implements a number of throttles.
Also, as pointed by Deer Hunter in his comment:
What's wrong with using SEDE (Stack Exchange Data Explorer)? There are
already hundreds of queries that explore popularity of language tags. That's data.stackexchange.com