예제 #1
0
파일: parser.c 프로젝트: markcheno/topaz
static void UnlessStatement(void)
{
	Int32 l1,l2;

	SkipToken(tUNLESS);
	
	Expr();
	
	if( Token==tTHEN ) NextToken();

	l1 = vm_genI(op_jz,0);
	
	StatementList();
	
	if( Token==tELSE )
	{
		SkipToken(tELSE);
		l2 = vm_genI(op_jmp,0);
		vm_patch(l1,vm_addr());
		l1 = l2;
		StatementList();
	}

	vm_patch(l1,vm_addr());

	SkipToken(tEND);
}
예제 #2
0
파일: parser.c 프로젝트: markcheno/topaz
static void CaseStatement(void)
{
	Int32 ii=0;
	Int32 l1,l2[MAXCOND];

	SkipToken(tCASE);

	Expr();

	while( Token==tWHEN )
	{
		SkipToken(tWHEN);
		vm_gen0(op_dup);
		Expr();
		if( Token==tTHEN ) NextToken();
		vm_gen0(op_eql);
		l1 = vm_genI(op_jz,0);
		StatementList();
		l2[ii++] = vm_genI(op_jmp,0);
		vm_patch(l1,vm_addr());
	}
	
	if( Token==tELSE )
	{
		SkipToken(tELSE);
		StatementList();
	}

	while( --ii >= 0 )
		vm_patch(l2[ii],vm_addr());

	vm_gen0(op_pop);
	
	SkipToken(tEND);
}
예제 #3
0
/* <Rat11F> ::= # <Opt Function Definitions>
                # <Opt Declaration List> <Statement List> # */
