Advertisement
happy-barney

glr sample for daxim

Oct 28th, 2020
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. === Result
  2. echo -n "bbaca" | ./a.out
  3. [A] -> a
  4. [B] -> b
  5. [A] -> B
  6. [A] -> a
  7. [B] -> b c
  8. [A] -> B
  9.  
  10. === grammar
  11.  
  12. %{
  13.  
  14. #include <stdio.h>
  15. #define YYSTYPE char const *
  16. int yylex (void);
  17. void yyerror (char const *);
  18.  
  19. %}
  20.  
  21. %start start
  22.  
  23. %glr-parser
  24.  
  25. %%
  26.  
  27. start: A
  28.  
  29. A
  30. : 'a' { printf ("[A] -> a\n"); }
  31. | B { printf ("[A] -> B\n"); }
  32.  
  33. B
  34. : 'b' A %dprec 1 { printf ("[B] -> b\n"); }
  35. | 'b' A 'c' A %dprec 2 { printf ("[B] -> b c\n"); }
  36. ;
  37.  
  38. %%
  39.  
  40. void yyerror(const char *str)
  41. {
  42. fprintf(stderr, "yyerror: %s\n", str);
  43. }
  44.  
  45. int yywrap(){}
  46.  
  47. int main()
  48. {
  49. return yyparse();
  50. }
  51.  
  52. === lexer
  53.  
  54. %%
  55.  
  56. a { return 'a'; }
  57. b { return 'b'; }
  58. c { return 'c'; }
  59.  
  60. %%
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement