2
0
mirror of https://github.com/boostorg/parser.git synced 2026-01-21 17:12:16 +00:00
Files
parser/doc/html/boost_parser/tutorial/hello__whomever.html
2024-12-08 17:19:48 -06:00

103 lines
9.2 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello, Whomever</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Parser">
<link rel="up" href="../tutorial.html" title="Tutorial">
<link rel="prev" href="terminology.html" title="Terminology">
<link rel="next" href="a_trivial_example.html" title="A Trivial Example">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="spirit-nav">
<a accesskey="p" href="terminology.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="a_trivial_example.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_parser.tutorial.hello__whomever"></a><a class="link" href="hello__whomever.html" title="Hello, Whomever">Hello, Whomever</a>
</h3></div></div></div>
<p>
This is just about the most minimal example of using Boost.Parser that one
could write. We take a string from the command line, or <code class="computeroutput"><span class="string">"World"</span></code>
if none is given, and then we parse it:
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">parser</span><span class="special">/</span><span class="identifier">parser</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
<span class="keyword">namespace</span> <span class="identifier">bp</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">parser</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span> <span class="special">*</span> <span class="identifier">argv</span><span class="special">[])</span>
<span class="special">{</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">input</span> <span class="special">=</span> <span class="string">"World"</span><span class="special">;</span>
<span class="keyword">if</span> <span class="special">(</span><span class="number">1</span> <span class="special">&lt;</span> <span class="identifier">argc</span><span class="special">)</span>
<span class="identifier">input</span> <span class="special">=</span> <span class="identifier">argv</span><span class="special">[</span><span class="number">1</span><span class="special">];</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">result</span><span class="special">;</span>
<span class="identifier">bp</span><span class="special">::</span><span class="identifier">parse</span><span class="special">(</span><span class="identifier">input</span><span class="special">,</span> <span class="special">*</span><span class="identifier">bp</span><span class="special">::</span><span class="identifier">char_</span><span class="special">,</span> <span class="identifier">result</span><span class="special">);</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Hello, "</span> <span class="special">&lt;&lt;</span> <span class="identifier">result</span> <span class="special">&lt;&lt;</span> <span class="string">"!\n"</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
</p>
<p>
The expression <code class="computeroutput"><span class="special">*</span><span class="identifier">bp</span><span class="special">::</span><span class="identifier">char_</span></code>
is a parser-expression. It uses one of the many parsers that Boost.Parser
provides: <code class="computeroutput"><a class="link" href="../../boost/parser/char_.html" title="Global char_">char_</a></code>.
Like all Boost.Parser parsers, it has certain operations defined on it. In
this case, <code class="computeroutput"><span class="special">*</span><span class="identifier">bp</span><span class="special">::</span><span class="identifier">char_</span></code>
is using an overloaded <code class="computeroutput"><span class="keyword">operator</span><span class="special">*</span></code> as the C++ version of a <a href="https://en.wikipedia.org/wiki/Kleene_star" target="_top">Kleene
star</a> operator. Since C++ has no postfix unary <code class="computeroutput"><span class="special">*</span></code>
operator, we have to use the one we have, so it is used as a prefix.
</p>
<p>
So, <code class="computeroutput"><span class="special">*</span><span class="identifier">bp</span><span class="special">::</span><span class="identifier">char_</span></code>
means "any number of characters". In other words, it really cannot
fail. Even an empty string will match it.
</p>
<p>
The parse operation is performed by calling the <code class="computeroutput"><a class="link" href="../../boost/parser/parse_id2.html" title="Function template parse">parse()</a></code>
function, passing the parser as one of the arguments:
</p>
<pre class="programlisting"><span class="identifier">bp</span><span class="special">::</span><span class="identifier">parse</span><span class="special">(</span><span class="identifier">input</span><span class="special">,</span> <span class="special">*</span><span class="identifier">bp</span><span class="special">::</span><span class="identifier">char_</span><span class="special">,</span> <span class="identifier">result</span><span class="special">);</span>
</pre>
<p>
The arguments here are: <code class="computeroutput"><span class="identifier">input</span></code>,
the range to parse; <code class="computeroutput"><span class="special">*</span><span class="identifier">bp</span><span class="special">::</span><span class="identifier">char_</span></code>,
the parser used to do the parse; and <code class="computeroutput"><span class="identifier">result</span></code>,
an out-parameter into which to put the result of the parse. Don't get too
caught up on this method of getting the parse result out of <code class="computeroutput"><a class="link" href="../../boost/parser/parse_id2.html" title="Function template parse">parse()</a></code>; there are multiple ways
of doing so, and we'll cover all of them in subsequent sections.
</p>
<p>
Also, just ignore for now the fact that Boost.Parser somehow figured out
that the result type of the <code class="computeroutput"><span class="special">*</span><span class="identifier">bp</span><span class="special">::</span><span class="identifier">char_</span></code>
parser is a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>. There are clear rules for this
that we'll cover later.
</p>
<p>
The effects of this call to <code class="computeroutput"><a class="link" href="../../boost/parser/parse_id2.html" title="Function template parse">parse()</a></code>
is not very interesting — since the parser we gave it cannot ever
fail, and because we're placing the output in the same type as the input,
it just copies the contents of <code class="computeroutput"><span class="identifier">input</span></code>
to <code class="computeroutput"><span class="identifier">result</span></code>.
</p>
</div>
<div class="copyright-footer">Copyright © 2020 T. Zachary Laine<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="terminology.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="a_trivial_example.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>