Compiler Design Lab


https://www.onlinegdb.com/online_c_compiler


/******************************************************************************

1. Write a C program to identify whether a given line is a comment or not.

*******************************************************************************/

#include<stdio.h>
#include<conio.h>
void main()
{
    char com[30];
    int i=2,a=0;
    clrscr();
    printf("\n Enter comment:");
    gets(com);
    if(com[0]=='/')
    {
        if(com[1]=='/')
    printf("\n It is a comment");
    else if(com[1]=='*')
    {
        for(i=2;i<=30;i++)
        {
            if(com[i]=='*'&&com[i+1]=='/')
            {
                printf("\n It is a comment");
                a=1;
                break;
               
            } else continue;
           
        }
        if(a==0)
        printf("\n It is not a comment");
       
    } else printf("\n It is not a comment");
       
    } else printf("\n It is not a comment");
    getch();
   
}
______________________________________________________________________________________________


******************************************************************************

2. Write a C program to recognize strings under 'a', 'a*b+', 'abb'.

*******************************************************************************/
#include<stdio.h>
#include<conio.h>
 #include<string.h>
#include<stdlib.h>
 void main()
{
char s[20],c;
int state=0,i=0;
clrscr();
printf("\n Enter a string:");
gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
 if(c=='a') state=1;
else if(c=='b')
state=2;
else state=6;
break;
case 1: c=s[i++];
if(c=='a')
 state=3;
else if(c=='b')
state=4;
else
state=6;
                break;
case 2: c=s[i++];
 if(c=='a') state=6;
else if(c=='b')
state=2;
 else state=6;
break;
 case 3: c=s[i++]; if(c=='a')
state=3;
else if(c=='b')
state=2;
else
state=6;
break;
case 4: c=s[i++];
                if(c=='a')
 state=6;
else if(c=='b')
state=5;
else state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 6: printf("\n %s is not recognised.",s);
exit(0);
} }
if(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s); getch();
}

***************************************************************************************
3. Write a C program to test whether a given identifier is valid or not
****************************************************************************************

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
    char a[10];
    int flag,
    i=1;
    clrscr();
    printf("\n Enter an identifier:");
    gets(a);
    if(isalpha(a[0]))
    flag=1;
    else
    printf("\n Not a valid identifier");
    while(a[i]!='\0')
    {
        if(!isdigit(a[i])&&!isalpha(a[i]))
        {
            flag=0; break;
           
        } i++;
       
    }
    if(flag==1)
    printf("\n Valid identifier");
    getch();
   
}


***************************************************************************************
4. Write a C program to simulate lexical analyzer for validating operators.
****************************************************************************************

#include<stdio.h>
#include<conio.h>
void main()
{
    char s[5];
    clrscr();
    printf("\n Enter any operator:");
    gets(s);
    switch(s[0])
    {
        case'>':
        if(s[1]=='=')
        printf("\n Greater than or equal");
        else printf("\n Greater than");
        break;
        case'<':
        if(s[1]=='=')
        printf("\n Less than or equal");
        else
        printf("\nLess than");
        break;
        case'=':
        if(s[1]=='=')
        printf("\nEqual to");
        else printf("\nAssignment");
        break;
        case'!':
        if(s[1]=='=')
        printf("\nNot Equal");
        else printf("\n Bit Not");
        break;
        case'&':
        if(s[1]=='&')
        printf("\nLogical AND");
        else
        printf("\n Bitwise AND");
        break;
        case'|':
        if(s[1]=='|')
        printf("\nLogical OR");
        else
        printf("\nBitwise OR");
        break;
        case'+':
        printf("\n Addition");
        break;
        case'-':
        printf("\nSubstraction");
        break;
        case'*':
        printf("\nMultiplication");
        break;
        case'/':
        printf("\nDivision");
        break;
        case'%':
        printf("Modulus");
        break;
        default:
        printf("\n Not a operator");
       
    }
    getch();
   
}

1 comment: