Logo

14.1 Step by step parsing of MSE

To show how PetitParser works, we take as case study creating a parser to manipulate MSE files. That is right: we pretend Moose has no infrastructure for handling MSE files and we have to create one from scratch.

The only input we consider is the official grammar of MSE given using the following rules:

Root := Document ?
Document := OPEN ElementNode * CLOSE
ElementNode := OPEN ELEMENTNAME Serial ? AttributeNode * CLOSE
Serial := OPEN ID NATURAL CLOSE
AttributeNode := OPEN SIMPLENAME ValueNode * CLOSE
ValueNode := Primitive | Reference | ElementNode
Primitive := STRING | NUMBER | Boolean
Boolean := TRUE | FALSE
Reference := IntegerReference | NameReference
IntegerReference := OPEN REF NATURAL CLOSE
NameReference := OPEN REF ELEMENTNAME CLOSE
OPEN := "("
CLOSE := ")"
ID := "id:"
REF := "ref:"
TRUE := "true"
FALSE := "false"
ELEMENTNAME := letter ( letter | digit ) * ( "." letter ( letter | digit ) * ) 
SIMPLENAME := letter ( letter | digit ) *
NATURAL := digit +
NUMBER := "-" ? digit + ( "." digit + ) ? ( ( "e" | "E" ) ( "-" | "+" ) ? digit + ) ?
STRING := ( "'" [^'] * "'" ) +
digit := \[0-9] 
letter := \[a-zA-Z_]
comment := """ [^"] * """

Based on this, we first want to define a grammar to be able to read the file (see Section 14.1.1, Section 14.1.2, Section 14.1.3 and Section 14.1.4). Once able to read, we learn how to test the grammar (see Section 14.1.5), and how to build a parser that produces a better output (see Section 14.1.6).

Further examples can be found at:

User Contributed Notes

tudor (18 April 2013, 7:55 am)

Thanks. INTEGER should have been NATURAL. It is fixed now.

srpelissier (10 April 2013, 4:06 pm)

INTEGER used in Serial is not described.

rafatals3ode (13 June 2012, 9:23 pm)

ask ra'fat

hamzehahu (1 May 2012, 4:07 pm)

How can I convert MSE file to XML FILE?

Add a Note