2. TDD Your Shape Grammar
Make it can compile “rulessimpleStreets.cga”
2
3. 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
4. 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
5. 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
7. 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
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 – 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