Exemplo n.º 1
0
static enum token
t_lex(char *s, struct exists_data *ed)
{
    struct t_op const *op;

    ed->no_op = 0;
    if (s == NULL) {
        ed->t_wp_op = NULL;
        return EOI;
    }

    if ((op = findop(s)) != NULL) {
        if (!((op->op_type == UNOP && isoperand(ed)) ||
        (op->op_num == LPAREN && *(ed->t_wp+1) == 0))) {
            ed->t_wp_op = op;
            return (enum token)op->op_num;
        }
    }
    if (strlen(*(ed->t_wp)) > 0 && !op && !ed->t_wp_op) {
        ed->t_wp_op = findop("-N");
        ed->no_op = 1;
        return (enum token)ed->t_wp_op->op_num;
    } else {
        ed->t_wp_op = NULL;
    }
    return OPERAND;
}
Exemplo n.º 2
0
void postfix(char *infix,char *result)
{
int i=0,under,outpos=0;
char topsymb='+',symb;
p.top=-1;
while(infix[i]!='\0')
{
symb=infix[i++];
if(isoperand(symb)==1)
result[outpos++]=symb;
else
{
pop(&p,&topsymb,&under);
while(under==0 && prcd(topsymb,symb))
{
result[outpos++]=topsymb;
pop(&p,&topsymb,&under);
}
if(under==0)
push(&p,topsymb);
if(under==1 || (symb!=')'))
push(&p,symb);
else
pop(&p,&topsymb,&under);
}
}
while(empty(&p)!=1)
{
pop(&p,&topsymb,&under);
result[outpos++]=topsymb;
}
result[outpos]='\0';
}
struct tree* convert(char *c)
{
	treecreate();
	char x;
	int i=0;
	while(c[i]!='\0')
	{
		x=c[i++];
		if(isoperand(x)==1)
		{
			struct tree *node=(struct tree*)malloc(sizeof(struct tree));
			treepush(node);
		}
		else
		{
			struct tree *node=(struct tree*)malloc(sizeof(struct tree));
			node->right=treepop();
			node->left=treepop();
			node->data=x;
			treepush(node);
		}
	}		
	struct tree *node=(struct tree*)malloc(sizeof(struct tree));
	node=treepop();
	return node;
}
void evaluate(struct stack *s2,struct tree *node)
{
	char x;
		if(node!=NULL)
	{
		evaluate(s2,node->left);	
		evaluate(s2,node->right);	
		x=node->data;
		if(isoperand(x)==1)
			push(s2,x);
		else
		{
			int op1=pop(s2);
			int op2=pop(s2);
			switch(x)
			{
				case '+':{push(s2,op2+op1);break;}
				case '-':{push(s2,op2-op1);break;}
				case '*':{push(s2,op2*op1);break;}
				case '/':{push(s2,(int)(op2/op1));break;}
				case '$':{push(s2,(int)(pow(op2,op1)));break;}
				default:{printf("wrong symbol\n");exit(3);}
			}
		}
	}
}
void postfix(struct stack *s1,char *infix,char *result)
{
int i=0,x=0;
char c,temp;
while(infix[i]!='\0')
{
	c=infix[i++];
	if(isoperand(c)==1)
	{
		result[x++]=c;
		continue;
	}
	else
	{
		if(c=='(' || c=='{' || c=='[')
		{
			push(s1,c);
			continue;
		}
		if(c==')' || c=='}' || c==']')
		{
			c=pop(s1);
			while(!(c=='(' || c=='{' || c=='['))
			{
				result[x++]=c;
				c=pop(s1);
			}
			continue;
		}
		if(s.top==-1)
		push(s1,c);
		else
		{
			temp=pop(s1);
			if(preference(temp,c)==1)
			{
				result[x++]=temp;
				push(s1,c);
				continue;
			}
			else
			{
				push(s1,temp);
				push(s1,c);
			}
	
		}
	}
}
while(s.top!=-1)
{
result[x++]=pop(s1);
}
result[x]='\0';
}//end of postfix
Exemplo n.º 6
0
Arquivo: test.c Projeto: alimon/dash
static enum token t_lex(char **tp)
{
    struct t_op const *op;
    char *s = *tp;

