HEX
Server: Apache
System: Windows NT MAGNETO-ARM 10.0 build 22000 (Windows 10) AMD64
User: Michel (0)
PHP: 7.4.7
Disabled: NONE
Upload Files
File: C:/Ruby27-x64/share/doc/ruby/html/lib/racc/rdoc/grammar_en_rdoc.html
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">

<title>grammar.en - RDoc Documentation</title>

<script type="text/javascript">
  var rdoc_rel_prefix = "../../../";
  var index_rel_prefix = "../../../";
</script>

<script src="../../../js/navigation.js" defer></script>
<script src="../../../js/search.js" defer></script>
<script src="../../../js/search_index.js" defer></script>
<script src="../../../js/searcher.js" defer></script>
<script src="../../../js/darkfish.js" defer></script>

<link href="../../../css/fonts.css" rel="stylesheet">
<link href="../../../css/rdoc.css" rel="stylesheet">




<body id="top" role="document" class="file">
<nav role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2>
    <a href="../../../index.html" rel="home">Home</a>
  </h2>

  <div id="table-of-contents-navigation">
    <a href="../../../table_of_contents.html#pages">Pages</a>
    <a href="../../../table_of_contents.html#classes">Classes</a>
    <a href="../../../table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#label-Racc+Grammar+File+Reference">Racc Grammar File Reference</a>
    <li><a href="#label-Global+Structure">Global Structure</a>
    <li><a href="#label-Class+Block+and+User+Code+Block">Class Block and User Code Block</a>
    <li><a href="#label-Comments">Comments</a>
    <li><a href="#label-Class+Block">Class Block</a>
    <li><a href="#label-Grammar+Block">Grammar Block</a>
    <li><a href="#label-Operator+Precedence">Operator Precedence</a>
    <li><a href="#label-expect">expect</a>
    <li><a href="#label-Declaring+Tokens">Declaring Tokens</a>
    <li><a href="#label-Options">Options</a>
    <li><a href="#label-Converting+Token+Symbol">Converting Token Symbol</a>
    <li><a href="#label-Start+Rule">Start Rule</a>
    <li><a href="#label-User+Code+Block">User Code Block</a>
  </ul>
