Go to the first, previous, next, last section, table of contents.


Semantic actions

Parsers are never created just to accept sentences from some language; they will always do something in response. In a gp grammar, such actions can be added using `{...}'. For example, to let the parser from the previous example (ex1) note on its output each time it has just read an opening parenthesis (`('), we could modify the expr rule like this:

expr: '(' { [[[stdio out] print "read another `('"] nl]; } expr* ')';

and end up with something quite unreadable. To increase readability, especially when we need to add a lot of semantic actions, we adopt the coding style of putting the actions clearly indented and on a separate line:

expr:
          '('
                {
                  [[[stdio out] print "read another `('"] nl];
                }
          expr* ')'
        ;


Go to the first, previous, next, last section, table of contents.