    if (s == 0) {
        t_wp_op = (struct t_op *)0;
        return EOI;
    }

    op = getop(s);
    if (op && !(op->op_type == UNOP && isoperand(tp)) &&
            !(op->op_num == LPAREN && !tp[1])) {
        t_wp_op = op;
        return op->op_num;
    }

    t_wp_op = (struct t_op *)0;
    return OPERAND;
}
Exemplo n.º 7
0
static enum token
t_lex(char *s)
{
	struct t_op const *op;

	if (s == NULL) {
		t_wp_op = NULL;
		return EOI;
	}

	if ((op = findop(s)) != NULL) {
		if (!((op->op_type == UNOP && isoperand()) ||
		    (op->op_num == LPAREN && *(t_wp+1) == 0))) {
			t_wp_op = op;
			return op->op_num;
		}
	}
	t_wp_op = NULL;
	return OPERAND;
}
Exemplo n.º 8
0
static enum token
t_lex(char *s)
{
	struct t_op const *op;

	op = ops;

	if (s == 0) {
		t_wp_op = (struct t_op *)0;
		return EOI;
	}
	while (op->op_text) {
		if (strcmp(s, op->op_text) == 0) {
			if ((op->op_type == UNOP && isoperand()) ||
			    (op->op_num == LPAREN && *(t_wp+1) == 0))
				break;
			t_wp_op = op;
			return op->op_num;
		}
		op++;
	}
	t_wp_op = (struct t_op *)0;
	return OPERAND;
}
Exemplo n.º 9
0
/*To Covert Infix To Prefix*/
void intopre(char str1[],char pre[])
{
 	 STK s1;
int len,flag;
len=strlen(str1);
int check=0,cnt=len-1,pos=0;
char elem;

while(cnt>=0)  /*while condition*/
{
flag=0;
if(isoperand(str1[cnt]))   /*Checking for Operand*/
{
printf("%c",str1[cnt]);
cnt--;
pos++;
}
else
{
check=prcd(str1[cnt]);
while(check==false)
{
pre[pos]=str1[cnt];
flag=1;
pos++;
cnt--;
}
if(flag==0)
{
elem=pop(&s1);
printf("%c",elem);
}
}

}
}
Exemplo n.º 10
0
static enum token
t_lex(shinstance *psh, char *s)
{
	struct t_op const *op;

	op = ops;

	if (s == 0) {
		psh->t_wp_op = NULL;
		return EOI;
	}
	while (op->op_text) {
		if (strcmp(s, op->op_text) == 0) {
			if ((op->op_type == UNOP && isoperand(psh)) ||
			    (op->op_num == LPAREN && *(psh->t_wp+1) == 0))
				break;
			psh->t_wp_op = op;
			return op->op_num;
		}
		op++;
	}
	psh->t_wp_op = NULL;
	return OPERAND;
}
Exemplo n.º 11
0
int main()
{
    scanf("%s",inf);
    int i,flag=0,part1=0,part2=1;
    for(i=0;i<strlen(inf);i++)
    {
        if(inf[i]=='(')
		{
			s[++top]='(';
		}
		else if(inf[i]=='[')
		{
			s[++top]='[';
		}
		else if(inf[i]=='{')
		{
			s[++top]='{';
		}
		else if(inf[i]==')')
		{
			if(s[top]!='(')
            {
                flag=1;
                break;
            }
            top--;
		}
		else if(inf[i]==']')
		{
			if(s[top]!='[')
            {
                flag=1;
                break;
            }
            top--;
		}
		else if(inf[i]=='}')
		{
			if(s[top]!='{')
            {
                flag=1;
                break;
            }
            top--;
		}
    }
    if(flag==1)
    {
        part1=0;//printf("Incorrect Bracket placements\n");
    }
    else
    {
        if(top==-1)
        {
            part1=1;
        }
        else
        {
            part1=0;
        }
    }
    inf[strlen(inf)]=';';
    rem();//to remove all brackets
    for(i=0;inf2[i]!=';';i++)
    {
        if(isoperand(i) && isoperand(i+1))
        {
            part2=0;
            break;
        }
        if(inf2[i+1]!=';')
        {
            if( isoperand(i) && isoperator(i+1))
            {
                if(inf2[i+2]==';')
                {
                    part2=0;
                    break;
                }
                else if( !isoperand(i+2) )
                {
                    part2=0;
                    break;
                }
            }
        }
    }
    if( part1 && part2)
    {
        printf("Correct\n");
    }
    else
    {
        printf("Incorrect:/\n");
    }
    return 0;
}
Exemplo n.º 12
0
int getop ( char s [] )

