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: