public void parse(Lexico scanner, Semantico semanticAnalyser) throws LexicalError, SyntaticError, SemanticError { this.scanner = scanner; this.semanticAnalyser = semanticAnalyser; stack.clear(); stack.push(new Integer(DOLLAR)); stack.push(new Integer(START_SYMBOL)); currentToken = scanner.nextToken(); while (!step()) ; }
private boolean step() throws LexicalError, SyntaticError, SemanticError { if (currentToken == null) { int pos = 0; if (previousToken != null) pos = previousToken.getPosition() + previousToken.getLexeme().length(); currentToken = new Token(DOLLAR, "$", pos); } int x = ((Integer) stack.pop()).intValue(); int a = currentToken.getId(); if (x == EPSILON) { return false; } else if (isTerminal(x)) { if (x == a) { if (stack.empty()) return true; else { previousToken = currentToken; currentToken = scanner.nextToken(); return false; } } else { throw new SyntaticError(PARSER_ERROR[x], currentToken.getPosition()); } } else if (isNonTerminal(x)) { if (pushProduction(x, a)) return false; else throw new SyntaticError(PARSER_ERROR[x], currentToken.getPosition()); } else // isSemanticAction(x) { if (executaAcoesSemanticas) { semanticAnalyser.executeAction(x - FIRST_SEMANTIC_ACTION, previousToken); } return false; } }