473,698 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1562
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()!=E OF)) ...

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.goo gle.com...
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
19038
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
56
3741
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, ...) { variables ... do this or that }
47
9139
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
3331
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 it as good as a programming language can get? Like so many of you, I programmed speech recognizers, image recognition systems, a portion of a chess program, lots of numeric code using STL, and tons of other applications in C++, (even firmware...
19
1803
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 discuss how the people thinking about OOP :-) Regards,
17
2309
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# and I absolutely *love* it. It makes me think more about what I'm doing it before I just spew code into the editor. I'm writing better code than ever. The only thing so far that I don't like about it is the switch construct. I can't do this:
669
25954
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
34
3612
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 all of you. What do you want in that web framework(Easy use of Database, Easy use of XML, GUI Designer, etc...)? I'm wating your answers. Thank you for all answers...!
0
8612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.