473,405 Members | 2,187 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,405 software developers and data experts.

Symantic - syntactic

Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.

Also the grammar of the C language is applied at what stage of the
compilation?
AFAIK following are the stages -

Step I : Lexical Analysis ( Tokenizing > Parsing )
Step II : Syntactical Analysis
Step III : Symantic Analysis
Step IV : Translation of the unit.

I want to know actually what is involved in the synamtic analysis step
!

Thanx in advance..
Nov 14 '05 #1
18 3510

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message news:17**************************@posting.google.c om...
Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.
I may be wrong in the following in the following, but let's try.

{
int *p;
*p = 10;
}

is syntactically correct, but sematically wrong. Storing 10 into an
uninitialized location has got nothing to do with the syntax. Whereas, it is
semantically wrong because it has no meaning.

Also the grammar of the C language is applied at what stage of the
compilation?
I believe at syntax analysis. I think semantic analysis is not required to
be performed by a compiler.
AFAIK following are the stages -

Step I : Lexical Analysis ( Tokenizing > Parsing )
Step II : Syntactical Analysis
Step III : Symantic Analysis
Step IV : Translation of the unit.

I want to know actually what is involved in the synamtic analysis step
!

Thanx in advance..


--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Nov 14 '05 #2
Jim
On Fri, 2 Jul 2004 12:43:46 +0530, "Vijay Kumar R Zanvar"
<vi*****@globaledgesoft.com> wrote:

"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message news:17**************************@posting.google.c om...
Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.


I may be wrong in the following in the following, but let's try.

{
int *p;
*p = 10;
}

is syntactically correct, but sematically wrong. Storing 10 into an
uninitialized location has got nothing to do with the syntax. Whereas, it is
semantically wrong because it has no meaning.

Also the grammar of the C language is applied at what stage of the
compilation?


I believe at syntax analysis. I think semantic analysis is not required to
be performed by a compiler.


I would say, broadly speaking, that it's the difference between the
compiler emitting an error, because the code is syntactically wrong,
or a warning, because the code might be wrong, semantically.

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

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically
semantically
wrong.
int main()
{
int i;
int i;
return 0;
}
Also the grammar of the C language is applied at what stage of the
compilation?
When syntax and semantics are analyzed.
AFAIK following are the stages -

Step I : Lexical Analysis ( Tokenizing > Parsing )
Step II : Syntactical Analysis
Step III : Symantic Analysis
Step IV : Translation of the unit. I want to know actually what is involved in the synamtic analysis step
semantic
!


The standard does not define a 'semantic analysis step'. It defines
eight 'phases of translation'. The definition of Phase 7 states
(among other things) "tokens are syntactically and semantically analyzed",
but does not specifically require a particular order or method.

The standard does define semantics for lexical elements (i.e. tokens
and preprocessing-tokens), and further semantics for subsets of lexical
elements, such as keywords and identifiers.

================================================== ======================
ISO/IEC 9899:1999 (E)

5.1.1.2 Translation phases

1 The precedence among the syntax rules of translation is specified by
the following phases (5)

1. Physical source file multibyte characters are mapped, in an
implementation-defined manner, to the source character set
(introducing new-line characters for end-of-line indicators) if
necessary. Trigraph sequences are replaced by corresponding
single-character internal representations.

2. Each instance of a backslash character (\) immediately followed
by a new-line character is deleted, splicing physical source
lines to form logical source lines. Only the last backslash on
any physical source line shall be eligible for being part of
such a splice. A source file that is not empty shall end in a
new-line character, which shall not be immediately preceded by
a backslash character before any such splicing takes place.

3. The source file is decomposed into preprocessing tokens (6) and
sequences of white-space characters (including comments). A source
file shall not end in a partial preprocessing token or in a partial
comment. Each comment is replaced by one space character. New-line
characters are retained. Whether each nonempty sequence of white-
space characters other than new-line is retained or replaced by
one space character is implementation-defined.

4. Preprocessing directives are executed, macro invocations are
expanded,
and _Pragma unary operator expressions are executed. If a character
sequence that matches the syntax of a universal character name is
produced by token concatenation (6.10.3.3), the behavior is
undefined.
A #include preprocessing directive causes the named header or source
file to be processed from phase 1 through phase 4, recursively. All
preprocessing directives are then deleted.

