<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Boost.Spirit.Qi - Parser bricht einfach ab]]></title><description><![CDATA[<p>Halloele,</p>
<p>Ich hab mir mit boost.spirit einen Parser fuer Expressions gebaut. Folgendes Programm:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;typeinfo&gt;

#include &lt;boost/spirit/include/qi.hpp&gt;

#include &quot;ExpressionGrammar.hpp&quot;

int main()
{
	constexpr char const input[] = &quot;++ blubb /* f(// x)&quot;;

	auto begin = input, end = input + sizeof input - 1;

	ExpressionGrammar&lt;char const*&gt; g;
	Expression* result;

	if(!bsq::phrase_parse(begin, end, g, bsq::blank, result) || begin != end)
	{
		std::cerr &lt;&lt; &quot;parse error here: &quot; &lt;&lt; begin &lt;&lt; '\n';
		return 1;
	}

	std::cerr &lt;&lt; &quot;success\n&quot;;
	std::cout &lt;&lt; *result;
}
</code></pre>
<p>Erwartetes Ergebnis: GeneralizedExpression(&quot;++&quot;, Variable(&quot;blubb&quot;), &quot;/<em>&quot;, FunctionCall(&quot;f&quot;, { GeneralizedExpression(&quot;//&quot;, Variable(&quot;x&quot;)) }))<br />
Tatsaechlicher Output: parse error here: blubb /</em> f(// x)</p>
<p>So sieht meine ExpressionGrammar aus:</p>
<pre><code>template &lt;typename Iter&gt;
struct ExpressionGrammar : bsq::grammar&lt;Iter, Expression*()&gt;
{
	ExpressionGrammar() : ExpressionGrammar::base_type(start)
	{
		using namespace bsq;
		using namespace bp;

		identifier      = alpha &gt;&gt; *(alnum | '_');
		variable        = identifier[_val = new_&lt;Variable&gt;(_1)];
		literal         = int_[_val = new_&lt;IntLiteral&gt;(_1)] | double_[_val = new_&lt;DoubleLiteral&gt;(_1)];
		functionCall    = identifier[_val = new_&lt;FunctionCall&gt;(_1)] &gt;&gt; '(' &gt;&gt; generalizedExpr[push_back(bp::at_c&lt;1&gt;(*_val), _1)] % ',' &gt;&gt; ')';
		primaryExpr    %= literal | variable | ('(' &gt;&gt; generalizedExpr &gt;&gt; ')') | functionCall;
		opChar         %= bsq::char_('+') | '-' | '*' | '/' | '%' | '^' | '!' | '$' | '&amp;' | '|' | '=' | '?' | '&lt;' | '&gt;' | '~';
		op             %= +opChar;
		generalizedExpr = eps[_val = new_&lt;GeneralizedExpression&gt;()] &gt;&gt; +(op | primaryExpr)[push_back(bp::at_c&lt;0&gt;(*_val), _1)];
		start          %= generalizedExpr;
	}

private:
	bsq::rule&lt;Iter, std::string()&gt; identifier;
	bsq::rule&lt;Iter, Variable*()&gt; variable;
	bsq::rule&lt;Iter, Literal*()&gt; literal;
	bsq::rule&lt;Iter, FunctionCall*()&gt; functionCall;
	bsq::rule&lt;Iter, Expression*()&gt; primaryExpr;
	bsq::rule&lt;Iter, char()&gt; opChar;
	bsq::rule&lt;Iter, std::string()&gt; op;
	bsq::rule&lt;Iter, GeneralizedExpression*()&gt; generalizedExpr;
	bsq::rule&lt;Iter, Expression*()&gt; start;
};
</code></pre>
<p>Ich habe schon zuerst geglaubt, die Grammatik sei mehrdeutig aufgrund von identifier und functionCall, aber dem ist offenbar nicht so.</p>
<p>Hat jemand eine Idee, woran das liegen koennte? Ich hab ja schon damit gekaempft, das kompiliert zu bekommen, aber jetzt weiss ich echt nicht mehr weiter. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
Sollte es notwendig sein, kann ich natuerlich auch noch meine AST Klassen reinstellen.</p>
<p>Der Kellerautomat</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/318006/boost-spirit-qi-parser-bricht-einfach-ab</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Jul 2026 21:39:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/318006.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 27 Jun 2013 19:03:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Boost.Spirit.Qi - Parser bricht einfach ab on Thu, 27 Jun 2013 19:03:03 GMT]]></title><description><![CDATA[<p>Halloele,</p>
<p>Ich hab mir mit boost.spirit einen Parser fuer Expressions gebaut. Folgendes Programm:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;typeinfo&gt;

#include &lt;boost/spirit/include/qi.hpp&gt;

#include &quot;ExpressionGrammar.hpp&quot;

int main()
{
	constexpr char const input[] = &quot;++ blubb /* f(// x)&quot;;

	auto begin = input, end = input + sizeof input - 1;

	ExpressionGrammar&lt;char const*&gt; g;
	Expression* result;

	if(!bsq::phrase_parse(begin, end, g, bsq::blank, result) || begin != end)
	{
		std::cerr &lt;&lt; &quot;parse error here: &quot; &lt;&lt; begin &lt;&lt; '\n';
		return 1;
	}

	std::cerr &lt;&lt; &quot;success\n&quot;;
	std::cout &lt;&lt; *result;
}
</code></pre>
<p>Erwartetes Ergebnis: GeneralizedExpression(&quot;++&quot;, Variable(&quot;blubb&quot;), &quot;/<em>&quot;, FunctionCall(&quot;f&quot;, { GeneralizedExpression(&quot;//&quot;, Variable(&quot;x&quot;)) }))<br />
Tatsaechlicher Output: parse error here: blubb /</em> f(// x)</p>
<p>So sieht meine ExpressionGrammar aus:</p>
<pre><code>template &lt;typename Iter&gt;
struct ExpressionGrammar : bsq::grammar&lt;Iter, Expression*()&gt;
{
	ExpressionGrammar() : ExpressionGrammar::base_type(start)
	{
		using namespace bsq;
		using namespace bp;

		identifier      = alpha &gt;&gt; *(alnum | '_');
		variable        = identifier[_val = new_&lt;Variable&gt;(_1)];
		literal         = int_[_val = new_&lt;IntLiteral&gt;(_1)] | double_[_val = new_&lt;DoubleLiteral&gt;(_1)];
		functionCall    = identifier[_val = new_&lt;FunctionCall&gt;(_1)] &gt;&gt; '(' &gt;&gt; generalizedExpr[push_back(bp::at_c&lt;1&gt;(*_val), _1)] % ',' &gt;&gt; ')';
		primaryExpr    %= literal | variable | ('(' &gt;&gt; generalizedExpr &gt;&gt; ')') | functionCall;
		opChar         %= bsq::char_('+') | '-' | '*' | '/' | '%' | '^' | '!' | '$' | '&amp;' | '|' | '=' | '?' | '&lt;' | '&gt;' | '~';
		op             %= +opChar;
		generalizedExpr = eps[_val = new_&lt;GeneralizedExpression&gt;()] &gt;&gt; +(op | primaryExpr)[push_back(bp::at_c&lt;0&gt;(*_val), _1)];
		start          %= generalizedExpr;
	}

private:
	bsq::rule&lt;Iter, std::string()&gt; identifier;
	bsq::rule&lt;Iter, Variable*()&gt; variable;
	bsq::rule&lt;Iter, Literal*()&gt; literal;
	bsq::rule&lt;Iter, FunctionCall*()&gt; functionCall;
	bsq::rule&lt;Iter, Expression*()&gt; primaryExpr;
	bsq::rule&lt;Iter, char()&gt; opChar;
	bsq::rule&lt;Iter, std::string()&gt; op;
	bsq::rule&lt;Iter, GeneralizedExpression*()&gt; generalizedExpr;
	bsq::rule&lt;Iter, Expression*()&gt; start;
};
</code></pre>
<p>Ich habe schon zuerst geglaubt, die Grammatik sei mehrdeutig aufgrund von identifier und functionCall, aber dem ist offenbar nicht so.</p>
<p>Hat jemand eine Idee, woran das liegen koennte? Ich hab ja schon damit gekaempft, das kompiliert zu bekommen, aber jetzt weiss ich echt nicht mehr weiter. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
Sollte es notwendig sein, kann ich natuerlich auch noch meine AST Klassen reinstellen.</p>
<p>Der Kellerautomat</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2334855</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2334855</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Thu, 27 Jun 2013 19:03:03 GMT</pubDate></item><item><title><![CDATA[Reply to Boost.Spirit.Qi - Parser bricht einfach ab on Thu, 27 Jun 2013 20:05:43 GMT]]></title><description><![CDATA[<pre><code>primaryExpr := literal 
                  |  variable
                  |  '(' generalizedExpr ')'
                  |  functionCall
</code></pre>
<p>variable -&gt; identifier<br />
functionCall -&gt; identifier</p>
<p>Die Regeln variable und functionCall haben die gleiche First Menge und sind damit mehrdeutig. Jede versucht ein functionCall ableiten zu wollen, wird eine Produktion mit variable versucht und dann als Fehler abgebrochen.</p>
<p>Ob die Rekursion mit generalizedExpr klappt, bin ich mit greade nicht sicher.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2334870</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2334870</guid><dc:creator><![CDATA[Zeus]]></dc:creator><pubDate>Thu, 27 Jun 2013 20:05:43 GMT</pubDate></item><item><title><![CDATA[Reply to Boost.Spirit.Qi - Parser bricht einfach ab on Thu, 27 Jun 2013 20:31:53 GMT]]></title><description><![CDATA[<p>Ich habe versucht, einfach mal functionCall aus der Grammatik rauszunehmen und das Ergebnis ist dasselbe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2334873</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2334873</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Thu, 27 Jun 2013 20:31:53 GMT</pubDate></item><item><title><![CDATA[Reply to Boost.Spirit.Qi - Parser bricht einfach ab on Thu, 27 Jun 2013 20:54:31 GMT]]></title><description><![CDATA[<p>Kellerautomat schrieb:</p>
<blockquote>
<p>Hat jemand eine Idee, woran das liegen koennte?</p>
</blockquote>
<p>Daran:</p>
<p>Kellerautomat schrieb:</p>
<blockquote>
<p>Ich hab mir mit boost.spirit einen Parser fuer Expressions gebaut.</p>
</blockquote>
<p>Es ist prinzipiell nicht möglich Boost.Spirit, zu debuggen. Entweder es geht, oder es geht nicht, dann wird aufs neue probiert. Davon abgesehen ist es unmöglich, für den Nutzer ansprechende Fehlermeldungen zu generieren. Ich bin bisher immer besser damit gefahren, meine Parser (für komplexere Grammatiken) selber zu schreiben.</p>
<p>Aber wenn du schon Boost.Spirit verwendest, solltest du zumindest den Schwächen (Undebuggbarkeit) ins Auge schauen und dich damit abfinden.</p>
<p><a href="http://boost-spirit.com/home/articles/doc-addendum/best-practices/" rel="nofollow">http://boost-spirit.com/home/articles/doc-addendum/best-practices/</a> schrieb:</p>
<blockquote>
<p>Take things one step at a time. Don’t try to write a grammar that covers all the complexity of your input. Start with the simplest piece of the input and write a parser for that. Gradually add more rules to your grammar as you cover more complexity in the input.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2334879</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2334879</guid><dc:creator><![CDATA[spiritus]]></dc:creator><pubDate>Thu, 27 Jun 2013 20:54:31 GMT</pubDate></item><item><title><![CDATA[Reply to Boost.Spirit.Qi - Parser bricht einfach ab on Thu, 27 Jun 2013 21:20:59 GMT]]></title><description><![CDATA[<p>Kellerautomat schrieb:</p>
<blockquote>
<p>Ich habe versucht, einfach mal functionCall aus der Grammatik rauszunehmen und das Ergebnis ist dasselbe.</p>
</blockquote>
<p>Ok und was machst du um whitespaces abzufangen? Zwischen &quot;++&quot; und &quot;blubb&quot; ist ein &quot; &quot;. Das &quot;++&quot; scheint Spirit geschluckt zu haben und den Rest der Eingabe als Fehler ausgespuckt.</p>
<p>Nachtrag:<br />
Ops den Code hast du programmiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2334883</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2334883</guid><dc:creator><![CDATA[Zeus]]></dc:creator><pubDate>Thu, 27 Jun 2013 21:20:59 GMT</pubDate></item><item><title><![CDATA[Reply to Boost.Spirit.Qi - Parser bricht einfach ab on Thu, 27 Jun 2013 21:40:03 GMT]]></title><description><![CDATA[<p>Zeus schrieb:</p>
<blockquote>
<p>Ok und was machst du um whitespaces abzufangen? Zwischen &quot;++&quot; und &quot;blubb&quot; ist ein &quot; &quot;. Das &quot;++&quot; scheint Spirit geschluckt zu haben und den Rest der Eingabe als Fehler ausgespuckt.</p>
</blockquote>
<p>Dazu uebergebe ich blank an phrase_parse. Der Whitespace nach ++ wurde auch noch geschluckt, das erste &quot;fehlerhafte&quot; Zeichen ist das 'b' von blubb.</p>
<p>Zeus schrieb:</p>
<blockquote>
<p>Nachtrag:<br />
Ops den Code hast du programmiert.</p>
</blockquote>
<p>Was meinst du?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2334886</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2334886</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Thu, 27 Jun 2013 21:40:03 GMT</pubDate></item><item><title><![CDATA[Reply to Boost.Spirit.Qi - Parser bricht einfach ab on Thu, 27 Jun 2013 22:35:38 GMT]]></title><description><![CDATA[<p>Okay, ich habe das Problem nun selbst gefunden. Ich muss offenbar bei jeder einzelnen Regel sowie bei der Grammatik einen Skipper angeben. Warum das so ist, ist mir allerdings nicht klar. Meine Grammatik sieht nun so aus:</p>
<pre><code>identifier     %= alpha &gt;&gt; *(alnum | '_');
		variable        = identifier[_val = new_&lt;Variable&gt;(_1)];
		literal         = int_[_val = new_&lt;IntLiteral&gt;(_1)] | double_[_val = new_&lt;DoubleLiteral&gt;(_1)];
		functionCall    = identifier[_val = new_&lt;FunctionCall&gt;(_1)] &gt;&gt; '(' &gt;&gt; generalizedExpr[push_back(bp::at_c&lt;1&gt;(*_val), _1)] % ',' &gt;&gt; ')';
		primaryExpr    %= literal | functionCall | variable | ('(' &gt;&gt; generalizedExpr &gt;&gt; ')');
		opChar         %= char_('+') | char_('-') | char_('*') | char_('/') | char_('%') | char_('^') | char_('!') | char_('$') | char_('&amp;') | char_('|') | char_('=') | char_('?') | char_('&lt;') | char_('&gt;') | char_('~');
		op             %= +opChar;
		generalizedExpr = eps[_val = new_&lt;GeneralizedExpression&gt;()] &gt;&gt; +(op | primaryExpr)[push_back(bp::at_c&lt;0&gt;(*_val), _1)];
		start          %= generalizedExpr;
</code></pre>
<p>Warum die ganzen char_? Weil wenn ich char_ ueberall ausser beim ersten Zeichen weglasse, die ganzen anderen Zeichen zwar erkannt werden, aber vom parser nicht zurueckgegeben werden - stattdessen bekomme ich \0. Waere auch dankbar, wenn mir jemand erklaeren koennte warum das so ist und mir evtl einen einfacheren Weg zeigen koennte.</p>
<p>Den functionCall musste ich uebrigens vor variable schieben, damit er zuerst versucht wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2334887</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2334887</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Thu, 27 Jun 2013 22:35:38 GMT</pubDate></item></channel></rss>