daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  /*Title: Write a C program to simulate
  2. lexical analyzer for validating operators.*/
  3.  
  4. #include<stdio.h>
  5. #include<conio.h>
  6. void main()
  7. {
  8. char s[5];
  9. printf("\n Enter any operator:  ");
  10. gets(s);
  11.  
  12. switch(s[0])
  13. {
  14.  
  15. case'>':
  16.      {
  17.          if(s[1]=='\0')
  18. printf("\n Operator name: Greater than \n");
  19.          else if(s[1]=='=' && s[2]=='\0')
  20. printf("\n Operator name: Greater than or equal \n");
  21.          else
  22. printf("\n Not any operator \n");
  23.     break;
  24.           }
  25.  
  26. case'<':
  27.     {
  28.          if(s[1]=='\0')
  29. printf("\n Operator name: Less than \n");
  30.          else if(s[1]=='=' && s[2]=='\0')
  31. printf("\n Operator name: Less than or equal \n");
  32.          else if(s[1]=='>' && s[2]=='\0')
  33. printf("\n Operator name: Not equal to \n");
  34.          else
  35. printf("\n Not any operator \n");
  36.     break;
  37.           }
  38.  
  39. case'=':
  40.     {
  41.          if(s[1]=='\0')
  42. printf("\n Operator name: Assignment Operator \n");
  43.          else if(s[1]=='=' && s[2]=='\0')
  44. printf("\n Operator name: Equal to \n");
  45.          else
  46. printf("\n Not any operator \n");
  47.     break;
  48.           }
  49.  
  50.  
  51.  
  52. case'!':
  53.     {
  54.          if(s[1]=='\0')
  55. printf("\n Operator name: Bitwise Not \n");
  56.          else if(s[1]=='=' && s[2]=='\0')
  57. printf("\n Operator name: Not Equal \n");
  58.          else
  59. printf("\n Not any operator \n");
  60.     break;
  61.           }
  62.  
  63. case'&':
  64.     {
  65.          if(s[1]=='\0')
  66. printf("\n Operator name: Bitwise AND \n");
  67.          else if(s[1]=='&' && s[2]=='\0')
  68. printf("\n Operator name: Logical AND \n");
  69.          else
  70. printf("\n Not any operator \n");
  71.     break;
  72.           }
  73.  
  74. case'|':
  75.     {
  76.          if(s[1]=='\0')
  77. printf("\n Operator name: Bitwise OR \n");
  78.          else if(s[1]=='|' && s[2]=='\0')
  79. printf("\n Operator name: Logical OR \n");
  80.          else
  81. printf("\n Not any operator \n");
  82.     break;
  83.           }
  84.  
  85.  
  86. case'+':
  87.           {
  88.          if(s[1]=='\0')
  89. printf("\n Operator name: Addition operator \n");
  90.          else if(s[1]=='+' && s[2]=='\0')
  91. printf("\n Increment Operator \n");
  92.          else
  93. printf("\n Not any operator \n");
  94.     break;
  95.           }
  96.  
  97.  
  98. case'-':
  99.      {
  100.          if(s[1]=='\0')
  101. printf("\n Operator name: Subtraction operator \n");
  102.          else if(s[1]=='-' && s[2]=='\0')
  103. printf("\n Operator name: Decrement Operator \n");
  104.          else
  105. printf("\n Not any operator \n");
  106.     break;
  107.           }
  108.  
  109. case'*':
  110.      {
  111.          if(s[1]=='\0')
  112. printf("\n Operator name: Multiplication Operator \n ");
  113.          else
  114. printf("\n Not any operator \n");
  115.     break;
  116.           }
  117.  
  118. case'/':
  119.      {
  120.          if(s[1]=='\0')
  121. printf("\n Operator name: Division Operator \n");
  122.          else
  123. printf("\n Not any operator \n");
  124.     break;
  125.           }
  126.  
  127.  
  128. case'%':
  129.     {
  130.          if(s[1]=='\0') printf("\n Modulus Operator \n");
  131.          else printf("\n Not any operator \n");
  132.        break;
  133.           }
  134.  
  135. default:
  136. printf("\n Not an operator \n");
  137. }
  138. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top