struct statement *elsepart;
};
- struct expr {
- struct expression *e;
- };
- struct stmt {
- struct statement *s;
- };
-
# code
#include <stdlib.h>
#include <fcntl.h>
free_expression(e->right);
free(e);
}
- static void free_expr(struct expr *e)
- {
- free_expression(e->e);
- free(e);
- }
+
static void free_statement(struct statement *s)
{
if (!s)
free(s);
}
- static void free_stmt(struct stmt *s)
- {
- free_statement(s->s);
- free(s);
- }
-
static void print_expression(struct expression *e)
{
if (e->left && e->right) printf("(");
~~~~~~
-Program -> Statementlist ${ print_statement($1.s, 0); }$
+Program -> Statementlist ${ print_statement($1, 0); }$
OptNL -> NEWLINE
|
OptSemi -> NEWLINE
| ;
-$stmt
- Statementlist -> Statements ${ $0.s = $1.s; $1.s = NULL; }$
+$*statement
+ Statementlist -> Statements ${ $0 = $1; $1 = NULL; }$
Statements -> Statements Statement ${
{
struct statement **s;
$0 = $1;
- s = &($0.s);
+ s = &$0;
while (*s)
s = &(*s)->next;
if (body[2])
- *s = $2.s;
+ *s = $2;
if (body[0])
- $1.s = NULL;
+ $1 = NULL;
if (body[2])
- $2.s = NULL;
+ $2 = NULL;
}
}$
- | Statements OptSemi ${ $0.s = $1.s; $1.s = NULL; }$
- | ${ $0.s = NULL; }$
- | ERROR ${ printf("statement ERROR\n"); $0.s = NULL; }$
+ | Statements OptSemi ${ $0 = $1; $1 = NULL; }$
+ | ${ $0 = NULL; }$
+ | ERROR ${ printf("statement ERROR\n"); $0 = NULL; }$
- Block -> OptNL { Statementlist } OptNL ${ $0 = $3; $3.s = NULL; }$
- | : Statementlist ${ $0 = $2; $2.s = NULL; }$
+ Block -> OptNL { Statementlist } OptNL ${ $0 = $3; $3 = NULL; }$
+ | : Statementlist ${ $0 = $2; $2 = NULL; }$
Statement -> Factor = Expression OptSemi ${
- $0.s = calloc(1, sizeof(struct statement));
- $0.s->expr = calloc(1, sizeof(struct expression));
- $0.s->expr->left = $1.e; $1.e = NULL;
- $0.s->expr->op = $2.txt;
- $0.s->expr->right = $3.e; $3.e = NULL;
+ $0 = calloc(1, sizeof(struct statement));
+ $0->expr = calloc(1, sizeof(struct expression));
+ $0->expr->left = $1; $1 = NULL;
+ $0->expr->op = $2.txt;
+ $0->expr->right = $3; $3 = NULL;
}$
| if Expression Block OptNL ${
- $0.s = calloc(1, sizeof(struct statement));
- $0.s->expr = $2.e; $2.e = NULL;
- $0.s->thenpart = $3.s;
- $3.s = NULL;
+ $0 = calloc(1, sizeof(struct statement));
+ $0->expr = $2; $2 = NULL;
+ $0->thenpart = $3;
+ $3 = NULL;
}$
| if Expression Statement ${
- $0.s = calloc(1, sizeof(struct statement));
- $0.s->expr = $2.e; $2.e = NULL;
- $0.s->thenpart = $3.s;
- $3.s = NULL;
+ $0 = calloc(1, sizeof(struct statement));
+ $0->expr = $2; $2 = NULL;
+ $0->thenpart = $3;
+ $3 = NULL;
}$
| if Expression Block OptNL else Block ${
- $0.s = calloc(1, sizeof(struct statement));
- $0.s->expr = $2.e; $2.e = NULL;
- $0.s->thenpart = $3.s;
- $3.s = NULL;
- $0.s->elsepart = $6.s;
- $6.s = NULL;
+ $0 = calloc(1, sizeof(struct statement));
+ $0->expr = $2; $2 = NULL;
+ $0->thenpart = $3;
+ $3 = NULL;
+ $0->elsepart = $6;
+ $6 = NULL;
}$
| if Expression Block OptNL else Statement ${
- $0.s = calloc(1, sizeof(struct statement));
- $0.s->expr = $2.e; $2.e = NULL;
- $0.s->thenpart = $3.s;
- $3.s = NULL;
- $0.s->elsepart = $6.s;
- $6.s = NULL;
+ $0 = calloc(1, sizeof(struct statement));
+ $0->expr = $2; $2 = NULL;
+ $0->thenpart = $3;
+ $3 = NULL;
+ $0->elsepart = $6;
+ $6 = NULL;
}$
-$expr
+$*expression
Expression -> Expression + Term ${
- $0.e = calloc(1, sizeof(struct expression));
- $0.e->op = $2.txt;
- $0.e->left = $1.e; $1.e = NULL;
- $0.e->right = $3.e; $3.e= NULL;
+ $0 = calloc(1, sizeof(struct expression));
+ $0->op = $2.txt;
+ $0->left = $1; $1 = NULL;
+ $0->right = $3; $3 = NULL;
}$
| Expression - Term ${
- $0.e = calloc(1, sizeof(struct expression));
- $0.e->op = $2.txt;
- $0.e->left = $1.e; $1.e = NULL;
- $0.e->right = $3.e; $3.e= NULL;
+ $0 = calloc(1, sizeof(struct expression));
+ $0->op = $2.txt;
+ $0->left = $1; $1 = NULL;
+ $0->right = $3; $3 = NULL;
}$
- | Term ${ $0.e = $1.e; $1.e = NULL; }$
+ | Term ${ $0 = $1; $1 = NULL; }$
Term -> Term * Factor ${
- $0.e = calloc(1, sizeof(struct expression));
- $0.e->op = $2.txt;
- $0.e->left = $1.e; $1.e = NULL;
- $0.e->right = $3.e; $3.e= NULL;
+ $0 = calloc(1, sizeof(struct expression));
+ $0->op = $2.txt;
+ $0->left = $1; $1 = NULL;
+ $0->right = $3; $3 = NULL;
}$
| Term / Factor ${
- $0.e = calloc(1, sizeof(struct expression));
- $0.e->op = $2.txt;
- $0.e->left = $1.e; $1.e = NULL;
- $0.e->right = $3.e; $3.e= NULL;
+ $0 = calloc(1, sizeof(struct expression));
+ $0->op = $2.txt;
+ $0->left = $1; $1 = NULL;
+ $0->right = $3; $3 = NULL;
}$
- | Factor ${ $0.e = $1.e; $1.e = NULL; }$
+ | Factor ${ $0 = $1; $1 = NULL; }$
Factor -> IDENTIFIER ${
- $0.e = calloc(1, sizeof(struct expression));
- $0.e->op = $1.txt;
+ $0 = calloc(1, sizeof(struct expression));
+ $0->op = $1.txt;
}$