difference between syntax and semantic errors
C Programming c programming notes for gate gate cse c programming questions

Difference Between Syntax and Semantic Errors – [ Interview ]

Syntax and Semantic in Programming Language

This tutorial covers the basic introduction of syntax and semantic with example. Difference between syntax and semantic errors is also explained.

Some basic definition types questions are generally asked in Technical Interview.

Here in this post we have answer these questions are explained properly. Students are kindly requested to read the tutorial till the end.

Syntax and semantic these are related terms in context of a programming language.

Syntax and semantic both have equal importance for any language to make a program in that programming language more user friendly and understandable.

So let’s first understand the meaning of syntax and semantic.

Frequently Asked Questions

By the end of this tutorial students will be able to answer the following questions

  • What is syntax ?
  • What is semantic ?
  • What is difference between syntax and semantic ?
  • What is concrete syntax?
  • What is abstract syntax ?
  • What is difference between logical error and run time error ?
  • What is difference between compiler and interpreter?

So let’s start with introduction with syntax.

What is Syntax?

In context of a programming language Syntax refers to the structure of the program without concerning about it’s meaning.

Syntax gives the emphasis on design or layout of the program. How the program looks?

Syntax also involves the validation of  sequence of symbols and the instruction used in the program.

When compiler compiles a program the it perform syntax analysis tasks. Compiler perform

Lexical analysis of syntax is performed to define the rules for identifiers, literals, punctuators and operator

Concrete syntax specifies the representation of the programs with the help of lexical symbols such as it’s alphabets.

Abstract syntax is used by compiler just for the important program information.

What is Semantic ?

Semantics term in a programming language is used to figure out the relationship among the syntax and the model of computation used in the program

Semantic gives the emphasis on the interpretation of and make it easy for the programmer to understand the program and predict the output.

In any any language Semantics deals with the meaning of a sentence.

In programming let us understand syntax and semantic with the help of an example.

Consider a function abc() as shown below. If call a function that can only work on two values of a ‘char’ variable.

 abc(char d){

if(d == ‘y’){

//DO SOMETHING

}

else if(d == ‘n’){

//DO ANOTHERTHING

}

}

And we call this function with some other value:

abc(‘a’);

Now we will understand the difference between syntax and semantic errors here in this section.
Syntactic errors or syntax errors are also known as compilation errors are caused by violation of the grammar rules of the language.
The compiler detects, isolate these errors and give terminate the source program after listing the errors.
Some of the common syntactic errors in c programming language are
• missing or misplaced.
• missing return type for a procedure.
• missing or duplicate variable declaration.
Semantic errors are logical errors. If there is a semantic error in a program, it will run successfully, in the sense that the computer will not generate any error messages, but it will not do the right thing.
The problem is that the meaning of the program (its semantics) is wrong. Identifying semantic errors can be tricky because it requires working backward by looking at the output of the program and trying to figure out what it is doing.
difference between syntax and semantic errors

Difference Between Run Time Error and Logical Error

Run-time errors: Errors such as mismatch of data types or array out of bound error are known as runtime errors.
Run time errors are generally go undetected by the compiler so programs with run-time error will run but produce erroneous results.
Logical errors: These are the errors related with the logic of the program execution.
These errors are not detected by the compiler and are primarily due to a poor understanding of the problem or a lack of clarity of hierarchy of operators. Such errors cause incorrect result.

Difference Between Compiler and Interpreter

As we discussed the difference between syntax and semantic errors in previous section.
These are two types of language translators. A compiler converts the source program (user-written program) into an object code (machine language by checking the entire program before execution.
If the program is error free, object program is created and loaded into memory for execution.
A compiler produces an error list of the program in one go and all have to be taken care even before the execution of first statement begin.
It takes less time for execution.
An interpreter is also a language translator that translates and executes statements in the program one by one.
It work on one statement at a time and if error free, executes the instruction before going to second instruction.
Debugging is simpler in interpreter as it is done in stages. An interpreter takes more time for execution of a program as compared to a compiler.

Conclusion and Summary

In this tutorial we have discussed several basic questions related to syntax and semantic.

I hope that after reading this tutorial students will be able to answer the questions discussed in the tutorial.

I hope that this tutorial will assist the student to understand the concept of syntax and semantic and also the difference between syntax and semantic errors.

I kindly request to readers please give your feedback and suggestion. If you find any mistake in this tutorial then comment.

All the Best !!

Keywords: Syntax error, semantic error, logical error, runtime error, compiler, interpreter.

Leave a Reply

Your email address will not be published. Required fields are marked *