{
    int i = 0;
    int c = 0;
    int c2 = 0;
    int t = 0;
    unsigned short int match = 0;
    unsigned short int exit = 0;
   
    while ( ( s [ 0 ] = c = getch () ) == ' ' || c == '\t' ) 
        ;
    s [ 1 ] = '\0';
    
   if ( isdigit ( c ) || c == '.' ) {
        i = 0;
    
        if ( isdigit ( c ) )	/* getting whole part */
            while ( isdigit ( s [ ++i ] = c = getch () ) )
            ;
    
        if ( c == '.' )		/* getting fractional part */
            while ( isdigit ( s [ ++i ] = c = getch () ) ){
                printf ( "s [ i = %d ] = %d in digit and %c in char;\n", i, s [ i ], s [ i ] );
            };   

        s [ i ] = '\0';
        if ( c != ' ' ) 
            return 0;
//        if ( c != EOF )
  //          ungetch ( c );
         printf ( "Whole string is:%s;\n", s );    
        return NUMBER_0;
    }
    else {
        if ( isoperand ( c ) || c == '\n' ) {
            return c;
        }
        else {
            if ( isletter ( c ) ) {
                clear_string ( string_symb );
                string_symb [ 0 ] = c;
                str_symb_p = 1;
                if ( ( c2 = getch () ) == ' ' ) {
                    ungetch ( c2 );
                    string_symb [ 1 ] = '\0';
                    str_symb_p = 1;
                    match = 0;
                    if ( ( match = recogn_string ( string_symb ) ) > 0 ) {
                        if ( last_val > 0 ) {
//                            def_pre_last_val ( match );   
                            pre_last_val = last_val;
                            last_val = match;
                        }
                        else {
//                            def_last_val ( match );
                            last_val = match;
                        };
                        clear_string ( string_symb );
                        return convert_match_to_predefided ( match );
//                        return match;
                    }
                    else {
                        return 0;                   
                    }
                }
                else {
                    if ( isletter ( c2 ) == 1 ){
                        string_symb [ 1 ] = c2;
                        str_symb_p = 2;
                        match = 0;
                    }
                    else {
                        clear_string ( string_symb );
                        return 0;
                    };
                    printf ( "2.string_symb = %s. str_symb_p = %d.\n", string_symb, str_symb_p );
                    match = 0;
                    while ( match < 1 ) {
                        if ( str_symb_p > MAXLEN ) {
                            clear_string ( string_symb );
                            return 0;
                        };
                        match = recogn_string ( string_symb );
                        printf ( "after recognizing match = %d, str_symb_p = %d, and string : \'%s\'.\n", match, str_symb_p, string_symb );
                        if ( match == 0 ) {
                           string_symb [ str_symb_p ++ ] = c = getch (); 
                        }
                        else {
                            clear_string ( string_symb );
                            return convert_match_to_predefided ( match );
//                            return match;
                        }
                        if ( isletter ( c ) == 0 ) {
                            clear_string ( string_symb );
                            return 0;
                        }
                    };
                    printf ( "after recogn match = %d, string = %s;\n", match, string_symb );    
                };  
            }
            else {
                if ( c == '=' )
                    return c;
                if ( c == EOF ) {
                    return ( -1 ); 
                }
                else {
                    return 0; 
                };
            }
        }      

    }
}