void statement(void)
{
	token tok = next_token();
	expr_rec source,target;

	switch (tok){
		case ID:
			/*<statement> ::= ID := <expression>; */
			match(ID);
			target = process_id();
			match(ASSIGNOP);
			expression(& source); 
			match(SEMICOLON);
			assign(target,source);
			break;

		case READ:
			/*<statement> ::= READ (<id list>); */
			match(READ); match(LPAREN);
			id_list(); match(RPAREN);
			match(SEMICOLON);
			break;

		case WRITE:
			/*<statement> ::= WRITE (<expr list>); */
			match(WRITE); match(LPAREN);
			expr_list(); match(RPAREN);
			match(SEMICOLON);
			break;

		default:
			syntax_error(tok);
			break;
	}
}
예제 #2
0
파일: parser.c 프로젝트: Master76/compiler
int statement(int count)
{
char buffer[200];
token * begin;
begin=c;
memset(buffer, 0, sizeof buffer);

	//ID:=<expression>;
	if(c->token_number == ID)
	{
        	advance();
		if(c->token_number == ASSIGNOP)
		{
            		advance();
			if(expression())
			{
                		//advance();
				if(c->token_number == SEMICOLON)
				{
					advance();
                                        while(c!=begin)
                                        {
                                            //fprintf(of_d.temp1, "Token number %d\tToken type %s\t\tActual %s\n", begin->token_number, begin->token_type, begin->buffer);
                                            sprintf(buffer+strlen(buffer),"%s",begin->buffer);
                                            begin=(token *) begin->next;
                                        }
                                        //fprintf(of_d.temp1,"\nPROGRAM LINE: %s\n\n\n",buffer);
                                        //fprintf(of_d.listing_file, "%d:\t%s\n", count, buffer);
                                        //valid statement
                                        return 1;
				}
				else
				{
                        printf("Missing semicolon\n");
					return 0;
				}
			}
			else
			{
				//keep increment pointer until after ;?
				printf("Invalid expression\n");
				return 0;
			}
		}
		else
		{
			//keep increment pointer until after ;?
			printf("Missing assignment operator\n");
			return 0;
		}
	}
	//READ(<id_list>);
	else if (c->token_number == READ)
	{
        	advance();
		if (c->token_number == LPAREN)
		{
			if(id_list()==1)
			{
                		advance();
				if (c->token_number == RPAREN)
				{
                    			advance();
					if (c->token_number == SEMICOLON)
					{
					    advance();
                                            while(c!=begin)
                                            {
                                                //fprintf(of_d.temp1, "Token number %d\tToken type %s\t\tActual %s\n", begin->token_number, begin->token_type, begin->buffer);
                                                sprintf(buffer+strlen(buffer),"%s",begin->buffer);
                                                begin=(token *) begin->next;
                                            }
                                            //fprintf(of_d.temp1,"\nPROGRAM LINE: %s\n\n\n",buffer);
                                            //valid statement
                                            return 1;
					}
					else
					{
                        		    printf("Missing semicolon\n");
				            return 0;
					}
				}
				else
				{
                    			printf("Missing right parentheses\n");
					return 0;
				}
			}
			else
			{
               			 printf("Expected at least one identifier\n");
				return 0;
			}
		}
		else
		{
            		printf("Expected left parentheses\n");
			return 0;
		}
	}
	//WRITE(<expr_list>);
	else if (c->token_number == WRITE)
	{
		advance();
		if (c->token_number == LPAREN)
		{
            		advance();
			if(expr_list()==1)
			{
				if (c->token_number == RPAREN)
				{
                    	   		advance();
					if (c->token_number == SEMICOLON)
					 {
				             advance();
                                             while(c!=begin)
                                       	     {
                                                 //fprintf(of_d.temp1, "Token number %d\tToken type %s\t\tActual %s\n", begin->token_number, begin->token_type, begin->buffer);
                                                 sprintf(buffer+strlen(buffer),"%s",begin->buffer);
                                                 begin=(token *) begin->next;
                                             }
                                        //fprintf(of_d.temp1,"\nPROGRAM LINE: %s\n\n\n",buffer);
                                        //valid statement
                                        return 1;
					}
					else
					{
                            		    printf("Missing semicolon\n");
					    return 0;
					}
				}
			}
			else
			{
                		printf("Invalid expression in list\n");
				return 0;
			}
		}
		else
		{
           		 printf("Expected left parentheses\n");
			return 0;
		}

	}
	//else not a statement
	else
	{
		//printf("Not a statement\n");
		return 0;
	}
}