Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #103850

Re: compile "a = 2*f+(d-g/3)*(9-i)"

From BartC <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: compile "a = 2*f+(d-g/3)*(9-i)"
Date 2017-02-13 23:21 +0000
Organization A noiseless patient Spider
Message-ID <o7teuv$jt1$1@dont-email.me> (permalink)
References <ce233d83-2f17-4dad-8f38-9d0aa0fde624@googlegroups.com> <720c394b-bdb5-4b2d-8d51-38185ddd9d8f@googlegroups.com> <o7t3ne$6e2$1@dont-email.me> <0d305c57-cf15-42ed-b440-55cb709fbe38@googlegroups.com>

Show all headers | View raw


On 13/02/2017 20:36, fir wrote:
> W dniu poniedziałek, 13 lutego 2017 21:10:21 UTC+1 użytkownik Bart napisał:

>> https://rosettacode.org/wiki/Compiler
>>
>> Look at the links to the tasks (the different parts of a compiler).
>> There is sample code in various languages. Including C.
>>
> wonder if there some simple rules i could use and what they are

 > a = 2*f+(d-g/3)*(9-i) ->
 >
 > t1=2*f
 > t2=g/3
 > t3=9-1

> m not sure hoe to do that? define input array
> in form of 'sylables' (register names + operators) and go in passes and reduce it and
> create output array as array of only (r = a operator b) ? this could be some idea maybe,
>
> could read this general writings but it consumes to much time sometimes to find
> most important points
>
> some more hints?

What are you trying to do?

* Evaluate an expression?
* Convert an expression into code for VM?
* Into code for a real processor?
* Into three-address code (this is what your t1/t2/t3 looks like)?

What format is the expression in:

* In text as it is normally written
* In text but in reverse Polish (that might be easier for you...)
* Represented as a data structure (because tokenising is very fiddly)?

If this is aiming towards writing a compiler or something, then there's 
quite a lot involved, but also masses of information around.

I would also recommend not using C for this, not unless speed is a 
consideration. Probably you will get fed up with it, that will be sooner 
if you have to mess about with C taking care of millions of details.

But here is one demo in C, that takes an expression 2+2 represented as 
reverse polish in a data structure, and evaluates it. I've no idea if 
this is of any use (I just fancied writing some C):

#include <stdio.h>
#include <stdlib.h>

typedef long long int Int;

enum {add,sub,mul,divide,load,end};

typedef struct {int op; Int value;} Term;

Term expr[]={		// 2+2
  {load, 2},
  {load, 2},
  {add, 0},
  {end, 0}};

Int stack[1000];
int stackindex=-1;

Int pop(void){return stack[stackindex--];}
void push(Int x){stack[++stackindex]=x;}

Int evaluate(Term* expr){
Int b,result, stop=0;

while (1) {
	switch (expr->op) {
	case add:
		push(pop()+pop());
		break;
	case sub:
		b=pop();
		push(pop()-b);
		break;
	case mul:
		push(pop()*pop());
		break;
	case divide:
		b=pop();
		if (b==0) {puts("div by zero"); exit(0);}
		push(pop()/b);
		break;
	case load:
		push(expr->value);
		break;
	case end:
		return pop();
	}
	++expr;
}
return 0;
}

int main(void){
Int x;

x=evaluate(expr);

printf("Result = %lld\n",x);

}


-- 
Bartc

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 11:43 -0800
  Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 11:44 -0800
    Re: compile "a = 2*f+(d-g/3)*(9-i)" BartC <bc@freeuk.com> - 2017-02-13 20:10 +0000
      Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 12:36 -0800
        Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 13:31 -0800
        Re: compile "a = 2*f+(d-g/3)*(9-i)" BartC <bc@freeuk.com> - 2017-02-13 23:21 +0000
          Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-14 00:27 -0800
  Re: compile "a = 2*f+(d-g/3)*(9-i)" supercat@casperkitty.com - 2017-02-13 13:29 -0800
    Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 13:44 -0800
      Re: compile "a = 2*f+(d-g/3)*(9-i)" supercat@casperkitty.com - 2017-02-13 14:01 -0800
        Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 14:16 -0800
          Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-13 14:17 -0800
  Re: compile "a = 2*f+(d-g/3)*(9-i)" luser droog <luser.droog@gmail.com> - 2017-02-13 19:36 -0800
    Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-14 00:32 -0800
  Re: compile "a = 2*f+(d-g/3)*(9-i)" John Bode <jfbode1029@gmail.com> - 2017-02-15 13:27 -0800
    Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-15 13:47 -0800
      Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-15 14:13 -0800
        Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-15 14:16 -0800
          Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-15 14:40 -0800
      Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-16 14:57 -0800
        Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-16 15:02 -0800
          Re: compile "a = 2*f+(d-g/3)*(9-i)" fir <profesor.fir@gmail.com> - 2017-02-21 11:15 -0800

csiph-web