Syntax and Semantics are very significant terms relating to any programming language. The syntax in a programming language involves the set of permitted phrases of a language whereas semantics expresses the associated meaning of those phrases.
There are some relationships between syntax and semantics where each semantic element is linked to at least one syntactic rendition and the other one assures that each syntactic representation has a distinctive meaning.
Content: Syntax Vs Semantics
|Basis for comparison||Syntax||Semantics|
|Basic||Permitted phrases of a language.||Interpretation of the phrases.|
|Errors||Handled at the compile time.||Confronted at runtime.|
|Relation||Syntactic interpretation must have some distinctive meaning.||Semantic component is associated with a syntactic representation.|
Definition of Syntax
The Syntax of a programming language is used to signify the structure of programs without considering their meaning. It basically emphasizes the structure, layout of a program with their appearance. It involves a collection of rules which validates the sequence of symbols and instruction used in a program. The pragmatic and computation model figures these syntactic components of a programming language. The tools evolved for the specification of the syntax of the programming languages are regular, context-free and attribute grammars.
However, what is the use of grammar in this aspect? The Grammars generally are the rewriting rules whose purpose is to recognize and generate the programs. Grammar does not rely on the computation model instead used in the description of the structure of the language. The grammar contains a finite set of grammatical categories (such as noun phrase, verb phrase, article, noun, etc), solitary words (elements of the alphabets) and the well-formed rules to specify the order within which components of the grammatical categories should appear.
Syntax analysis is a task performed by a compiler which examines whether the program has a proper associated derivation tree or not.
The syntax of a programming language can be interpreted using the following formal and informal techniques:
- Lexical syntax for defining the rules for basic symbols involving identifiers, literals, punctuators and operators.
- Concrete syntax specifies the real representation of the programs with the help of lexical symbols like its alphabet.
- Abstract syntax conveys only the vital program information.
Types of grammars
- Context-free grammar is pervasively used to figure out the whole language’s structure.
- Regular expressions describe the lexical units (tokens) of a programming language.
- Attribute grammars specify the context-sensitive part of the language.
Definition of Semantics
Semantics term in a programming language is used to figure out the relationship among the syntax and the model of computation. It emphasizes the interpretation of a program so that the programmer could understand it in an easy way or predict the outcome of program execution. An approach known as syntax-directed semantics is used to map syntactical constructs to the computational model with the help of a function.
The programming language semantics can be described by the various techniques – Algebraic semantics, Axiomatic semantics, Operational semantics, Denotational semantics, and Translation semantics.
- Algebraic semantics interprets the program by defining an algebra.
- Axiomatic semantics determine the meaning of a program by building assertions about an association that detain at each point in the execution of the program (i.e. implicitly).
- Operational semantics compares the languages to the abstract machine, and the program is then evaluated as a sequence of the state transitions.
- Denotational semantics expresses the meaning of the program in the form of a set of functions operating on the program state.
- Translational semantics focuses on the methods used for translating a program into another language.
Key Differences Between Syntax and Semantics
- Syntax refers to the structure of a program written in a programming language. On the other hand, semantics describes the relationship between the sense of the program and the computational model.
- Syntactic errors are handled at the compile time. As against, semantic errors are difficult to find and encounters at the runtime.
- For example, in c++ a variable “s” is declared as “int s;”, to initialize it we must use an integer value. Instead of using integer we have initialized it with “Seven”. This declaration and initialization is syntactically correct but semantically incorrect because “Seven” does not represent integer form.
The syntax of a programming language is a collection of rules to specify the structure or form of code whereas semantics refers to the interpretation of the code or the associated meaning of the symbols, characters or any part of a program.