dc.peg 663 B

12345678910111213141516171819202122232425262728
  1. # Grammar
  2. Expr <- SPACE Sum EOL { printf("%d\n", pop()); }
  3. / (!EOL .)* EOL { printf("error\n"); }
  4. Sum <- Product ( PLUS Product { int r= pop(), l= pop(); push(l + r); }
  5. / MINUS Product { int r= pop(), l= pop(); push(l - r); }
  6. )*
  7. Product <- Value ( TIMES Value { int r= pop(), l= pop(); push(l * r); }
  8. / DIVIDE Value { int r= pop(), l= pop(); push(l / r); }
  9. )*
  10. Value <- NUMBER { push(atoi(yytext)); }
  11. / OPEN Sum CLOSE
  12. # Lexemes
  13. NUMBER <- < [0-9]+ > SPACE
  14. PLUS <- '+' SPACE
  15. MINUS <- '-' SPACE
  16. TIMES <- '*' SPACE
  17. DIVIDE <- '/' SPACE
  18. OPEN <- '(' SPACE
  19. CLOSE <- ')' SPACE
  20. SPACE <- [ \t]*
  21. EOL <- '\n' / '\r\n' / '\r'