Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
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? –  delnan 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? –  delnan Oct 30 '11 at 21:32
    
@Johanes That's the reverse of the situation in the question. –  NickC Oct 30 '11 at 22:11
1  
@ Johannes Schaub: What rule makes "int;" valid? The grammar defines the syntax. –  Casey Patton Oct 30 '11 at 22:18

3 Answers 3

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).

share|improve this answer

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.

share|improve this answer
    
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...

Meaning:

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.

share|improve this answer
6  
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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