SubprogramDeclarationsPtr Parser::parseSubprogramDeclarations() { if (m_curToken.tokenType() != TokenType::Function && m_curToken.tokenType() != TokenType::Procedure) { return SubprogramDeclarationsPtr(); } SubprogramDeclarationPtr declaration = parseSubprogramDeclaration(); if (m_errorCode <= ErrorCodes::NoError) { if (!match(TokenType::Semicolon)) { reportError(ErrorCodes::ExpectedSemicolon); panic(subprogramDeclarationsFollow, subprogramDeclarationsFollowSize); return SubprogramDeclarationsPtr(); } SubprogramDeclarationsPtr subprograms(new SubprogramDeclarations); subprograms->list.push_back(declaration); SubprogramDeclarationsPtr rest = parseSubprogramDeclarations(); if (rest) { subprograms->list.insert(subprograms->list.end(), rest->list.begin(), rest->list.end()); } return subprograms; } return SubprogramDeclarationsPtr(); }
void Parser::parseSubprogramDeclarations(){ if(isNext(tc_FUNCTION) || isNext(tc_PROCEDURE)){ parseSubprogramDeclaration(); match(tc_SEMICOL); if(m_parserError){ TokenCode synch[] = {tc_FUNCTION, tc_PROCEDURE, tc_BEGIN, tc_NONE}; recover(synch); // if(isNext(tc_SEMICOL)){ // match(tc_SEMICOL); // } } parseSubprogramDeclarations(); } }
void Parser::parseProgram(){ match(tc_PROGRAM); if(m_parserError){ TokenCode synch[] = {tc_ID, tc_NONE}; recover(synch); // if(isNext(tc_PROGRAM)){ // match(tc_PROGRAM); // } } SymbolTableEntry* start = NULL; if(isNext(tc_ID)){ start = m_currentToken->getSymTabEntry(); } match(tc_ID); if(m_parserError){ TokenCode synch[] = {tc_SEMICOL, tc_NONE}; recover(synch); // if(isNext(tc_ID)){ // match(tc_ID); // } } match(tc_SEMICOL); if(m_parserError){ TokenCode synch[] = {tc_VAR, tc_FUNCTION, tc_PROCEDURE, tc_BEGIN, tc_NONE}; recover(synch); // if(isNext(tc_SEMICOL)){ // match(tc_SEMICOL); // } } parseDeclarations(); m_code->generate(cd_GOTO, NULL, NULL, start); parseSubprogramDeclarations(); m_code->generate(cd_LABEL, NULL, NULL, start); parseCompoundStatement(); match(tc_DOT); if(m_parserError){ TokenCode synch[] = {tc_EOF, tc_NONE}; recover(synch); // if(isNext(tc_DOT)){ // match(tc_DOT); // } } m_code->generate(cd_RETURN, NULL, NULL, NULL); }
ProgramPtr Parser::parseProgram() { if (!match(TokenType::Program)) { reportError(ErrorCodes::ExpectedProgram); panic(programFollow, programFollowSize); return ProgramPtr(); } IdentifierPtr programName = parseIdentifier(); if (m_errorCode > ErrorCodes::NoError) { panic(programFollow, programFollowSize); return ProgramPtr(); } if (!match(TokenType::LParen)) { reportError(ErrorCodes::ExpectedLParen); panic(programFollow, programFollowSize); return ProgramPtr(); } IdentifiersPtr inputOutput = parseIdentifierList(); if (m_errorCode > ErrorCodes::NoError) { panic(programFollow, programFollowSize); return ProgramPtr(); } if (!match(TokenType::RParen)) { reportError(ErrorCodes::ExpectedRParen); panic(programFollow, programFollowSize); return ProgramPtr(); } if (!match(TokenType::Semicolon)) { reportError(ErrorCodes::ExpectedSemicolon); panic(programFollow, programFollowSize); return ProgramPtr(); } DeclarationsPtr globalVariables = parseDeclarations(); if (m_errorCode > ErrorCodes::NoError) { panic(programFollow, programFollowSize); return ProgramPtr(); } SubprogramDeclarationsPtr functions = parseSubprogramDeclarations(); if (m_errorCode > ErrorCodes::NoError) { panic(programFollow, programFollowSize); return ProgramPtr(); } CompoundStatementPtr mainProgram = parseCompoundStatement(); if (m_errorCode > ErrorCodes::NoError) { panic(programFollow, programFollowSize); return ProgramPtr(); } if (!match(TokenType::Period)) { reportError(ErrorCodes::ExpectedPeriod); panic(programFollow, programFollowSize); return ProgramPtr(); } ProgramPtr program(new Program); program->programName = programName; program->inputOutput = inputOutput; program->variables = globalVariables; program->functions = functions; program->mainProgram = mainProgram; return program; }