Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1) Change <code>Literal("if")</code> to <code>Keyword("if")</code> (and so on, down to <code>Literal("void")</code>), to prevent matching the leading "if" of a variable named <code>"ifactor"</code>.</p> <p>2) <code>nums</code>, <code>alphas</code>, and <code>alphanums</code> are not expressions, they are strings, that can be used with the Word class to define some typical sets of characters when defining "words" like "a number is a word made up of nums", or "an identifier is a word that starts with an alpha, followed by zero or more alphanums." So instead of: </p> <pre><code>number = nums identifier = alphas + OneOrMore(alphanums) </code></pre> <p>you want</p> <pre><code>number = Word(nums) identifier = Word(alphas, alphanums) </code></pre> <p>3) Instead of <code>Combine</code>, I think you want <code>Group</code>. Use <code>Combine</code> when you want the matched tokens to be contiguous with no intervening whitespace, and will concatenate the tokens and return them as a single string. <code>Combine</code> is often used in cases like this:</p> <pre><code>realnum = Combine(Word(nums) + "." + Word(nums)) </code></pre> <p>Without <code>Combine</code>, parsing <code>"3.14"</code> would return the list of strings <code>['3', '.', '14']</code>, so we add <code>Combine</code> so that the parsed result for realnum is <code>'3.14'</code> (which you could then pass to a parse action to convert to the actual floating value <code>3.14</code>). <code>Combine</code>s enforcement of no intervening whitespace also keeps us from accidentally parsing <code>'The answer is 3. 10 is too much.'</code> and thinking the <code>"3. 10"</code> represents a real number.</p> <p>4) This should not cause your error, but your input string has <em>lots</em> of extra spaces. If you get your grammar working, you should be able to parse <code>"int x;"</code> just as well as <code>"int x ;"</code>.</p> <p>Hope some of these hints get you going. Have you read any online pyparsing articles or tutorials? And please look through the online examples. You'll need to get a good grasp of how <code>Word</code>, <code>Literal</code>, <code>Combine</code>, etc. perform their individual parsing tasks.</p> <p>5) You have mis-implemented the recursive definitions for term and statement. Instead of assigning <code>''</code> to them, write:</p> <pre><code>term = Forward() statement = Forward() </code></pre> <p>Then when you go to actually define them with their recursive definitions, use the <code>&lt;&lt;</code> operator (and be sure to enclose the RHS in <code>()</code>'s).</p> <pre><code>term &lt;&lt; (... term definition ...) statement &lt;&lt; (... statement definition ...) </code></pre> <p>You can find an example of a recursive parser <a href="http://www.ptmcg.com/geo/python/confs/examples/list1.py.txt" rel="noreferrer">here</a>, and a presentation on basic pyparsing usage <a href="http://www.ptmcg.com/geo/python/confs/TxUnconf2008Pyparsing.html" rel="noreferrer">here</a> - see the section titled "Parsing Lists" for more step-by-step on how the recursion is handled.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload