Reputation: 1048
I have already seen the explanation of the difference between syntax and semantics, such as this What is the difference between syntax and semantics? But is there any difference between "grammar" and "syntax" when we discuss compiler?
Upvotes: 3
Views: 1682
Reputation: 915
Example of Clite grammar (concrete syntax statements) in EBNF format:
Program ::= int main ( ) { Declarations Statements }
Declarations ::= { Declaration }
Declaration ::= Type Identifier [ [ Integer ] ] { , Identifier [ [ Integer ] ] }
Type ::= int | bool | float | char
Statements ::= { Statement }
Statement ::= ; | Block | Assignment | IfStatement | WhileStatement
Block ::= { Statements }
Example of Clite abstract syntax rules:
Assignment = Variable target; Expression source
Expression = Variable | Value | Binary | Unary
Binary = Operator op; Expression term1, term2
Unary = Operator op; Expression term
Variable = String id
Value = Integer value
Operator = + | - | * | / | !
A good explanation is provided in the chapter "Linking Syntax and Semantics" of Programming Languages: Principles and Paradigms by Allen B. Tucker and Robert E. Noonan (link here)
While the two (grammar or concrete syntax, vs abstract syntax) are, in a sense, redundant, in another sense they are not. That is, the concrete syntax tells the programmer concretely what to write in order to have a valid program in language X. However, the abstract syntax allows valid programs in language X and language Y to share common abstract representations. Ideally, an interpreter or run-time system for a program worries .less about how certain ideas, like loops, are expressed concretely and more about what specific computational requirements are to be conveyed out of the expression and into the run-time environment for the program. That is the role of the abstract syntax: to provide a link between syntax and semantics, between form and function. (p51)
I recommend reading the full chapter for a better understanding of the role and usage of both grammar and abstract syntax in the context of compilation.
Upvotes: 0
Reputation: 370465
A grammar is a series of productions that generate the valid "words" of a language. It is a way to specify the syntax of a language. Another way to specify the syntax would be using plain English, but that would end up being very verbose for non-trivial languages if you want it to be precise enough to serve as a specification.
As an example consider the following text:
A program is a series of zero or more statements.
A statement is either the keyword "var", followed by an identifier, followed by a semicolon; an identifier followed by "++" or "--", followed by a semicolon; or the keyword "while", followed by an identifier, followed by the keyword "do", followed by zero or more statements, followed by the keyword "end".
This describes the syntax of a very simple programming language, but it is not a grammar. Here is a grammar that describes the same language:
program ::= statement*
statement ::= "var" ID ";"
| ID "++" ";"
| ID "--" ";"
| "while" ID "do" statement* "end"
Upvotes: 3