</div>


  <div id="project-metadata">
    <div id="fileindex-section" class="nav-section">
  <h3>Pages</h3>

  <ul class="link-list">
  
    <li><a href="../../../CONTRIBUTING_md.html">CONTRIBUTING</a>
  
    <li><a href="../../../COPYING.html">COPYING</a>
  
    <li><a href="../../../COPYING_ja.html">COPYING.ja</a>
  
    <li><a href="../../../LEGAL.html">LEGAL</a>
  
    <li><a href="../../../NEWS.html">NEWS</a>
  
    <li><a href="../../../NEWS-1_8_7.html">NEWS-1.8.7</a>
  
    <li><a href="../../../NEWS-1_9_1.html">NEWS-1.9.1</a>
  
    <li><a href="../../../NEWS-1_9_2.html">NEWS-1.9.2</a>
  
    <li><a href="../../../NEWS-1_9_3.html">NEWS-1.9.3</a>
  
    <li><a href="../../../NEWS-2_0_0.html">NEWS-2.0.0</a>
  
    <li><a href="../../../NEWS-2_1_0.html">NEWS-2.1.0</a>
  
    <li><a href="../../../NEWS-2_2_0.html">NEWS-2.2.0</a>
  
    <li><a href="../../../NEWS-2_3_0.html">NEWS-2.3.0</a>
  
    <li><a href="../../../NEWS-2_4_0.html">NEWS-2.4.0</a>
  
    <li><a href="../../../NEWS-2_5_0.html">NEWS-2.5.0</a>
  
    <li><a href="../../../NEWS-2_6_0.html">NEWS-2.6.0</a>
  
    <li><a href="../../../README_ja_md.html">README.ja</a>
  
    <li><a href="../../../README_md.html">README</a>
  
    <li><a href="../../../bug_triaging_rdoc.html">bug_triaging</a>
  
    <li><a href="../../../contributing_rdoc.html">contributing</a>
  
    <li><a href="../../../contributors_rdoc.html">contributors</a>
  
    <li><a href="../../../dtrace_probes_rdoc.html">dtrace_probes</a>
  
    <li><a href="../../../extension_ja_rdoc.html">extension.ja</a>
  
    <li><a href="../../../extension_rdoc.html">extension</a>
  
    <li><a href="../../../globals_rdoc.html">globals</a>
  
    <li><a href="../../../keywords_rdoc.html">keywords</a>
  
    <li><a href="../../../lib/racc/pre-setup.html">pre-setup</a>
  
    <li><a href="../../../lib/racc/rdoc/grammar_en_rdoc.html">grammar.en</a>
  
    <li><a href="../../../maintainers_rdoc.html">maintainers</a>
  
    <li><a href="../../../marshal_rdoc.html">marshal</a>
  
    <li><a href="../../../regexp_rdoc.html">regexp</a>
  
    <li><a href="../../../security_rdoc.html">security</a>
  
    <li><a href="../../../signals_rdoc.html">signals</a>
  
    <li><a href="../../../standard_library_rdoc.html">standard_library</a>
  
    <li><a href="../../../syntax_rdoc.html">syntax</a>
  
    <li><a href="../../../syntax/assignment_rdoc.html">assignment</a>
  
    <li><a href="../../../syntax/calling_methods_rdoc.html">calling_methods</a>
  
    <li><a href="../../../syntax/comments_rdoc.html">comments</a>
  
    <li><a href="../../../syntax/control_expressions_rdoc.html">control_expressions</a>
  
    <li><a href="../../../syntax/exceptions_rdoc.html">exceptions</a>
  
    <li><a href="../../../syntax/literals_rdoc.html">literals</a>
  
    <li><a href="../../../syntax/methods_rdoc.html">methods</a>
  
    <li><a href="../../../syntax/miscellaneous_rdoc.html">miscellaneous</a>
  
    <li><a href="../../../syntax/modules_and_classes_rdoc.html">modules_and_classes</a>
  
    <li><a href="../../../syntax/precedence_rdoc.html">precedence</a>
  
    <li><a href="../../../syntax/refinements_rdoc.html">refinements</a>
  
    <li><a href="../../../win32/README_win32.html">README.win32</a>
  
  </ul>
</div>

  </div>
</nav>

<main role="main" aria-label="Page lib/racc/rdoc/grammar.en.rdoc">

<h1 id="label-Racc+Grammar+File+Reference"><a href="../../../Racc.html"><code>Racc</code></a> Grammar <a href="../../../File.html"><code>File</code></a> Reference<span><a href="#label-Racc+Grammar+File+Reference">&para;</a> <a href="#top">&uarr;</a></span></h1>

<h2 id="label-Global+Structure">Global Structure<span><a href="#label-Global+Structure">&para;</a> <a href="#top">&uarr;</a></span></h2>

<h2 id="label-Class+Block+and+User+Code+Block"><a href="../../../Class.html"><code>Class</code></a> Block and User Code Block<span><a href="#label-Class+Block+and+User+Code+Block">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>There are two blocks on the toplevel. One is the &#39;class&#39; block, the other is the &#39;user code&#39; block. The &#39;user code&#39; block MUST be placed after the &#39;class&#39; block.</p>

<h2 id="label-Comments">Comments<span><a href="#label-Comments">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>You can insert comments about all places. Two styles of comments can be used, Ruby style &#39;#.….&#39; and C style &#39;/*.…..*/&#39;.</p>

<h2 id="label-Class+Block"><a href="../../../Class.html"><code>Class</code></a> Block<span><a href="#label-Class+Block">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The class block is formed like this:</p>

<pre>class CLASS_NAME
  [precedence table]
  [token declarations]
  [expected number of S/R conflicts]
  [options]
  [semantic value conversion]
  [start rule]
rule
  GRAMMARS</pre>

