I understand the difference between syntax and semantics -

Syntax: how the symbols are combined to form a valid expression or statement.
Semantics: the meaning of those symbols that form an expression or statement.

But what is the grammar? For example: sometimes I hear people say that some construct is "grammatically incorrect but syntactically it is correct". What does it mean?

  • FWIW, this sounds like nonsense to me. If the language's grammar accepts the piece of code, it conforms the syntax. Perhaps someone has a very broad (and nonstandard) definition of "syntax". Context/source? – user7043 Oct 30 '11 at 20:35
  • @delnan. Not true. For example int; is grammatically valid, but syntactically ill-formed in C++. The grammar has no problem with this code, but syntax constraints require that a name is provided if the first part of a declaration contains no class-specifier or enum-specifier or, in C++11, friend-specifier. – Johannes Schaub - litb Oct 30 '11 at 21:16
  • @JohannesSchaub-litb: Care to cite the part of the grammar that makes this valid? – user7043 Oct 30 '11 at 21:32
  • @Johanes That's the reverse of the situation in the question. – Nicole Oct 30 '11 at 22:11
  • 2
    @ Johannes Schaub: What rule makes "int;" valid? The grammar defines the syntax. – Casey Patton Oct 30 '11 at 22:18

A grammar is a set of rules that define the syntax for a particular language.

When people are talking specifically about a parser (especially one generated with a parser generator like yacc, Byacc, ANTLR, etc.), they may do a bit more hair-splitting, and talk specifically about those syntactical rules that are encoded using the generator's rules, vs. those parts that are enforced separately by code attached to a rule. For example, in C when you define an array, the size you specify for the array must be strictly positive (not zero). The grammar rule might basically say something like:

typename var_name '[' unsigned_int ']'

...and then separately, there would be a bit of code to check that the unsigned_int was non-zero. In this case, it could make some sense to talk about the requirements of the syntax and the grammar separately from each other, with the two having slightly different requirements (that, enforced together, we presume fit the requirements of the language itself).


The difference is fuzzy and not worth worrying about too much.

People will sometimes include context-sensitive constraints under the umbrella of syntactic correctness. The most common example is a type system. Another is Java's "no statements after return" rule. This simplifies formal discussion: the syntax yields a language (a set of sentences/expressions/programs) which is the domain of the semantics; anything else is "not a program", and the semantics need not bother with it.

In contrast, "grammar" typically refers to a method of describing context-free languages (attribute grammars notwithstanding).

The reason it's not worth worrying about much is that type systems are as often considered the "static semantics" of a language as they are a "syntactic discipline for correctness". And sometimes a language doesn't quite have a proper context-free grammar; C, for example, must feed information from the parser back into the lexer.

Pragmatically, anyone who relies on a distinction between "syntactic" and "grammatical" had better say so and explain what they mean.

  • I don't understand why the difference is fuzzy. The grammar describes the syntax. – Casey Patton Oct 31 '11 at 0:59
  • 1
    @Casey, no, according to one usage of the word "syntax", the grammar specifies a superset of the syntax. – Ryan Culpepper Oct 31 '11 at 1:25

A grammar is a set of rules to define a language. Rather, the grammar describes the syntax and semantics. A language might have two different grammars:

  • Syntax grammar (a set of rules that describes the ordering of symbols in the language)
  • Semantics grammar (a set of rules describing the valid semantic placement and use of those symbols)

For example, a part of the grammar in C might look something like:

if statement -> if_keyword "(" expression ")" if_block
if_keyword -> "if"
logical_statement -> some other stuff here...


an if statement is made of an if keyword followed by a parenthesis followed by an expression followed by a parenthesis followed by an if block
an if keyword is ....

Take a look at this way of defining a grammar. If you're really curious about grammars, take a look at GNU Bison, which is basically a tool for describing the grammar of a language.

The "grammatically incorrect but syntactically correct" doesn't make too much sense. Maybe they're referring to a grammar that describes the semantics of a language. It would certainly make more sense to just say "not semantically correct" though.

  • 7
    No, grammar does not define semantics and should never do it, unless it is something exotic, like contextfreeart.org – SK-logic Oct 31 '11 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.