![Page 1: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/1.jpg)
ciere.com
Using X3A Spirit X3 Tutorial and Workshop
ciere consultingJoel de Guzman and Michael Caisse
[email protected] | [email protected] c© 2015
Joel de Guzman and Michael Caisse Using X3
![Page 2: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/2.jpg)
ciere.com
Introduction Elements
Part I
Introduction
Joel de Guzman and Michael Caisse Using X3
![Page 3: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/3.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Outline
1 IntroductionSpirit X3Concepts
2 ElementsParsersRulesGrammarsAttributes
Joel de Guzman and Michael Caisse Using X3
![Page 4: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/4.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
I Next generation of SpiritI Modern C++14 language featuresI Hackable, simpler internal design.I Minimal code base and dependenciesI Compiles faster and runs fasterI Better error handlingI Optimized attribute processing
Joel de Guzman and Michael Caisse Using X3
![Page 5: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/5.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
I Next generation of SpiritI Modern C++14 language featuresI Hackable, simpler internal design.I Minimal code base and dependenciesI Compiles faster and runs fasterI Better error handlingI Optimized attribute processing
Joel de Guzman and Michael Caisse Using X3
![Page 6: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/6.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
I Next generation of SpiritI Modern C++14 language featuresI Hackable, simpler internal design.I Minimal code base and dependenciesI Compiles faster and runs fasterI Better error handlingI Optimized attribute processing
Joel de Guzman and Michael Caisse Using X3
![Page 7: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/7.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
I Next generation of SpiritI Modern C++14 language featuresI Hackable, simpler internal design.I Minimal code base and dependenciesI Compiles faster and runs fasterI Better error handlingI Optimized attribute processing
Joel de Guzman and Michael Caisse Using X3
![Page 8: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/8.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
I Next generation of SpiritI Modern C++14 language featuresI Hackable, simpler internal design.I Minimal code base and dependenciesI Compiles faster and runs fasterI Better error handlingI Optimized attribute processing
Joel de Guzman and Michael Caisse Using X3
![Page 9: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/9.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
I Next generation of SpiritI Modern C++14 language featuresI Hackable, simpler internal design.I Minimal code base and dependenciesI Compiles faster and runs fasterI Better error handlingI Optimized attribute processing
Joel de Guzman and Michael Caisse Using X3
![Page 10: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/10.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
I Next generation of SpiritI Modern C++14 language featuresI Hackable, simpler internal design.I Minimal code base and dependenciesI Compiles faster and runs fasterI Better error handlingI Optimized attribute processing
Joel de Guzman and Michael Caisse Using X3
![Page 11: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/11.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Outline
1 IntroductionSpirit X3Concepts
2 ElementsParsersRulesGrammarsAttributes
Joel de Guzman and Michael Caisse Using X3
![Page 12: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/12.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
Domain Specific Embedded Language
Joel de Guzman and Michael Caisse Using X3
![Page 13: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/13.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
Domain Specific Embedded Language
Parsing
Joel de Guzman and Michael Caisse Using X3
![Page 14: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/14.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
Domain Specific Embedded Language
C++ via Expression Templates
Joel de Guzman and Michael Caisse Using X3
![Page 15: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/15.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Spirit X3
Domain Specific Embedded Language
PEG - Parsing Expression Grammar
Joel de Guzman and Michael Caisse Using X3
![Page 16: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/16.jpg)
Ad-hoc Parsingstd::string::const_iterator iter = argument.begin();std::string::const_iterator iter_end = argument.end();while( iter != iter_end ){
if( *iter == ’+’ ){
if( building_key ){ key += ’ ’; }else { value += ’ ’; }
}else if( *iter == ’=’ ){
building_key = false;}else if( *iter == ’&’ ){
argument_map[ key ] = value;key = "";value = "";building_key = true;
}else if( *iter == ’?’ ){}else{
if( building_key ){ key += *iter; }else { value += *iter; }
}
++iter;}
![Page 17: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/17.jpg)
Ad-hoc Parsing and Generatingboost::regex expression( "(request_firmware_version)|(calibrate_sensor_gain)|(calibrate_sensor)|(power_off_sensor)" );boost::smatch match;
if( boost::regex_search( product_data, match, expression ) ){
if( match[ 1 ].matched ){
message_to_send += char( STX );message_to_send += char( 0x11 );message_to_send += char( ETX );
}else if( match[ 2 ].matched ){
message_to_send += char( STX );message_to_send += char( 0x12 );message_to_send += char( ETX );
}else if( match[ 3 ].matched ){
boost::regex expression( "calibrate_sensor (\\d+) (\\d+)" );if( boost::regex_search( product_data, match, expression ) ){
try{
message_to_send += char( STX );message_to_send += char( 0x13 );message_to_send += char( boost::lexical_cast< int >( match[ 1 ] ) + 0x10 );message_to_send += char( boost::lexical_cast< int >( match[ 2 ] ) + 0x10 );message_to_send += char( ETX );
}catch( ... ){
message_to_send.clear();}
}}else if( match[ 4 ].matched ){
message_to_send += char( STX );message_to_send += char( 0x15 );message_to_send += char( ETX );
}}
![Page 18: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/18.jpg)
Concepts
PEG grammar Email (not really)
name <- [a-z]+ ("." [a-z]+)*host <- [a-z]+ "." ("com" / "org" / "net")email <- name "@" host
auto name = +char_("a-z") >> *(’.’ >> +char_("a-z"));auto host = +char_("a-z") >> ’.’ >> ("com" | "org" | "net");auto email = name >> ’@’ >> host;
![Page 19: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/19.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Concepts
I ParsersI RulesI Attribute Parsing
Joel de Guzman and Michael Caisse Using X3
![Page 20: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/20.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Parsers
Joel de Guzman and Michael Caisse Using X3
![Page 21: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/21.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Parsers
Joel de Guzman and Michael Caisse Using X3
![Page 22: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/22.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Rules
Joel de Guzman and Michael Caisse Using X3
![Page 23: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/23.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Rules
Joel de Guzman and Michael Caisse Using X3
![Page 24: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/24.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Rules
Joel de Guzman and Michael Caisse Using X3
![Page 25: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/25.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Attributes
Synthesized Attribute
Joel de Guzman and Michael Caisse Using X3
![Page 26: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/26.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Attributes
Synthesized Attribute
Joel de Guzman and Michael Caisse Using X3
![Page 27: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/27.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Attributes
Joel de Guzman and Michael Caisse Using X3
![Page 28: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/28.jpg)
ciere.com
Introduction Elements Spirit X3 Concepts
Grammars??
shhhhhh ....
Joel de Guzman and Michael Caisse Using X3
![Page 29: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/29.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Outline
1 IntroductionSpirit X3Concepts
2 ElementsParsersRulesGrammarsAttributes
Joel de Guzman and Michael Caisse Using X3
![Page 30: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/30.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Parser
Data Stream → X3 → Abstract Syntax Tree (AST)
Joel de Guzman and Michael Caisse Using X3
![Page 31: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/31.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First, Simple Example
A parser for integers is simply:
Example (Integer Parser)int_
A parser for doubles:
Example (Double Parser)double_
A literal string parser:
Example (Parse literal string "foo")lit("foo")
Joel de Guzman and Michael Caisse Using X3
![Page 32: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/32.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First, Simple Example
A parser for integers is simply:
Example (Integer Parser)int_
A parser for doubles:
Example (Double Parser)double_
A literal string parser:
Example (Parse literal string "foo")lit("foo")
Joel de Guzman and Michael Caisse Using X3
![Page 33: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/33.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First, Simple Example
A parser for integers is simply:
Example (Integer Parser)int_
A parser for doubles:
Example (Double Parser)double_
A literal string parser:
Example (Parse literal string "foo")lit("foo")
Joel de Guzman and Michael Caisse Using X3
![Page 34: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/34.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First, Simple Example
We can use the parser with the x3::parse API.
std::string input( "1234" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,int_ );
Joel de Guzman and Michael Caisse Using X3
![Page 35: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/35.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First, Simple Example
We can use the parser with the x3::parse API.
std::string input( "1234" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,int_ );
Joel de Guzman and Michael Caisse Using X3
![Page 36: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/36.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First, Simple Example
We can use the parser with the x3::parse API.
std::string input( "1234" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,int_ );
Joel de Guzman and Michael Caisse Using X3
![Page 37: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/37.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First, Simple Example
We can use the parser with the x3::parse API.
std::string input( "1234" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,int_ );
Joel de Guzman and Michael Caisse Using X3
![Page 38: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/38.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First, Simple Example
We can use the parser with the x3::parse API.
std::string input( "1234" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,int_ );
Joel de Guzman and Michael Caisse Using X3
![Page 39: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/39.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First, Simple Example
Parsing the double in just as simple.
std::string input( "1234.56" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,double_ );
Joel de Guzman and Michael Caisse Using X3
![Page 40: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/40.jpg)
Some of the Available Parsers
Type Parser Examplesigned short_, int_, long_, long_long,
int_(-42)578, -1865, 99301
unsigned bin, oct, hex, ushort_, ulong_,uint_, ulong_long, uint_(82)
01101, 24, 7af2, 243
real float_, double_, long_double,double_(123.5)
-1.9023, 9328.11928
boolean bool_, true_, false_ true, false
binary byte_, word, dword, qword,word(0xface)
big endian big_word, big_dword, big_qword,big_dword(0xdeadbeef)
litteendian
litte_word, litte_dword,litte_qword, little_dword(0xefbeadde)
![Page 41: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/41.jpg)
Some of the Available Parsers
Type Parser Examplesigned short_, int_, long_, long_long,
int_(-42)578, -1865, 99301
unsigned bin, oct, hex, ushort_, ulong_,uint_, ulong_long, uint_(82)
01101, 24, 7af2, 243
real float_, double_, long_double,double_(123.5)
-1.9023, 9328.11928
boolean bool_, true_, false_ true, false
binary byte_, word, dword, qword,word(0xface)
big endian big_word, big_dword, big_qword,big_dword(0xdeadbeef)
litteendian
litte_word, litte_dword,litte_qword, little_dword(0xefbeadde)
![Page 42: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/42.jpg)
Some of the Available Parsers
Type Parser Examplesigned short_, int_, long_, long_long,
int_(-42)578, -1865, 99301
unsigned bin, oct, hex, ushort_, ulong_,uint_, ulong_long, uint_(82)
01101, 24, 7af2, 243
real float_, double_, long_double,double_(123.5)
-1.9023, 9328.11928
boolean bool_, true_, false_ true, false
binary byte_, word, dword, qword,word(0xface)
big endian big_word, big_dword, big_qword,big_dword(0xdeadbeef)
litteendian
litte_word, litte_dword,litte_qword, little_dword(0xefbeadde)
![Page 43: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/43.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Some of the Available Parsers
Type Parser Examplecharacter char_, char_(’x’), char_(x),
char_(’a’,’z’), char_("a-z8A-Z"),~char_(’a’)
a b e $ 1 }
lit(’a’), ’a’ a
string string("foo"), string(s), lit("bar"),"bar", lit(s)
classification alnum, alpha, blank, cntrl, digit,graph, lower, print, punct, space,upper, xdigit
Joel de Guzman and Michael Caisse Using X3
![Page 44: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/44.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Some of the Available Parsers
Type Parser Examplecharacter char_, char_(’x’), char_(x),
char_(’a’,’z’), char_("a-z8A-Z"),~char_(’a’)
a b e $ 1 }
lit(’a’), ’a’ a
string string("foo"), string(s), lit("bar"),"bar", lit(s)
classification alnum, alpha, blank, cntrl, digit,graph, lower, print, punct, space,upper, xdigit
Joel de Guzman and Michael Caisse Using X3
![Page 45: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/45.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Some of the Available Parsers
Type Parser Examplecharacter char_, char_(’x’), char_(x),
char_(’a’,’z’), char_("a-z8A-Z"),~char_(’a’)
a b e $ 1 }
lit(’a’), ’a’ a
string string("foo"), string(s), lit("bar"),"bar", lit(s)
classification alnum, alpha, blank, cntrl, digit,graph, lower, print, punct, space,upper, xdigit
Joel de Guzman and Michael Caisse Using X3
![Page 46: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/46.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Some of the Available Parsers
Type Parser Examplecharacter char_, char_(’x’), char_(x),
char_(’a’,’z’), char_("a-z8A-Z"),~char_(’a’)
a b e $ 1 }
lit(’a’), ’a’ a
string string("foo"), string(s), lit("bar"),"bar", lit(s)
classification alnum, alpha, blank, cntrl, digit,graph, lower, print, punct, space,upper, xdigit
Joel de Guzman and Michael Caisse Using X3
![Page 47: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/47.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Some of the Available Parsers
Type Parser Examplecharacter char_, char_(’x’), char_(x),
char_(’a’,’z’), char_("a-z8A-Z"),~char_(’a’)
a b e $ 1 }
lit(’a’), ’a’ a
string string("foo"), string(s), lit("bar"),"bar", lit(s)
classification alnum, alpha, blank, cntrl, digit,graph, lower, print, punct, space,upper, xdigit
Joel de Guzman and Michael Caisse Using X3
![Page 48: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/48.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Some of the Available Parsers
Type Parser Examplecharacter char_, char_(’x’), char_(x),
char_(’a’,’z’), char_("a-z8A-Z"),~char_(’a’)
a b e $ 1 }
lit(’a’), ’a’ a
string string("foo"), string(s), lit("bar"),"bar", lit(s)
classification alnum, alpha, blank, cntrl, digit,graph, lower, print, punct, space,upper, xdigit
Joel de Guzman and Michael Caisse Using X3
![Page 49: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/49.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Sequence of Parsers
Combining parsers allows us to build more complex parsers.
std::string input( "876 1234.56" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,int_ >> ’ ’ >> double_ );
Joel de Guzman and Michael Caisse Using X3
![Page 50: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/50.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Sequence of Parsers
Combining parsers allows us to build more complex parsers.
std::string input( "876 1234.56" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,int_ >> ’ ’ >> double_ );
Joel de Guzman and Michael Caisse Using X3
![Page 51: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/51.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Sequence of Parsers
Combining parsers allows us to build more complex parsers.
std::string input( "876 1234.56" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,int_ >> ’ ’ >> double_ );
Joel de Guzman and Michael Caisse Using X3
![Page 52: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/52.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Sequence of Parsers
Combining parsers allows us to build more complex parsers.
std::string input( "876 1234.56" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,int_ >> ’ ’ >> double_ );
Joel de Guzman and Michael Caisse Using X3
![Page 53: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/53.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Sequence of Parsers
Combining parsers allows us to build more complex parsers.
std::string input( "876 1234.56" );
auto iter = input.begin();auto end_iter = input.end();
x3::parse( iter, end_iter,int_ >> ’ ’ >> double_ );
Joel de Guzman and Michael Caisse Using X3
![Page 54: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/54.jpg)
Operators
Description PEG Spirit X3Sequence a b a >> bAlternative a | b a | bZero of more (Kleene) a* *aOne or more (Plus) a+ +aOptional a? -aAnd-predicate &a &aNot-predicate !a !aDifference a - bExpectation a > bList a % b
![Page 55: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/55.jpg)
Operators
Description PEG Spirit X3Sequence a b a >> bAlternative a | b a | bZero of more (Kleene) a* *aOne or more (Plus) a+ +aOptional a? -aAnd-predicate &a &aNot-predicate !a !aDifference a - bExpectation a > bList a % b
Read as a is followed by b
int_ >> ’ ’ >> double_"42 -89.3"
char_ >> ’:’ >> int_"a:19"
![Page 56: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/56.jpg)
Operators
Description PEG Spirit X3Sequence a b a >> bAlternative a | b a | bZero of more (Kleene) a* *aOne or more (Plus) a+ +aOptional a? -aAnd-predicate &a &aNot-predicate !a !aDifference a - bExpectation a > bList a % b
Either a or b are allowed.Evaluated in listed order.
alpha | digit | punct"a""9"";""+" fails to parse
![Page 57: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/57.jpg)
Operators
Description PEG Spirit X3Sequence a b a >> bAlternative a | b a | bZero of more (Kleene) a* *aOne or more (Plus) a+ +aOptional a? -aAnd-predicate &a &aNot-predicate !a !aDifference a - bExpectation a > bList a % b
*alpha >> int_"z86""abcde99""99"
+alpha >> int_"z86""abcde99""99" parse fails
-alpha >> int_"z86""abcde99" parse fails"99"
![Page 58: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/58.jpg)
Operators
Description PEG Spirit X3Sequence a b a >> bAlternative a | b a | bZero of more (Kleene) a* *aOne or more (Plus) a+ +aOptional a? -aAnd-predicate &a &aNot-predicate !a !aDifference a - bExpectation a > bList a % b
And-predicate can providebasic look-ahead. It matches awithout consuming a.
int_ >> &char_(’;’)"86;""-99" fails to parse
![Page 59: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/59.jpg)
Operators
Description PEG Spirit X3Sequence a b a >> bAlternative a | b a | bZero of more (Kleene) a* *aOne or more (Plus) a+ +aOptional a? -aAnd-predicate &a &aNot-predicate !a !aDifference a - bExpectation a > bList a % b
Not-predicate can providebasic look-ahead. If a doesmatch the parse is successfulwithout consuming a.
"for" >> !(alnum|’_’)"for()""forty" fails to parse
![Page 60: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/60.jpg)
Operators
Description PEG Spirit X3Sequence a b a >> bAlternative a | b a | bZero of more (Kleene) a* *aOne or more (Plus) a+ +aOptional a? -aAnd-predicate &a &aNot-predicate !a !aDifference a - bExpectation a > bList a % b
Match a but not b.
"/*">> *(char_ - "*/")>> "*/"
"/* comment */"
Always fails.
lit("obiwatanabe") -"obiwa"
![Page 61: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/61.jpg)
Operators
Description PEG Spirit X3Sequence a b a >> bAlternative a | b a | bZero of more (Kleene) a* *aOne or more (Plus) a+ +aOptional a? -aAnd-predicate &a &aNot-predicate !a !aDifference a - bExpectation a > bList a % b
a must be followed by b. Nobacktracking allowed. ASequence returns no-match,an Expectation throwsexpectation_failure<iter>
char_(’o’)> char_(’k’)
"ok""ox" throws exception
![Page 62: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/62.jpg)
Operators
Description PEG Spirit X3Sequence a b a >> bAlternative a | b a | bZero of more (Kleene) a* *aOne or more (Plus) a+ +aOptional a? -aAnd-predicate &a &aNot-predicate !a !aDifference a - bExpectation a > bList a % b
Shortcut for:a >> *( b >> a )
int_ % ’,’
"9,2,42,-187,76"
![Page 63: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/63.jpg)
Combining Parsers - Parse key/value pairs
std::string input( "foo : bar , ""gorp : smart , ""falcou : \"crazy frenchman\" , ""name : sam " );
auto iter = input.begin();auto iter_end = input.end();
phrase_parse( iter, iter_end,// ------ start parser -------
( name >> ’:’ >> ( quote | name ) ) % ’,’
// ------- end parser --------, space );
![Page 64: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/64.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Outline
1 IntroductionSpirit X3Concepts
2 ElementsParsersRulesGrammarsAttributes
Joel de Guzman and Michael Caisse Using X3
![Page 65: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/65.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Combining Parsers - Rules
Rules allow us to organize parsers into named units. Theyprovide a few facilities:
I Allows us to name parsersI Specify the attribute typeI Allows for recursion (the rule may recursively call itself
directly or indirectly)I Provide error handling (on_error)I Attach custom handlers when a match is found
(on_sucess)
Joel de Guzman and Michael Caisse Using X3
![Page 66: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/66.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Combining Parsers - Rules
Using C++11 auto.
auto name = alpha >> *alnum;
auto quote = ’"’ >> *( char_(’"’) ) >> ’"’;
Joel de Guzman and Michael Caisse Using X3
![Page 67: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/67.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Combining Parsers - Rules
Using C++11 auto.
auto name = alpha >> *alnum;
auto quote = ’"’ >> *( char_(’"’) ) >> ’"’;
CautionOnly use auto for non-recursive rules.
Joel de Guzman and Michael Caisse Using X3
![Page 68: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/68.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Combining Parsers - Rules
Using X3 Rules.
auto name = x3::rule<class name>{}= alpha >> *alnum;
auto quote = x3::rule<class quote>{}= ’"’ >> *( ~char_(’"’) ) >> ’"’;
Joel de Guzman and Michael Caisse Using X3
![Page 69: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/69.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Combining Parsers - Rules
Using X3 Rules.
auto name = x3::rule<class name>{}= alpha >> *alnum;
auto quote = x3::rule<class quote>{}= ’"’ >> *( ~char_(’"’) ) >> ’"’;
Joel de Guzman and Michael Caisse Using X3
![Page 70: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/70.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Combining Parsers - Rules
The ID tag to be used by the rule.
auto name = x3::rule<class name>{}= alpha >> *alnum;
auto quote = x3::rule<class quote>{}= ’"’ >> *( ~char_(’"’) ) >> ’"’;
Joel de Guzman and Michael Caisse Using X3
![Page 71: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/71.jpg)
Combining Parsers - Parse key/value pairs refined
std::string input( "foo : bar , ""gorp : smart , ""falcou : \"crazy frenchman\" , ""name : sam " );
auto iter = input.begin();auto iter_end = input.end();
auto name = alpha >> *alnum;auto quote = ’"’
>> lexeme[ *(~char_(’"’)) ]>> ’"’
;
phrase_parse(iter, iter_end,
( name >> ’:’ >> (quote | name) ) % ’,’
, space);
![Page 72: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/72.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Outline
1 IntroductionSpirit X3Concepts
2 ElementsParsersRulesGrammarsAttributes
Joel de Guzman and Michael Caisse Using X3
![Page 73: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/73.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
No Grammar in X3
Grammars are not required in X3
Joel de Guzman and Michael Caisse Using X3
![Page 74: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/74.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Outline
1 IntroductionSpirit X3Concepts
2 ElementsParsersRulesGrammarsAttributes
Joel de Guzman and Michael Caisse Using X3
![Page 75: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/75.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Getting Parse Results
How do we get at the parsed results?
std::string input( "foo : bar , ""gorp : smart , ""falcou : \"crazy frenchman\" , ""name : sam " );
std::map<std::string, std::string> key_value_map;
// Do something clever here ??????????
Joel de Guzman and Michael Caisse Using X3
![Page 76: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/76.jpg)
Parsers Expose Attributes - Synthesized Attributes
X3 Parser Type Attribute TypeLiterals ’a’, "abc", int_(42), ...
No attribute
Primitives int_, char_, double_, ... int, char, double, ...
bin, oct, hex unsignedstring("abc") "abc"
Non-terminal rule<Tag, A> A
Operators a >> b tuple<A, B>a | b boost::variant<A,B>
*a std::vector<A>+a std::vector<A>-a boost::optional<A>&a, !a No attribute
a % b std::vector<A>
![Page 77: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/77.jpg)
Parsers Expose Attributes - Synthesized Attributes
X3 Parser Type Attribute TypeLiterals ’a’, "abc", int_(42), ...
No attributePrimitives int_, char_, double_, ... int, char, double, ...
bin, oct, hex unsignedstring("abc") "abc"
Non-terminal rule<Tag, A> A
Operators a >> b tuple<A, B>a | b boost::variant<A,B>
*a std::vector<A>+a std::vector<A>-a boost::optional<A>&a, !a No attribute
a % b std::vector<A>
![Page 78: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/78.jpg)
Parsers Expose Attributes - Synthesized Attributes
X3 Parser Type Attribute TypeLiterals ’a’, "abc", int_(42), ...
No attribute
Primitives int_, char_, double_, ... int, char, double, ...
bin, oct, hex unsignedstring("abc") "abc"
Non-terminal rule<Tag, A> A
Operators a >> b tuple<A, B>a | b boost::variant<A,B>
*a std::vector<A>+a std::vector<A>-a boost::optional<A>&a, !a No attribute
a % b std::vector<A>
![Page 79: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/79.jpg)
Parsers Expose Attributes - Synthesized Attributes
X3 Parser Type Attribute TypeLiterals ’a’, "abc", int_(42), ...
No attribute
Primitives int_, char_, double_, ... int, char, double, ...
bin, oct, hex unsignedstring("abc") "abc"
Non-terminal rule<Tag, A> A
Operators a >> b tuple<A, B>a | b boost::variant<A,B>
*a std::vector<A>+a std::vector<A>-a boost::optional<A>&a, !a No attribute
a % b std::vector<A>
![Page 80: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/80.jpg)
Parsers Expose Attributes - Synthesized Attributes
X3 Parser Type Attribute TypeLiterals ’a’, "abc", int_(42), ...
No attribute
Primitives int_, char_, double_, ... int, char, double, ...
bin, oct, hex unsignedstring("abc") "abc"
Non-terminal rule<Tag, A> A
Operators a >> b tuple<A, B>a | b boost::variant<A,B>
*a std::vector<A>+a std::vector<A>-a boost::optional<A>&a, !a No attribute
a % b std::vector<A>
![Page 81: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/81.jpg)
Parsers Expose Attributes - Synthesized Attributes
X3 Parser Type Attribute TypeLiterals ’a’, "abc", int_(42), ...
No attribute
Primitives int_, char_, double_, ... int, char, double, ...
bin, oct, hex unsignedstring("abc") "abc"
Non-terminal rule<Tag, A> A
Operators a >> b tuple<A, B>a | b boost::variant<A,B>*a std::vector<A>+a std::vector<A>-a boost::optional<A>&a, !a No attribute
a % b std::vector<A>
![Page 82: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/82.jpg)
Parsers Expose Attributes - Synthesized Attributes
X3 Parser Type Attribute TypeLiterals ’a’, "abc", int_(42), ...
No attribute
Primitives int_, char_, double_, ... int, char, double, ...
bin, oct, hex unsignedstring("abc") "abc"
Non-terminal rule<Tag, A> A
Operators a >> b tuple<A, B>a | b boost::variant<A,B>
*a std::vector<A>+a std::vector<A>-a boost::optional<A>&a, !a No attribute
a % b std::vector<A>
![Page 83: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/83.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First Attribute Example
We can simply provide a reference to the parse API and get theSynthesized Attribute.
std::string input( "1234" );auto iter = input.begin();auto end_iter = input.end();
int result;parse( iter, end_iter,
int_,result );
Joel de Guzman and Michael Caisse Using X3
![Page 84: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/84.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First Attribute Example
We can simply provide a reference to the parse API and get theSynthesized Attribute.
std::string input( "1234" );auto iter = input.begin();auto end_iter = input.end();
int result;parse( iter, end_iter,
int_,result );
Joel de Guzman and Michael Caisse Using X3
![Page 85: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/85.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
A First Attribute Example
We can simply provide a reference to the parse API and get theSynthesized Attribute.
std::string input( "1234" );auto iter = input.begin();auto end_iter = input.end();
int result;parse( iter, end_iter,
int_,result );
Joel de Guzman and Michael Caisse Using X3
![Page 86: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/86.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Parse a string into a std::string
Attribute parsing can produce compatible attributes
std::string input( "pizza" );auto iter = input.begin();auto end_iter = input.end();
std::string result;parse( iter, end_iter,
*char_,result );
std::string is compatible with std::vector<char>attribute of the *char_ parser.
Joel de Guzman and Michael Caisse Using X3
![Page 87: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/87.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Parse a string into a std::string
Attribute parsing can produce compatible attributes
std::string input( "pizza" );auto iter = input.begin();auto end_iter = input.end();
std::string result;parse( iter, end_iter,
*char_,result );
std::string is compatible with std::vector<char>attribute of the *char_ parser.
Joel de Guzman and Michael Caisse Using X3
![Page 88: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/88.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Parse a string into a std::string
Attribute parsing can produce compatible attributes
std::string input( "pizza" );auto iter = input.begin();auto end_iter = input.end();
std::string result;parse( iter, end_iter,
*char_,result );
std::string is compatible with std::vector<char>attribute of the *char_ parser.
Joel de Guzman and Michael Caisse Using X3
![Page 89: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/89.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Attribute Parsing - Sequence Parse API
std::string input( "cosmic pizza" );auto iter = input.begin();auto end_iter = input.end();
std::string result1;std::string result2;
parse( iter, end_iter,
*(~char_(’ ’)) >> ’ ’ >> *char_,result1,result2 );
Joel de Guzman and Michael Caisse Using X3
![Page 90: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/90.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Attribute Parsing - Sequence Parse API
std::string input( "cosmic pizza" );auto iter = input.begin();auto end_iter = input.end();
std::string result1;std::string result2;
parse( iter, end_iter,
*(~char_(’ ’)) >> ’ ’ >> *char_,result1,result2 );
Joel de Guzman and Michael Caisse Using X3
![Page 91: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/91.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Attribute Parsing - Sequence Parse API
std::string input( "cosmic pizza" );auto iter = input.begin();auto end_iter = input.end();
std::string result1;std::string result2;
parse( iter, end_iter,
*(~char_(’ ’)) >> ’ ’ >> *char_,result1,result2 );
Joel de Guzman and Michael Caisse Using X3
![Page 92: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/92.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Attribute Parsing - Sequence Parse API
Compatible attributes to the rescue!
std::string input( "cosmic pizza" );auto iter = input.begin();auto end_iter = input.end();
std::pair<std::string, std::string> result;
parse( iter, end_iter,
*(~char_(’ ’)) >> ’ ’ >> *char_,result );
Joel de Guzman and Michael Caisse Using X3
![Page 93: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/93.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Attribute Parsing - Sequence Parse API
Compatible attributes to the rescue!
std::string input( "cosmic pizza" );auto iter = input.begin();auto end_iter = input.end();
std::pair<std::string, std::string> result;
parse( iter, end_iter,
*(~char_(’ ’)) >> ’ ’ >> *char_,result );
Joel de Guzman and Michael Caisse Using X3
![Page 94: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/94.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Attribute Parsing - Sequence Parse API
Compatible attributes to the rescue!
std::string input( "cosmic pizza" );auto iter = input.begin();auto end_iter = input.end();
std::pair<std::string, std::string> result;
parse( iter, end_iter,
*(~char_(’ ’)) >> ’ ’ >> *char_,result );
Joel de Guzman and Michael Caisse Using X3
![Page 95: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/95.jpg)
Attribute Parsing - Compatibility
Attribute parsing is where the Spirit Magic lives.
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 96: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/96.jpg)
Attribute Parsing - Compatibility
The rule’s (synthesized) attribute must be compatible with its (RHS) definition.
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 97: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/97.jpg)
Attribute Parsing - Compatibility
a: char, b: std::vector<char> → ( a >> b ): std::vector<char>
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 98: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/98.jpg)
Attribute Parsing - Compatibility
a: char, b: std::vector<char> → ( a >> b ): std::vector<char>
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 99: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/99.jpg)
Attribute Parsing - Compatibility
a: char, b: std::vector<char> → ( a >> b ): std::vector<char>
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 100: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/100.jpg)
Attribute Parsing - Compatibility
a: unused, b: vector<char>, c: unused → ( a >> b >> c): std::vector<char>
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 101: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/101.jpg)
Attribute Parsing - Compatibility
a: unused, b: vector<char>, c: unused → ( a >> b >> c): std::vector<char>
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 102: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/102.jpg)
Attribute Parsing - Compatibility
a: unused, b: vector<char>, c: unused → ( a >> b >> c): std::vector<char>
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 103: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/103.jpg)
Attribute Parsing - Compatibility
a: string, b: string → ( a | b): variant<string, string> → string
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 104: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/104.jpg)
Attribute Parsing - Compatibility
a: string, b: string → ( a | b): variant<string, string> → string
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 105: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/105.jpg)
Attribute Parsing - Compatibility
a: string, b: string → ( a | b): variant<string, string> → string
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 106: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/106.jpg)
Attribute Parsing - Compatibility
a: string, b: unused, c: string → ( a >> b >> c): tuple<string, string>
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 107: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/107.jpg)
Attribute Parsing - Compatibility
a: string, b: unused, c: string → ( a >> b >> c): tuple<string, string>
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 108: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/108.jpg)
Attribute Parsing - Compatibility
a: string, b: unused, c: string → ( a >> b >> c): tuple<string, string>
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 109: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/109.jpg)
Attribute Parsing - Compatibility
a: std::pair<string, string> → ( a % b ): vector< std::pair<string, string> >
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 110: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/110.jpg)
Attribute Parsing - Compatibility
a: std::pair<string, string> → ( a % b ): vector< std::pair<string, string> >
std::string input( "foo : bar ,""gorp : smart ,""falcou : \"crazy frenchman\" " );
auto iter = input.begin();auto iter_end = input.end();
auto name = rule<class name, std::string>()= alpha >> *alnum;
auto quote = rule<class quote, std::string>()= ’"’>> lexeme[ *(~char_(’"’)) ]>> ’"’;
auto item = rule<class item, std::pair<std::string, std::string>>()name >> ’:’ >> ( quote | name );
std::map< std::string, std::string > key_value_map;
phrase_parse( iter, iter_end,item % ’,’,space,key_value_map );
![Page 111: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/111.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Rule Declarations
The rule’s attribute type (optional).
auto name = x3::rule<class name, name_attr>{}= alpha >> *alnum;
auto quote = x3::rule<class quote, quote_attr>{}= ’"’ >> *( ~char_(’"’) ) >> ’"’;
Joel de Guzman and Michael Caisse Using X3
![Page 112: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/112.jpg)
ciere.com
Introduction Elements Parsers Rules Grammars Attributes
Rule Declarations
The rule’s attribute type (optional).
auto name = x3::rule<class name, name_attr>{}= alpha >> *alnum;
auto quote = x3::rule<class quote, quote_attr>{}= ’"’ >> *( ~char_(’"’) ) >> ’"’;
Joel de Guzman and Michael Caisse Using X3
![Page 113: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/113.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing
Part II
Tidbits
Joel de Guzman and Michael Caisse Using X3
![Page 114: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/114.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Grammars from Scratch
Outline
3 Grammars from ScratchGrammars from Scratch
4 Fun with X3IntroductionCode OrganizationASTsGrammarsError Handling
5 AttributesAST Traversal
Joel de Guzman and Michael Caisse Using X3
![Page 115: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/115.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Grammars from Scratch
Build on Success
I Start smallI Alternatives are a natural place to buildI Leaves up
I Compose and testI Test early and oftenI Parsing first, Attributes secondI Allow the natural AST to fall outI Refine grammar/AST
Joel de Guzman and Michael Caisse Using X3
![Page 116: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/116.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Grammars from Scratch
Build on Success
I Start smallI Alternatives are a natural place to buildI Leaves up
I Compose and testI Test early and oftenI Parsing first, Attributes secondI Allow the natural AST to fall outI Refine grammar/AST
Joel de Guzman and Michael Caisse Using X3
![Page 117: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/117.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Grammars from Scratch
Build on Success
I Start smallI Alternatives are a natural place to buildI Leaves up
I Compose and testI Test early and oftenI Parsing first, Attributes secondI Allow the natural AST to fall outI Refine grammar/AST
Joel de Guzman and Michael Caisse Using X3
![Page 118: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/118.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Grammars from Scratch
Build on Success
I Start smallI Alternatives are a natural place to buildI Leaves up
I Compose and testI Test early and oftenI Parsing first, Attributes secondI Allow the natural AST to fall outI Refine grammar/AST
Joel de Guzman and Michael Caisse Using X3
![Page 119: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/119.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Grammars from Scratch
Build on Success
I Start smallI Alternatives are a natural place to buildI Leaves up
I Compose and testI Test early and oftenI Parsing first, Attributes secondI Allow the natural AST to fall outI Refine grammar/AST
Joel de Guzman and Michael Caisse Using X3
![Page 120: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/120.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Grammars from Scratch
Build on Success
I Start smallI Alternatives are a natural place to buildI Leaves up
I Compose and testI Test early and oftenI Parsing first, Attributes secondI Allow the natural AST to fall outI Refine grammar/AST
Joel de Guzman and Michael Caisse Using X3
![Page 121: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/121.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Outline
3 Grammars from ScratchGrammars from Scratch
4 Fun with X3IntroductionCode OrganizationASTsGrammarsError Handling
5 AttributesAST Traversal
Joel de Guzman and Michael Caisse Using X3
![Page 122: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/122.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
x3_fun
A calculator example supporting functions.
Joel de Guzman and Michael Caisse Using X3
![Page 123: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/123.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
x3_fun
Input:
(123 + 456) * 789
Output:
456831
Joel de Guzman and Michael Caisse Using X3
![Page 124: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/124.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
x3_fun
Input:
sin(45 * (pi / 180))
Output:
0.707
Joel de Guzman and Michael Caisse Using X3
![Page 125: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/125.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Outline
3 Grammars from ScratchGrammars from Scratch
4 Fun with X3IntroductionCode OrganizationASTsGrammarsError Handling
5 AttributesAST Traversal
Joel de Guzman and Michael Caisse Using X3
![Page 126: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/126.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Code Organization
Parser Directory Structure
I funI ast.hppI ast_adapted.hppI common.hppI expression.hppI expression_def.hpp
I srcI expression.cpp
I test
Joel de Guzman and Michael Caisse Using X3
![Page 127: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/127.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Code Organization
Parser Directory Structure
I funI ast.hppI ast_adapted.hppI common.hppI expression.hppI expression_def.hpp
I srcI expression.cpp
I test
Joel de Guzman and Michael Caisse Using X3
![Page 128: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/128.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Outline
3 Grammars from ScratchGrammars from Scratch
4 Fun with X3IntroductionCode OrganizationASTsGrammarsError Handling
5 AttributesAST Traversal
Joel de Guzman and Michael Caisse Using X3
![Page 129: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/129.jpg)
ASTs Part 1 (ast.hpp)
struct nil {};struct signed_;struct expression;struct function_call;
struct operand :x3::variant<
nil, double, x3::forward_ast<signed_>, x3::forward_ast<expression>, x3::forward_ast<function_call>
>{
using base_type::base_type;using base_type::operator=;
};
![Page 130: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/130.jpg)
ASTs Part 2 (ast.hpp)
struct signed_{
char sign;operand operand_;
};
struct operation : x3::position_tagged{
char operator_;operand operand_;
};
struct expression : x3::position_tagged{
operand first;std::list<operation> rest;
};
struct function_call : x3::position_tagged{
std::string name;std::list<expression> arguments;
};
![Page 131: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/131.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Code Organization
Parser Directory Structure
I funI ast.hppI ast_adapted.hppI common.hppI expression.hppI expression_def.hpp
I srcI expression.cpp
I test
Joel de Guzman and Michael Caisse Using X3
![Page 132: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/132.jpg)
Fusion Adaptation (ast_adapted.hpp)
BOOST_FUSION_ADAPT_STRUCT(fun::ast::signed_,(char, sign)(fun::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(fun::ast::operation,(char, operator_)(fun::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(fun::ast::expression,(fun::ast::operand, first)(std::list<fun::ast::operation>, rest)
)
BOOST_FUSION_ADAPT_STRUCT(fun::ast::function_call,(std::string, name)(std::list<fun::ast::expression>, arguments)
)
![Page 133: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/133.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Outline
3 Grammars from ScratchGrammars from Scratch
4 Fun with X3IntroductionCode OrganizationASTsGrammarsError Handling
5 AttributesAST Traversal
Joel de Guzman and Michael Caisse Using X3
![Page 134: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/134.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Code Organization
Parser Directory Structure
I funI ast.hppI ast_adapted.hppI common.hppI expression.hppI expression_def.hpp
I srcI expression.cpp
I test
Joel de Guzman and Michael Caisse Using X3
![Page 135: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/135.jpg)
Simple Grammars (common.hpp)
Using BOOST_SPIRIT_DEFINE
using x3::raw;using x3::lexeme;using x3::alpha;using x3::alnum;
struct identifier_class;typedef
x3::rule<identifier_class, std::string>identifier_type;identifier_type const identifier = "identifier";
auto const identifier_def= raw[lexeme[(alpha | ’_’) >> *(alnum | ’_’)]];
BOOST_SPIRIT_DEFINE(identifier);
![Page 136: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/136.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Simple Grammars (common.hpp)
Using BOOST_SPIRIT_DEFINE
struct identifier_class;
typedefx3::rule<identifier_class, std::string>
identifier_type;identifier_type const identifier = "identifier";
auto const identifier_def= raw[lexeme[(alpha | ’_’) >> *(alnum | ’_’)]];
BOOST_SPIRIT_DEFINE(identifier);
Joel de Guzman and Michael Caisse Using X3
![Page 137: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/137.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Simple Grammars (common.hpp)
Using BOOST_SPIRIT_DEFINE
struct identifier_class;
typedefx3::rule<identifier_class, std::string>
identifier_type;identifier_type const identifier = "identifier";
auto const identifier_def= raw[lexeme[(alpha | ’_’) >> *(alnum | ’_’)]];
BOOST_SPIRIT_DEFINE(identifier);
Joel de Guzman and Michael Caisse Using X3
![Page 138: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/138.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Simple Grammars (common.hpp)
Using BOOST_SPIRIT_DEFINE
struct identifier_class;
typedefx3::rule<identifier_class, std::string>
identifier_type;identifier_type const identifier = "identifier";
auto const identifier_def= raw[lexeme[(alpha | ’_’) >> *(alnum | ’_’)]];
BOOST_SPIRIT_DEFINE(identifier);
Joel de Guzman and Michael Caisse Using X3
![Page 139: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/139.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Simple Grammars (common.hpp)
Using BOOST_SPIRIT_DEFINE
struct identifier_class;
typedefx3::rule<identifier_class, std::string>
identifier_type;identifier_type const identifier = "identifier";
auto const identifier_def= raw[lexeme[(alpha | ’_’) >> *(alnum | ’_’)]];
BOOST_SPIRIT_DEFINE(identifier);
Joel de Guzman and Michael Caisse Using X3
![Page 140: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/140.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Rule Naming Convention
Example (The Rule ID)identifier_class
Example (The Rule Type)identifier_type
Example (The Rule Definition)identifier_def
Example (The Rule)identifier
Joel de Guzman and Michael Caisse Using X3
![Page 141: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/141.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Rule Naming Convention
Example (The Rule ID)identifier_class
Example (The Rule Type)identifier_type
Example (The Rule Definition)identifier_def
Example (The Rule)identifier
Joel de Guzman and Michael Caisse Using X3
![Page 142: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/142.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Rule Naming Convention
Example (The Rule ID)identifier_class
Example (The Rule Type)identifier_type
Example (The Rule Definition)identifier_def
Example (The Rule)identifier
Joel de Guzman and Michael Caisse Using X3
![Page 143: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/143.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Rule Naming Convention
Example (The Rule ID)identifier_class
Example (The Rule Type)identifier_type
Example (The Rule Definition)identifier_def
Example (The Rule)identifier
Joel de Guzman and Michael Caisse Using X3
![Page 144: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/144.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Rule Naming Convention
Example (The Rule ID)identifier_class
Example (The Rule Type)identifier_type
Example (The Rule Definition)identifier_def
Example (The Rule)identifier
Joel de Guzman and Michael Caisse Using X3
![Page 145: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/145.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Code Organization
Parser Directory Structure
I funI ast.hppI ast_adapted.hppI common.hppI expression.hppI expression_def.hpp
I srcI expression.cpp
I test
Joel de Guzman and Michael Caisse Using X3
![Page 146: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/146.jpg)
Declaring a Grammar (expression.hpp)
Using BOOST_SPIRIT_DECLARE
namespace parser{
struct expression_class;typedef
x3::rule<expression_class, ast::expression>expression_type;BOOST_SPIRIT_DECLARE(expression_type);
}
parser::expression_type const& expression();
![Page 147: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/147.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Code Organization
Parser Directory Structure
I funI ast.hppI ast_adapted.hppI common.hppI expression.hppI expression_def.hpp
I srcI expression.cpp
I test
Joel de Guzman and Michael Caisse Using X3
![Page 148: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/148.jpg)
Defining a Grammar (expression_def.hpp)
struct additive_expr_class;struct multiplicative_expr_class;struct unary_expr_class;struct primary_expr_class;struct argument_list_class;struct function_call_class;
![Page 149: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/149.jpg)
Defining a Grammar (expression_def.hpp)
typedef x3::rule<additive_expr_class, ast::expression>additive_expr_type;
typedefx3::rule<multiplicative_expr_class, ast::expression>
multiplicative_expr_type;
typedefx3::rule<unary_expr_class, ast::operand>
unary_expr_type;
typedefx3::rule<primary_expr_class, ast::operand>
primary_expr_type;
typedefx3::rule<argument_list_class, std::list<ast::expression>>
argument_list_type;
typedefx3::rule<function_call_class, ast::function_call>
function_call_type;
![Page 150: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/150.jpg)
Defining a Grammar (expression_def.hpp)
expression_type constexpression = "expression";
additive_expr_type constadditive_expr = "additive_expr";
multiplicative_expr_type constmultiplicative_expr = "multiplicative_expr";
unary_expr_type constunary_expr = "unary_expr";
primary_expr_type constprimary_expr = "primary_expr";
argument_list_type constargument_list = "argument_list";
function_call_type constfunction_call = "function_call";
![Page 151: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/151.jpg)
Defining a Grammar (expression_def.hpp)
auto const additive_expr_def =multiplicative_expr>> *( (char_(’+’) > multiplicative_expr)
| (char_(’-’) > multiplicative_expr))
;
auto const multiplicative_expr_def =unary_expr>> *( (char_(’*’) > unary_expr)
| (char_(’/’) > unary_expr))
;
auto const unary_expr_def =primary_expr
| (char_(’-’) > primary_expr)| (char_(’+’) > primary_expr);
![Page 152: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/152.jpg)
Defining a Grammar (expression_def.hpp)
auto argument_list_def = expression % ’,’;
auto function_call_def =identifier
>> -(’(’ > argument_list > ’)’);
auto const primary_expr_def =double_
| function_call| ’(’ > expression > ’)’;
auto const expression_def = additive_expr;
![Page 153: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/153.jpg)
Defining a Grammar (expression_def.hpp)
BOOST_SPIRIT_DEFINE(expression
, additive_expr, multiplicative_expr, unary_expr, primary_expr, argument_list, function_call
);
![Page 154: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/154.jpg)
Defining a Grammar (expression_def.hpp)
Decorators: Annotations and Error Handlers
struct unary_expr_class : annotation_base {};struct primary_expr_class : annotation_base {};struct function_call_class : annotation_base {};
struct expression_class :annotation_base, error_handler_base {};
![Page 155: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/155.jpg)
Defining a Grammar (expression_def.hpp)
namespace fun{
parser::expression_type const& expression(){
return parser::expression;}
}
![Page 156: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/156.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Code Organization
Parser Directory Structure
I funI ast.hppI ast_adapted.hppI common.hppI expression.hppI expression_def.hpp
I srcI expression.cpp
I test
Joel de Guzman and Michael Caisse Using X3
![Page 157: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/157.jpg)
Instantiating a Grammar (config.hpp)
// Our Iterator Typetypedef std::string::const_iterator iterator_type;
// The Phrase Parse Contexttypedef
x3::phrase_parse_context<x3::ascii::space_type>::typephrase_context_type;
// Our Error Handlertypedef error_handler<iterator_type> error_handler_type;
// Combined Error Handler and Phrase Parse Contexttypedef x3::with_context<
error_handler_tag, std::reference_wrapper<error_handler_type> const, phrase_context_type>::type
context_type;
![Page 158: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/158.jpg)
Instantiating a Grammar (expression.cpp)
namespace fun { namespace parser{
BOOST_SPIRIT_INSTANTIATE(expression_type, iterator_type, context_type);
}}
![Page 159: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/159.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Outline
3 Grammars from ScratchGrammars from Scratch
4 Fun with X3IntroductionCode OrganizationASTsGrammarsError Handling
5 AttributesAST Traversal
Joel de Guzman and Michael Caisse Using X3
![Page 160: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/160.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Error Handling
Expectation Operator
auto const additive_expr_def =multiplicative_expr>> *( (char_(’+’) > multiplicative_expr)
| (char_(’-’) > multiplicative_expr))
;
Joel de Guzman and Michael Caisse Using X3
![Page 161: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/161.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Error Handling
Expectation Operator
auto const additive_expr_def =multiplicative_expr>> *( (char_(’+’) > multiplicative_expr)
| (char_(’-’) > multiplicative_expr))
;
Joel de Guzman and Michael Caisse Using X3
![Page 162: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/162.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Error Handling
Expect Directive
auto const additive_expr_def =multiplicative_expr>> *( (char_(’+’) >> expect[multiplicative_expr])
| (char_(’-’) >> expect[multiplicative_expr]))
;
Joel de Guzman and Michael Caisse Using X3
![Page 163: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/163.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Error Handling
Expectation Failure
template <typename Iterator>struct expectation_failure : std::runtime_error{public:
expectation_failure(Iterator where, std::string const& which);~expectation_failure() throw();
std::string which() const;Iterator const& where() const;
/*...*/};
Joel de Guzman and Michael Caisse Using X3
![Page 164: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/164.jpg)
Defining a Grammar (expression_def.hpp)
Decorators: Annotations and Error Handlers
struct unary_expr_class : annotation_base {};struct primary_expr_class : annotation_base {};struct function_call_class : annotation_base {};
struct expression_class :annotation_base, error_handler_base {};
![Page 165: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/165.jpg)
Error Handling
Error Handler
// X3 Error Handler Utility
template <typename Iterator>
using error_handler = x3::error_handler<Iterator>;
// tag used to get our error handler from the context
struct error_handler_tag;
struct error_handler_base
{
error_handler_base();
template <typename Iterator, typename Exception, typename Context>
x3::error_handler_result on_error(
Iterator& first, Iterator const& last
, Exception const& x, Context const& context);
std::map<std::string, std::string> id_map;
};
![Page 166: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/166.jpg)
Error Handling
error_handler_base::on_error
template <typename Iterator, typename Exception, typename Context>
inline x3::error_handler_result
error_handler_base::on_error(
Iterator& first, Iterator const& last
, Exception const& x, Context const& context)
{
std::string which = x.which();
auto iter = id_map.find(which);
if (iter != id_map.end())
which = iter->second;
std::string message = "Error! Expecting: " + which + " here:";
auto& error_handler = x3::get<error_handler_tag>(context).get();
error_handler(x.where(), message);
return x3::error_handler_result::fail;
}
![Page 167: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/167.jpg)
Error Handling
error_handler_base constructor
inline error_handler_base::error_handler_base(){
id_map["expression"] = "Expression";id_map["additive_expr"] = "Expression";id_map["multiplicative_expr"] = "Expression";id_map["unary_expr"] = "Expression";id_map["primary_expr"] = "Expression";id_map["argument_list"] = "Argument List";
}
![Page 168: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/168.jpg)
Annotations
Annotating the AST with the iterator position
struct annotation_base
{
template <typename Iterator, typename Context>
void on_success(Iterator const& first, Iterator const& last
, ast::operand& ast, Context const& context);
template <typename T, typename Iterator, typename Context>
inline void on_success(Iterator const& first, Iterator const& last
, T& ast, Context const& context);
};
![Page 169: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/169.jpg)
Annotations
annotation_base::on_success
template <typename T, typename Iterator, typename Context>
inline voidannotation_base::on_success(Iterator const& first, Iterator const& last
, T& ast, Context const& context)
{
auto& error_handler = x3::get<error_handler_tag>(context).get();
error_handler.tag(ast, first, last);
}
![Page 170: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/170.jpg)
Annotations
annotation_base::on_success
template <typename Iterator, typename Context>inline voidannotation_base::on_success(Iterator const& first, Iterator const& last, ast::operand& ast, Context const& context)
{auto& error_handler
= x3::get<error_handler_tag>(context).get();
auto annotate = [&](auto& node){
error_handler.tag(node, first, last);};
ast.apply_visitor(x3::make_lambda_visitor<void>(annotate));
}
![Page 171: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/171.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Error Handling
Bad Syntax
foo(123, $%)
Error Message
In file bad_arguments.fun, line 1:Error! Expecting: ’)’ here:foo(123, $%)_______^_
Joel de Guzman and Michael Caisse Using X3
![Page 172: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/172.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Test Driven Development
Test Directory Structure
I funI srcI test
I parse_expressionI function_call1.inputI function_call1.expectI bad_arguments.inputI bad_arguments.expectI ...
I parse_expression_test.cppI eval_expression
I ...I eval_expression_test.cpp
Joel de Guzman and Michael Caisse Using X3
![Page 173: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/173.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Test Driven Development
Test Directory Structure
I funI srcI test
I parse_expressionI function_call1.inputI function_call1.expectI bad_arguments.inputI bad_arguments.expectI ...
I parse_expression_test.cppI eval_expression
I ...I eval_expression_test.cpp
Joel de Guzman and Michael Caisse Using X3
![Page 174: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/174.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Test Driven Development
Test Directory Structure
I funI srcI test
I parse_expressionI function_call1.inputI function_call1.expectI bad_arguments.inputI bad_arguments.expectI ...
I parse_expression_test.cppI eval_expression
I ...I eval_expression_test.cpp
Joel de Guzman and Michael Caisse Using X3
![Page 175: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/175.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Test Driven Development
Test Directory Structure
I funI srcI test
I parse_expressionI function_call1.inputI function_call1.expectI bad_arguments.inputI bad_arguments.expectI ...
I parse_expression_test.cppI eval_expression
I ...I eval_expression_test.cpp
Joel de Guzman and Michael Caisse Using X3
![Page 176: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/176.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing Intro Organization ASTs Grammars Error Handling
Test Driven Development
Test Directory Structure
I funI srcI test
I parse_expressionI function_call1.inputI function_call1.expectI bad_arguments.inputI bad_arguments.expectI ...
I parse_expression_test.cppI eval_expression
I ...I eval_expression_test.cpp
Joel de Guzman and Michael Caisse Using X3
![Page 177: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/177.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing AST Traversal
Outline
3 Grammars from ScratchGrammars from Scratch
4 Fun with X3IntroductionCode OrganizationASTsGrammarsError Handling
5 AttributesAST Traversal
Joel de Guzman and Michael Caisse Using X3
![Page 178: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/178.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing AST Traversal
Attribute Parsing vs Semantic Actions
Avoid semantic actions! Generate ASTs instead.
I Imperative semantic actions are ugly warts in an elegantdeclarative grammar.
I Semantic actions look even uglier and verbose in X3 withnative C++ lambda.
I Use semantic actions only to facilitate the generation of anattribute.
I If you really can’t avoid semantic actions, at least makethem side-effect free. Back tracking can cause havoc whenactions are called multiple times.
Joel de Guzman and Michael Caisse Using X3
![Page 179: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/179.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing AST Traversal
Attribute Parsing vs Semantic Actions
Avoid semantic actions! Generate ASTs instead.
I Imperative semantic actions are ugly warts in an elegantdeclarative grammar.
I Semantic actions look even uglier and verbose in X3 withnative C++ lambda.
I Use semantic actions only to facilitate the generation of anattribute.
I If you really can’t avoid semantic actions, at least makethem side-effect free. Back tracking can cause havoc whenactions are called multiple times.
Joel de Guzman and Michael Caisse Using X3
![Page 180: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/180.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing AST Traversal
Attribute Parsing vs Semantic Actions
Avoid semantic actions! Generate ASTs instead.
I Imperative semantic actions are ugly warts in an elegantdeclarative grammar.
I Semantic actions look even uglier and verbose in X3 withnative C++ lambda.
I Use semantic actions only to facilitate the generation of anattribute.
I If you really can’t avoid semantic actions, at least makethem side-effect free. Back tracking can cause havoc whenactions are called multiple times.
Joel de Guzman and Michael Caisse Using X3
![Page 181: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/181.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing AST Traversal
Attribute Parsing vs Semantic Actions
Avoid semantic actions! Generate ASTs instead.
I Imperative semantic actions are ugly warts in an elegantdeclarative grammar.
I Semantic actions look even uglier and verbose in X3 withnative C++ lambda.
I Use semantic actions only to facilitate the generation of anattribute.
I If you really can’t avoid semantic actions, at least makethem side-effect free. Back tracking can cause havoc whenactions are called multiple times.
Joel de Guzman and Michael Caisse Using X3
![Page 182: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/182.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing AST Traversal
Attribute Parsing vs Semantic Actions
Avoid semantic actions! Generate ASTs instead.
I Imperative semantic actions are ugly warts in an elegantdeclarative grammar.
I Semantic actions look even uglier and verbose in X3 withnative C++ lambda.
I Use semantic actions only to facilitate the generation of anattribute.
I If you really can’t avoid semantic actions, at least makethem side-effect free. Back tracking can cause havoc whenactions are called multiple times.
Joel de Guzman and Michael Caisse Using X3
![Page 183: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/183.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing AST Traversal
Attribute Parsing vs Semantic Actions
Avoid semantic actions! Generate ASTs instead.
I Imperative semantic actions are ugly warts in an elegantdeclarative grammar.
I Semantic actions look even uglier and verbose in X3 withnative C++ lambda.
I Use semantic actions only to facilitate the generation of anattribute.
I If you really can’t avoid semantic actions, at least makethem side-effect free. Back tracking can cause havoc whenactions are called multiple times.
Joel de Guzman and Michael Caisse Using X3
![Page 184: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/184.jpg)
Printing the AST
struct printer{
typedef void result_type;
printer(std::ostream& out): out(out)
{}
void operator()(ast::nil) const { BOOST_ASSERT(0); }void operator()(double ast) const;void operator()(ast::operation const& ast) const;void operator()(ast::signed_ const& ast) const;void operator()(ast::expression const& ast) const;void operator()(ast::function_call const& ast) const;
std::ostream& out;};
![Page 185: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/185.jpg)
Printing the AST
void printer::operator()(double ast) const{
out << ast;}
void printer::operator()(ast::operation const& ast) const{
switch (ast.operator_){
case ’+’: out << " + "; break;case ’-’: out << " - "; break;case ’*’: out << " * "; break;case ’/’: out << " / "; break;
default:BOOST_ASSERT(0);return;
}boost::apply_visitor(*this, ast.operand_);
}
![Page 186: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/186.jpg)
Printing the AST
void printer::operator()(ast::expression const& ast) const{
if (ast.rest.size())out << ’(’;
boost::apply_visitor(*this, ast.first);for (auto const& oper : ast.rest)
(*this)(oper);if (ast.rest.size())
out << ’)’;}
![Page 187: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/187.jpg)
Printing the AST
void printer::operator()(ast::function_call const& ast) const{
out << ast.name;if (ast.arguments.size())
out << ’(’;bool first = true;for (auto const& arg : ast.arguments){
if (first)first = false;
elseout << ", ";
(*this)(arg);}if (ast.arguments.size())
out << ’)’;}
![Page 188: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/188.jpg)
The Interpreter
class interpreter{public:
typedef std::function<void(x3::position_tagged, std::string const&)>
error_handler_type;
template <typename ErrorHandler>interpreter(ErrorHandler const& error_handler);
template <typename F>void add_function(std::string name, F f);
float eval(ast::expression const& ast);
private:
std::map<std::string
, std::pair<std::function<double(double* args)>, std::size_t>>fmap;
error_handler_type error_handler;};
![Page 189: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/189.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing AST Traversal
The Interpreter
// Add some functions:interp.add_function("pi", []{ return M_PI; });interp.add_function("sin", [](double x){ return std::sin(x); });interp.add_function("cos", [](double x){ return std::cos(x); });
Joel de Guzman and Michael Caisse Using X3
![Page 190: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/190.jpg)
ciere.com
Grammars from Scratch Fun with X3 Attribute Parsing AST Traversal
The Interpreter
sin(45 * (pi / 180))
Joel de Guzman and Michael Caisse Using X3
![Page 191: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/191.jpg)
The Interpreter
template <typename F>
inline void interpreter::add_function(std::string name, F f)
{
static_assert(detail::arity<F>::value <= detail::max_arity,
"Function F has too many arguments (maximum == 5).");
std::function<double(double* args)> f_adapter = detail::adapter_function<F>(f);
fmap[name] = std::make_pair(f_adapter, detail::arity<F>::value);
}
![Page 192: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/192.jpg)
The Interpreter
double interpreter_impl::operator()(double lhs, ast::operation const& ast) const
{
double rhs = boost::apply_visitor(*this, ast.operand_);
switch (ast.operator_)
{
case ’+’: return lhs + rhs;
case ’-’: return lhs - rhs;
case ’*’: return lhs * rhs;
case ’/’: return lhs / rhs;
default:
BOOST_ASSERT(0);
return -1;
}
}
![Page 193: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/193.jpg)
The Interpreter
double interpreter_impl::operator()(ast::function_call const& ast) const
{
auto iter = fmap.find(ast.name);
if (iter == fmap.end()) {
error_handler(ast, "Undefined function " + ast.name + ’.’);
return -1;
}
if (iter->second.second != ast.arguments.size()) {
std::stringstream out;
out << "Wrong number of arguments to function " << ast.name << " ("
<< iter->second.second << " expected)." << std::endl;
error_handler(ast, out.str());
return -1;
}
// Get the args
double args[detail::max_arity];
double* p = args;
for (auto const& arg : ast.arguments)
*p++ = (*this)(arg);
// call user function
return iter->second.first(args);
}
![Page 194: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/194.jpg)
ciere.com
Part III
Workshop
Joel de Guzman and Michael Caisse Using X3
![Page 195: Using X3 - A Spirit X3 Tutorial and Workshop - Ciereciere.com/cppnow15/using_x3.pdf · Joel de Guzman and Michael Caisse Using X3. ciere.com IntroductionElements ParsersRulesGrammarsAttributes](https://reader034.vdocument.in/reader034/viewer/2022042620/5ad76d167f8b9a5b538c9ffb/html5/thumbnails/195.jpg)
ciere.com
Workshop
http://ciere.com/cppnow15/
Joel de Guzman and Michael Caisse Using X3