5. Each source character set member and escape sequence in character
constants and string literals is converted to the corresponding
member
of the execution character set; if there is no corresponding member,
it is converted to an implementation-defined member other than the
null
(wide) character.(7)

6. Adjacent string literal tokens are concatenated.

7. White-space characters separating tokens are no longer significant.
Each preprocessing token is converted into a token. The resulting
tokens are syntactically and semantically analyzed and translated as
a translation unit.

8. All external object and function references are resolved. Library
components are linked to satisfy external references to functions
and
objects not defined in the current translation. All such translator
output is collected into a program image which contains information
needed for execution in its execution environment.

5) Implementations shall behave as if these separate phases occur, even
though
many are typically folded together in practice.

(6) As described in 6.4, the process of dividing a source file's
characters
into preprocessing tokens is context-dependent. For example, see the
handling of < within a #include preprocessing directive.

(7) An implementation need not convert all non-corresponding source
characters
to the same execution character.
================================================== ======================

-Mike
Nov 14 '05 #4
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:kd****************@newsread1.news.pas.earthli nk.net...

The standard does not define a 'semantic analysis step'. It defines
eight 'phases of translation'. The definition of Phase 7 states
(among other things) "tokens are syntactically and semantically analyzed",
but does not specifically require a particular order or method.


I meant to add: also note footnote (5).

-Mike
Nov 14 '05 #5
Nitin Bhardwaj wrote:

Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.

[...]

int compare(int x, int y)
{
if ( x = y ) /* should be "==" */
return(0);
else if ( x < y )
return(-1);
else
return(1);
}

I suppose the following could, technically, qualify as well:

i = i++;

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #6
ni*************@hotmail.com (Nitin Bhardwaj) wrote in message news:<17**************************@posting.google. com>...
Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.
Just a textbook example but consider

int a[10];
a[(float)5] = 23;

Will be correct syntactically but semantically it is incorrect. Also
syntactically

a[10] is correct syntactically but semantically it is wrong, and the
compiler _might_ give you a diagnostic.

Then

(void) 23 + 23;
(void*) p_v + 23;

+ Plethora of others.

Also the grammar of the C language is applied at what stage of the
compilation?
AFAIK following are the stages -

Step I : Lexical Analysis ( Tokenizing > Parsing )
Step II : Syntactical Analysis <<-->> The grammar is _generally_ built up at this state. Step III : Symantic Analysis
Step IV : Translation of the unit.

I want to know actually what is involved in the synamtic analysis step
!

Thanx in advance..


Sort of, but if you want to know really what they are consult the
standard, if you want to know how they are implemented pick up the
Dragon[1].
[1] Compilers Tools and Techniques by Aho, Ullman and Sethi [The above
example is from this book only]

--
Imanpreet Singh Arora
#isingh# #AT# #acm# #DOT# #org#
Nov 14 '05 #7
In article <17**************************@posting.google.com >,
Nitin Bhardwaj <ni*************@hotmail.com> wrote:
Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.
--------
int foo(int i)
{
/*Add 17 to i*/
j+=42;
return i;
}
--------

Also the grammar of the C language is applied at what stage of the
compilation?
AFAIK following are the stages -

Step I : Lexical Analysis ( Tokenizing > Parsing )
Step II : Syntactical Analysis
Step III : Symantic Analysis
Step IV : Translation of the unit.

I want to know actually what is involved in the synamtic analysis step
!


The grammar is used for syntactic analysis, which you have at step II.
(But you're missing preprocessing, which happens between tokenizing and
parsing (actually between tokenizing-for-preprocessing and retokenizing-
for-parsing), and parsing belongs with the syntactic analysis, not
lexical.) The C standard defines various "translation phases" that
I'm not familiar with but my recollection of which doesn't match your
list of steps here, so using these terms in comp.lang.c may cause some
slight confusion.

