fork download
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<string.h>
  4.  
  5. int main() {
  6. FILE *input = fopen("input.c", "r");
  7. char ch, str[20], dig[20];
  8. int l = 1, i = 0, k = 0;
  9. char keyword[6][10] = {"int", "void", "if", "else", "do", "while"};
  10.  
  11. printf("Line No.\tToken\t\tLexeme\n");
  12.  
  13. while((ch = fgetc(input)) != EOF) {
  14. if (ch == '\n') {
  15. l++;
  16. } else if (isspace(ch)) {
  17. continue;
  18. } else if (ispunct(ch)) {
  19. if (ch == '/' && (ch = fgetc(input)) == '/') {
  20. while ((ch = fgetc(input)) != '\n');
  21. l++;
  22. } else {
  23. printf("%d\t\tOperator/Symbol\t%c\n", l, ch);
  24. }
  25. } else if (isdigit(ch)) {
  26. dig[k++] = ch;
  27. while (isdigit(ch = fgetc(input))) dig[k++] = ch;
  28. dig[k] = '\0';
  29. ungetc(ch, input);
  30. printf("%d\t\tNumber\t\t%s\n", l, dig);
  31. k = 0;
  32. } else if (isalpha(ch)) {
  33. str[i++] = ch;
  34. while (isalnum(ch = fgetc(input))) str[i++] = ch;
  35. str[i] = '\0';
  36. ungetc(ch, input);
  37.  
  38. int isKeyword = 0;
  39. for (int j = 0; j < 6; j++) {
  40. if (strcmp(str, keyword[j]) == 0) {
  41. printf("%d\t\tKeyword\t\t%s\n", l, str);
  42. isKeyword = 1;
  43. break;
  44. }
  45. }
  46. if (!isKeyword) {
  47. printf("%d\t\tIdentifier\t%s\n", l, str);
  48. }
  49. i = 0;
  50. }
  51. }
  52.  
  53. fclose(input);
  54. return 0;
  55. }
  56.  
Success #stdin #stdout #stderr 0.03s 6960KB
stdin
input.c
#include<stdio.h>
void main()
{
int a,c;/*Declaration of
variables*/
/*hi
welcome
to mbc*/
scanf("%d",&a);//for input a value
c=a+10;
//print the result
printf("Sum = %s",c);
}
stdout
Standard output is empty
stderr
ERROR: /home/AhlO8e/prog:55:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ?    Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ?    Exception: (3) program ? EOF: exit