Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. %{
  2. #include "y.tab.h"
  3. #include <stdlib.h>
  4. %}
  5.  
  6. %%
  7. [0-9]+ {yylval = atoi(yytext); return INTEGER;}
  8. [+] return PLUS;
  9. [n] return NL;
  10. [ t] ; /* skip whitespace */
  11. . {printf("Unknown character '%c'n", yytext[0]); return 0;}
  12. %%
  13.  
  14. int yywrap(void) {
  15. return 1;
  16. }
  17.  
  18. %{
  19. #include <stdio.h>
  20. int yylex(void);
  21. void yyerror(char *);
  22. %}
  23.  
  24. %token INTEGER PLUS NL
  25. %left PLUS MINUS
  26.  
  27. %%
  28.  
  29. prog: prog expr NL { printf("%dn", $2); }
  30. |
  31. ;
  32. expr: INTEGER { $$ = $1; }
  33. | expr PLUS expr { $$ = $1 + $3; }
  34. ;
  35. %%
  36.  
  37. void yyerror(char *s) {
  38. fprintf(stderr, "%sn", s);
  39. }
  40.  
  41. int main(void) {
  42. yyparse();
  43. return 0;
  44. }
  45.  
  46. bison -y -d badd.y
  47. flex badd.l
  48. gcc y.tab.c lex.yy.c -o badd.exe
  49.  
  50. %{
  51. #include "ladd.h"
  52. #include <stdlib.h>
  53. extern int yylval;
  54. %}
  55.  
  56. %%
  57. [0-9]+ {yylval = atoi(yytext); return INTEGER;}
  58. [+] return PLUS;
  59. [n] return NL;
  60. [ t] ; /* skip whitespace */
  61. . {printf("Unknown character '%c'n", yytext[0]); return 0;}
  62. %%
  63.  
  64. int yywrap(void) {
  65. return 1;
  66. }
  67.  
  68. %include { #include <assert.h> }
  69. %syntax_error { printf("Lemon syntax errorn"); }
  70. %token_type {int}
  71. %left PLUS MINUS .
  72.  
  73. start ::= prog .
  74.  
  75. prog ::= prog expr(a) NL . { printf("%dn", a); }
  76. prog ::= .
  77.  
  78. expr(a) ::= INTEGER(b) . { a = b; }
  79. expr(a) ::= expr(b) PLUS expr(c) . { a = b + c; }
  80.  
  81. lemon ladd.y
  82. flex ladd.l
  83. gcc main.c ladd.c lex.yy.c -o ladd.exe
  84.  
  85. start ::= prog .
  86.  
  87. prog ::= prog print NL .
  88. prog ::= .
  89.  
  90. print ::= expr(a) . { printf("%dn", a); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement