Posts

Write a program to remove the Left Recursion from a given grammar.

#include<stdio.h>    #include<string.h>    #define SIZE 10    int main () {         char non_terminal;         char beta,alpha;         int num;         int i;        char production[10][SIZE];         int index=3; /* starting of the string following "->" */         printf("Enter Number of Production : ");         scanf("%d",&num);         printf("Enter the grammar as E->E-A :\n");         for(i=0;i<num;i++){              scanf("%s",production[i]);         }         for(i=0;i<num;i++){              printf("\nGRAMMAR : : : %s",production[i]);              non_terminal=production[i][0];              if(non_terminal==production[i][index]) {                   alpha=production[i][index+1];                   printf(" is left recursive.\n");                   while(production[i][index]!=0 && production[i][index]!='|')  {            index++;  }                  if(production[i][in

Use macro features of C language

9 10 11 12 13 14 15 16 17 18 #include<stdio.h> #include<conio.h> #define pf printf #define sf scanf #define vm void main() #define cls clrscr #define gt getch #define val 100 void main ( ) {   int n ;   cls ( ) ;   printf ( "Enter a number : " ) ;   scanf ( "%d" , & n ) ;   printf ( "Given Number  = " % d ",n);   p(" \ nval = % d " , val ) ;   getch ( ) ; }

Write a SAL program in text file and generate SYMTAB and LITTAB

#include<stdio.h> #include<conio.h> #include<ctype.h> void main() { FILE *f; char ch; int line=0,word=0; clrscr(); f=fopen("student","w+"); printf("Enter text press ctrol+z to quit\n"); do { ch=getchar(); putc(ch,f); } while(ch!=EOF);   printf("Total byts of files =%d ",ftell(f)); rewind(f); while((ch=getc(f))!=EOF) { if(ch=='\n') line++; if(isspace(ch)||ch=='\t'||ch=='\n') word++; putchar(ch); } fclose(f); printf("\n no of line=%d\n",line); printf("no of word=%d\n",word); getch(); }

Write a program to implement the lexical analyzer.

Image
PRACTICAL-01 AIM:-  write a sal program to implement the lexical analyzer. Solution :- #include<string.h> #include<ctype.h> #include<stdio.h> void keyword(char str[10]) {     if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str )==0||strcmp("float",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0||strcmp("case",str)==0)         printf("\n%s is a keyword",str);     else         printf("\n%s is an identifier",str); } main() {     FILE *f1,*f2,*f3;     char c,str[10],st1[10];     int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;     printf("\nEnter the c Program: ");/*gets(st1);*/     f1=fopen("input","w");     while((c=getchar())!=EOF)         putc(c,f1);     fclose(f1);     f1=fope