Intro To BOOST.Spirit


Published on

Intro To BOOST.Spirit

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Intro To BOOST.Spirit

  1. 1. Intro to Boost SpiritWill Shen2013/03/25
  2. 2. BOOST Spirit.QiSpirit.Qi is designed to be a practicalparsing tool.The ability to generate a fully-workingparser from a formal EBNF specificationinlined in C++ significantly reducesdevelopment time.3
  3. 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
  4. 4. Parsing Semantic Actionsvoid Print(int n);Presented are various ways to attach semanticactions:• Using plain function pointer• Using simple function object• Using Boost.Bind with a plain function• Using Boost.Bind with a member function• Using Boost.Lambda5
  5. 5. Parsing Semantic Actions"{integer}"6
  6. 6. The First Complex parser 1/21. 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 secondnumber is optional)• OR a single real number.This parser can parse complex numbers of the form:• (123.45, 987.65)• (123.45)• 123.457
  7. 7. The First Complex parser 2/28
  8. 8. Sum – Adding Numbers9
  9. 9. Number List –stuffing numbers into a std::vector10
  10. 10. Roman Numerals11
  11. 11. Roman Numerals - GrammarsA grammar encapsulates one or more rules. Ithas the same template parameters as the rule.You declare a grammar by:1. deriving a struct (or class) from the grammar classtemplate2. declare one or more rules as member variables3. initialize the base grammar class by giving it the start rule(its the first rule that gets called when the grammar startsparsing)4. initialize your rules in your constructor12
  12. 12. Roman Numerals - GrammarsThe grammar and start rulesignature is unsigned().We did not specify a skip-parser_val is another Phoenixplaceholder representing therules synthesized attribute.eps is a special spirit parserthat consumes no input butis always successful.The expression a || b reads:match a or b and insequence. That is, if both aand b match, it must be insequence13
  13. 13. Employee – Parsing into structs14•we need to tell Boost.Fusion aboutour employee struct to make it afirst-class fusion citizen that thegrammar can utilize.•a struct is just a form of a tuple
  14. 14. Employee – Parsing into structs15
  15. 15. Calculator GrammarExpr ::= Term (+ Term | - Term)*Term ::= Factor (* Factor | / Factor)*Factor ::= [-] (Number | ( Expr ))Number ::= Digit+“10+9-8*7/6”16
  16. 16. Calculator17Calc GrammarParser(spirit.qi)Calculator“10+9-8*7/6”“10”
  17. 17. The Calculator class18Calc ParserFull match
  18. 18. Boost::spirit::qi19Expr ::= Term (+ Term | - Term)*Term ::= Factor (* Factor | / Factor)*Factor ::= [-] (Number | ( Expr ))Number ::= Digit+