fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. char nonTerminal;
  6. char alpha[100][100], beta[100][100];
  7. int alphaCount = 0, betaCount = 0;
  8.  
  9. %}
  10.  
  11. %%
  12. ([A-Z])->.* {
  13. char *line = yytext;
  14.  
  15. nonTerminal = line[0];
  16. char *rhs = strchr(line, '>') + 1;
  17.  
  18. char *token = strtok(rhs, "|");
  19. alphaCount = betaCount = 0;
  20.  
  21. while (token != NULL) {
  22. if (token[0] == nonTerminal) {
  23. strcpy(alpha[alphaCount++], token + 1);
  24. } else {
  25. strcpy(beta[betaCount++], token);
  26. }
  27. token = strtok(NULL, "|");
  28. }
  29.  
  30. if (alphaCount == 0) {
  31. printf("%s\n", line);
  32. } else {
  33. printf("%c -> ", nonTerminal);
  34. for (int i = 0; i < betaCount; i++) {
  35. printf("%s%c' ", beta[i], nonTerminal);
  36. if (i != betaCount - 1) printf("| ");
  37. }
  38.  
  39. printf("\n%c' -> ", nonTerminal);
  40. for (int i = 0; i < alphaCount; i++) {
  41. printf("%s%c' ", alpha[i], nonTerminal);
  42. if (i != alphaCount - 1) printf("| ");
  43. }
  44. printf("| ε\n");
  45. }
  46. }
  47.  
  48. \n ;
  49. . ;
  50. %%
  51.  
  52. int main() {
  53. yylex();
  54. return 0;
  55. }
  56.  
Success #stdin #stdout #stderr 0.02s 6924KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/uEuRLj/prog:2:1: Syntax error: Operator expected
ERROR: /home/uEuRLj/prog:55:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit