I've been trying to write a language translator using flex and yacc with C.Generally I am getting it done in a way but I found myself not understanding some important things in order to finish it.
First of all I can't seem to understand how the following grammar rules work
declaration-list → declaration-list declaration
local-declarations → local-declarations declaration
I have seen an example here How do i implement If statement in Flex/bison on a "statements statement", quoting:
struct AstElement* makeStatement(struct AstElement* result, struct AstElement* toAppend)
{
if(!result)
{
result = checkAlloc(sizeof(*result));
result->kind = ekStatements;
result->data.statements.count = 0;
result->data.statements.statements = 0;
}
assert(ekStatements == result->kind);
result->data.statements.count++;
result->data.statements.statements = realloc(result->data.statements.statements, result->data.statements.count*sizeof(*result->data.statements.statements));
result->data.statements.statements[result->data.statements.count-1] = toAppend;
return result;
}
But I don't know if it will be the same for the other rules as well, plus, I want to understand what I am doing and not just copy.
My second problem is in the following grammar rule
declaration → type-spec id ; | type-spec id [ num ] ;
type-specifier → int
I am getting the whole point here, what it is for, what it should do etc. but I can't convert my understandings to code and C.