The result of the syntactic analysis is a parse tree that represents
the structure of the code, and the semantic step takes this parse tree
and decides "what it means". (F'rexample, the parse tree for 'i+=42'
will indicate that it's an addition-assignment operator with arguments
'i' (which is an identifier) and '42' (which is an integer constant),
and the semantic analysis will look up what exactly the identifier
'i' refers to and check that it's valid in that scope. It's up to the
programmer to make sure that it's actually meaningful and consistent
with what should be happening.)

This is more of a compiler question than a C question. comp.compilers
comes to mind as a possibly better place to ask for more details.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Summary: Two authorities disagree, and you'll have to choose between them for
yourself. Or perhaps adopt an entirely different authority, like Lawrence
Welk: "A-one-an'-a-two's complement." --Eric Sosman in comp.lang.c
Nov 14 '05 #8
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote in message news:<2k************@uni-berlin.de>...
"Nitin Bhardwaj" <ni*************@hotmail.com> wrote in message news:17**************************@posting.google.c om...
Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.


I may be wrong in the following in the following, but let's try.

{
int *p;
*p = 10;
}

is syntactically correct, but sematically wrong. Storing 10 into an
uninitialized location has got nothing to do with the syntax. Whereas, it is
semantically wrong because it has no meaning.

Also the grammar of the C language is applied at what stage of the
compilation?


I believe at syntax analysis. I think semantic analysis is not required to
be performed by a compiler.


Then you think wrong. Read your own example or examples posted by others.

--
Imanpreet Singh Arora
i"kaur" #AT# acm #DOT# org
mv kaur singh
Nov 14 '05 #9

"Kenneth Brody" <ke******@spamcop.net> wrote in message
news:40***************@spamcop.net...
Nitin Bhardwaj wrote:

Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong. [...]

int compare(int x, int y)
{
if ( x = y ) /* should be "==" */


This is both syntactically and semantically correct from
a language standpoint. However it might not be what the
coder meant. :-)
return(0);
else if ( x < y )
return(-1);
else
return(1);
}

I suppose the following could, technically, qualify as well:

i = i++;


I think that's a constraint violation, not a semantic violation.
It's certainly not a syntax error.

-Mike
Nov 14 '05 #10
On 1 Jul 2004 23:52:57 -0700, ni*************@hotmail.com (Nitin
Bhardwaj) wrote:
Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.


char *fmt = "%s";
printf(fmt,943.12);

or even...

printf("%s",943.12);

... if one's compiler doesn't diagnose the mismatch for constant
format strings.

Oz

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #11
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:0e*****************@newsread2.news.pas.earthl ink.net...
"Kenneth Brody" <ke******@spamcop.net> wrote in message
news:40***************@spamcop.net...
Nitin Bhardwaj wrote:

Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.


...
I suppose the following could, technically, qualify as well:

i = i++;


I think that's a constraint violation, not a semantic violation.


The opposite. It voilates semantics in the sense that no semantics are defined for the
expression. However, it does not obviously violate a constraint. The most applicable one
would be 6.5.2.4p1...

The operand of the postfix increment or decrement operator shall
have qualified or unqualified real or pointer type and shall be
a modifiable lvalue.

Note: 6.5p2 is not a constraint.

--
Peter
Nov 14 '05 #12
In <17**************************@posting.google.com > ni*************@hotmail.com (Nitin Bhardwaj) writes:
Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.


Yup, any program bug qualifies as such. Arbitrary example:

int main()
{
int i, j = i; /* i has an indeterminate value */
}

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13
Nitin Bhardwaj wrote:

Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.


int pointer = NULL;
*pointer = 0;

--
pete
Nov 14 '05 #14
pete wrote:
Nitin Bhardwaj wrote:
Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.

int pointer = NULL;
*pointer = 0;


Smoking material?
error: invalid type argument of `unary *'.

Allin Cottrell
Nov 14 '05 #15
Allin Cottrell wrote:

pete wrote:
Nitin Bhardwaj wrote:
Hi all,

Can anyone please provide a code snippet containing a construct that
is syntactically correct but symantically wrong.

int pointer = NULL;
*pointer = 0;


Smoking material?


Untested code.
I usually get caught whenever I post untested code.
error: invalid type argument of `unary *'.


int *pointer = NULL;
*pointer = 0;

--
pete
Nov 14 '05 #16

