2

I'm trying to write my own scripting language using flex and bison. I have a basic parser and I would like to add a for statement very similar to the C language for statement. It is not clear to me how to code the action associated to the for statement

Suppose I have the following production for the 'for' statement:

forStatement: FOR '(' expr ';' expr ';' expr ')' statements END; {}

It is not clear to me what to do in the action associated to this production.

Intuitively I understand that I should do something, in the action associated to the previous statement, such as :

evaluate($3);
while(evaluate($5)) { execute($9); evaluate($7); }
evaluate($7);

where evaluate and execute are two C functions.

So I have two questions (suppose to write C code for the action associated to the grammar production):

  1. What is the task of 'evaluate'. I mean, how do I evaluate the expression at every loop considering that the value of the expression potentially changes at every step of the loop?
  2. What is the task of 'execute'. I mean, how do I evaluate the statements inside the for loop considering that each statement has a different outcome at every step of the loop.

The values of the three expressions 'expr' changes at runtime and the same is true for the statements inside the for body.

5
  • Well, how have you dealt with any of your other statements? The structure of for is a bit twisted, but you should be able to reuse what you've already got for the rest of your language. (And what is that else block doing there with no if?)
    – Mat
    Mar 24, 2013 at 15:16
  • Wall to tell the truth I don' have much else. I do have expressions involving mathematical operations, variable assignements, and a symbol table to store delacred variables. I fixed the else stuff. My idea was to start with this since a do...while and while statements are similar and a if is also similar since is like a while loop executing once.
    – Salvatore
    Mar 24, 2013 at 15:24
  • 1
    Then I'm afraid your question is a bit too broad for Stack Overflow. There are entire books dedicated to writing compilers/interpreters, it is challenging (but very educational I hear). And you should be looking at starting with the most simple things, not the most complex. (I.e. start with the plain if, then add else, then possibly while.) You'll find lots of resources here alredy (e.g. via this: stackoverflow.com/questions/453870/…)
    – Mat
    Mar 24, 2013 at 15:33
  • Anyway, I'm not asking to provide me the code of the execute and evaluate functions. I just need to understand at least in general what to do. The problem I'm trying to address is: how do I use a 'fresh' value for 'expr' and 'statements' at each loop. I don't want to reuse the value computed the first time. Probably the solution is to visit each time the AST created by the parser starting from $3, $5, $7, and $9 as root nodes recomputing all the values?
    – Salvatore
    Mar 24, 2013 at 15:45
  • Yes, I understand what you're trying to do. What I'm saying is that what you're trying to do is potentially very complex. Whole books are written about how you interpret or compile arbitrary statements/expressions. And I don't think you're on the right track with this, you should probably be looking into creating an AST first, then interpreting it. As I said, this is a very broad topic. Maybe someone can answer with some pointers, but you should be doing some research yourself at this point too.
    – Mat
    Mar 24, 2013 at 15:48

2 Answers 2

2

Looking at your two questions, you seem to miss that an execution engine (a kind of software CPU) is needed. This engine needs to remember the state of variables, return 'addresses' of loops, ... (depending on implementation method chosen).

So parsing is just the first step. Think about creating a data structure for each statement and expression, and have these executed by this engine.

Have a look at stack-based systems.

1

This is a FAQ, not only on SO, but also in Bison. Please, read the documentation: http://www.gnu.org/software/bison/manual/html_node/Implementing-Gotos_002fLoops.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.