Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aaa;
- import java.io.IOException;
- public class parser {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- System.out.println("Digite uma expresao:" );
- Parser parse = new Parser();
- parse.expr();
- }
- static class Parser{
- char lookahead;
- public Parser() throws IOException {
- lookahead = (char) System.in.read();
- }
- // expr -> term rest
- void expr() throws IOException {
- term();
- rest();
- }
- void rest() throws IOException {
- if(lookahead == '+') {
- match('+');
- term();
- System.out.print('+');
- System.out.print(' ');
- rest();
- } else if(lookahead == '-') {
- match('-');
- term();
- System.out.print('-');
- System.out.print(' ');
- rest();
- } else {
- }
- }
- void term() throws IOException {
- char t = lookahead;
- if (Character.isDigit(t)) { // checa se eh um numero
- match(lookahead);
- System.out.print(t);
- System.out.print(' ');
- }
- }
- void match(char t) throws IOException {
- if(lookahead == t) {
- lookahead = (char) System.in.read();
- } else {
- throw new RuntimeException("Erro de sintaxe");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment