The rules that describe correctly structured programs in a language.
According to the Merriam-Webster dictionary syntax means
- the way in which linguistic elements (as words) are put together to form constituents (as phrases or clauses);
- the part of grammar dealing with this.
Dictionary.com is more elaborate and defines syntax as:
Linguistics: a. the study of the rules for the formation of grammatical sentences in a language. b. the study of the patterns of formation of sentences and phrases from words. c. the rules or patterns so studied: English syntax. d. a presentation of these: a syntax of English. ** e. an instance of these: the syntax of a sentence.
Computers: the grammatical rules and structural patterns governing the ordered use of appropriate words and symbols for issuing commands, writing code, etc., in a particular software application or programming language.
Wikipedia says: the syntax of a programming language is the set of rules that define the combinations of symbols that are considered to be correctly structured programs in that language.
In linguistics, a Grammar is a concept that includes syntax. However, in the cases that are relevant for meta-programming they can be used interchangeably. We will use them as synonyms.
In programming languages a further subdivision can be made:
- Lexical syntax defines the form of the lowest level textual items such as keywords, numeric constants, and string constants.
- Context-free syntax defines the global structure of statements, procedures and modules.
A Parser checks that a text in language L indeed adheres to the syntax rules of language L. There are two possible answers:
- Yes. A ParseTree is returned that shows how the text adheres to the syntax rules.
- No. Error messages pin point the location where the text deviates from the syntax rules.