private void program(SymbolTable sym) throws ParseException { start("program"); while (lex.tokenCategory() != Lexer.endOfInput) { declaration(sym); if (lex.match(";")) lex.nextLex(); else throw new ParseException(18); } stop("program"); }
public void parse() throws ParseException { sym = new GlobalSymbolTable(); sym.enterType("int", PrimitiveType.IntegerType); sym.enterType("real", PrimitiveType.RealType); sym.enterFunction("printInt", new FunctionType(PrimitiveType.VoidType)); sym.enterFunction("printReal", new FunctionType(PrimitiveType.VoidType)); sym.enterFunction("printStr", new FunctionType(PrimitiveType.VoidType)); lex.nextLex(); program(sym); if (lex.tokenCategory() != lex.endOfInput) parseError(3); // expecting end of file }
private Type type(SymbolTable sym) throws ParseException { start("type"); Type result = null; if (lex.isIdentifier()) { result = sym.lookupType(lex.tokenText()); lex.nextLex(); // we are a terminal, so call nextLex to setup next state } else if (lex.match("^")) { lex.nextLex(); result = new PointerType(type(sym)); } else if (lex.match("[")) { lex.nextLex(); if (lex.tokenCategory() != Lexer.intToken) throw new ParseException(32); // expected an integer int lower = (new Integer(lex.tokenText()).intValue()); lex.nextLex(); if (!lex.match(":")) throw new ParseException(19); // needed to see : lex.nextLex(); if (lex.tokenCategory() != Lexer.intToken) throw new ParseException(32); // expected an integer int upper = (new Integer(lex.tokenText()).intValue()); lex.nextLex(); if (!lex.match("]")) throw new ParseException(24); // should have seen ] lex.nextLex(); result = sym.lookupType(lex.tokenText()); // type(sym); //might use this instead of nextLex(); // gotta advance so we are at next token lex.nextLex(); result = new ArrayType(lower, upper, result); } else { throw new ParseException(30); // we expected a type name } stop("type"); return result; }
private void constantDeclaration(SymbolTable sym) throws ParseException { start("constantDeclaration"); if (!lex.match("const")) throw new ParseException(6); lex.nextLex(); if (!lex.isIdentifier()) throw new ParseException(27); lex.nextLex(); if (!lex.match("=")) throw new ParseException( 20); // TODO: i think this is the wrong exception. check againset buds using test100 lex.nextLex(); int tid = lex.tokenCategory(); if (tid != Lexer.stringToken || tid != Lexer.realToken || tid != Lexer.intToken) throw new ParseException(31); lex.nextLex(); if (sym.nameDefined(lex.tokenText())) throw new ParseException(35, lex.tokenText()); // TODO enterConstant stop("constantDeclaration"); }
private void parameterList(SymbolTable sym) throws ParseException { start("parameterList"); int tid = lex.tokenCategory(); if (lex.match("not") || lex.match("new") || lex.match("(") || lex.match("-") || lex.match("&") || lex.isIdentifier() || tid == Lexer.realToken || tid == Lexer.intToken || tid == Lexer.stringToken) { expression(sym); while (lex.match(",")) { lex.nextLex(); expression(sym); } } stop("parameterList"); }
private void term(SymbolTable sym) throws ParseException { start("term"); int tid = lex.tokenCategory(); if (lex.match("(")) { lex.nextLex(); expression(sym); if (!lex.match(")")) throw new ParseException(22); lex.nextLex(); } else if (lex.match("not")) { lex.nextLex(); term(sym); } else if (lex.match("new")) { lex.nextLex(); type(sym); } else if (lex.match("-")) { lex.nextLex(); term(sym); } else if (lex.match("&")) { lex.nextLex(); reference(sym); } else if (lex.isIdentifier()) { reference(sym); if (lex.match("(")) { lex.nextLex(); parameterList(sym); if (!lex.match(")")) throw new ParseException(22); lex.nextLex(); } } else if (tid == Lexer.intToken || tid == Lexer.realToken || tid == Lexer.stringToken) { lex.nextLex(); } else { throw new ParseException(33); // TODO: is this right?!?! } stop("term"); }