Changes

Jump to navigation Jump to search
1,474 bytes added ,  22:26, April 25, 2012
→‎Example: add notes and examples
Line 213: Line 213:  
| accessdate = 2012-04-22
 
| accessdate = 2012-04-22
 
}}</ref>
 
}}</ref>
−
      
  ''Expression'' -> ''Term''                        # an ''Expression'' is either a ''Term''
 
  ''Expression'' -> ''Term''                        # an ''Expression'' is either a ''Term''
Line 226: Line 225:  
  ''AddSubOp''  -> '''+'''. '''-'''                        # addition and subtraction have the same priority
 
  ''AddSubOp''  -> '''+'''. '''-'''                        # addition and subtraction have the same priority
 
  ''Number''    -> '''0''', '''1''', ... '''9'''                # this simple-minded definition of a ''Number'' is enough for this example
 
  ''Number''    -> '''0''', '''1''', ... '''9'''                # this simple-minded definition of a ''Number'' is enough for this example
 +
 +
Notes:
 +
* Both ''Expression'' and ''Term'' are each defined via a left-recursive rule; this ensures that operators of the same priority are applied from left to right.
 +
* The distinction between ''Expression'' and ''Term'' ensures that multiplication/division are applied before addition/subtraction.
 +
* ''Expression'' is indirectly recursive via ''Term'' and ''Primary'' to allow bracketed sub-expressions to any depth.
 +
 +
Example 1: generate 2 + 3 * 4
 +
 +
  ''Expression'' -> ''Expression'' ''AddSubOp'' ''Term''
 +
            -> ''Term'' ''AddSubOp'' ''Term''
 +
            -> ''Primary'' ''AddSubOp'' ''Term''
 +
            -> '''2''' '''+''' ''Term''                                # '''+''' can not be applied until ''Term'' has been evaluated
 +
            -> '''2''' '''+''' ''Term'' ''MulDivOp'' ''Primary''
 +
            -> '''2''' '''+''' ''Primary'' ''MulDipOp'' ''Primary''
 +
            -> '''2''' '''+''' '''3''' '''*''' '''4'''
 +
 +
Example 2: generate 2 * 3 + 4
 +
 +
  ''Expression'' -> ''Expression'' ''AddSubOp'' ''Term''
 +
            -> ''Term'' ''AddSubOp'' ''Term''
 +
            -> ''Term'' ''MulDivOp'' ''Primary'' ''AddSubOp'' ''Term''
 +
            -> ''Primary'' ''MulDivOp'' ''Primary'' ''AddSubOp'' ''Term''
 +
            -> '''2''' '''*''' '''3''' ''AddSubOp'' ''Term''                      # ''Term'' to left of ''AddSubOp'' is evaluated first
 +
            -> '''2''' '''*''' '''3''' ''AddSubOp'' ''Primary'' 
 +
            -> '''2''' '''*''' '''3''' '''+''' '''4'''
    
===Level 1===
 
===Level 1===
SkipCaptcha
265

edits

Navigation menu