example.y 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  3. Exceptions. See /LICENSE for license information.
  4. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. */
  6. %{
  7. #include <stdio.h>
  8. extern int yylex();
  9. extern int yyparse();
  10. extern FILE* yyin;
  11. void yyerror(const char *s) { fprintf(stderr, "%s\n", s); }
  12. int main() { while (yyparse()) {} }
  13. %}
  14. %define api.value.type {int}
  15. %token INT LSH EQEQ
  16. %%
  17. interpreter:
  18. %empty
  19. | interpreter expression ';' { printf("%d\n", $2); }
  20. expression: compare_expression | compare_operand;
  21. compare_expression: compare_lhs EQEQ compare_operand { $$ = ($1 == $3); };
  22. compare_lhs: compare_expression | compare_operand;
  23. compare_operand: add_expression | multiply_expression | shift_expression | primary_expression;
  24. add_expression: add_lhs '+' add_operand { $$ = ($1 + $3); };
  25. add_lhs: add_expression | add_operand;
  26. add_operand: multiply_expression | multiply_operand;
  27. multiply_expression: multiply_lhs '*' multiply_operand { $$ = ($1 * $3); };
  28. multiply_lhs: multiply_expression | multiply_operand;
  29. multiply_operand: primary_expression;
  30. shift_expression: shift_lhs LSH shift_operand { $$ = ($1 << $3); };
  31. shift_lhs: shift_expression | shift_operand;
  32. shift_operand: primary_expression;
  33. primary_expression: INT | '(' expression ')' { $$ = $2; };
  34. %%