[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Preceding action
From: |
Hans Åberg |
Subject: |
Re: Preceding action |
Date: |
Fri, 10 Nov 2017 21:57:15 +0100 |
> On 10 Nov 2017, at 19:17, Péter <address@hidden> wrote:
>
> Can one assume anything about the order of grammar actions?
> At a given action, can one be sure which action was the previous (just
> finished) action?
>
> For example, in case of a list:
>
> list: element {/*action xx*/} | list ',' element {/*action yy*/};
> element: A | B | C | D ;
>
> At action yy, is it true (always) that the (immediately) preceding action
> must be an action xx or action yy?
>
> In other words: can one action start something and subsequent actions
> continue and finish it? For example, taking the list elements one by one,
> left to right, and appending them to a list (or concatenating to a buffer).
The $k values of the rule items that come before the action are on the stack
when the action executes. (One can also have mid-rule actions-see the manual.)
So if you want to build a list, including the empty one, pseudocode for that
might be, showing off the Bison feature of using action variable names $x, $y,
etc., which help against numbering errors; and alos defining strings for token
values, which is more flexible than just using single character tokens:
%token LEFT_BRACKET "["
%token RIGHT_BRACKET "]"
%token COMMA ","
%%
list:
"[" sequence "]"
sequence:
%empty { $$ = new list; }
| sequence[x] "," element[y] { $$ = $x.append($y); }
;