Implement a compiler and interpreter for a programming language where each program consists of a single arithmetic expression preceded by a sequence of assignment statements with arithmetic expressions involving integers and variables named with single lower-case characters. For example, given the input
(x = 1)
(y = (x + 1))
(((x + y) * 3) + (4 * x))
your program should print the value 13.


Difficulty level
This exercise is mostly suitable for students
 
 void printList2(matrix *node) {
	matrix *temp, *curr;
	matrix *head ;
	if(node)
	{
	head=node->right;
	printf("\n%d , %d ;\n", node->data.row, node->data.col);
	for (curr = head->right; curr != head; curr = curr->right)
		printf("%d , %d , %d ;\n", curr->data.row, curr->data.col, curr->data.value);
	printf("\n%d , %d ;\n", node->data.row, node->data.col);
	for (temp = head->down; temp != head; temp = temp->down)
	printf("%d , %d , %d ;\n", temp->data.row, temp->data.col, temp->data.value);
	}
}
int compare(matrix *node, matrix *nodeA) {
   //printList2(node);
    //printList2(nodeA);
	matrix *temp, *curr; 
	matrix *tempA, *currA; 
	matrix *head ;
	matrix *headA ;
	
	if(node)
	{
	    head=node->right;
	    headA=nodeA->right;
	    if(node->data.row!=nodeA->data.row || node->data.col!=nodeA->data.col) return 0;
	    
	    curr = head->right;
	    currA= headA->right;
	    while(1)
	    {
	        if(curr==head && currA==headA) break;

	        if((curr==head && currA!=headA) || (curr!=head && currA==headA) )
	        {
	            return 0;
	        }
	       
	       if((curr->data.row!=currA->data.row) || (curr->data.col!=currA->data.col)|| (curr->data.value!=currA->data.value)) return 0;
		    curr = curr->right;
		    currA = currA->right;
		    
	    }
	    
	     
	    if(node->data.row!=nodeA->data.row || node->data.col!=nodeA->data.col) return 0;
        
 
	    temp = head->down;
	    tempA = headA->down;
	   	 while(1)
	    {
	        if(temp==head && tempA==headA) break;

	        if((temp==head && tempA!=headA) || (temp!=head && tempA==headA) )
	        {
	            return 0;
	        }
	            
	       if((temp->data.row!=tempA->data.row) || (temp->data.col!=tempA->data.col)|| (temp->data.value!=tempA->data.value)) return 0;
		    temp = temp->down;
		    tempA = tempA->down;
	    }
	}

	if((node==NULL && nodeA==NULL) || (node && nodeA) )
	    return 1;

	 return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<limits.h>
#define N 20
 
#include<cstdio>
#include<cstdlib>
#include<string>
typedef char element[LINE_MAX];

typedef struct {
    element data[N];
    int top;
} stack;

stack CreateStack()
{
    stack p;
    p.top = -1;
    return p;
}

int isEmptyStack(stack p)
{
    return (p.top == -1);
}

int isFullStack(stack p)
{
    return (p.top == N - 1);
}

int Push(stack *p, element e)
{
    if (isFullStack(*p)) return 0;
    strcpy(p->data[++p->top] , e);
    return 1;
}

int Pop(stack *p)
{
    if (isEmptyStack(*p)) return 0;
    p->top--;
    return 1;
}

int Top(stack p, element *e)
{
    if (isEmptyStack(p)) return 0;
    strcpy(*e , p.data[p.top]);
    return 1;
}

int toint(element e)
{
    int i;
    int value=0, mult=1;

    for(i=strlen(e)-1;i>0;i--,mult*=10)
        value+=(e[i]-'0')*mult;
    //treat first element maybe it's -
    
    if(e[0]=='-')
        return -value;
    value+=(e[0]-'0')*mult;
    return value;
}

int isvariable(element e)
{
    return e[0]>='a' && e[0]<='z';
}

 
int is_digit(element e)
{
    
    return e[0]>='0' && e[0]<='9';
}

int evaluate()
{
    char str[LINE_MAX];
    stack s;
    int i ,value, mult;
    element e1,op,e2;
    int v[26];
    int v1, v2;
    
    s=CreateStack();

    while (fgets(str,LINE_MAX,stdin) != NULL)
    {
        str[strcspn(str, "\r\n")] = 0;
        //printf("%s\n",str);
        for(i=0;str[i];i++)
        {
        
            if(str[i]==')')
            {
                
                if(Top(s,&e2) && isvariable(e2))
                {
                    Pop(&s);
                }
                else
                {
                    v2=0;
                    mult=1;
                
                    while(Top(s,&e2) ) 
                    {
                        if(is_digit(e2) || is_digit(e2+1))
                        {
                            v2+=toint(e2)*mult; mult*=10;
                            Pop(&s);
                      
                        }
                        else
                            break;
                    }
                    //printf("##v2=%d##\n",v2);
                }
                
 
                Top(s,&op);Pop(&s);
                
                
                if(Top(s,&e1) && isvariable(e1))
                {
                    Pop(&s);
                }
                else
                {
                    v1=0;
                    mult=1;
                
                    while(Top(s,&e1) ) 
                    {
                        if(is_digit(e1) || *e1=='-')
                        {
                            v1+=toint(e1)*mult; mult*=10;
                            Pop(&s);
                      
                        }
                        else
                            break;
                    }
                }
                
            
               
                Pop(&s); // remove (
                //printf("%d  %d\n",v1 , v2);
                
                if(*op=='=') 
                {
                    
                    v[*e1-'a'] = v2;
                    
                    //printf("e1=%c v2=%d \n",e1[0],v2);
                    
                }
                else
                {
                    if(isvariable(e1)) v1=v[*e1-'a']; 
                    if(isvariable(e2)) v2=v[*e2-'a']; 
                  //printf("%d  %d\n",v1 , v2);
                    if(*op=='+') value = v1+v2;
                    if(*op=='-') value =v1-v2;
                    if(*op=='*') value =v1*v2;
    
                     //printf("op=%c v1=%d v2=%d value=%d\n",*op, v1,v2,value);
                    sprintf (e1, "%d",value);  

                    Push(&s,e1);
                    
                }
                
            }
            else
            {
                e1[0]=str[i];
                e1[1]='\0';

               Push(&s,e1);
               
            }
        }
        
        
        
        if(Top(s,&e1) && Pop(&s))
            return toint(e1);
    }
    return 0;
}

int main(){
    printf("%d",evaluate());
    return 0;
    
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Implementation of a set using an array of long integers as bits