public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: query related to semantic behaviourial comparision in C thru gcc
@ 2004-03-10 22:01 lrtaylor
  0 siblings, 0 replies; 3+ messages in thread
From: lrtaylor @ 2004-03-10 22:01 UTC (permalink / raw)
  To: u02139, gcc-help

Hi,

If you're using the same compiler text book that I used (we implemented
a Tiger compiler, too), then I pity you... :-)

I believe this is expected behavior in C.  "If" statements expect a
"boolean" expression, which is interpreted as meaning a non-zero result
is true, zero is false.  Basically, it's really an integer expression.
Symtab cannot be converted to an integer (or, if it can, the compiler
doesn't realize it) and cannot therefore be used in the "if" statement.
The pointer to Symtab, however, can be converted to an integer, and can
therefore be used in the expression.  The net result of using a pointer
is that if the statement is true, then it means the pointer (not the
location being pointed to) has a value.  Otherwise, it's NULL (zero).

Cheers,
Lyle


-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Silver Chandrakant L.
Sent: Wednesday, March 10, 2004 12:50 PM
To: gcc-help@gcc.gnu.org
Subject: query related to semantic behaviourial comparision in C thru
gcc

Hi,
 I want to ask you something related to the behaviour i found in C, 
actually compiled thru GCC 3.2.2

The problem is or could be in the semantic behaviour.
            ~~                    ~~~~~~~~

Please note the following excerpt from the attached file program
symdef.c
~~~~~~~~~~~~~~~~~~~
~~~~~~~~
void printSymtab(void)
{
  Symtab *tptr;
  Symtab t;


  puts("**************\n\t\tSymTable**************");
  puts("    Name              PIB    PIH    BlkNo ");

  printf("%u", t);
  if (t) ;

  if (tptr);

  if (*tptr);

  return;
}
``````````````````````````````````````````````
Compilation
~~~~~~~~~~~
[u02139@cs123]$ gcc -Wall symdef.c
symdef.c: In function `printSymtab':
symdef.c:84: invalid operands to binary !=
symdef.c:88: invalid operands to binary !=
[u02139@cs123]$


``````````````````````````````````````````````
The error/semantic behaviour to be pointed out is, when we print the 
variable "t" it is allowed, but when comparison is done implicit or 
explicit it gives an error.


void printSymtab(void)
{
  Symtab *tptr;
  Symtab t;


  puts("**************\n\t\tSymTable**************");
  puts("    Name              PIB    PIH    BlkNo ");

  printf("%u", t);      	<-------------ALLOWED
  if (t) ;			<-------------NOT ALLOWED (line 84)

  if (tptr);			<-------------This is OKAY

  if (*tptr);			<---------NOT ALLOWED/possibly it is
error
								(line
88)	
  return;
}


Why so ? could please explain me this semantic behaviour. When allowed
in 
one case can also be implemented/used in other case. 
Actually it shd be allowed in either cases.

I'm doing CC(compiler construction) course in our department and was 
working out for Tiger language. I just started writing code for the 
semantic analysis phase, and during compilation such error was 
encountered. Actually, line 88 was done by mistake, but helped get into 
these other things. 

The files used for compilation are attached.

Pleaze help me, 
Waiting for your response.

Bye & take care.
-- 
-SilverZ.
--
Always forgive your enemies - Nothing annoys them so much.
--------------------------------------
Silver Chandrakant L
   MCA (Batch 2002)
   IInd year
   University of Pune.
   India.

chandrakant_silver@hotmail.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: query related to semantic behaviourial comparision in C thru gcc
@ 2004-03-10 20:30 Eljay Love-Jensen
  0 siblings, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2004-03-10 20:30 UTC (permalink / raw)
  To: Silver Chandrakant L., gcc-help

Hi Silver,

void printSymtab(void)
{
   Symtab *tptr;
   Symtab t;


   puts("**************\n\t\tSymTable**************");
   puts("    Name              PIB    PIH    BlkNo ");

   printf("%u", t);      	<-------------ALLOWED
// t gets passed in by address.  Even though you are using that address
// as a "%u" ... which isn't going to be pretty.

   if (t) ;			<-------------NOT ALLOWED (line 84)
// Assuming Symtab is a struct, you are using a struct as an expression.

   if (tptr);			<-------------This is OKAY
// Testing a pointer as being non-null is okay.  Style-wars aside.

   if (*tptr);			<---------NOT ALLOWED/possibly it is error
								(line 88)	
// Assuming Symtab is a struct, you are using a struct as an expression.

   return;
}

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

* query related to semantic behaviourial comparision in C thru gcc
@ 2004-03-10 20:25 Silver Chandrakant L.
  0 siblings, 0 replies; 3+ messages in thread
From: Silver Chandrakant L. @ 2004-03-10 20:25 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: TEXT/PLAIN, Size: 2263 bytes --]

Hi,
 I want to ask you something related to the behaviour i found in C, 
actually compiled thru GCC 3.2.2

The problem is or could be in the semantic behaviour.
            ~~                    ~~~~~~~~

Please note the following excerpt from the attached file program symdef.c
~~~~~~~~~~~~~~~~~~~                                              ~~~~~~~~
void printSymtab(void)
{
  Symtab *tptr;
  Symtab t;


  puts("**************\n\t\tSymTable**************");
  puts("    Name              PIB    PIH    BlkNo ");

  printf("%u", t);
  if (t) ;

  if (tptr);

  if (*tptr);

  return;
}
``````````````````````````````````````````````
Compilation
~~~~~~~~~~~
[u02139@cs123]$ gcc -Wall symdef.c
symdef.c: In function `printSymtab':
symdef.c:84: invalid operands to binary !=
symdef.c:88: invalid operands to binary !=
[u02139@cs123]$


``````````````````````````````````````````````
The error/semantic behaviour to be pointed out is, when we print the 
variable "t" it is allowed, but when comparison is done implicit or 
explicit it gives an error.


void printSymtab(void)
{
  Symtab *tptr;
  Symtab t;


  puts("**************\n\t\tSymTable**************");
  puts("    Name              PIB    PIH    BlkNo ");

  printf("%u", t);      	<-------------ALLOWED
  if (t) ;			<-------------NOT ALLOWED (line 84)

  if (tptr);			<-------------This is OKAY

  if (*tptr);			<---------NOT ALLOWED/possibly it is error
								(line 88)	
  return;
}


Why so ? could please explain me this semantic behaviour. When allowed in 
one case can also be implemented/used in other case. 
Actually it shd be allowed in either cases.

I'm doing CC(compiler construction) course in our department and was 
working out for Tiger language. I just started writing code for the 
semantic analysis phase, and during compilation such error was 
encountered. Actually, line 88 was done by mistake, but helped get into 
these other things. 

The files used for compilation are attached.

Pleaze help me, 
Waiting for your response.

Bye & take care.
-- 
-SilverZ.
--
Always forgive your enemies - Nothing annoys them so much.
--------------------------------------
Silver Chandrakant L
   MCA (Batch 2002)
   IInd year
   University of Pune.
   India.

chandrakant_silver@hotmail.com


[-- Attachment #2: Type: TEXT/PLAIN, Size: 901 bytes --]

#ifndef __SYMDEF_H__

#define __SYMDEF_H__

#define HASHTAB_SIZE 50

/*********SYMTAB*****************************/
typedef struct symtab_tag  /* Symbol Table */
{
  char     *symname;   /* symbol name */
  unsigned pib;        /* previous in block */
  unsigned pih;        /* previous in hash  */
  unsigned blkno;      /* block no */
  struct symtab_tag *next;
}Symtab;

/*********BLKTAB*****************************/
typedef struct blktab_tag /* Block Table */
{
  char     *blkname;   /* block name */
  unsigned lastentry;  /* last entry */
  struct blktab_tag *next;
}Blktab;

/*********STACK*****************************/
typedef struct stack_node_tag  /* Stack */
{
  int data;
  struct stack_node_tag *next;
}Stack;


/*********FUNCTIONs*****************************/
void initStack(void);
int isEmpty(void);
int push(int data);
int pop(void);

#endif

[-- Attachment #3: Type: TEXT/PLAIN, Size: 1192 bytes --]

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "symdef.h"

#define NEWNODE (Stack *)malloc(sizeof(Stack))

int hashtab[HASHTAB_SIZE];
Stack *top=NULL;
Symtab *head;

void initStack(void)
{
  top=NULL;
  
  return;
}

int isEmpty(void)
{
  return(top == NULL ? 1 : 0);
}

int push(int data)
{
  Stack *t;
  
  t=NEWNODE;
  t->data=data;
  t->next=top;
  top=t;

  return 0;
}

int pop(void)
{
  Stack *t;
  int data;

  if (isEmpty())
	return -1;

  data=top->data;
  t=top;
  top=top->next;

  return data;
}

void initHash(void)
{
  bzero(&hashtab, sizeof(hashtab));

  return;
}

/*
 *     str : hash string
 * Returns : hashed value
 */
unsigned hash(char *str)
{
  return(str ? (str[0] * ( str[0] && str[1] ? str[1] : str[0]) % HASHTAB_SIZE) : 0);
}

Symtab *addSym(char *sym)
{
  
  return NULL;
}

void printSymtab(void)
{
  Symtab *tptr;
  Symtab t;


  puts("**************\n\t\tSymTable**************");
  puts("    Name              PIB    PIH    BlkNo ");

  printf("%u", t);
  if (t) ;

  if (tptr);

  if (*tptr);

  return;
}

int main(void)
{

  return 0;
}

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-03-10 20:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-10 22:01 query related to semantic behaviourial comparision in C thru gcc lrtaylor
  -- strict thread matches above, loose matches on Subject: below --
2004-03-10 20:30 Eljay Love-Jensen
2004-03-10 20:25 Silver Chandrakant L.

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).