Context-free grammar (CFG) is a formalism used to describe the syntax or structure of programming languages and other formal languages. It consists of a set of production rules that define how symbols (non-terminals and terminals) can be combined to form valid strings in the language.