You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
12 KiB

3 months ago
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  5. <link href="cpphoafparser.css" rel="stylesheet">
  6. <link href="cpp2html.css" rel="stylesheet">
  7. <title>Using the cpphoafparser Library [cpphoafparser Library for the Hanoi Omega-Automata Format (HOAF), version 0.99.2]</title>
  8. </head>
  9. <body>
  10. <div class="nav"><a class="nav" href="index.html">^ Overview</a></div>
  11. <h1>
  12. Using the <span class="blue1">cpp</span><span class="blue2">hoaf</span><span class="blue3">parser</span> Library<br/>
  13. <span style="font-size:70%">The <span class="blue">cpphoafparser</span>
  14. Library for the Hanoi Omega-Automata Format (HOAF), version 0.99.2</span>
  15. </h1>
  16. <p>The cpphoafparser library can be used to parse and process &omega;-automata
  17. in the HOA format. We assume here that the reader is familiar with the basic concepts of
  18. the <a href="http://adl.github.io/hoaf/">Hanoi Omega-Automata Format (HOAF)</a>.
  19. </p>
  20. <h2><a name="general">General Structure</a></h2>
  21. <p>
  22. The two main building blocks of the parser library are the <em>parser</em> and
  23. classes implementing the <em>HOAConsumer</em> interface. A <em>HOAConsumer</em> has functions
  24. corresponding to the various elements that can occur in a HOA automaton. While parsing, the parser
  25. calls each of these functions to indicate that this particular element has just occurred.
  26. </p>
  27. <p>cpphoafparser is a <emph>header-only</emph> C++ library, i.e., it suffices to
  28. add the <span class="classname">include</span> directory to the include path of your compiler
  29. and include the corresponding header files. All cpphoafparser elements are contained in the
  30. <span class="classname">cpphoafparser</span> namespace. It uses C++11 features, so ensure that your
  31. compiler is configured to support this standard version.
  32. </p>
  33. <h3>A Basic Parser</h3>
  34. <p>The most basic use of the cpphoafparser library is shown in the following code snippet
  35. (<span class="classname">src/basic_parser_1.cc</span></span>):</p>
  36. <div class="code">
  37. <pre><span class="pre">#include "cpphoafparser/consumer/hoa_consumer_print.hh"
  38. #include "cpphoafparser/parser/hoa_parser.hh"
  39. </span><span class="keyword">
  40. using namespace</span> cpphoafparser<span class="operator">;</span><span class="comment">
  41. /** The most basic HOA parser: Read an automaton from input and print it to the output. */</span><span class="type">
  42. int</span><span class="keyword"> main</span><span class="operator">(</span><span class="type">int</span> argc<span class="operator">,</span><span class="keyword"> const</span><span class="type"> char</span><span class="operator">*</span> argv<span class="operator">[]) {</span>
  43. HOAConsumer<span class="operator">::</span>ptr consumer<span class="operator">(</span><span class="keyword">new</span> HOAConsumerPrint<span class="operator">(</span>std<span class="operator">::</span>cout<span class="operator">));</span><span class="flow">
  44. try</span><span class="operator"> {</span>
  45. HOAParser<span class="operator">::</span>parse<span class="operator">(</span>std<span class="operator">::</span>cin<span class="operator">,</span> consumer<span class="operator">);
  46. }</span><span class="flow"> catch</span><span class="operator"> (</span>std<span class="operator">::</span>exception<span class="operator">&amp;</span> e<span class="operator">) {</span>
  47. std<span class="operator">::</span>cerr<span class="operator"> &lt;&lt;</span> e<span class="operator">.</span>what<span class="operator">() &lt;&lt;</span> std<span class="operator">::</span>endl<span class="operator">;</span><span class="flow">
  48. return</span><span class="int"> 1</span><span class="operator">;
  49. }</span><span class="flow">
  50. return</span><span class="int"> 0</span><span class="operator">;
  51. }</span>
  52. </pre>
  53. </div>
  54. <p>
  55. Here, the parser reads from <span class="classname">std::cin</span>
  56. and calls into a <span class="classname">HOAConsumerPrint</span>
  57. object that just outputs the elements to <span class="classname">std::cout</span>.</p>
  58. <h3>Chaining Multiple HOAConsumers: HOAIntermediate</h3>
  59. <p>The <span class="classname">HOAIntermediate</span> class, which itself
  60. implements the <span class="classname">HOAConsumer</span> interface, provides the basis for chaining multiple
  61. <span class="classname">HOAConsumers</span>, one after another, each reading the output of the previous one.
  62. </p>
  63. <p>
  64. Consider the following example
  65. (<span class="classname">src/basic_parser_2.cc</span></span>):</p>
  66. <div class="code">
  67. <pre><span class="pre">#include "cpphoafparser/consumer/hoa_intermediate.hh"
  68. #include "cpphoafparser/consumer/hoa_consumer_null.hh"
  69. #include "cpphoafparser/parser/hoa_parser.hh"
  70. </span><span class="keyword">
  71. using namespace</span> cpphoafparser<span class="operator">;</span><span class="comment">
  72. /* An HOAIntermediate that counts invocations of addState */</span><span class="keyword">
  73. class</span> CountStates<span class="operator"> :</span><span class="keyword"> public</span> HOAIntermediate<span class="operator"> {</span><span class="keyword">
  74. public</span><span class="operator">:</span><span class="keyword">
  75. typedef</span> std<span class="operator">::</span>shared_ptr<span class="operator">&lt;</span>CountStates<span class="operator">&gt;</span> ptr<span class="operator">;</span><span class="type">
  76. unsigned int</span> count<span class="operator"> =</span><span class="int"> 0</span><span class="operator">;</span>
  77. CountStates<span class="operator">(</span>HOAConsumer<span class="operator">::</span>ptr next<span class="operator">) :</span> HOAIntermediate<span class="operator">(</span>next<span class="operator">) {
  78. }</span><span class="keyword">
  79. virtual</span><span class="type"> void</span> addState<span class="operator">(</span><span class="type">unsigned int</span> id<span class="operator">,</span>
  80. std<span class="operator">::</span>shared_ptr<span class="operator">&lt;</span>std<span class="operator">::</span>string<span class="operator">&gt;</span> info<span class="operator">,</span>
  81. label_expr<span class="operator">::</span>ptr labelExpr<span class="operator">,</span>
  82. std<span class="operator">::</span>shared_ptr<span class="operator">&lt;</span>int_list<span class="operator">&gt;</span> accSignature<span class="operator">)</span> override<span class="operator"> {</span>
  83. count<span class="operator">++;</span>
  84. next<span class="operator">-&gt;</span>addState<span class="operator">(</span>id<span class="operator">,</span> info<span class="operator">,</span> labelExpr<span class="operator">,</span> accSignature<span class="operator">);
  85. }
  86. };</span><span class="comment">
  87. /** Demonstrating the use of HOAIntermediates */</span><span class="type">
  88. int</span><span class="keyword"> main</span><span class="operator">(</span><span class="type">int</span> argc<span class="operator">,</span><span class="keyword"> const</span><span class="type"> char</span><span class="operator">*</span> argv<span class="operator">[]) {</span>
  89. HOAConsumer<span class="operator">::</span>ptr hoaNull<span class="operator">(</span><span class="keyword">new</span> HOAConsumerNull<span class="operator">());</span>
  90. CountStates<span class="operator">::</span>ptr counter<span class="operator">(</span><span class="keyword">new</span> CountStates<span class="operator">(</span>hoaNull<span class="operator">));</span><span class="flow">
  91. try</span><span class="operator"> {</span>
  92. HOAParser<span class="operator">::</span>parse<span class="operator">(</span>std<span class="operator">::</span>cin<span class="operator">,</span> counter<span class="operator">);</span>
  93. std<span class="operator">::</span>cout<span class="operator"> &lt;&lt;</span><span class="string"> "Number of state definitions = "</span><span class="operator"> &lt;&lt;</span> counter<span class="operator">-&gt;</span>count<span class="operator"> &lt;&lt;</span> std<span class="operator">::</span>endl<span class="operator">;
  94. }</span><span class="flow"> catch</span><span class="operator"> (</span>std<span class="operator">::</span>exception<span class="operator">&amp;</span> e<span class="operator">) {</span>
  95. std<span class="operator">::</span>cerr<span class="operator"> &lt;&lt;</span> e<span class="operator">.</span>what<span class="operator">() &lt;&lt;</span> std<span class="operator">::</span>endl<span class="operator">;</span><span class="flow">
  96. return</span><span class="int"> 1</span><span class="operator">;
  97. }</span><span class="flow">
  98. return</span><span class="int"> 0</span><span class="operator">;
  99. }</span>
  100. </pre>
  101. </div>
  102. <p>We derive <span class="classname">CountStates</span> from <span class="classname">HOAIntermediate</span>.
  103. Its constructor takes the next <span class="classname">HOAConsumer</span> in the chain, which can
  104. again be a <span class="classname">HOAIntermediate</span>. Here, we are passing a
  105. <span class="classname">HOAConsumerNull</span> object, which does nothing when called, acting as a "no-operation"
  106. end of the consumer chain. Inside the <span class="classname">CountStates</span>, we override the
  107. <span class="classname">addState</span> method, which is called for each <em>State:</em>
  108. definition in the automaton.
  109. We count the number of definitions and pass along the arguments to the next consumer.
  110. In the end, we just output the count of the definitions.
  111. </p>
  112. <h2>The HOAConsumer API</h2>
  113. <p>
  114. A good starting point for learning about the HOAConsumer API is the
  115. <a
  116. href="http://automata.tools/hoa/cpphoafparser/docs/api-html/classcpphoafparser_1_1_h_o_a_consumer.html">documentation of the HOAConsumer interface</a>
  117. as well as the rest of the <a href="http://automata.tools/hoa/cpphoafparser/docs/api-html/index.html">API documentation</a>.
  118. <p>Another good introduction is the source code of the <span class="classname">HOAConsumerPrint</span> class,
  119. as it translates back from the method calls in the <span class="classname">HOAConsumer</span> interface to the
  120. textual representation of the HOA format.
  121. </p>
  122. <p>Some further tidbits that might be useful:</p>
  123. <ul>
  124. <li>The boolean expressions for the explicit transition labels and the acceptance condition
  125. are represented by
  126. <a href="http://automata.tools/hoa/cpphoafparser/docs/api-html/classcpphoafparser_1_1_boolean_expression.html"><span class="classname">BooleanExpression</span></a>,
  127. with appropriate <span class="classname">Atoms</span> for the leaf nodes in the abstract syntax tree.
  128. The <span class="classname">BooleanExpressions</span> are designed to
  129. be <em>immutable</em>. Therefore, subtrees can be safely shared between expressions.
  130. </li>
  131. <li>
  132. If you encounter an error inside one of the <span class="classname">HOAConsumer</span> method calls, throw an
  133. <a href="http://automata.tools/hoa/cpphoafparser/docs/api-html/classcpphoafparser_1_1_h_o_a_consumer_exception.html">HOAConsumerException</a>.
  134. </li>
  135. <li>Memory managment is generally handled via <span class="classname">shared_ptr</span> pointers.</li>
  136. <li>
  137. To trace the method calls into a <span class="classname">HOAConsumer</span> or <span class="classname">HOAIntermediate</span>,
  138. wrap it into a <a href="http://automata.tools//hoa/cpphoafparser/docs/api-html/classcpphoafparser_1_1_h_o_a_intermediate_trace.html"><span class="classname">HOAIntermediateTrace</span></a>
  139. object. This will print all the function invocations to the standard output, including the parameters.
  140. </li>
  141. </ul>
  142. <hr>
  143. <p>If you have further questions, find bugs or want to tell
  144. us about your use of the cpphoafparser library, please feel free to contact us!</p>
  145. <p>(c) 2015-2016
  146. Joachim Klein &lt;klein&#064;tcs.inf.tu-dresden.de&gt;,
  147. David M&uuml;ller &lt;david.mueller&#064;tcs.inf.tu-dresden.de&gt;
  148. </body>
  149. </html>