help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

About the FOR loop semantic action


From: Ilyes Gouta
Subject: About the FOR loop semantic action
Date: Sat, 20 Oct 2007 13:41:31 +0200

Hi all,

I'm in the process of writing an interpreter for my simplified C-like
language. I've already added the support for expressions and "if then
else" statements but I'm having some hard time figuring out how to
support loops.

My grammar is defined as following:

#define YYSTYPE_IS_DECLARED

typedef struct {
    union {
        int ival;
        float fval;
    } val;
    int type;
    char* id;
} YYSTYPE;

unary_expr:
    CONSTINT                              { $<val.ival>$ =
$<val.ival>1; $<type>$ = 0; }
    | CONSTFLOAT                       { $<val.fval>$ = $<val.fval>1;
$<type>$ = 1; }
    | IDENTIFIER                           { getvalue(&($$), $<id>1); }
    | '(' expr ')'                                {
getunaryvalue(&($$), &($2)); }
;

expr:
    unary_expr
    | expr '+' unary_expr                 { add(&($$), &($1), &($3)); }
    | expr '-' unary_expr                 { substract(&($$), &($1), &($3)); }
    | expr '*' unary_expr                 { multiply(&($$), &($1), &($3)); }
    | expr '/' unary_expr                 { divide(&($$), &($1), &($3)); }
    | '-' unary_expr                        { minus(&($$), &($2)); }
    | expr EQ unary_expr               { cmpeq(&($$), &($1), &($3)); }
    | expr NE unary_expr               { cmpne(&($$), &($1), &($3)); }
    | expr LE unary_expr                { cmple(&($$), &($1), &($3)); }
    | expr GE unary_expr                { cmpge(&($$), &($1), &($3)); }
    | expr '<' unary_expr                 { cmplt(&($$), &($1), &($3)); }
    | expr '>' unary_expr                 { cmpgt(&($$), &($1), &($3)); }
;

assignment_stmt:
    IDENTIFIER '=' expr ';'               { setvalue($<id>1, &($3)); }
;

for_stmt:
    FOR '(' expr ';' expr ';' expr ')' block
;

My question is, how am I going to write my action so that it executes
the for_stmt block while the second expression/condition is valid?

I also have a second questio: Is it possible to instruct bison to skip
one part of a semantic rule if a given condition isn't met? It's
typically useful for the "if then else" closure, i.e:

if_stmt:
    IF expr { update(&($2)); } block { reset(); }
;

where block won't be parsed/executed if the expr isn't valid.

BR,
Ilyes Gouta.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]