-2

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.

Community
  • 1
  • 1
codeNewbie
  • 19
  • 4
  • 1
    You should write code that does whatever you want your program to do when it matches the rule. – user253751 Sep 04 '16 at 20:58
  • Well yeah,i knew that much.i get the feeling that the code i need is specific though. – codeNewbie Sep 04 '16 at 21:06
  • What do you want your program to do when it matches the rule? – user253751 Sep 05 '16 at 02:39
  • @immibis the project doesnt say something specific to be done at this part.Though on a fairly similar project,in C++, there is the following code decl_list : decl_list decl { $$ = $1; $$->push_back($2); } | decl{ $$ = new vector; $$->push_back($1); } ; – codeNewbie Sep 05 '16 at 11:48
  • It seems that the project you refer to translates a full `decl`aration into a statement pushed onto *something unknown*. Look like a high-level language translator, not a compiler (am I wrong ?). What do you want to do ? – Jean-Baptiste Yunès Sep 05 '16 at 18:32
  • @Jean Yeah, i am so sorry if i blew up with my explanations.A language translator is what im trying to do,you are not wrong. – codeNewbie Sep 05 '16 at 21:02
  • then you should catch the source code structure and transform it into destination code structure, cannot help much. In your example, it pushes a internal declaration statement representation into the stack of destination language statements list. I suppose that at the end it writes all statements in the destination language. – Jean-Baptiste Yunès Sep 05 '16 at 21:21

1 Answers1

2

In Yacc you can provide code for any rule encountered by the parser and that code (in general) provide a value for the parent rule from it derived, at the end the value is the transformed program. For your second example:

declaration → type-spec id ; | type-spec id [ num ] ;

type-specifier → int

The second rule should probably return something to identify the encountered int type; that could be the string itself or some internal identifier for that type. So it should be something like:

type-specifier → int { $$ = $1 };

which says the value of type-specifier is what the lexer send for token int.

Then suppose it has been derived from the other rule, that one is clearly something that is used to declared a simple variable or an array, then the yacc rule could be something like:

declaration → type-spec id         { addSymbol($1,$2); } ;
            | type-spec id [ num ] { addSymbolForArray($1,$2,$3); };

where the two function respectively add a new symbol to your internal representation of symbol table, with the type (if you need to encode the type somewhere in the symbol table), the identifier and in the array case the size of the array.

The same apply for any rule, but sometimes rules makes mostly nothing, as (probably) your:

declaration-list → declaration-list declaration

as declarations have been tackled inside declaration derivation rule.

Remember that this is a simple suggestion to make things clearer to you, there is a lot of things to take care about. But the principles are there: ask yourself, what you have to do when a rule has been parsed, what does it means for your semantic?

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Thank you very much for your answer.It indeed made some things clearer for me.If you have some time,please take a look at my latest comment on my question.I am trying to figure out if i should put something similar to that code to mine,since the projects are pretty identical. – codeNewbie Sep 05 '16 at 12:02