Is Syntax and semantics different? What is difference between syntax and semantics of programming language?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people's questions, and connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Sweta
The syntax of a programming language, broadly speaking, is that set of rules and writing conventions that allow the formation of correct programs in a language, from the point of view of ‘presentation’ only.
That is, syntax has nothing to do with ‘meaning’ Or run-time behavior of a program.
Although syntax is concerned only with the form of a program, it is inextricably tied to semantics which is the meaning of the program.
Usually, semantics is defined in terms of program’s run-time behavior: what happens when the program is executed with a certain set of inputs, what statements are executed, what values are assigned to the variables, and what output is produced.
Hello,
Syntax is nothing but a set of strict rules which are never meant to be broken otherwise you’ll encounter errors while semantics is like grammar in other sense you can all it logic. Every statement you write as a code it should define itself, it should give it a meaning. Code will surely work if syntax is correct but semantics is illogical and wrong. Let me demonstrate with examples:
Here’s a sentence ‘A car’:
The syntax is correct but the semantics is wrong. Syntax is that A should be used before a consonant sound but at the same time A car doesn’t contain any meaning, it’s illogical.
Here’s another sentence ‘A car was flying on the road’:
Syntax is correct as it is in accordance of the grammar but the semantics is wrong because this sentence is logically wrong and is without meaning, as we all know that a car can’t fly. Grammar rules are followed but still meaning less.
Take another one ‘A car was on the road’:
Syntax and semantics both are correct as it is grammatically correct and it also gives complete meaning.
I Hope you are clear with your doubts.