grammar is now complete

This commit is contained in:
overflowerror 2021-05-20 20:01:31 +02:00
parent e01841c1eb
commit 0eb49b5161
4 changed files with 78 additions and 2 deletions

View file

@ -2,6 +2,7 @@
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "common.h"
@ -21,4 +22,6 @@ void _panic(const char* function, const char* format, ...) {
}
fprintf(stderr, "\n");
exit(4);
}

View file

@ -15,10 +15,14 @@ extern void yyerror(char*);
%union {
struct tree tree;
struct params params;
struct template template;
char* text;
}
%type <template> template
%type <template> metaSection
%type <tree> mainSection
%type <params> parameter
@ -27,6 +31,7 @@ extern void yyerror(char*);
%type <text> statementHeader
%type <text> blockStatement
%type <text> metaStatement
%type <text> statement
%type <text> output
%type <text> texts
@ -37,16 +42,40 @@ extern void yyerror(char*);
%token STATEMENT_BEGIN STATEMENT_END
%token OUTPUT_BEGIN OUTPUT_END
%start file
%start template
%%
file: metaSection SECTION mainSection
template: metaSection SECTION mainSection
{
$$ = $1;
$$.tree = $3;
}
| mainSection
{
$$ = newTemplate();
$$.tree = $1;
}
;
metaSection: /* empty */
{
$$ = newTemplate();
}
| PARAMS_BEGIN parameters PARAMS_END metaSection
{
$$ = $4;
if ($$.params.no != 0) {
yyerror("only one parameter block allowed in meta section");
YYERROR;
}
$$.params = $2;
}
| STATEMENT_BEGIN metaStatement STATEMENT_END metaSection
{
$$ = $4;
addStat(&$$.stats, $2);
}
;
parameters: /* empty */
@ -77,6 +106,9 @@ moreParameters: /* empty */
;
metaStatement: statement
{
$$ = $1;
}
;
mainSection: /* empty */

View file

@ -135,3 +135,29 @@ struct params combineParams(struct params p1, struct params p2) {
return p;
}
struct stats newStats() {
return (struct stats) {
.texts = NULL,
.no = 0
};
}
void addStat(struct stats* stats, char* text) {
void* tmp = realloc(stats->texts, sizeof(char*) * (stats->no + 1));
if (tmp == NULL) {
panic("realloc");
}
stats->texts = tmp;
stats->texts[stats->no++] = text;
}
struct template newTemplate() {
return (struct template) {
.params = newParams(),
.stats = newStats(),
.tree = newTree()
};
}

View file

@ -40,5 +40,20 @@ struct params newParams();
void addParam(struct params*, char*, char*);
struct params combineParams(struct params, struct params);
struct stats {
char** texts;
size_t no;
};
struct stats newStats();
void addStat(struct stats*, char*);
struct template {
struct params params;
struct tree tree;
struct stats stats;
};
struct template newTemplate();
#endif