Data structure for compiling a postfix expression

Hi, I am writing an rpn calculator in C and I have to compile the rpn expression into bytecode. But I'm not sure what the data structure will be for their presentation ??? Still stack data structure

 struct stack_t{
        int type;
        union {  
              double val;
              char *str;
              /* probably some more */
             }u;
    };
+3
source share
3 answers

Actually, it depends on the feature set that you are going to support. For example, if your calculator supports integers (e.g. integer division), you will probably need intyour union.

+1
source

- RPN , . //. , , -.

, , . ? ?

Forth, RPN.

0

, , , , "" , , , . X11.

typedef struct {
    word tag;
    word pad;
    dword padw;
} mark_;

typedef struct {
    word tag;
    word pad;
    integer val;
} int_;

typedef struct {
    word tag;
    word pad;
    real val;
} real_;

typedef struct {
    word tag;
    word sz;
    word ent;
    word off;
} comp_;

typedef union {
    word tag;

    mark_ mark_;
    int_ int_;
    real_ real_;
    comp_ comp_;
} object;

, , . .

: "" "type", , .


: , , - .u . stk.u.val stk.real_.val stk.int_.val. , . tag - , , .

0

Source: https://habr.com/ru/post/1733807/


All Articles