Intro to Boost Spirit
Will Shen
2013/03/25
TDD Your Shape Grammar
Make it can compile “rulessimpleStreets.cga”
2
BOOST Spirit.Qi
Spirit.Qi is designed to be a practical
parsing tool.
The ability to generate a fully-working
parser from a formal EBNF specification
inlined in C++ significantly reduces
development time.
3
Tutorials – Parsing Floating Number
• Parsing a number: double_
• Parsing two numbers: double_ >> double_
• Parsing zero of more numbers: *double_
• Parsing a comma-delimited list of numbers:
double_ >> *( char_(„,‟) >> double_)
4
Parsing Semantic Actions
void Print(int n);
Presented are various ways to attach semantic
actions:
• Using plain function pointer
• Using simple function object
• Using Boost.Bind with a plain function
• Using Boost.Bind with a member function
• Using Boost.Lambda
5
Parsing Semantic Actions
"{integer}"
6
The First Complex parser 1/2
1. Alternates: e.g. a | b. Try a first. If it succeeds, good.
If not, try the next alternative, b.
2. Optional: e.g. -p. Match the parser p zero or one time.
The complex parser presented above reads as:
• One or two real numbers in parentheses, separated by comma (the second
number is optional)
• OR a single real number.
This parser can parse complex numbers of the form:
• (123.45, 987.65)
• (123.45)
• 123.45
7
The First Complex parser 2/2
8
Sum – Adding Numbers
9
Number List –
stuffing numbers into a std::vector
10
Roman Numerals
11
Roman Numerals - Grammars
A grammar encapsulates one or more rules. It
has the same template parameters as the rule.
You declare a grammar by:
1. deriving a struct (or class) from the grammar class
template
2. declare one or more rules as member variables
3. initialize the base grammar class by giving it the start rule
(its the first rule that gets called when the grammar starts
parsing)
4. initialize your rules in your constructor
12
Roman Numerals - Grammars
The grammar and start rule
signature is unsigned().
We did not specify a skip-
parser
_val is another Phoenix
placeholder representing the
rule's synthesized attribute.
eps is a special spirit parser
that consumes no input but
is always successful.
The expression a || b reads:
match a or b and in
sequence. That is, if both a
and b match, it must be in
sequence
13
Employee – Parsing into structs
14
•we need to tell Boost.Fusion about
our employee struct to make it a
first-class fusion citizen that the
grammar can utilize.
•a struct is just a form of a tuple
Employee – Parsing into structs
15
Calculator Grammar
Expr ::= Term ('+' Term | '-' Term)*
Term ::= Factor ('*' Factor | '/' Factor)*
Factor ::= ['-'] (Number | '(' Expr ')')
Number ::= Digit+
“10+9-8*7/6”
16
Calculator
17
Calc Grammar
Parser
(spirit.qi)
Calculator
“10+9-8*7/6”
“10”
The Calculator class
18
Calc Parser
Full match
Boost::spirit::qi
19
Expr ::= Term ('+' Term | '-' Term)*
Term ::= Factor ('*' Factor | '/' Factor)*
Factor ::= ['-'] (Number | '(' Expr ')')
Number ::= Digit+

Intro To BOOST.Spirit

  • 1.
    Intro to BoostSpirit Will Shen 2013/03/25
  • 2.
    TDD Your ShapeGrammar Make it can compile “rulessimpleStreets.cga” 2
  • 3.
    BOOST Spirit.Qi Spirit.Qi isdesigned to be a practical parsing tool. The ability to generate a fully-working parser from a formal EBNF specification inlined in C++ significantly reduces development time. 3
  • 4.
    Tutorials – ParsingFloating Number • Parsing a number: double_ • Parsing two numbers: double_ >> double_ • Parsing zero of more numbers: *double_ • Parsing a comma-delimited list of numbers: double_ >> *( char_(„,‟) >> double_) 4
  • 5.
    Parsing Semantic Actions voidPrint(int n); Presented are various ways to attach semantic actions: • Using plain function pointer • Using simple function object • Using Boost.Bind with a plain function • Using Boost.Bind with a member function • Using Boost.Lambda 5
  • 6.
  • 7.
    The First Complexparser 1/2 1. Alternates: e.g. a | b. Try a first. If it succeeds, good. If not, try the next alternative, b. 2. Optional: e.g. -p. Match the parser p zero or one time. The complex parser presented above reads as: • One or two real numbers in parentheses, separated by comma (the second number is optional) • OR a single real number. This parser can parse complex numbers of the form: • (123.45, 987.65) • (123.45) • 123.45 7
  • 8.
    The First Complexparser 2/2 8
  • 9.
    Sum – AddingNumbers 9
  • 10.
    Number List – stuffingnumbers into a std::vector 10
  • 11.
  • 12.
    Roman Numerals -Grammars A grammar encapsulates one or more rules. It has the same template parameters as the rule. You declare a grammar by: 1. deriving a struct (or class) from the grammar class template 2. declare one or more rules as member variables 3. initialize the base grammar class by giving it the start rule (its the first rule that gets called when the grammar starts parsing) 4. initialize your rules in your constructor 12
  • 13.
    Roman Numerals -Grammars The grammar and start rule signature is unsigned(). We did not specify a skip- parser _val is another Phoenix placeholder representing the rule's synthesized attribute. eps is a special spirit parser that consumes no input but is always successful. The expression a || b reads: match a or b and in sequence. That is, if both a and b match, it must be in sequence 13
  • 14.
    Employee – Parsinginto structs 14 •we need to tell Boost.Fusion about our employee struct to make it a first-class fusion citizen that the grammar can utilize. •a struct is just a form of a tuple
  • 15.
    Employee – Parsinginto structs 15
  • 16.
    Calculator Grammar Expr ::=Term ('+' Term | '-' Term)* Term ::= Factor ('*' Factor | '/' Factor)* Factor ::= ['-'] (Number | '(' Expr ')') Number ::= Digit+ “10+9-8*7/6” 16
  • 17.
  • 18.
  • 19.
    Boost::spirit::qi 19 Expr ::= Term('+' Term | '-' Term)* Term ::= Factor ('*' Factor | '/' Factor)* Factor ::= ['-'] (Number | '(' Expr ')') Number ::= Digit+