Advertisement
musifter

AoC day 18 (yacc file)

Dec 18th, 2020
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define YYSTYPE long long
  6.  
  7. long long Result = 0;
  8. %}
  9.  
  10. %token INT EOL
  11.  
  12. %left '+' '*'
  13.  
  14. %start File
  15.  
  16. %%
  17.  
  18. File: | File Line
  19.  
  20. Line: Expression EOL { Result += $1; }
  21.  
  22. Expression:
  23. INT { $$ = $1; }
  24. | '(' Expression ')' { $$ = $2; }
  25. | Expression '+' Expression { $$ = $1 + $3; }
  26. | Expression '*' Expression { $$ = $1 * $3; }
  27. ;
  28.  
  29. %%
  30.  
  31. int yyerror( char *s ) {
  32. printf( "Error: %s\n", s );
  33. }
  34.  
  35. int main() {
  36. yyparse();
  37. printf( "%lld\n", Result );
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement