What is actually 'semantics of a language' ?

N

Nitin Bhardwaj

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..
 
R

Richard Bos

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
 
U

Usman Muzaffar

Nitin Bhardwaj said:
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
 
H

Harti Brandt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top