1

I am building a small compiler for an assignment language.

Consider the following rule:

var_block : 
  | LPAREN var_decl+ RPAREN { var_scope := var_scope + 1 };

Is the semantic action triggered when var_block is first recognized or is it triggered once the end of the production is reached (in this case, RPAREN)?

2 Answers2

7

Your question is exactly why you shouldn't do this.

Don't do side-effect which are order-sensitive in production rules. Productions rules should be used to build up a data structure that represent your program. Once this is done, you can analyze/execute/whatever it.

Drup
  • 3,679
  • 13
  • 14
1

It is triggered after the whole production is read.

James Wilcox
  • 5,307
  • 16
  • 25
  • Thanks for your response. What would be the best way to keep track of context changes between declarations of objects contained in different blocks and with different storage properties? – Santiago Nuñez-Corrales Aug 15 '17 at 19:14
  • It depends on exactly what you're trying to do. One easy thing to do would be to make the semantic value of each production a syntax tree, then the `var_block` production would just wrap its list of children in another tree node that marks the scope. – James Wilcox Aug 15 '17 at 19:29