early 14c., gramarye (late 12c. in surnames), from Old French gramaire "learning," especially Latin and philology, "grammar, (magic) incantation, spells, mumbo-jumbo," "irregular semi-popular adoption" [OED] of Latin grammatica, from Greek grammatike tekhne "art of letters," with a sense of both philology and literature in the broadest sense, fem. adjective from gramma "letter," from stem of graphein "to draw or write" (see -graphy). An Old English word for it was stæfcræft.
Form grammar is from late 14c. Restriction to "rules of language" is a post-classical development, but as this type of study was until 16c. limited to Latin, Middle English gramarye also came to mean "learning in general, knowledge peculiar to the learned classes" (early 14c.), which included astrology and magic; hence the secondary meaning of "occult knowledge" (late 15c.), which evolved in Scottish into glamor (q.v.).
A grammar school (late 14c.) originally was "a school in which the learned languages are grammatically taught" [Johnson, who also has grammaticaster "a mean verbal pedant"]. In U.S. (1842) the term was put to use in the graded system for "a school between primary and secondary where English grammar is taught."
The rules for standard use of words. A grammar is also a system for classifying and analyzing the elements of language.