Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Syntax versus Semantics

 
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure this is the right forum for this question... but what is the difference between syntax and semantics of a programming language?
Any examples?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do these words mean when referring to any language?
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The term syntax referes to grammatical structure whereas the term semantics refers to its meaning. Syntatically valid structure does not imply it to be semantically valid. For example, the sentence "egg reads books" is a valid sentence but it makes no sense.
In context of a programming language,a program with syntax error cannot be compiled.A program with semantics error can be compiled(not always true) and run but would give incorrect result.
Consider the following piece of code:

In this code, a missing semicolon (a = 2) is a syntax error but assigning c as an integer is an example of semantic error since the value returned by b/a expression will most likely to be float.
Most of the compilers are unable to catch semantic errors though Java does catch some of such errors like type conversion.
 
nate saider
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dirk:
I guess that is a matter of semantics. (I jest)
Snigdha:
That's great. That was very clarifying (especially the code example). Thanks.
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nate,
Plainly speaking:
1) If you make an error with the syntax, you'll have the compiler complain about it.
2) If you make an error with the semantics, you'll have people complain about it (especially your boss).
Ex Animo Java!
-- Val
 
Are you here to take over the surface world? Because this tiny ad will stop you!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic