Changes between Version 3 and Version 4 of NQP-rx Operator Precedence Parsing

Show
Ignore:
Timestamp:
03/22/10 06:31:34 (5 years ago)
Author:
Austin_Hastings
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • NQP-rx Operator Precedence Parsing

    v3 v4  
    5151(If you don't know about the ternary, don't fret. Just nod and smile when your buddies get all giddy...) 
    5252 
    53 There's a couple of choices for how to recognize ternary, but the most direct is the easiest, and since there's only one ternary op, it's the route the optable parser takes: ''you do it!'' 
     53There's a couple of choices for how to recognize ternary, but the most direct is the easiest, and since there's only one ternary op, it's the route the optable parser takes: ''you do it! '' 
    5454 
    5555The trick is that parsing the individual `<op>` strings is still the responsibility of the coder. So is exponentiation going to be `^` or `**`? It's up to you. And so ternary is treated as a ''binary'' operator, because that code is already written. It's just that you need to recognize both parts of the op, ''and'' recognize the intermediary sub-expression: 
     
    192192'''IMPORTANT:''' Never mark a unary operator as anything but unary associative. Even if the language grammar specifies "oh, this operator is left-associative", you must mark it as `unary`. Marking it as unary tells the optable "don't look for anything else but this", whereas marking it `left` or `right` would imply "there should be another operand somewhere, you should look for it" - resulting in painful horrible death, or worse. 
    193193 
    194 === left, right === 
     194==== left, right ==== 
    195195 
    196196A left- or right- associative operator will require two or more arguments, but will group them differently when they appear in bunches. A left association, like addition, will create a group starting on the left:  `a + b + c` becomes `(a+b) +c`. A right association, like exponentiation, groups the other way:  `x ** y ** z` becomes `(x** (y**z))`.  
     
    200200'''IMPORTANT:''' See note in unary, above. 
    201201 
    202 === list === 
     202==== list ==== 
    203203 
    204204List associativity is a special treatment, mainly for the comma(`,`) operator. Instead of building a tree, the expression parser will build an array with the operands in order: