473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What is actually 'semantics of a language' ?

Hello all,

I am puzzled by the term 'semantics' !
Well I know about 'syntax of a language' - as defined by its
associated grammar

Then what is this semantics?
What does the C Compiler checks during semantic analysis?
And does it produce error OR warnings when it encounters a semantic
mistake?

Somebody pointed out in reply to my previous post that the following
is a semantic error:

#include <stdio.h>
char *f();
int main(void)
{
char *p = f();
return 0;
}

char *f()
{
return "FooBar";
}
AFAIK, this is a Run-time bug..since the string literal will be popped
off the stack when f() returns, but is it a semantic error ? If yes,
how ?

Thanks..
Nov 14 '05 #1
4 1545
ni*************@hotmail.com (Nitin Bhardwaj) wrote:
I am puzzled by the term 'semantics' !
Well I know about 'syntax of a language' - as defined by its
associated grammar

Then what is this semantics?
Meaning. Like "syntax", the term was borrowed from natural linguistics
with more or less the same meaning as it has there.
What does the C Compiler checks during semantic analysis?
Create the actual code.
And does it produce error OR warnings when it encounters a semantic
mistake?
It can't, usually. Semantic errors most often involve code that is
theoretically correct, but does not do what the programmer intended it
to do. For example, writing

if ((in=getch()!=EOF)) ...

when you meant

if ((in=getch())!=EOF) ...

It's correct C; it's perfectly well-defined; it just doesn't do what was
expected.
#include <stdio.h>
char *f();
int main(void)
{
char *p = f();
return 0;
}

char *f()
{
return "FooBar";
} but is it a semantic error ?


No. It's not an error at all. "FooBar" is a string literal; string
literals have static duration; returning a pointer to one is perfectly
safe. ITYM something like this:

char *f()
{
char a[]="FooBar";
return a;
}

This creates a local array; initialises it; and returns a pointer to the
local array, not to the string literal. That _is_ an error, but is it
semantic? I don't know. It's a borderline case. The term "semantic" is
not strictly defined, anyway. Be that as it may, IIRC some compilers
will warn about this.

Richard
Nov 14 '05 #2
"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
Hello all,

I am puzzled by the term 'semantics' !
Well I know about 'syntax of a language' - as defined by its
associated grammar

Then what is this semantics?
What does the C Compiler checks during semantic analysis?
And does it produce error OR warnings when it encounters a semantic
mistake?

Somebody pointed out in reply to my previous post that the following
is a semantic error:

#include <stdio.h>
char *f();
int main(void)
{
char *p = f();
return 0;
}

char *f()
{
return "FooBar";
}
AFAIK, this is a Run-time bug..since the string literal will be popped
off the stack when f() returns, but is it a semantic error ? If yes,
how ?

Thanks..

You're overanalyzing.

Simplest definition of "semantic" is "meaning".
When someone says some bit of code is semantically
incorrect, they mean it compiles, but doesn't do
what the author intended. In other words, it's nothing
more than what you call "a run-time bug".

No compiler does "semantic analysis"; a compiler can't
tell a "semantic error"; if it could, there'd soon be no
more bugs in software, and we'd all either fabulously
rich or permanently unemployed (or both). :)

hth,
-usman
Nov 14 '05 #3
On Fri, 8 Jul 2004, Nitin Bhardwaj wrote:

NB>Hello all,
NB>
NB>I am puzzled by the term 'semantics' !
NB>Well I know about 'syntax of a language' - as defined by its
NB>associated grammar
NB>
NB>Then what is this semantics?

Given the line

c = a + b;

the semantic of this line is that you want to add the contents of the two
variables a and b and assign the result to the variable c (taking into
account the specific rules for the types of a, b and c). The syntax
definition, on the other hand, says nothing about the meaning of '=' and
'+'. If you go to a language where operators can be overloaded, you may
define, for example, '+' to clear the screen, assign -1 to it's left
operand and return a 0. The syntax is the same, the semantic is entirely
different.

NB>What does the C Compiler checks during semantic analysis?

Given again the line above, the compiler would, for example, check that
not both variable are pointers. This isn't done at the syntax level. The
syntax analysis step formally just checks whether the input conforms to
the rules specified by the BNF description of the language.

NB>And does it produce error OR warnings when it encounters a semantic
NB>mistake?

Sure. See above.

NB>
NB>Somebody pointed out in reply to my previous post that the following
NB>is a semantic error:
NB>
NB>#include <stdio.h>
NB>char *f();
NB>int main(void)
NB>{
NB> char *p = f();
NB> return 0;
NB>}
NB>
NB>char *f()
NB>{
NB> return "FooBar";
NB>}
NB>
NB>
NB>AFAIK, this is a Run-time bug..since the string literal will be popped
NB>off the stack when f() returns, but is it a semantic error ? If yes,
NB>how ?

Well, I would assume that is correct code, because "FooBar" isn't an
automatic variable and will not be popped off from anywhere. Perhaps
the poster had something in mind like:

char *
foo(void)
{
char c;

c = 'a';
return (&c);
}

while this is syntactically correct, it isn't semantically. There is a
rule somewhere (I hope :-) that you should not return the address of an
automatic variable.

Note, that the semantic is usually given as prosa and the syntax in some
form of BNF. A notable exception is the Algol standardisation process that
tried to come up with a formal description of the semantic. You may try to
lookup this, but be warned: this is _VERY_ hard to understand.

Besides syntax and semantic errors I would also identify a class of logic
errors like:

if (a == 0);
func();

This is syntactically and semantically correct, albeit surely wrong and
a nice compiler will warn you (it once took me two days to find that
error).

harti
Nov 14 '05 #4
In 'comp.lang.c', ni*************@hotmail.com (Nitin Bhardwaj) wrote:
I am puzzled by the term 'semantics' !


The semantic of semantic is semantic!

No kidding, it means ... the meaning of!

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
56
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
35
by: GTO | last post by:
I do not believe that C# is the future of C++. I also do not believe that adding two thousand new library functions to the standard library is the future of C++. But what is the future of C++? Is...
19
by: G.Ashok | last post by:
Hi All, What is your weightage of the 3 characteristics of Object-Oriented Programming i.e. Inheritance, Encapsulation and Polymorphism. for a total of 100% Please quote your values and let's...
17
by: Mike Hofer | last post by:
While I'd toyed with C, C++, and Java over the last 20 years or so, my principal language has been BASIC, QBASIC, then Visual Basic, and finally Visual Basic .NET. But lately, I've been using C#...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
34
by: emrahayanoglu | last post by:
Hello Everyone, Now, I'm working on a new web framework. I tried many test on the other programming languages. Then i decided to use python on my web framework project. Now i want to listen...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.