Grammar is the study of the rules which govern the use of a language. That set of rules is also called the language's grammar, and each language has its own, distinct grammar. An expert in that field of study is called a grammarian. Grammar falls within a larger set of studies which is called linguistics.
Speakers of a language follow that language's grammar as a common convention that makes the language coherent. Violation of the grammar makes one's speech ambiguous and difficult to understand. Because of its importance in facilitating communication, grammar is a primary focus in education from a young age through advanced learning.
Most languages' grammars developed naturally over time through usage, as early languages lacked formal, written rules. Each speaker was required to develop an innate sense of grammar by observing usage.
Formal grammars are codifications of the usages that were developed by observation. As the rules became established and developed the concept of grammatical correctness arose. This can often create a gulf between contemporary usage and the form accepted as correct.
Each computer programming language also has a formal grammar which defines its use.
Artificial languages are more common in the modern day, whether they have been designed to aid human communication (Esperanto), created as part of a work of Fiction, (Klingon Language and Elvish language) or used for the purpose of computer programming (Java programming language). Each of these artifical languages have their own grammar.
There are a number of types of grammar that linguists recognise.
Descriptive Grammar is the method of describing the rules used by a language so people can check whether specific rules have been broken or not.
Generative Grammar is a way of describing a language so that if the rules are followed, then grammatically correct forms of that language can be constructed.
See the following grammatical terms: