Your SlideShare is downloading. ×
0
×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Intro To BOOST.Spirit

2,097

Published on

Intro To BOOST.Spirit

Intro To BOOST.Spirit

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,097
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
21
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Intro to Boost SpiritWill Shen2013/03/25
  • 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. 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. 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. Parsing Semantic Actions"{integer}"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. The First Complex parser 2/28
  • 8. Sum – Adding Numbers9
  • 9. Number List –stuffing numbers into a std::vector10
  • 10. Roman Numerals11
  • 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. 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. 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. Employee – Parsing into structs15
  • 15. Calculator GrammarExpr ::= Term (+ Term | - Term)*Term ::= Factor (* Factor | / Factor)*Factor ::= [-] (Number | ( Expr ))Number ::= Digit+“10+9-8*7/6”16
  • 16. Calculator17Calc GrammarParser(spirit.qi)Calculator“10+9-8*7/6”“10”
  • 17. The Calculator class18Calc ParserFull match
  • 18. Boost::spirit::qi19Expr ::= Term (+ Term | - Term)*Term ::= Factor (* Factor | / Factor)*Factor ::= [-] (Number | ( Expr ))Number ::= Digit+

×