Пример #1
0
/* <If> ::= if ( <Condition> ) <Statement> <If Prime> */
bool SyntaxAnalyzer::If()
{
    if(rrr == k_if)
    {
        NextRecord();
        if(rrr == s_openbracketround)
        {
            NextRecord();
            Condition();
            if(rrr == s_closebracketround)
            {
                NextRecord();
				Statement();
                IfPrime();
                logger.Log("<If> ::= if ( <Condition> ) <Statement> <If Prime>");
                return true;
            }
            else
            {
                insert(s_closebracketround);
                return true;
            }
        }
        else
        {
            insert(s_openbracketround);
            return true;
        }
    }
    else
    {
        // no error
        return false;
    }
}
Пример #2
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);
    }
}
Пример #3
0
/* <Assign> ::= <Identifier> := <Expression> ; */
bool SyntaxAnalyzer::Assign()
{
	if(rrr == Identifier)
	{
        NextRecord();
        if(rrr == o_equal)
        {
            NextRecord();
            Expression();
            if(rrr == s_semicolon)
            {
                NextRecord();
                logger.Log("Assign ::= <Identifier> := <Expression>");
                return true;
            }
            else
            {
                insert(s_semicolon);
                return true;
            }               
        }
        else
        {
            insert(o_equal);
            return true;
        }
	}
    return false;
}
Пример #4
0
/* <Qualifier> ::= int | boolean | real */
bool SyntaxAnalyzer::Qualifier()
{
	if(rrr == k_int)
	{
        NextRecord();
		logger.Log("<Qualifier> ::= int");
        return true;
	}
	else if(rrr == k_boolean)
	{
        NextRecord();        
		logger.Log("<Qualifier> ::= boolean");
        return true;
	}
	else if(rrr == k_real)
	{
        NextRecord();
		logger.Log("<Qualifier> ::= real");
        return true;
	}
	else{
        // no error
        return false;
	}
}
Пример #5
0
/* <While> ::= while ( <Condition> ) <Statement> */
bool SyntaxAnalyzer::While()
{  
    if(rrr == k_while)
    {
        NextRecord();
        if(rrr == s_openbracketround)
        {
            NextRecord();
            Condition();
            if(rrr == s_closebracketround)
            {
				NextRecord();
                Statement();
                // log
				logger.Log("While> ::= while ( <Condition> ) <Statement>");
                return true;
            }
            else
            {
                insert(s_closebracketround);
                return true;
            }
        }
        else
        {
            insert(s_openbracketround);
            return true;
        }
    }
    else
    {
        // no error
        return false;
    } 
}
Пример #6
0
void SyntaxAnalyzer::insert(Record record)
{
    this->logger.Log("Error! Expected Record { " + record.ToString() + "}");
    this->logger.Log("Inserted fake expected Record");
    this->rrr = record;
    NextRecord();
}
Пример #7
0
/* <Parameter> ::= <IDs> : <Qualifier> */
bool SyntaxAnalyzer::Parameter()
{
	if(IDs())
    {        
        if(rrr == s_colon)
        {
            NextRecord();
	        if(Qualifier())
            {                
				logger.Log("<Parameter> ::= <IDs> : <Qualifier>");
                return true;
            }
            else
            {
               insert(k_int);
               return true;
            }
        }
        else
        {
            insert(s_colon);
            return true;
        }
    }
    else
    {
        // no error
        return false;
    }
}
Пример #8
0
/* <Write> ::= print ( <Expression> ); */
bool SyntaxAnalyzer::Write()
{
    if(rrr == k_print)
    {
        NextRecord();
        if(rrr == s_openbracketround)
        {
            NextRecord();
            Expression();
            if(rrr == s_closebracketround)
            {
                NextRecord();
                if(rrr == s_semicolon)
                {
                    NextRecord();
					logger.Log("<Write> ::= print ( <Expression> );");
                    
                    return true;
                }
                else
                {
                    insert(s_semicolon);
                    return true;
                }
            }
            else
            {
                insert(s_closebracketround);
                return true;
            }
        }
        else
        {
            insert(s_openbracketround);
            return true;
        }
    }
    else
    {
        // no error
		return false;
    }
}
Пример #9
0
/* <If Prime> ::= endif | else <Statement> endif */
void SyntaxAnalyzer::IfPrime()
{
    if(rrr == k_endif)
    {
        NextRecord();
        logger.Log("<If Prime> ::= endif>");
    }
    else if(rrr == k_else)
    {
        NextRecord();
        Statement();
        if(rrr == k_endif)
        {
            NextRecord();
			logger.Log("<If Prime> ::= endif | else <Statement> endif");
        }
        else
        {
            insert(k_endif);
        }
    }
    else
    {
        // assume they forgot an else
        insert(k_else);
        if(Statement())
        {
            if(rrr == k_endif)
            {
                NextRecord();
			    logger.Log("<If Prime> ::= endif | else <Statement> endif");
            }
            else
            {
                insert(k_endif);
            }
        }
        else
        {
            insert(k_endif);
        }
    }
}
Пример #10
0
/* <Read> ::= scan ( <IDs> ); */
bool SyntaxAnalyzer::Read()
{
    if(rrr == k_scan)
    { 
        NextRecord();
        if(rrr == s_openbracketround)
        {
            NextRecord();
            IDs();
            
            if(rrr == s_closebracketround)
            {
                NextRecord();
                if(rrr == s_semicolon)
                {
                    NextRecord();
					logger.Log("<Read> ::= scan ( <IDs> );");
                    return true;
                }
                else
                {
                    insert(s_semicolon);
                    return true;
                }
            }
            else
            {
                insert(s_closebracketround);
                return true;
            }
        }
        else
        {
            insert(s_openbracketround);
            return true;
        }
    }
    else
    {
        // no error
        return false;
    }
}
Пример #11
0
/* <Return> ::=  return <Return Prime> */
bool SyntaxAnalyzer::Return()
{
    if(rrr == k_return)
    {		
        NextRecord();
		ReturnPrime();
        logger.Log("<Return> ::=  return <Return Prime>");
        return true;
    }
    return false;
}
Пример #12
0
/* <IDs Prime> ::= , <IDs> | epsilon */
void SyntaxAnalyzer::IDsPrime()
{
    if(rrr == s_comma)
    {
        NextRecord();
	    IDs();
        logger.Log("<IDs Prime> ::= , <IDs>");
    }
    else
    {
		logger.Log("<IDs Prime> ::= epsilon");
    }
}
Пример #13
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);
    }
}
Пример #14
0
/* <Return Prime> ::= <Expression> ; | ; epsilon */
void SyntaxAnalyzer::ReturnPrime()
{
    if(rrr == s_semicolon)
    {
        NextRecord();
        logger.Log("<Return Prime> ::= ; epsilon");
    }
    else
    {
		Expression();
        if(rrr == s_semicolon)
        {
            NextRecord();
            // log
            logger.Log("<Return Prime> ::= <Expression> ;");
        }
        else
        {
            insert(s_semicolon);
        }
    }
}
Пример #15
0
/* <Function> ::= function <Identifier> ( <Opt Parameter List> ) <Opt Declaration List> <Body> */
void SyntaxAnalyzer::Function()
{
    if(rrr == k_function)
    {
        NextRecord();
        if(rrr == Identifier)
        {
            NextRecord();
            if(rrr == s_openbracketround)
            {
                NextRecord();
                OptParameterList();                
                if(rrr == s_closebracketround)
                {			
                    NextRecord();
                    OptDeclarationList();
                    Body();
                    logger.Log("<Function> ::= function <Identifier> ( <Opt Parameter List> ) <Opt Declaration List> <Body>");
                }
                else
                {
                    insert(s_closebracketround);
                }
            }
            else
            {
                insert(s_openbracketround);
            }
        }
        else
        {
            insert(Identifier, "MISSING IDENTIFIER");
        }
    }
    else
    {
        insert(k_function);
    }
}
Пример #16
0
/* <PrimaryPrime> ::= [<IDs>] | epsilon */
void SyntaxAnalyzer::PrimaryPrime()
{
    if(rrr == s_openbracketsquare)
    {
        NextRecord();
        IDs();
        if(rrr == s_closebracketsquare)
        {
            NextRecord();
            // log
            logger.Log("<PrimaryPrime> ::= [<IDs>]");
        }
        else
        {
            insert(s_closebracketsquare);
        }
    }
    else
    {       
		logger.Log("<PrimaryPrime> ::= epsilon");
    }
}
Пример #17
0
/* <Expression Prime> ::= + <Term> <Expression Prime> | - <Term> <Expression Prime> | epsilon */
void SyntaxAnalyzer::ExpressionPrime()
{
    
    if(rrr == o_plus)
    {
        NextRecord();
        Term();
        ExpressionPrime();
		logger.Log("<Expression Prime> ::= + <Term> <Expression Prime>");
    }
    else if(rrr == o_minus)
    {	
        NextRecord();
		Term();
        ExpressionPrime();
        logger.Log("<Expression Prime> ::= + <Term> <Expression Prime>");        
    }
    else// if(epsilon())
    {
		logger.Log(" <Expression Prime> ::= epsilon");
    }
}
Пример #18
0
/* <Term Prime> ::= * <Factor> <Term Prime> | / <Factor> <Term Prime> | epsilon */
void SyntaxAnalyzer::TermPrime()
{
    
    if(rrr == o_multiply)
    {
        NextRecord();
		logger.Log("<Term Prime> ::= * <Factor> <Term Prime>");
		Factor();
	    TermPrime();
    }
    else if(rrr == o_divide)
    {
        NextRecord();
		logger.Log("<Term Prime> ::= / <Factor> <Term Prime>");
        Factor();
        TermPrime();
    }
    else
    {
		logger.Log("<Term Prime> ::= epsilon");
    }
}
Пример #19
0
/* <Factor> ::= - <Primary> | <Primary> */
void SyntaxAnalyzer::Factor()
{ 
    if(rrr == o_minus)
    {	
        NextRecord();
	    Primary();
        logger.Log("<Factor> ::= - <Primary>");
    }
	else
    {		
        Primary();
        logger.Log("<Factor> ::= <Primary>");
    }
}
Пример #20
0
/* <IDs> ::= <Identifier> <IDs Prime> */
bool SyntaxAnalyzer::IDs()
{
    if(rrr == Identifier)
    {		
        NextRecord();
        IDsPrime();
        logger.Log("<IDs> ::= <Identifier> <IDs Prime>");
        return true;
    }
    else
    {
        // no error
        return false;
    }
}
Пример #21
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;
    }
}
Пример #22
0
/* <Parameter List Prime> ::= , <Parameter List> | epsilon */
void SyntaxAnalyzer::ParameterListPrime()
{
    if(rrr == s_comma)
    {
        NextRecord();
        paramList = false;
		ParameterList();
        logger.Log("<Parameter List Prime> ::= , <Parameter List>");
    }
    else
    {
        // epsilon
		logger.Log("<Parameter List Prime> ::= epsilon");
    }
}
Пример #23
0
/* <Primary> ::= <Identifier> <PrimaryPrime> | <Integer> | ( <Expression> ) | <Real> | true | false */
void SyntaxAnalyzer::Primary()
{
    
    if(rrr == Identifier)
    {   
        NextRecord();
		PrimaryPrime();
        logger.Log("<Primary> ::= <Identifier><PrimaryPrime> ");
    }
    else if(rrr == Integer)
    {
        NextRecord();
		logger.Log("<Primary> ::= <Integer>");
    }
    else if(rrr == s_openbracketround)
    {
        NextRecord();
        Expression();
        if(rrr == s_closebracketround)
        {
            NextRecord();
			logger.Log("<Primary> := ( <Expression> )");
        }
        else
        {
            insert(s_openbracketround);
        }
    }
    else if(rrr == Real)
    {
        NextRecord();
		logger.Log("<Primary> ::= <Real> ");
    }
    else if(rrr == k_true)
    {
        NextRecord();
		logger.Log("<Primary> ::= <true> ");
    }
    else if(rrr == k_false)
    {
        NextRecord();
		logger.Log("<Primary> ::= <false> ");
    }   
    else
    {
        insert(Identifier, "MISSING_PRIMARY");
    }
}
Пример #24
0
/* <Declaration List> ::= <Declaration> ; <Declaration List Prime> */
bool SyntaxAnalyzer::DeclarationList()
{
	if(Declaration())
    {        
        if(rrr == s_semicolon)
        {
		    NextRecord();
	        DeclarationListPrime();
            logger.Log("<Declaration List> ::= <Declaration> ; <Declaration List Prime>");
            return true;
        }
        else
        {
            insert(s_semicolon);
            return true;
        }
	}
    else
    {
        // no error
        return false;
    }
}
Пример #25
0
/* <Relop> ::= = | /= | > | < | => | <= */
void SyntaxAnalyzer::Relop()
{
    if(rrr == o_equalequal)
    {
        NextRecord();
		logger.Log("<Relop> ::= =");
    }
    else if(rrr == o_notequal)
    {
        NextRecord();
		logger.Log("<Relop> ::= /=");
    }
    else if(rrr == o_greatorthan)
    {
        NextRecord();
		logger.Log("<Relop> ::= >");
	}
	else if(rrr == o_lessthan)
	{
        NextRecord();
		logger.Log("<Relop> ::= <");
	}
    else if(rrr == o_greatororequal)
    {
        NextRecord();
		logger.Log("<Relop> ::= =>");
    }
    else if(rrr == o_lessorequal)
    {
        NextRecord();
		logger.Log("<Relop> ::= =<");
    }
    else
    {
		insert(o_equalequal);		
	}
}
Пример #26
0
int main(int argc, char **argv)
{
    register int i;
    char   *current;
    char   *next;
    char   *uid;
    char   *pw;
    int    rc;
    int    fd;
    struct stat    buff;
    char   *auid = 0;
    char   *apw = 0;
    char   *filenm = 0;
    char   *area = 0;
    RPC2_EncryptionKey	key;
    
 /* parse arguments    */
    for (i = 1; i < argc; i++) {
	if (argv[i][0] == '-') {
	    if (strcmp(argv[i], "-f") == 0) {
		filenm = argv[++i];
		continue;
	    }
	    break;
	}
	if (auid == 0) {
	    auid = argv[i];
	    continue;
	}
	if (apw == 0) {
	    apw = argv[i];
	    continue;
	}
	break;
    }

    if (!auid || !apw || !filenm) {
	printf("usage: newuser -f filename authuserid authpasswd\n");
	fflush(stdout);
	exit(-1);
    }
 /* Bind to auth server using auid and apw */

    U_InitRPC();
    memset(key, 0, sizeof(RPC2_EncryptionKey));
    strncpy(key, apw, sizeof(RPC2_EncryptionKey));
    rc = U_BindToServer(1, auid, key, &AuthID);
    if(rc != AUTH_SUCCESS) {
	printf("Bind to Auth Server failed %s\n",U_Error(rc));
	fflush(stdout);
	exit(-2);
    }

 /* open input file and read it into area malloc */
    if(stat(filenm, &buff)) {
	printf("Could not stat %s because %d\n",filenm, errno);
	fflush(stdout);
	exit(-3);
    }
    area = (char *)malloc(buff.st_size+1);
    fd = open(filenm, O_RDONLY, 0);
    if(fd <= 0) {
	printf("Could not open %s because %d\n",filenm, errno);
	fflush(stdout);
	exit(-4);
    }
    rc = read(fd, area, buff.st_size);
    if(rc != buff.st_size) {
	printf("Could nor read %s got %d bytes instead of %d, error = %d\n",
		filenm, rc, buff.st_size, errno);
	fflush(stdout);
	exit(-5);
    }
    close(fd);
    *(area+buff.st_size+1) = '\0';

 /* parse data in area and pass it to AddNewUser. The first field in each line is the
    uid, the second field in each line is the password. Fields are blank separated */

    for(current = area; current < area+buff.st_size; current = next) {
	next = NextRecord(current);	/* next line */
	uid = current;				/* use first field  */
	pw = NextField(uid);			/* use second field */
	MakeString(uid);			/* make uid a string */
	MakeString(pw);			/* make pw a string */
	AddNewUser(uid, pw);
    }

 /* clean up and Unbind the connection */
    if(area) free(area);
    RPC2_Unbind(AuthID);
    return(0);
}