2020-05-15 23:51:08 +00:00
|
|
|
%{
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
int yylex();
|
|
|
|
|
|
|
|
void yyerror(char *s)
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
fprintf(stderr, "%s\n", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
/* Data structures for storing a programme. */
|
|
|
|
|
|
|
|
typedef struct var // a variable
|
|
|
|
{
|
|
|
|
char *name;
|
2020-05-17 01:20:08 +00:00
|
|
|
int class;
|
2020-05-16 22:06:02 +00:00
|
|
|
int value; // 0 = bool, 1 = int
|
2020-05-15 23:51:08 +00:00
|
|
|
struct var *next;
|
|
|
|
} var;
|
|
|
|
|
|
|
|
typedef struct varlist // variable reference (used for print statement)
|
|
|
|
{
|
|
|
|
struct var *var;
|
|
|
|
struct varlist *next;
|
|
|
|
} varlist;
|
|
|
|
|
|
|
|
typedef struct expr // boolean expression
|
|
|
|
{
|
2020-05-16 22:21:25 +00:00
|
|
|
int type; // TRUE, FALSE, OR, AND, EQUIV, NOT, 0 (variable), PLUS, TIMES, INT, EQUALS, LT
|
2020-05-17 01:20:08 +00:00
|
|
|
int class; // 0 = bool, 1 = int, 2 = both
|
2020-05-15 23:51:08 +00:00
|
|
|
var *var;
|
|
|
|
struct expr *left, *right;
|
|
|
|
} expr;
|
|
|
|
|
|
|
|
typedef struct stmt // command
|
|
|
|
{
|
2020-05-16 22:06:02 +00:00
|
|
|
int type; // ASSIGN, ';', WHILE, IF, PRINT
|
2020-05-15 23:51:08 +00:00
|
|
|
var *var;
|
|
|
|
expr *expr;
|
|
|
|
struct stmt *left, *right;
|
|
|
|
varlist *list;
|
|
|
|
} stmt;
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
/* All data pertaining to the programme are accessible from these two vars. */
|
|
|
|
|
|
|
|
var *program_vars;
|
2020-05-16 22:06:02 +00:00
|
|
|
var *program_ints;
|
2020-05-15 23:51:08 +00:00
|
|
|
stmt *program_stmts;
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
/* Functions for settting up data structures at parse time. */
|
|
|
|
|
2020-05-17 01:20:08 +00:00
|
|
|
var* make_ident (char *s, int class) // type = 0 for a boolean var, type = 1 for an int var
|
2020-05-15 23:51:08 +00:00
|
|
|
{
|
|
|
|
var *v = malloc(sizeof(var));
|
|
|
|
v->name = s;
|
2020-05-17 01:20:08 +00:00
|
|
|
v->class = class;
|
2020-05-15 23:51:08 +00:00
|
|
|
v->value = 0; // make variable false initially
|
|
|
|
v->next = NULL;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
var* find_ident (char *s)
|
|
|
|
{
|
|
|
|
var *v = program_vars;
|
2020-05-16 22:06:02 +00:00
|
|
|
while (v && strcmp(v->name,s))
|
|
|
|
v = v->next;
|
|
|
|
if (!v) {
|
2020-05-16 22:21:25 +00:00
|
|
|
// Check in int vars
|
2020-05-16 22:06:02 +00:00
|
|
|
v = program_ints;
|
|
|
|
while (v && strcmp(v->name, s))
|
|
|
|
v = v->next;
|
|
|
|
}
|
|
|
|
if (!v) {
|
|
|
|
yyerror("undeclared variable");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
var* make_int(int val)
|
|
|
|
{
|
|
|
|
var *v = malloc(sizeof(var));
|
|
|
|
v->name = NULL;
|
2020-05-17 01:20:08 +00:00
|
|
|
v->class = 1; // int
|
2020-05-16 22:06:02 +00:00
|
|
|
v->value = val;
|
|
|
|
v->next = NULL;
|
2020-05-15 23:51:08 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
varlist* make_varlist (char *s)
|
|
|
|
{
|
|
|
|
var *v = find_ident(s);
|
|
|
|
varlist *l = malloc(sizeof(varlist));
|
|
|
|
l->var = v;
|
|
|
|
l->next = NULL;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2020-05-17 01:20:08 +00:00
|
|
|
expr* make_expr (int type, int class, var *var, expr *left, expr *right)
|
2020-05-15 23:51:08 +00:00
|
|
|
{
|
|
|
|
expr *e = malloc(sizeof(expr));
|
|
|
|
e->type = type;
|
2020-05-17 01:20:08 +00:00
|
|
|
e->class = class;
|
2020-05-15 23:51:08 +00:00
|
|
|
e->var = var;
|
|
|
|
e->left = left;
|
|
|
|
e->right = right;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt* make_stmt (int type, var *var, expr *expr,
|
|
|
|
stmt *left, stmt *right, varlist *list)
|
|
|
|
{
|
|
|
|
stmt *s = malloc(sizeof(stmt));
|
|
|
|
s->type = type;
|
|
|
|
s->var = var;
|
|
|
|
s->expr = expr;
|
|
|
|
s->left = left;
|
|
|
|
s->right = right;
|
|
|
|
s->list = list;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
%}
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
|
|
/* types used by terminals and non-terminals */
|
|
|
|
|
|
|
|
%union {
|
|
|
|
char *i;
|
|
|
|
var *v;
|
|
|
|
varlist *l;
|
|
|
|
expr *e;
|
|
|
|
stmt *s;
|
2020-05-16 22:06:02 +00:00
|
|
|
int val;
|
2020-05-15 23:51:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 22:06:02 +00:00
|
|
|
%type <v> bool_declist
|
|
|
|
%type <v> int_declist
|
2020-05-15 23:51:08 +00:00
|
|
|
%type <l> varlist
|
|
|
|
%type <e> expr
|
|
|
|
%type <s> stmt assign
|
|
|
|
|
2020-05-16 22:06:02 +00:00
|
|
|
%token BOOL TYPE_INT WHILE DO OD IF THEN ELSE FI ASSIGN PRINT OR AND XOR NOT TRUE FALSE
|
2020-05-15 23:51:08 +00:00
|
|
|
%token <i> IDENT
|
2020-05-16 22:06:02 +00:00
|
|
|
%token <val> INT
|
2020-05-15 23:51:08 +00:00
|
|
|
|
|
|
|
%left ';'
|
|
|
|
|
|
|
|
%left OR XOR
|
|
|
|
%left AND
|
|
|
|
%right NOT
|
2020-05-16 00:49:34 +00:00
|
|
|
%left EQUIV
|
2020-05-16 22:06:02 +00:00
|
|
|
%left PLUS
|
|
|
|
%left TIMES
|
2020-05-16 22:21:25 +00:00
|
|
|
%left EQUALS
|
|
|
|
%left LT
|
2020-05-15 23:51:08 +00:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
2020-05-16 22:06:02 +00:00
|
|
|
prog : bools ints stmt { program_stmts = $3; }
|
|
|
|
| bools stmt { program_stmts = $2; }
|
|
|
|
| ints stmt { program_stmts = $2; }
|
|
|
|
| stmt { program_stmts = $1; }
|
|
|
|
|
|
|
|
bools : BOOL bool_declist ';' { program_vars = $2; }
|
2020-05-15 23:51:08 +00:00
|
|
|
|
2020-05-16 22:06:02 +00:00
|
|
|
bool_declist : IDENT { $$ = make_ident($1, 0); }
|
|
|
|
| bool_declist ',' IDENT { ($$ = make_ident($3, 0))->next = $1; }
|
2020-05-15 23:51:08 +00:00
|
|
|
|
2020-05-16 22:06:02 +00:00
|
|
|
ints : TYPE_INT int_declist ';' { program_ints = $2; }
|
|
|
|
|
|
|
|
int_declist : IDENT { $$ = make_ident($1, 1); }
|
|
|
|
| int_declist ',' IDENT { ($$ = make_ident($3, 1))->next = $1; }
|
2020-05-15 23:51:08 +00:00
|
|
|
|
|
|
|
stmt : assign
|
|
|
|
| stmt ';' stmt
|
|
|
|
{ $$ = make_stmt(';',NULL,NULL,$1,$3,NULL); }
|
|
|
|
| WHILE expr DO stmt OD
|
|
|
|
{ $$ = make_stmt(WHILE,NULL,$2,$4,NULL,NULL); }
|
2020-05-16 00:59:03 +00:00
|
|
|
| IF expr THEN stmt ELSE stmt FI
|
|
|
|
{ $$ = make_stmt(IF,NULL,$2,$4,$6,NULL); }
|
|
|
|
| IF expr THEN stmt FI
|
|
|
|
{ $$ = make_stmt(IF,NULL,$2,$4,NULL,NULL); }
|
2020-05-15 23:51:08 +00:00
|
|
|
| PRINT varlist
|
|
|
|
{ $$ = make_stmt(PRINT,NULL,NULL,NULL,NULL,$2); }
|
|
|
|
|
|
|
|
assign : IDENT ASSIGN expr
|
|
|
|
{ $$ = make_stmt(ASSIGN,find_ident($1),$3,NULL,NULL,NULL); }
|
|
|
|
|
|
|
|
varlist : IDENT { $$ = make_varlist($1); }
|
|
|
|
| varlist ',' IDENT { ($$ = make_varlist($3))->next = $1; }
|
|
|
|
|
2020-05-17 01:20:08 +00:00
|
|
|
expr : IDENT { $$ = make_expr(0, 2, find_ident($1), NULL, NULL); }
|
|
|
|
| expr XOR expr { $$ = make_expr(XOR, 0, NULL, $1, $3); }
|
|
|
|
| expr OR expr { $$ = make_expr(OR, 0, NULL, $1, $3); }
|
|
|
|
| expr AND expr { $$ = make_expr(AND, 0, NULL, $1, $3); }
|
|
|
|
| expr EQUIV expr { $$ = make_expr(EQUIV, 0, NULL, $1, $3); }
|
|
|
|
| NOT expr { $$ = make_expr(NOT, 0, NULL, $2, NULL); }
|
|
|
|
| TRUE { $$ = make_expr(TRUE, 0, NULL, NULL, NULL); }
|
|
|
|
| FALSE { $$ = make_expr(FALSE, 0, NULL, NULL, NULL); }
|
2020-05-15 23:51:08 +00:00
|
|
|
| '(' expr ')' { $$ = $2; }
|
2020-05-17 01:20:08 +00:00
|
|
|
| INT { $$ = make_expr(INT, 1, make_int($1), NULL, NULL); }
|
|
|
|
| expr TIMES expr { $$ = make_expr(TIMES, 1, NULL, $1, $3); }
|
|
|
|
| expr PLUS expr { $$ = make_expr(PLUS, 1, NULL, $1, $3); }
|
|
|
|
| expr LT expr { $$ = make_expr(LT, 0, NULL, $1, $3); }
|
|
|
|
| expr EQUALS expr { $$ = make_expr(EQUALS, 0, NULL, $1, $3); }
|
2020-05-16 22:06:02 +00:00
|
|
|
|
2020-05-15 23:51:08 +00:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
#include "langlex.c"
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
/* programme interpreter : */
|
|
|
|
|
|
|
|
int eval (expr *e)
|
|
|
|
{
|
|
|
|
switch (e->type)
|
|
|
|
{
|
|
|
|
case TRUE: return 1;
|
|
|
|
case FALSE: return 0;
|
2020-05-17 01:20:08 +00:00
|
|
|
case XOR: {
|
|
|
|
if (e->left->class != 0 || e->right->class != 0) {
|
|
|
|
yyerror("An expression has wrong types");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
return eval(e->left) ^ eval(e->right);
|
|
|
|
}
|
|
|
|
case OR: {
|
|
|
|
if (e->left->class != 0 || e->right != NULL && e->right->class != 0) {
|
|
|
|
yyerror("An expression has wrong types");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
return eval(e->left) || eval(e->right);
|
|
|
|
}
|
|
|
|
case AND: {
|
|
|
|
if (e->left->class != 0 || e->right->class != 0) {
|
|
|
|
yyerror("An expression has wrong types");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
return eval(e->left) && eval(e->right);
|
|
|
|
}
|
2020-05-16 00:49:34 +00:00
|
|
|
case EQUIV: {
|
2020-05-17 01:20:08 +00:00
|
|
|
if (e->left->class != 0 || e->right->class != 0) {
|
|
|
|
yyerror("An expression has wrong types");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2020-05-16 00:49:34 +00:00
|
|
|
int right = eval(e->right);
|
|
|
|
int left = eval(e->left);
|
|
|
|
return (right && left) || (!right && !left); // Équivalent avec left == right avec la définition.
|
2020-05-17 01:20:08 +00:00
|
|
|
// Toutefois, on privilégie cette forme car faux = 0 et vrai = tout entier non nul
|
2020-05-16 00:49:34 +00:00
|
|
|
}
|
2020-05-17 01:20:08 +00:00
|
|
|
case NOT: {
|
|
|
|
if (e->left->class != 0) {
|
|
|
|
yyerror("An expression has wrong types");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
return !eval(e->left);
|
|
|
|
}
|
|
|
|
case PLUS: {
|
|
|
|
if (e->left->class != 1 || e->right->class != 1) {
|
|
|
|
yyerror("An expression has wrong types");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2020-05-16 22:06:02 +00:00
|
|
|
return eval(e->left) + eval(e->right);
|
2020-05-17 01:20:08 +00:00
|
|
|
}
|
|
|
|
case TIMES: {
|
|
|
|
if (e->left->class != 1 || e->right->class != 1) {
|
|
|
|
yyerror("An expression has wrong types");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2020-05-16 22:06:02 +00:00
|
|
|
return eval(e->left) * eval(e->right);
|
2020-05-17 01:20:08 +00:00
|
|
|
}
|
|
|
|
case EQUALS: {
|
|
|
|
if (e->left->class != 1 || e->right->class != 1) {
|
|
|
|
yyerror("An expression has wrong types");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2020-05-16 22:21:25 +00:00
|
|
|
printf("%d, %d\n", eval(e->left), eval(e->right));
|
|
|
|
return eval(e->left) == eval(e->right) ? 1 : 0;
|
2020-05-17 01:20:08 +00:00
|
|
|
}
|
|
|
|
case LT: {
|
|
|
|
if (e->left->class != 1 || e->right->class != 1) {
|
|
|
|
yyerror("An expression has wrong types");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2020-05-16 22:21:25 +00:00
|
|
|
return eval(e->left) < eval(e->right) ? 1 : 0;
|
2020-05-17 01:20:08 +00:00
|
|
|
}
|
2020-05-16 22:06:02 +00:00
|
|
|
case INT:
|
|
|
|
case 0:
|
|
|
|
return e->var->value;
|
2020-05-15 23:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_vars (varlist *l)
|
|
|
|
{
|
|
|
|
// Attention, varlist is in reverse order (this was easier to set up
|
|
|
|
// at parse time, see the code for varlist in the grammar).
|
|
|
|
|
|
|
|
if (!l) return;
|
|
|
|
print_vars(l->next);
|
2020-05-17 01:20:08 +00:00
|
|
|
if (l->var->class == 0)
|
2020-05-16 22:06:02 +00:00
|
|
|
printf("%s = %c ", l->var->name, l->var->value ? 'T' : 'F');
|
2020-05-17 01:20:08 +00:00
|
|
|
else if (l->var->class == 1)
|
2020-05-16 22:06:02 +00:00
|
|
|
printf("%s = %d ", l->var->name, l->var->value);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "The type of the var %s is unknown", l->var->name);
|
2020-05-15 23:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void execute (stmt *s)
|
|
|
|
{
|
|
|
|
switch(s->type)
|
|
|
|
{
|
|
|
|
case ASSIGN:
|
2020-05-17 01:20:08 +00:00
|
|
|
if (s->var->class != s->expr->class) {
|
|
|
|
yyerror("An assignment has the wrong type");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2020-05-15 23:51:08 +00:00
|
|
|
s->var->value = eval(s->expr);
|
|
|
|
break;
|
|
|
|
case ';':
|
|
|
|
execute(s->left);
|
|
|
|
execute(s->right);
|
|
|
|
break;
|
|
|
|
case WHILE:
|
|
|
|
while (eval(s->expr)) execute(s->left);
|
|
|
|
break;
|
2020-05-16 00:59:03 +00:00
|
|
|
case IF:
|
|
|
|
if (eval(s->expr))
|
|
|
|
execute(s->left);
|
|
|
|
else if (s->right != NULL)
|
|
|
|
execute(s->right);
|
|
|
|
break;
|
2020-05-15 23:51:08 +00:00
|
|
|
case PRINT:
|
|
|
|
print_vars(s->list);
|
|
|
|
puts("");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc <= 1) { yyerror("no file specified"); exit(1); }
|
|
|
|
yyin = fopen(argv[1],"r");
|
|
|
|
if (!yyparse()) execute(program_stmts);
|
|
|
|
}
|