Saturday, 13 January 2018

1. IMPLEMENTATION OF SYMBOL TABLE

Ex. No: 1
Date:
IMPLEMENTATION OF SYMBOL TABLE

 AIM:
                    To write a C program to implement a symbol table.



ALGORITHM:


1.   Start the Program.

2.   Get the input from the user with the terminating symbol ‘$’.

3.   Allocate memory for the variable by dynamic memory allocation function.

4.   If the next character of the symbol is an operator then only the memory is allocated.

5.   While reading, the input symbol is inserted into symbol table along with its memory address.
6.   The steps are repeated till$is reached.

7.   To reach a variable, enter the variable to the searched and symbol table has been checked for corresponding variable, the variable along its address is displayed as result.
8.   Stop the program.






PROGRAM
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
void main()
{
            int i=0,j=0,x=0,n,flag=0; void *p,*add[15];
            char ch,srch,b[15],d[15],g[10],c;
            clrscr();
            printf("Expression terminated by $:");
            while((c=getchar())!='$')
            {
                        b[i]=c; i++;
            }
            n=i-1;
            printf("Given expression:::");
            i=0;
            while(i<=n)
            {
                        printf("%c",b[i]); i++;
            }
            printf("\n.....symbol table....\n");
            printf("symbol\taddr\ttype\n");
            while(j<=n)
            {
                        c=b[j];
                        if(isalpha(toascii(c)))
                        {
                                    if(j<=n)
                                    {
                                                p=malloc(c); add[x]=p;
                                                d[x]=c;
                                                printf("%c\t%d\tidentifier\n",c,p); goto b;
                                    }
                                    else
                                    {
                                                b:
                                                ch=b[j+1];
                                                if(ch=='+'||ch=='-'||ch=='*'||ch=='='||ch==’/’)
                                                {
                                                            p=malloc(c);
                                                            add[x]=p;
                                                            g[x]=ch;
                                                            printf("%c\t%p\t Operator \n",g[x],p);
                                                            x++;
                                                }
                                    }
                        } j++;
            }

            printf("the symbol is to be searched\n");
            scanf("%s",&srch);
            //srch=getch();
            for(i=0;i<=x;i++)
            {
                        if(srch==d[i]||srch==g[i])
                        {
                                    printf("symbol found...");
                                    printf("%c%s%p\n",srch,"@address",d[i]);
                                    flag=1;
                        }
            }
            if(flag==0)
            printf("symbol not found\n");
}




  ═══════════          Output ═════════════════════════════
║Expression terminated by $:C=A*B+C-D/H$                                     
║Given expression:::C=A*B+C-D/H                                                
║.....symbol table....                                                         
║symbol  addr    type                                                         
║C       2034    identifier                                                  
║=       083A     Operator                                                
║A       2178    identifier                                                   
║*       08C8     Operator                                                     
║B       2318    identifier                                                    
║+       0954     Operator                                                     
║C       2458    identifier                                                    
║-       09E2     Operator                                                     
║D       2602    identifier                                                    
║H       2674    identifier                                                    
║the symbol is to be searched                                                  
║+                                                                             
║symbol found...+@address0042                                                  
                                                                    
                                                                            
                                    
RESULT:

Thus the C program to implement the symbol table was executed and the output is verified.
                                                                                                    

No comments:

Post a Comment

11. Implementation of Simple Code Optimization Techniques (Constant Folding.,etc.)

Ex. No: 11 Date:                   11. Implementation of Simple Code Optimization Techniques (Constant Folding.,etc.) ...