fork download
  1. #include <stdio.h>
  2. #define MAXWORD 20
  3. #define LETTER 'a'
  4. #define DIGIT '0'
  5. #define NKEYS (sizeof keytab / sizeof(keytab[0]))
  6. /*whole bytes bytes in element*/
  7. main() /*count C keywords*/
  8. {
  9. int n, t;
  10. int getword(char*, int);
  11. char word[MAXWORD];
  12. while ((t=getword(word, MAXWORD)) != EOF)
  13. if (t== LETTER)
  14. if ((n = binary(word, keytab, NKEYS)) >= 0)
  15. keytab[n].keycount++;
  16. for (n = 0; n < NKEYS; n++)
  17. if (keytab[n].keycount > 0)
  18. printf("%4d %s\n",
  19. keytab[n].keycount, keytab[n].keyword);
  20. }
  21.  
  22. binary(word, tab, n)/* find word in tab[0]...tab[n-1] */
  23. char *word;
  24. struct key tab[0];
  25. int n;
  26. {
  27. int low, high, mid, cond;
  28.  
  29. low = 0;
  30. high = n - 1;
  31. while (low <= high) {
  32. mid = (low+high) / 2;
  33. if ((cond = strcmp(word, tab[mid].keyword)) < 0)
  34. high = mid - 1;
  35. else if (cond > 0)
  36. low = mid + 1;
  37. else
  38. return (mid);
  39. }
  40. return (-1);
  41. }
  42.  
  43. struct key
  44. {
  45. char *keyword;
  46. int keycount;
  47. }
  48. keytab[]={
  49. "break", 0,
  50. "case",0,
  51. "char", 0,
  52. "const", 0,
  53. "continue", 0,
  54. "default", 0,
  55. /*...*/
  56. "unsigned", 0,
  57. "while", 0
  58. };
  59.  
  60.  
  61. getword(w, lim) /* get next word from input */
  62. char *w;
  63. int lim;
  64. {
  65. int c, t;
  66.  
  67. if (type(c = *w++ = getch()) != LETTER)
  68. {
  69. *w = '\0';
  70. return(c);
  71. }
  72. while (-- lim > 0)
  73. {
  74. t = type (c = *w++ = getch());
  75. if (t != LETTER && t != DIGIT)
  76. {
  77. ungetch(c);
  78. break;
  79. }
  80. }
  81. *(w-1) = '\0';
  82. return(LETTER);
  83. }
  84. type(c) /* return type of ASCII character */
  85. int c;
  86. {
  87. if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
  88. return(LETTER);
  89. else if (c >= '0' && c <= '9')
  90. return(DIGIT);
  91. else
  92. return(c);
  93. }
  94.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:8: warning: return type defaults to ‘int’
prog.c: In function ‘main’:
prog.c:14: warning: implicit declaration of function ‘binary’
prog.c:14: error: ‘keytab’ undeclared (first use in this function)
prog.c:14: error: (Each undeclared identifier is reported only once
prog.c:14: error: for each function it appears in.)
prog.c: At top level:
prog.c:23: warning: return type defaults to ‘int’
prog.c: In function ‘binary’:
prog.c:24: error: array type has incomplete element type
prog.c:33: warning: implicit declaration of function ‘strcmp’
prog.c: At top level:
prog.c:49: warning: missing braces around initializer
prog.c:49: warning: (near initialization for ‘keytab[0]’)
prog.c:62: warning: return type defaults to ‘int’
prog.c: In function ‘getword’:
prog.c:67: warning: implicit declaration of function ‘type’
prog.c:67: warning: implicit declaration of function ‘getch’
prog.c:77: warning: implicit declaration of function ‘ungetch’
prog.c: At top level:
prog.c:85: warning: return type defaults to ‘int’
prog.c: In function ‘type’:
prog.c:87: warning: suggest parentheses around && within ||
stdout
Standard output is empty