rfop

Parser

Aug 9th, 2019
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. package aaa;
  2.  
  3. import java.io.IOException;
  4.  
  5. public class parser {
  6.  
  7. public static void main(String[] args) throws IOException {
  8. // TODO Auto-generated method stub
  9. System.out.println("Digite uma expresao:" );
  10. Parser parse = new Parser();
  11. parse.expr();
  12. }
  13.  
  14. static class Parser{
  15. char lookahead;
  16. public Parser() throws IOException {
  17. lookahead = (char) System.in.read();
  18. }
  19. // expr -> term rest
  20. void expr() throws IOException {
  21. term();
  22. rest();
  23. }
  24. void rest() throws IOException {
  25. if(lookahead == '+') {
  26. match('+');
  27. term();
  28. System.out.print('+');
  29. System.out.print(' ');
  30. rest();
  31. } else if(lookahead == '-') {
  32. match('-');
  33. term();
  34. System.out.print('-');
  35. System.out.print(' ');
  36. rest();
  37. } else {
  38.  
  39. }
  40.  
  41. }
  42. void term() throws IOException {
  43. char t = lookahead;
  44. if (Character.isDigit(t)) { // checa se eh um numero
  45. match(lookahead);
  46. System.out.print(t);
  47. System.out.print(' ');
  48. }
  49. }
  50.  
  51. void match(char t) throws IOException {
  52. if(lookahead == t) {
  53. lookahead = (char) System.in.read();
  54. } else {
  55. throw new RuntimeException("Erro de sintaxe");
  56. }
  57. }
  58.  
  59. }
  60.  
  61. }
Add Comment
Please, Sign In to add comment