<p>CLASS_NAME is a name of the parser class. This is the name of the generating parser class.</p>

<p>If CLASS_NAME includes &#39;::&#39;, <a href="../../../Racc.html"><code>Racc</code></a> outputs the module clause. For example, writing “class M::C” causes the code below to be created:</p>

<pre>module M
  class C
    :
    :
  end
end</pre>

<h2 id="label-Grammar+Block">Grammar Block<span><a href="#label-Grammar+Block">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The grammar block describes grammar which is able to be understood by the parser. Syntax is:</p>

<pre>(token): (token) (token) (token).... (action)

(token): (token) (token) (token).... (action)
       | (token) (token) (token).... (action)
       | (token) (token) (token).... (action)</pre>

<p>(action) is an action which is executed when its (token)s are found. (action) is a ruby code block, which is surrounded by braces:</p>

<pre>{ print val[0]
  puts val[1] }</pre>

<p>Note that you cannot use &#39;%&#39; string, here document, &#39;%r&#39; regexp in action.</p>

<p>Actions can be omitted. When it is omitted, &#39;&#39; (empty string) is used.</p>

<p>A return value of action is a value of the left side value ($$). It is the value of the result, or the returned value by `return` statement.</p>

<p>Here is an example of the whole grammar block.</p>

<pre>rule
  goal: definition rules source { result = val }

  definition: /* none */   { result = [] }
    | definition startdesig  { result[0] = val[1] }
    | definition
             precrule   # this line continues from upper line
      {
        result[1] = val[1]
      }

  startdesig: START TOKEN</pre>

<p>You can use the following special local variables in action:</p>
<ul><li>
<p>result ($$)</p>
</li></ul>

<p>The value of the left-hand side (lhs). A default value is <a href="0">val</a>.</p>
<ul><li>
<p>val ($1,$2,$3…)</p>
</li></ul>

<p>An array of value of the right-hand side (rhs).</p>
<ul><li>
<p>_values (…$-2,$-1,$0)</p>
</li></ul>

<p>A stack of values. DO NOT MODIFY this stack unless you know what you are doing.</p>

<h2 id="label-Operator+Precedence">Operator Precedence<span><a href="#label-Operator+Precedence">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>This function is equal to &#39;%prec&#39; in yacc. To designate this block:</p>

<pre class="ruby"><span class="ruby-identifier">prechigh</span>
  <span class="ruby-identifier">nonassoc</span> <span class="ruby-string">&#39;++&#39;</span>
  <span class="ruby-identifier">left</span>     <span class="ruby-string">&#39;*&#39;</span> <span class="ruby-string">&#39;/&#39;</span>
  <span class="ruby-identifier">left</span>     <span class="ruby-string">&#39;+&#39;</span> <span class="ruby-string">&#39;-&#39;</span>
  <span class="ruby-identifier">right</span>    <span class="ruby-string">&#39;=&#39;</span>
<span class="ruby-identifier">preclow</span>
</pre>

<p>`right` is yacc&#39;s %right, `left` is yacc&#39;s %left.</p>

<p>`=` + (symbol) means yacc&#39;s %prec:</p>

<pre>prechigh
  nonassoc UMINUS
  left &#39;*&#39; &#39;/&#39;
  left &#39;+&#39; &#39;-&#39;
preclow

rule
  exp: exp &#39;*&#39; exp
     | exp &#39;-&#39; exp
     | &#39;-&#39; exp       =UMINUS   # equals to &quot;%prec UMINUS&quot;
         :
         :</pre>

<h2 id="label-expect">expect<span><a href="#label-expect">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p><a href="../../../Racc.html"><code>Racc</code></a> has bison&#39;s “expect” directive.</p>

<pre># Example

class MyParser
rule
  expect 3
    :
    :</pre>

<p>This directive declares “expected” number of shift/reduce conflicts. If “expected” number is equal to real number of conflicts, <a href="../../../Racc.html"><code>Racc</code></a> does not print conflict warning message.</p>