void SyntaxAnalyzer::Rat11f()
{
    // start it off
    NextRecord();
    if(rrr == s_pound)
    {   
        NextRecord();
        OptFunctionDefinitions();
        // will return with next next record as a #
        if(rrr == s_pound)
        {
            NextRecord();
            OptDeclarationList();
            StatementList();
            if(rrr == s_pound)
            {
                NextRecord();
                logger.Log("<Rat11F> ::= # <Opt Function Definitions># <Opt Declaration List> <Statement List> #");
            }
			else
			{
                insert(s_pound);
			}
        }
		else
		{
            insert(s_pound);
		}
    }
    else
    {
        insert(s_pound);
    }
}
예제 #4
0
파일: parser.c 프로젝트: markcheno/topaz
static void ForStatement(void)
{
	Int32 l1,l2,l3,l4;
	
	SkipToken(tFOR);
	SkipToken(tLPAREN);
	
	/* init */
	if( Token != tSEMI )
	{
		AssignmentStatement();
        while( Token==tCOMMA )
        {
            SkipToken(tCOMMA);
    		AssignmentStatement();    
        }
	}
	SkipToken(tSEMI);
	
	/* cond */
	l1 = vm_addr();
    if( Token != tSEMI )
    {
	    Expr();
    }
    else
    {
    	vm_genI(op_pushint,1);
    }
	l2 = vm_genI(op_jnz,0);
	l3 = vm_genI(op_jmp,0);
	
	/* incr */
	SkipToken(tSEMI);
	vm_patch(l3,vm_addr());
	l4 = vm_addr();
    if( Token != tRPAREN )
    {
	    AssignmentStatement();
        while( Token==tCOMMA )
        {
            SkipToken(tCOMMA);
            AssignmentStatement();
        }
    }
	vm_genI(op_jmp,l1);
	
	/* statements */
	SkipToken(tRPAREN);
	vm_patch(l2,vm_addr());
	StatementList();
	vm_genI(op_jmp,l4);
	
	/* done */
	vm_patch(l3,vm_addr());
	CheckBreak();
	
	SkipToken(tEND);
}
예제 #5
0
파일: parser.c 프로젝트: markcheno/topaz
static void IfStatement(void)
{
	Int32 ii=0;
	Int32 l1,l2[MAXCOND];
	
	SkipToken(tIF);
	
	Expr();
	
	if( Token==tTHEN ) 
		NextToken();
	
	l1 = vm_genI(op_jz,0);
	
	StatementList();

	l2[ii++] = vm_genI(op_jmp,0);
	
	while( Token==tELSIF )
	{
		SkipToken(tELSIF);
		vm_patch(l1,vm_addr());
		Expr();
		if( Token==tTHEN ) NextToken();
		l1 = vm_genI(op_jz,0);
		StatementList();
		l2[ii++] = vm_genI(op_jmp,0);
	}

	vm_patch(l1,vm_addr());
	
	if( Token==tELSE )
	{
		SkipToken(tELSE);
		StatementList();
	}

	while( --ii >= 0 )
	{
		vm_patch(l2[ii],vm_addr());
	}
	
	SkipToken(tEND);
}
예제 #6
0
Node* Parser::ExternalDecl() {
	Node* def = FunctionDefinitionStmt();
	
	if(def == NULL) {
		if(simple)
			return StatementList();

		return DeclarationList();
	}
	return def;
}
예제 #7
0
/* <Statement List Prime> ::= <Stamement List> | epsilon */
void SyntaxAnalyzer::StatementListPrime()
{
	if(StatementList())
    {
		logger.Log("<Statement List Prime> ::= <Stamement List>");
    }
	else
    {
        // epsilon
		logger.Log("<Statement List Prime> ::= epsilon");
    }    
}
예제 #8
0
Node* Parser::StatementList() {
	Node *first = Statement();

	if(first != NULL) {
		Node *second = StatementList();
		if(second == NULL)
			return first;

		return new NodeStmt("(stmt)", first, second);
	}
	return NULL;
}
예제 #9
0
파일: parser.c 프로젝트: markcheno/topaz
static void MethodDefinition(void)
{
	Pointer name;
	SymPtr sym;
	Int32 l1,shift;
	
	SkipToken(tDEF);
	/* MatchToken(tVARIABLE); */

	name = GetIdent();
	/* maybe check for duplicates here? */
	sym = SymAdd(name);
	sym->kind = FUNCTION_KIND;
	sym->object.u.ival = vm_addr();
	sym->flags |= cur_scope;
	if( in_class && base_class!=NULL ) sym->clas=base_class;
    if( in_class && super_class!=NULL ) sym->super=super_class;
    
	NextToken();
	SymEnterScope();
	in_method=TRUE;
	l1 = vm_genI(op_link,0);
		
	/* check for any formal parameters */
	if( Token==tLPAREN )
	{
		FormalParamList(sym);
	}
	
	/* function statements */
	local_num=0;
	StatementList();
	sym->nlocs = local_num;

	/* backpatch any return statements */
	CheckReturn(sym);

	/* implicit return */
	shift = in_class ? 2 : 1;
	vm_genI(op_rts,sym->nlocs+sym->nargs+shift);
	
	/* reserve space for locals */
	vm_patch(l1,sym->nlocs);	
	
	SymExitScope(sym);
	in_method=FALSE;
	/* end of function */
	SkipToken(tEND);
	if( Token==tSEMI ) NextToken();
}
예제 #10
0
Node* Parser::CompoundStmt() {
	if(oper == FIGURE_LEFT_BRACKET) {
		Next();
		Node *decl = DeclarationList();
		Node *stmt = StatementList();

		if(oper != FIGURE_RIGHT_BRACKET)
			throw ParserException(currentToken, "Compound statement without '}'");
		else
			Next();
		
		return new NodeStmt("{stmt}", decl, stmt);
	}
	return NULL;
}
예제 #11
0
파일: parser.c 프로젝트: markcheno/topaz
static void UntilStatement(void)
{
	Int32 l1,l2;

	SkipToken(tUNTIL);

	l1 = vm_addr();
	Expr();
	if( Token==tDO ) NextToken();
	l2 = vm_genI(op_jnz,0);
	StatementList();
	vm_genI(op_jmp,l1);
	vm_patch(l2,vm_addr());
	CheckBreak();
	
	SkipToken(tEND);
}
예제 #12
0
bool syntaxparser::StatementList(){
	bool bStatementList = false;
	if (displayFlag){
		cout<<"<Statement List> ::= <Statement> <Statement List>"<<endl;
		printproduction("<Statement List> ::= <Statement> <Statement List>");
	}
	
	if(Statement()){
		bStatementList = true;
	}
	else
		error("Missing Statement");
		if (token == "keyword" || token == "identifier"){
			bStatementList = true;
			StatementList();
		}
	return bStatementList;
}
예제 #13
0
// Purpose: Tests the production rule for Compound
bool syntaxparser::Compound(){
	bool bCompound = false;
	if (lexeme == "{"){
		print();
		cout<<endl;
		Lexer();

		if(displayFlag){
			cout << "<compound> ::= { <Statement List> }" << endl;
			printproduction("<compound> ::= { <Statement List> }");
		}
		StatementList();
			if(lexeme == "}"){
				print();
				cout<<endl;
				bCompound = true;
				Lexer();
			}else
				error("Missing '}'");
	}
	return bCompound; 
}
예제 #14
0
/* <Body> ::= { <Statement List> } */
void SyntaxAnalyzer::Body()
{
    if(rrr == s_openbracketcurly)
    {
        NextRecord();
        StatementList();
        
        if(rrr == s_closebracketcurly)
        {
            NextRecord();
			logger.Log("<Body> ::= { <Statement List> }");
        }
        else
        {
            insert(s_closebracketcurly);
        }
    }
    else
    {
        insert(s_openbracketcurly);
    }
}
예제 #15
0
// Purpose: Tests the production rule for Body
bool syntaxparser::Body(){
	bool body = false;
	if(lexeme == "{"){
		 print(); 
		 Lexer(); 
		 if(displayFlag){
			cout<<endl<<"<Body> ::= { <Statement List> }"<<endl;
			printproduction("<Body> ::= { <Statement List> }");
		 }
		 StatementList();			
			if(lexeme == "}"){
				print();
				Lexer();
				body = true;
			}else{
				error("Missing '}'");				
			}
	}else{
		error("Missing '{'");		
	}
	return body;
}
예제 #16
0
void syntaxparser::Rat10F(){
	setDisplay();
	Lexer(); print();
	if( lexeme == "$$"){
		if (displayFlag){
			//cout << "<Rat10F> ::= $$<Opt Function Definitions> $$ <Opt Declaration List> <Statement List> $$" << endl;
			//printproduction("<Rat10F> ::= $$<Opt Function Definitions> $$ <Opt Declaration List> <Statement List> $$");
			cout << "<Rat10F> ::= $$ $$ <Opt Declaration List> <Statement List> $$" << endl;
			printproduction("<Rat10F> ::= $$ $$ <Opt Declaration List> <Statement List> $$");
			
		}	
		//OptFunctionDefinitions();
		Lexer();
		if( lexeme == "$$"){
			Lexer(); 
			OptDeclarationList();

			project3.StopAddingtoSymbolTable(); 
			
			StatementList();

			//project3.printsymboltable(assemblyfilename); 
			project3.printInstTable(assemblyfilename); // printing the project3 instruction table to file
			
			if( lexeme == "$$"){
				print();
				cout<<"========>>>>> Syntax is Correct! <<<<<=========="<<endl;
			}
			else if( lexeme != "$$")
				error("Missing Finishing $$");	
		}
		else if( lexeme != "$$")
			error("Missing $$ after Function definitions");
	}
	else if( lexeme != "$$")
		error("Missing $$");
}
예제 #17
0
/* <Compound> ::= { <Statement List> } */
bool SyntaxAnalyzer::Compound()
{ 
	if(rrr == s_openbracketcurly)
	{
        NextRecord();
        StatementList();
        if(rrr == s_closebracketcurly)
        {
            NextRecord();
			logger.Log("<Compound> ::= { <Statement List> }");
			return true;
        }
        else
        {
			insert(s_closebracketcurly);
            return false;
        }
	}
    else
    {
        // no error
	    return false;
    }
}