"pete" <pf*****@mindspring.com> wrote in message
news:40***********@mindspring.com...
Allin Cottrell wrote:

pete wrote:
Nitin Bhardwaj wrote:

>Hi all,
>
>Can anyone please provide a code snippet containing a construct that
>is syntactically correct but symantically wrong.
int pointer = NULL;
*pointer = 0;


Smoking material?


Untested code.
I usually get caught whenever I post untested code.
error: invalid type argument of `unary *'.


int *pointer = NULL;
*pointer = 0;


But there's nothing wrong with that code. It's syntactically
correct, and its behavior is well defined.

Perhaps you meant the second statement to be simply:

*pointer;

which would be a semantic error.

-Mike
Nov 14 '05 #17
Mike Wahler wrote:

"pete" <pf*****@mindspring.com> wrote in message
news:40***********@mindspring.com...
Allin Cottrell wrote:

pete wrote:
> Nitin Bhardwaj wrote:
>
>>Hi all,
>>
>>Can anyone please provide a code snippet
>>containing a construct that
>>is syntactically correct but symantically wrong.
int *pointer = NULL;
*pointer = 0;


But there's nothing wrong with that code. It's syntactically
correct, and its behavior is well defined.

Perhaps you meant the second statement to be simply:

*pointer;

which would be a semantic error.


No. I got it right that time.
*pointer = 0;
attempts to assign a value of 0 to an int type object,
pointed to by pointer, but pointer doesn't point to an object.

--
pete
Nov 14 '05 #18

"pete" <pf*****@mindspring.com> wrote in message
news:40***********@mindspring.com...
Mike Wahler wrote:

"pete" <pf*****@mindspring.com> wrote in message
news:40***********@mindspring.com...
Allin Cottrell wrote:
>
> pete wrote:
> > Nitin Bhardwaj wrote:
> >
> >>Hi all,
> >>
> >>Can anyone please provide a code snippet
> >>containing a construct that
> >>is syntactically correct but symantically wrong. int *pointer = NULL;
*pointer = 0;


But there's nothing wrong with that code. It's syntactically
correct, and its behavior is well defined.

Perhaps you meant the second statement to be simply:

*pointer;

which would be a semantic error.


No. I got it right that time.
*pointer = 0;
attempts to assign a value of 0 to an int type object,
pointed to by pointer, but pointer doesn't point to an object.


You're right. I must have looked at that cross-eyed the
first time. :-)

-Mike
Nov 14 '05 #19

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

Similar topics

0
by: Christopher T King | last post by:
Okay, so this is really two requests in one, and they're both kinda outlandish, but I'm gonna post them nonetheless: I've always thought xrange() to be ugly; it looks to be a lot of typing just...
5
by: F Jamitzky | last post by:
It is rather easy to define functions in python that mimic the special ruby syntactic sugar like: 5.times { print "Hello World!" } or .each { |food| eat food } In python these fragments...
13
by: Neil Zanella | last post by:
Hello, It seems to me that C# properties are nothing more than syntactic sugar for getters and setters. I wonder whether others hold a different point of view. Basically, what more do they have...
4
by: Bas | last post by:
Hi group, just out of curiosity, is there a list of all the syntactic sugar that is used in python? If there isn't such a list, could it be put on a wiki somewhere? The bit of sugar that I do...
34
by: glomde | last post by:
i I would like to extend python so that you could create hiercical tree structures (XML, HTML etc) easier and that resulting code would be more readable than how you write today with packages like...
13
by: Sam Kong | last post by:
Hi, While discussing C#'s using statement, a guy and I had an argument. In C# spec (15.13), there's an explanation like the following. using (R r1 = new R()) { r1.F(); } is precisely...
1
by: Gaurav | last post by:
Hi, What is syntactic and binary encapsulation. and what is the difference between the two. Any pointers? Thanks /Gaurav
11
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
37
by: Hilton | last post by:
Hi, for (int i = 0; i < list.Count; i++) has a hidden performance hit; i.e. list.Count gets evaluated each time, so we write something like: int listCount = list.Count; for (int i = 0; i <...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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...
0
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...
0
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 project—planning, coding, testing,...
0
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...

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.