<h2 id="label-Declaring+Tokens">Declaring Tokens<span><a href="#label-Declaring+Tokens">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>By declaring tokens, you can avoid many meaningless bugs. If declared token does not exist or existing token does not decleared, <a href="../../../Racc.html"><code>Racc</code></a> output warnings. Declaration syntax is:</p>

<pre class="ruby"><span class="ruby-identifier">token</span> <span class="ruby-constant">TOKEN_NAME</span> <span class="ruby-constant">AND_IS_THIS</span>
      <span class="ruby-constant">ALSO_THIS_IS</span> <span class="ruby-constant">AGAIN_AND_AGAIN</span> <span class="ruby-constant">THIS_IS_LAST</span>
</pre>

<h2 id="label-Options">Options<span><a href="#label-Options">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>You can write options for <a href="../../../Racc.html"><code>Racc</code></a> command in your <a href="../../../Racc.html"><code>Racc</code></a> file.</p>

<pre class="ruby"><span class="ruby-identifier">options</span> <span class="ruby-constant">OPTION</span> <span class="ruby-constant">OPTION</span> <span class="ruby-operator">...</span>
</pre>

<p>Options are:</p>
<ul><li>
<p>omit_action_call</p>
</li></ul>

<p>omits empty action call or not.</p>
<ul><li>
<p>result_var</p>
</li></ul>

<p>uses local variable “result” or not.</p>

<p>You can use &#39;no_&#39; prefix to invert their meanings.</p>

<h2 id="label-Converting+Token+Symbol">Converting Token <a href="../../../Symbol.html"><code>Symbol</code></a><span><a href="#label-Converting+Token+Symbol">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Token symbols are, as default,</p>

<pre>* naked token string in Racc file (TOK, XFILE, this_is_token, ...)
  --&gt; symbol (:TOK, :XFILE, :this_is_token, ...)
* quoted string (&#39;:&#39;, &#39;.&#39;, &#39;(&#39;, ...)
  --&gt; same string (&#39;:&#39;, &#39;.&#39;, &#39;(&#39;, ...)</pre>

<p>You can change this default by “convert” block. Here is an example:</p>

<pre>convert
  PLUS &#39;PlusClass&#39;      # We use PlusClass for symbol of `PLUS&#39;
  MIN  &#39;MinusClass&#39;     # We use MinusClass for symbol of `MIN&#39;
end</pre>

<p>We can use almost all ruby value can be used by token symbol, except &#39;false&#39; and &#39;nil&#39;.  These cause unexpected parse error.</p>

<p>If you want to use <a href="../../../String.html"><code>String</code></a> as token symbol, special care is required. For example:</p>

<pre>convert
  class &#39;&quot;cls&quot;&#39;            # in code, &quot;cls&quot;
  PLUS &#39;&quot;plus\n&quot;&#39;          # in code, &quot;plus\n&quot;
  MIN  &quot;\&quot;minus#{val}\&quot;&quot;   # in code, \&quot;minus#{val}\&quot;
end</pre>

<h2 id="label-Start+Rule">Start Rule<span><a href="#label-Start+Rule">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>&#39;%start&#39; in yacc. This changes start rule.</p>

<pre class="ruby"><span class="ruby-identifier">start</span> <span class="ruby-identifier">real_target</span>
</pre>

<h2 id="label-User+Code+Block">User Code Block<span><a href="#label-User+Code+Block">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>“User Code Block” is a Ruby source code which is copied to output. There are three user code blocks, “header” “inner” and “footer”.</p>

<p>Format of user code is like this:</p>

<pre>---- header
  ruby statement
  ruby statement
  ruby statement

---- inner
  ruby statement
     :
     :</pre>

<p>If four &#39;-&#39; exist on the line head, <a href="../../../Racc.html"><code>Racc</code></a> treats it as the beginning of the user code block.  The name of the user code block must be one word.</p>

</main>



<footer id="validator-badges" role="contentinfo">
  <p><a href="https://validator.w3.org/check/referer">Validate</a>
  <p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
  <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>