so i am facing a problem where it executes the PRINT command though return value is false
eg:
<code>func main(){
a -> 9;
print ( a );
if (a > 10){
print ( 99 );
}
}
</code>
<code>func main(){
a -> 9;
print ( a );
if (a > 10){
print ( 99 );
}
}
</code>
func main(){
a -> 9;
print ( a );
if (a > 10){
print ( 99 );
}
}
so 99 shouldn’t be printed as expected but it prints , how can i avoid it?
<code>Result: 9
Result: 99
statements : empty
statements : statement statements
Condition false, skipping if block
statements : empty
statements : statement statements
statements : statement statements
statements : statement statements
function : FUNC IDENT LPAR RPAR LCURL statements RCURL
functions : empty
functions : function functions
program : functions
</code>
<code>Result: 9
Result: 99
statements : empty
statements : statement statements
Condition false, skipping if block
statements : empty
statements : statement statements
statements : statement statements
statements : statement statements
function : FUNC IDENT LPAR RPAR LCURL statements RCURL
functions : empty
functions : function functions
program : functions
</code>
Result: 9
Result: 99
statements : empty
statements : statement statements
Condition false, skipping if block
statements : empty
statements : statement statements
statements : statement statements
statements : statement statements
function : FUNC IDENT LPAR RPAR LCURL statements RCURL
functions : empty
functions : function functions
program : functions
<code> %{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "variables.h"
#define MAX_VARS 100
typedef struct {
char *name;
int value;
} Var;
Var vars[MAX_VARS];
int var_count = 0;
int yylex();
void yyerror(const char *s);
int get_var_value(const char *name);
void set_var_value(const char *name, int value);
%}
%union {
int ival;
char *sval;
}
%token <sval> IDENTIFIER
%token <ival> INTEGER
%token FUNC
%token LPAR
%token RPAR
%token LCURL
%token RCURL
%token PRINT
%token SEMICOLON
%token PLUS
%token IS
%token MINUS
%token MULTIPLY
%token DIVIDE
%token GREATER
%token LESS
%token EQUAL
%token DOUBLE_EQUAL
%token IF
%left PLUS MINUS
%left MULTIPLY DIVIDE
%type <ival> expression
%type <ival> statement
%type <ival> statements
%type <ival> function
%type <ival> functions
%type <ival> program
%type <ival> var_decl
%type <ival> condition
%start program
%%
program : functions { printf("program : functionsn"); }
;
functions : function functions { printf("functions : function functionsn"); }
| /* empty */ { printf("functions : emptyn"); }
;
function : FUNC IDENTIFIER LPAR RPAR LCURL statements RCURL { printf("function : FUNC IDENT LPAR RPAR LBIG statements RBIGn"); }
;
statements : statement statements { printf("statements : statement statementsn"); }
| /* empty */ { printf("statements : emptyn"); }
;
statement : PRINT LPAR expression RPAR SEMICOLON { printf("Result: %dn", $3); }
| var_decl SEMICOLON {}
| IF LPAR condition RPAR LCURL statements RCURL {
if ($3 == 1) {
printf("Condition truen");
} else if ( $3 == 0) {
printf("Condition false,skipping if blockn");
//skip the {} block of the if condition
}
}
;
condition : expression GREATER expression { $$ = $1 > $3; }
| expression LESS expression { $$ = $1 < $3; }
| expression DOUBLE_EQUAL expression { $$ = $1 == $3; }
;
/* toke $1 ** toke $3 */
var_decl : IDENTIFIER IS INTEGER { set_var_value($1, $3); $$ = $3; }
;
expression : expression PLUS expression { $$ = $1 + $3; }
| expression MINUS expression { $$ = $1 - $3; }
| expression MULTIPLY expression { $$ = $1 * $3; }
| expression DIVIDE expression { $$ = $1 / $3; }
| INTEGER { $$ = $1; }
| IDENTIFIER { $$ = get_var_value($1); }
;
%%
int main(void) {
return yyparse();
}
void yyerror(const char *s) {
printf("ERROR: %sn", s);
}
</code>
<code> %{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "variables.h"
#define MAX_VARS 100
typedef struct {
char *name;
int value;
} Var;
Var vars[MAX_VARS];
int var_count = 0;
int yylex();
void yyerror(const char *s);
int get_var_value(const char *name);
void set_var_value(const char *name, int value);
%}
%union {
int ival;
char *sval;
}
%token <sval> IDENTIFIER
%token <ival> INTEGER
%token FUNC
%token LPAR
%token RPAR
%token LCURL
%token RCURL
%token PRINT
%token SEMICOLON
%token PLUS
%token IS
%token MINUS
%token MULTIPLY
%token DIVIDE
%token GREATER
%token LESS
%token EQUAL
%token DOUBLE_EQUAL
%token IF
%left PLUS MINUS
%left MULTIPLY DIVIDE
%type <ival> expression
%type <ival> statement
%type <ival> statements
%type <ival> function
%type <ival> functions
%type <ival> program
%type <ival> var_decl
%type <ival> condition
%start program
%%
program : functions { printf("program : functionsn"); }
;
functions : function functions { printf("functions : function functionsn"); }
| /* empty */ { printf("functions : emptyn"); }
;
function : FUNC IDENTIFIER LPAR RPAR LCURL statements RCURL { printf("function : FUNC IDENT LPAR RPAR LBIG statements RBIGn"); }
;
statements : statement statements { printf("statements : statement statementsn"); }
| /* empty */ { printf("statements : emptyn"); }
;
statement : PRINT LPAR expression RPAR SEMICOLON { printf("Result: %dn", $3); }
| var_decl SEMICOLON {}
| IF LPAR condition RPAR LCURL statements RCURL {
if ($3 == 1) {
printf("Condition truen");
} else if ( $3 == 0) {
printf("Condition false,skipping if blockn");
//skip the {} block of the if condition
}
}
;
condition : expression GREATER expression { $$ = $1 > $3; }
| expression LESS expression { $$ = $1 < $3; }
| expression DOUBLE_EQUAL expression { $$ = $1 == $3; }
;
/* toke $1 ** toke $3 */
var_decl : IDENTIFIER IS INTEGER { set_var_value($1, $3); $$ = $3; }
;
expression : expression PLUS expression { $$ = $1 + $3; }
| expression MINUS expression { $$ = $1 - $3; }
| expression MULTIPLY expression { $$ = $1 * $3; }
| expression DIVIDE expression { $$ = $1 / $3; }
| INTEGER { $$ = $1; }
| IDENTIFIER { $$ = get_var_value($1); }
;
%%
int main(void) {
return yyparse();
}
void yyerror(const char *s) {
printf("ERROR: %sn", s);
}
</code>
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "variables.h"
#define MAX_VARS 100
typedef struct {
char *name;
int value;
} Var;
Var vars[MAX_VARS];
int var_count = 0;
int yylex();
void yyerror(const char *s);
int get_var_value(const char *name);
void set_var_value(const char *name, int value);
%}
%union {
int ival;
char *sval;
}
%token <sval> IDENTIFIER
%token <ival> INTEGER
%token FUNC
%token LPAR
%token RPAR
%token LCURL
%token RCURL
%token PRINT
%token SEMICOLON
%token PLUS
%token IS
%token MINUS
%token MULTIPLY
%token DIVIDE
%token GREATER
%token LESS
%token EQUAL
%token DOUBLE_EQUAL
%token IF
%left PLUS MINUS
%left MULTIPLY DIVIDE
%type <ival> expression
%type <ival> statement
%type <ival> statements
%type <ival> function
%type <ival> functions
%type <ival> program
%type <ival> var_decl
%type <ival> condition
%start program
%%
program : functions { printf("program : functionsn"); }
;
functions : function functions { printf("functions : function functionsn"); }
| /* empty */ { printf("functions : emptyn"); }
;
function : FUNC IDENTIFIER LPAR RPAR LCURL statements RCURL { printf("function : FUNC IDENT LPAR RPAR LBIG statements RBIGn"); }
;
statements : statement statements { printf("statements : statement statementsn"); }
| /* empty */ { printf("statements : emptyn"); }
;
statement : PRINT LPAR expression RPAR SEMICOLON { printf("Result: %dn", $3); }
| var_decl SEMICOLON {}
| IF LPAR condition RPAR LCURL statements RCURL {
if ($3 == 1) {
printf("Condition truen");
} else if ( $3 == 0) {
printf("Condition false,skipping if blockn");
//skip the {} block of the if condition
}
}
;
condition : expression GREATER expression { $$ = $1 > $3; }
| expression LESS expression { $$ = $1 < $3; }
| expression DOUBLE_EQUAL expression { $$ = $1 == $3; }
;
/* toke $1 ** toke $3 */
var_decl : IDENTIFIER IS INTEGER { set_var_value($1, $3); $$ = $3; }
;
expression : expression PLUS expression { $$ = $1 + $3; }
| expression MINUS expression { $$ = $1 - $3; }
| expression MULTIPLY expression { $$ = $1 * $3; }
| expression DIVIDE expression { $$ = $1 / $3; }
| INTEGER { $$ = $1; }
| IDENTIFIER { $$ = get_var_value($1); }
;
%%
int main(void) {
return yyparse();
}
void yyerror(const char *s) {
printf("ERROR: %sn", s);
}
I am noob at coding and i am just trying to learn bison and flex , if the condition is false not to execute the statements inside the if block
and got to next block
eg:
<code>func main(){
a -> 9;
print ( a );
if (a > 10){
print ( 99 );
}
b -> 10;
print( b );
}
</code>
<code>func main(){
a -> 9;
print ( a );
if (a > 10){
print ( 99 );
}
b -> 10;
print( b );
}
</code>
func main(){
a -> 9;
print ( a );
if (a > 10){
print ( 99 );
}
b -> 10;
print( b );
}