What comes to mind when you think of grammar? English class and lots of rules? Memorizing parts of speech, like nouns and verbs? Diagramming sentences?
All are valid answers.
But let's take a look at grammar from the point of view of the linguist.
To do that, we need to consider the three distinct types of grammar which are part and parcel of every language: Mental, Descriptive, and Prescriptive.
Mental Grammar
Mental grammar is what the speaker of a language knows, often implicitly, about the grammar of that language. This has also been called linguistic competence or competence grammar. All these terms describe the complex system which allows a speaker to produce language that other speakers can understand. It includes sounds, vocabulary, the order of words in sentences, and even the appropriateness of a topic or a word in a particular social situation. Most of us carry this knowledge around in our heads and use it without much reflection. One way to clarify mental or competence grammar is to ask a friend a question about a sentence. They may not know why something is correct, but they will know if it is correct. One of the features of this type of grammar is this incredible sense of "correctness," and the ability to "hear" when something "sounds odd" in a language.
In the case of native speakers, we call this ability "native intuition." But even language learners who have achieved a high level of competence in a second language will be able to give similar intuitive response (even if they can't explain the rules).
Descriptive Grammar
The second type of grammar is descriptive grammar: a description of what speakers know intuitively about a language. Linguists try to discover the underlying rules of mental or competence grammar and describe them objectively. So, descriptive grammar is a model of competence grammar and as such is based on the best efforts of a linguist (and subject to criticism from other linguists). No matter how skilled a linguist is, describing grammar is an enormous task. In the first place, the knowledge is incredibly vast and complex. Secondly, the language itself is changing even while it's being described. Finally, the same data can be categorized in different but equally correct ways in order to arrive at generalizations. The ultimate goal of descriptive grammar is to form generalizations about a language that accurately reflect the rules that speakers have in their heads.
Prescriptive Grammar
Getting back to what most people think of as grammar - the rules we learn aren't meant to describe language at all. They're meant to prescribe and judge language as "good" or "bad." This kind of grammar - the third type - is called prescriptive grammar, because of its judgmental perspective. The contrast to descriptive grammar is marked. Descriptive grammar ultimately accepts the language that a speaker uses in an effort to describe it, recognizes that there may be several dialects that are used by different speakers, and understands that any one speaker may choose different styles for different situations. Meanwhile, prescriptive rules are rigid and subject to enforcement. Prescriptive grammar seeks to make all speakers conform to one standard in all situations. That tends to be a very formal level of language all the time.
Prescriptive grammar is the type we learned in school, when we memorized parts of speech and diagrammed sentences. How did it get to be accepted in schools, and why is it often disregarded, even by native speakers?
During the 17th and 18th centuries in Europe, Latin was considered "the perfect language." It was used by the educated classes. The argument for the perfection of Latin was reinforced by the fact that Latin had become a written language and, consequently, it had stopped changing in the normal ways that spoken languages do. So, the rules were also fixed. For many writers during that period, the rules of Latin were held as a standard for all languages - including English.
The problem was that English had a different origin and very different constructions. For example, how many times have you heard the prescriptive rule, "Never end a sentence with a preposition?"
This is a Latin rule.
But it doesn't quite apply to English. It sounds very formal - and even strange - when this Latin rule is enforced.
Would you say, for instance, "What are we waiting for?" Most of us would.
And most of us would prefer that to, "For what are we waiting?"
Still, "good" language is a requisite for social mobility, even when it is very dissimilar to the mental or descriptive grammar of a language.