E. Parsing and Execution

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Dictionary

E. Parsing and Execution
A sentence is evaluated by executing its phrases in a sequence determined by the parsing rules of the language. For example, in the sentence`10%3+2` ,`` the phrase`3+2` is evaluated first to obtain a result that is then used to divide`10`. In summary:

1. | Execution proceeds from right to left, except that when a right parenthesis is encountered, the segment enclosed by it and its matching left parenthesis is executed, and its result replaces the entire segment and its enclosing parentheses.
2. | Adverbs and conjunctions are executed before verbs; the phrase`,"2-a` is equivalent to`(,"2)-a` ,`` not to`,"(2-a)` .`` Moreover, the left argument of an adverb or conjunction is the entire verb phrase that precedes it. Thus, in the phrase`+/ . */b` ,`` the rightmost adverb`/` applies to the verb derived from the phrase`+/ . *` ,`` not to the verb`*` .
3. | A verb is applied dyadically if possible; that is, if preceded by a noun that is not itself the right argument of a conjunction.
4. | Certain trains form verbs, adverbs, and conjunctions, as described in § F.
5. | To ensure that these summary parsing rules agree with the precise parsing rules prescribed below, it may be necessary to parenthesize an adverbial or conjunctival phrase that produces anything other than a noun or verb.

One important consequence of these rules is that in an unparenthesized expression the right argument of any verb is the result of the entire phrase to its right. The sentence`3*p%q^|r-5` can therefore be read from left to right: the overall result is 3 times the result of the remaining phrase, which is the quotient of`p` and the part following the`%` ,`` and so on.
Parsing proceeds by moving successive elements (or their values except in the case of proverbs and names immediately to the left of a copula) from the tail end of a queue (initially the original sentence prefixed by a left marker `§`) to the top of a stack, and eventually executing some eligible portion of the stack and replacing it by the result of the execution. For example, if`a=: 1 2 3`,`` then`b=:+/2*a` would be parsed and executed as follows:

       § b =: + / 2 * a
       § b =: + / 2 *          1  2  3
       § b =: + / 2          * 1  2  3
       § b =: + /          2 * 1  2  3
       § b =: +         /  2 * 1  2  3
       § b =: +             /  2  4  6
       § b =:             + /  2  4  6
       § b             =: + /  2  4  6
       § b                       =: 12
       §                       b =: 12
       §                            12
                                  § 12

The foregoing illustrates two points: 1) Execution of the phrase`2 * 1 2 3` is deferred until the next element (the`/`) is transferred; had it been a conjunction, the`2` would have been its argument, and the monad`*` would have applied to`1 2 3`;`` and 2) Whereas the value of the name`a` moves to the stack, the name`b` (because it precedes a copula) moves unchanged, and the pronoun`b` is assigned the value`12`.
Parsing can be observed using the trace facility in`system\packages\misc\trace.ijs` .
The executions in the stack are confined to the first four elements only, and eligibility for execution is determined only by the class of each element (noun, verb, etc., an unassigned name being treated as a verb), as prescribed in the following parse table. The classes of the first four elements of the stack are compared with the first four columns of the table, and the first row that agrees in all four columns is selected. The bold italic elements in the row are then subjected to the action shown in the final column, and are replaced by its result. If no row is satisfied, the next element is transferred from the queue.

EDGE | VERB | NOUN | ANY |  | 0 Monad
EDGE+AVN | VERB | VERB | NOUN |  | 1 Monad
EDGE+AVN | NOUN | VERB | NOUN |  | 2 Dyad
EDGE+AVN | VERB+NOUN | ADV | ANY |  | 3 Adverb
EDGE+AVN | VERB+NOUN | CONJ | VERB+NOUN |  | 4 Conj
EDGE+AVN | VERB | VERB | VERB |  | 5 Trident
EDGE | CAVN | CAVN | ANY |  | 6 Bident
NAME+NOUN | ASGN | CAVN | ANY |  | 7 Is
LPAR | CAVN | RPAR | ANY |  | 8 Paren

Legend: | AVN | denotes | ADV+VERB+NOUN
| CAVN | denotes | CONJ+ADV+VERB+NOUN
| EDGE | denotes | MARK+ASGN+LPAR

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Dictionary