SubprogramDeclarationPtr Parser::parseSubprogramDeclaration() { SubprogramDeclarationPtr sub(new SubprogramDeclaration); parseSubprogramHead(sub); if (m_errorCode > ErrorCodes::NoError) { panic(subprogramDeclarationFollow, subprogramDeclarationFollowSize); return SubprogramDeclarationPtr(); } DeclarationsPtr declarations = parseDeclarations(); if (m_errorCode > ErrorCodes::NoError) { panic(subprogramDeclarationFollow, subprogramDeclarationFollowSize); return SubprogramDeclarationPtr(); } sub->declarations = declarations; CompoundStatementPtr body = parseCompoundStatement(); if (m_errorCode > ErrorCodes::NoError) { panic(subprogramDeclarationFollow, subprogramDeclarationFollowSize); return SubprogramDeclarationPtr(); } sub->body = body; return sub; }
// Declarations -> Decl | Decls Declarations *parseDeclarations( FILE *source ) { Token token = scanner(source); int i; Declaration decl; Declarations *decls; switch(token.type){ case FloatDeclaration: case IntegerDeclaration: decl = parseDeclaration(source, token); decls = parseDeclarations(source); return makeDeclarationTree( decl, decls ); case PrintOp: case Alphabet: for (i = strlen(token.tok) - 1; i >= 0; i--) { ungetc(token.tok[i], source); } return NULL; case EOFsymbol: return NULL; default: printf("Syntax Error: Expect declarations %s\n", token.tok); exit(1); } }
void Parser::parseDeclarations(){ if(isNext(tc_VAR)){ match(tc_VAR); EntryList ids = *(new EntryList()); parseIdentifierList(ids); match(tc_COLON); if(m_parserError){ TokenCode synch[] = {tc_ARRAY, tc_INTEGER, tc_REAL, tc_NONE}; recover(synch); // if(isNext(tc_COLON)){ // match(tc_COLON); // } } parseType(); m_code->generateVariables(ids); match(tc_SEMICOL); if(m_parserError){ TokenCode synch[] = {tc_VAR, tc_FUNCTION, tc_BEGIN, tc_PROCEDURE, tc_NONE}; recover(synch); // if(isNext(tc_SEMICOL)){ // match(tc_SEMICOL); // } } parseDeclarations(); } }
//h1, div.note, #hahah { margin: auto; color: #cc0000; } Rule CSSParser::parseRule(){ #ifdef CSS_DEBUG qDebug() << Q_FUNC_INFO; #endif /* CSS_DEBUG */ Rule rule{parseSimpleSelectors(), parseDeclarations()}; return rule; }
/* parser */ Program parser( FILE *source ) { Program program; program.declarations = parseDeclarations(source); program.statements = parseStatements(source); return program; }
DeclarationsPtr Parser::parseDeclarations() { if (!match(TokenType::Var)) { return DeclarationsPtr(); } IdentifiersPtr identifiers = parseIdentifierList(); if (m_errorCode > ErrorCodes::NoError) { panic(declarationsFollow, declarationsFollowSize); return DeclarationsPtr(); } if (!match(TokenType::Colon)) { reportError(ErrorCodes::ExpectedColon); panic(declarationsFollow, declarationsFollowSize); return DeclarationsPtr(); } TypePtr type = parseType(); if (m_errorCode > ErrorCodes::NoError) { panic(declarationsFollow, declarationsFollowSize); return DeclarationsPtr(); } if (!match(TokenType::Semicolon)) { reportError(ErrorCodes::ExpectedSemicolon); panic(declarationsFollow, declarationsFollowSize); return DeclarationsPtr(); } DeclarationsPtr declarations(new Declarations); for (std::vector<IdentifierPtr>::const_iterator i = identifiers->list.begin(); i != identifiers->list.end(); ++i) { DeclarationPtr declaration(new Declaration); declaration->id = *i; declaration->type = type; declarations->list.push_back(declaration); } DeclarationsPtr rest = parseDeclarations(); if (rest) { declarations->list.insert(declarations->list.end(), rest->list.begin(), rest->list.end()); } return declarations; }
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); }
Declarations *parseDeclarations( FILE *source )//changed { Token token = scanner(source); Declaration decl; Declarations *decls; switch(token.type){ case FloatDeclaration: case IntegerDeclaration: decl = parseDeclaration(source, token); decls = parseDeclarations(source); return makeDeclarationTree( decl, decls ); case PrintOp: case Alphabet: //ungetc(token.tok[0], source); ungetstring(token.tok,source); return NULL; case EOFsymbol: return NULL; default: printf("Syntax Error: Expect declarations %s\n", token.tok); exit(1); } }
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; }
void Parser::parseSubprogramDeclaration(){ parseSubprogramHead(); parseDeclarations(); parseCompoundStatement(); m_code->generate(cd_RETURN, NULL, NULL, NULL); }