Difference between revisions of "Recursive Descent Parsers"

From Progzoo
Jump to: navigation, search
(Production Rules)
(Production Rules)
Line 17: Line 17:
 
*{a:1, b:[1,2,3], c:{x:2}}
 
*{a:1, b:[1,2,3], c:{x:2}}
 
==Production Rules==
 
==Production Rules==
 +
An eta ε production allows the empty string as an option.
 +
 
  N -> ''number''
 
  N -> ''number''
 
  N -> [ N Z ]
 
  N -> [ N Z ]
Line 24: Line 26:
 
  Y -> , ''word'' : N Y
 
  Y -> , ''word'' : N Y
 
  Y -> ε
 
  Y -> ε
 +
Productions such as
 +
N -> ''number''
 +
Are permitted because the lexer can recognise numbers, similarly strings.

Revision as of 14:54, 7 March 2011

A Recursive Descent Parser takes a stream of tokens and builds a syntax tree.

JSON Data Format

As an example we will take the JSON data format. This is simple, but it does have the recursive structure that shows off the technique. JSON is used to represent data. There are two primitive type: string and number.

  • "one"
  • 123

There are lists For example:

  • [123, "one" ]

There are hash arrays For example:

  • {num:123, str: "one" }

You can have a list inside a list of course:

  • [1, 2, [3, 4]]

Similarly you can have a hash array of lists, and in JavaScript you can mix types:

  • {a:1, b:[1,2,3], c:{x:2}}

Production Rules

An eta ε production allows the empty string as an option.

N -> number
N -> [ N Z ]
N -> { word : N Y }
Z -> , N Z
Z -> ε
Y -> , word : N Y
Y -> ε

Productions such as

N -> number

Are permitted because the lexer can recognise numbers, similarly strings.