Changes

917 bytes added ,  20:20, April 22, 2012
→‎Chomsky hierarchy: rules for simple expressions
Line 205: Line 205:     
====Example====
 
====Example====
−
This example shows how context-free grammars can cope with arithmetic expressions involving possibly nested brackets. Also an expression such as "2 + 3 * 4" could be interpreted as either "(2 + 3) * 4" or as "2 + (3 * 4)". The latter interpretation is that which is normally used - multiplication "*" is said to have a higher priority than addition "+" - so the example also needs to incorporate this priority concept into the grammar.  
+
The following example shows how context-free grammars can cope with arithmetic expressions involving possibly nested brackets. Also an expression such as "2 + 3 * 4" could be interpreted as either "(2 + 3) * 4" or as "2 + (3 * 4)". The latter interpretation is that which is normally used - multiplication "*" is said to have a higher priority than addition "+" - so the example also needs to incorporate this priority concept into the grammar. Furthermore, consecutive operators of the same priority are applied from left to right, so that "8 - 2 + 3" is interpreted as "(8 - 2) + 3" = 9, rather than as "8 - (2 + 3)" = 3.  This ordering must also be built in to the grammar.
   −
To avoid distracting side-issues, this example deals with numbers consisting solely of a single digit.
+
To avoid distracting side-issues, this example only deals with numbers which consist solely of a single digit. The example is a simplified version of the definition of an arithmetic expression given in the Revised Algol 60 Report (Section 3.3.1)
 +
<ref>{{cite web
 +
| url = http://www.masswerk.at/algol60/report.htm
 +
| title = Revised Report on the Algorithmic Language Algol 60
 +
| accessdate = 2012-04-22
 +
}}</ref>
 +
 
 +
''Expression'' -> ''Term''
 +
''Expression'' -> ''Expression'' ''AddSubOp'' ''Term''
 +
''Term''      -> ''Primary''
 +
''Term''      -> ''Term'' ''MulDivOp'' ''Primary''
 +
 
 +
''Primary''    -> ''Number''
 +
''Primary''    -> '''(''' ''Expression'' ''')'''
 +
 
 +
''MulDivOp''  -> '''*'''. '''/'''
 +
''AddSubOp''  -> '''+'''. '''-'''
 +
''Number''    -> '''0''', '''1''', ... '''9'''
    
===Level 1===
 
===Level 1===
   −
===Level 0===
+
===Level 0==
    
==References==
 
==References==
SkipCaptcha
265

edits