Answers.Body: <p>Good question. This nested notation (also called: recursive annotation variation, RAV for short) is uncommon but valid[*]. In the example you provide, most online PGN readers have no problem parsing and accounting for all the variations and subvariations. So the way to read it is as follows: the nested structure is composed of independent variations, only if there's a closing parenthesis then it is an actual subvariation (so a specific branch out of a previous variation). An example from your PGN: <code>8... Qc7</code> is a variation, <code>(9.Qe1...)</code> is a subvariation and <code>(10.Bf3...)</code> is a subsubvariation. So the nesting is: <code>(8... Qc7 (subvariation (subsubvar...) ))</code>.</p>
  
  <p>Various libraries/modules may not have a built-in parsing system to tackle such PGN structure[**], so you may have to extend this on your own. For further reading, Tim Harding also explains the nested notation and its lack of implementation rather more succinctly <a href="http://portablegamenotation.com/LittlePGN.html" rel="noreferrer">here</a>, I briefly quote:</p>
  
  <blockquote>
    <p>An RAV (Recursive Annotation Variation) is a sequence of movetext
    containing one or more moves enclosed in parentheses. An RAV is used
    to represent an alternative variation. The alternate move sequence
    given by an RAV is one that may be legally played by first unplaying
    the move that appears immediately prior to the RAV. Because the RAV is
    a recursive construct, it may be nested. ... </p>
    
    <p>Some software may find it
    hard to handle nested variations too. Palview3 was the first
    freely-available PGN software, that I am aware of, that could create
    with variations in HTML pages, and it is restricted to two levels of
    notes (three levels counting the actual moves of the game). For most
    purposes, this is sufficient. Most Java Viewers can only show the
    actually played moves, except as text notes, but playing through a
    Palview3 web page is close to the experience of playing it through in
    a program like ChessBase: a major advance for chess webmasters.</p>
  </blockquote>
  
  <p>That said, nowadays, there are various online pgn-readers that already do this, some examples (so if you paste your PGN onto either of these, it can be fully read and displayed). </p>
  
  <ul>
  <li><a href="https://www.chess.com/analysis-board-editor" rel="noreferrer">https://www.chess.com/analysis-board-editor</a></li>
  <li><a href="https://chesstempo.com/pgn-viewer.html" rel="noreferrer">https://chesstempo.com/pgn-viewer.html</a></li>
  <li><a href="http://www.apronus.com/chess/wbeditor.php" rel="noreferrer">http://www.apronus.com/chess/wbeditor.php</a></li>
  </ul>
  
  <p>You may find additional information on the standards of PGN <a href="http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm" rel="noreferrer">here</a>. I will get back to you if I find other potentially useful elements.</p>
  
  <hr>
  
  <p>[*]: just to make sure, I also quickly parsed the text to make sure all parentheses are properly closed. </p>
  
  <p>[**]: for example, the <code>chess.pgn.read_game</code> method from <code>python-chess</code> fails to fully read this PGN, it only sees the main variation upto move <code>8... e5.</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-01T14:14:12.800Z
Answers.LastActivityDate: 2017-08-01T14:14:12.800Z
Answers.UserDisplayName: Ellie
Body: <p>I've received the following PGN text that I suspect invalid:</p>
  
  <pre><code>8.f4 
      {0.22/17 0 last book move}
  8...e5 
      ( 8...b5 
         ( 8...Qb6 
             ( 8...Qc7 9.a3 
  </code></pre>
  
  <p>What exactly the position the moves come from is unimportant. Please note the PGN text has <strong>four</strong> 8th Black moves (<code>8...e5</code>, <code>8...b5</code>, <code>8...Qb6</code>, <code>8..Qc7</code>), and two of them (<code>8...Qb6</code> and <code>8..Qc7</code>) are <strong>under</strong> <code>8..b5</code>. </p>
  
  <p>I suspect this is a mistake because the alternative Black moves should be represented like the diagram:</p>
  
  <p><a href="https://i.sstatic.net/nQpsv.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nQpsv.png" alt="enter image description here"></a></p>
  
  <p>I generated the diagram with the Scid vs PC software. Please note the alternative Black moves are written in parallel. For example, <code>8...Qb6</code> and <code>8..Qc7</code> <strong>are not</strong> included inside the <code>8...b5</code> bracket.</p>
  
  <p>Some softwares (e.g. Scid) are able to read it, but some fail (e.g. ChessX). My own PGN implementation can't handle the nested structure.</p>
  
  <p><strong>Q</strong>: Is the PGN text I posted valid? What does the PGN specification say?</p>
  
  <p><strong>EDITED:</strong></p>
  
  <p>The game is <a href="http://smallchess.com/Others/MaybeInvalid.pgn" rel="nofollow noreferrer">here</a>. Open the game in a text editor, and you will see the following:</p>
  
  <pre><code>e5 
      { 0.22/17 237 } 
      ( 8...b5 
          ( 8...Qb6 
              ( 8...Qc7 9.a3 
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-01T08:17:35.433Z
Comments.Text: I'll edit and post the whole game.
Comments.UserDisplayName: SmallChess
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-01T08:23:21.170Z
Comments.Text: @Bad_Bishop I edited and posted a link.
Comments.UserDisplayName: SmallChess
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-01T09:18:33.677Z
Comments.Text: @Bad_Bishop What software you have?
Comments.UserDisplayName: SmallChess
Tags: <pgn>
Title: Valid PGN variations?
ThreadText: # Valid PGN variations?
  
  (Asked by: username_0 on 2017-08-01)
  
  I've received the following PGN text that I suspect invalid:
  8.f4 
      {0.22/17 0 last book move}
  8...e5 
      ( 8...b5 
         ( 8...Qb6 
             ( 8...Qc7 9.a3 
  
  What exactly the position the moves come from is unimportant. Please note the PGN text has four 8th Black moves (8...e5, 8...b5, 8...Qb6, 8..Qc7), and two of them (8...Qb6 and 8..Qc7) are under 8..b5. 
  I suspect this is a mistake because the alternative Black moves should be represented like the diagram:
  
  I generated the diagram with the Scid vs PC software. Please note the alternative Black moves are written in parallel. For example, 8...Qb6 and 8..Qc7 are not included inside the 8...b5 bracket.
  Some softwares (e.g. Scid) are able to read it, but some fail (e.g. ChessX). My own PGN implementation can't handle the nested structure.
  Q: Is the PGN text I posted valid? What does the PGN specification say?
  EDITED:
  The game is here. Open the game in a text editor, and you will see the following:
  e5 
      { 0.22/17 237 } 
      ( 8...b5 
          ( 8...Qb6 
              ( 8...Qc7 9.a3
  
  **Tags:** pgn
  
  > **Comments:**
  > **username_0**: I'll edit and post the whole game.  
  > **username_0**: @Bad_Bishop I edited and posted a link.  
  > **username_0**: @Bad_Bishop What software you have?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-08-01. Score: 7 | Accepted answer
  
  Good question. This nested notation (also called: recursive annotation variation, RAV for short) is uncommon but valid[\*]. In the example you provide, most online PGN readers have no problem parsing and accounting for all the variations and subvariations. So the way to read it is as follows: the nested structure is composed of independent variations, only if there's a closing parenthesis then it is an actual subvariation (so a specific branch out of a previous variation). An example from your PGN: `8... Qc7` is a variation, `(9.Qe1...)` is a subvariation and `(10.Bf3...)` is a subsubvariation. So the nesting is: `(8... Qc7 (subvariation (subsubvar...) ))`.
  
  Various libraries/modules may not have a built-in parsing system to tackle such PGN structure[\*\*], so you may have to extend this on your own. For further reading, Tim Harding also explains the nested notation and its lack of implementation rather more succinctly [here](http://portablegamenotation.com/LittlePGN.html), I briefly quote:
  
  > An RAV (Recursive Annotation Variation) is a sequence of movetext
  > containing one or more moves enclosed in parentheses. An RAV is used
  > to represent an alternative variation. The alternate move sequence
  > given by an RAV is one that may be legally played by first unplaying
  > the move that appears immediately prior to the RAV. Because the RAV is
  > a recursive construct, it may be nested. ...
  >
  > Some software may find it
  > hard to handle nested variations too. Palview3 was the first
  > freely-available PGN software, that I am aware of, that could create
  > with variations in HTML pages, and it is restricted to two levels of
  > notes (three levels counting the actual moves of the game). For most
  > purposes, this is sufficient. Most Java Viewers can only show the
  > actually played moves, except as text notes, but playing through a
  > Palview3 web page is close to the experience of playing it through in
  > a program like ChessBase: a major advance for chess webmasters.
  
  That said, nowadays, there are various online pgn-readers that already do this, some examples (so if you paste your PGN onto either of these, it can be fully read and displayed).
  
  * <https://www.chess.com/analysis-board-editor>
  * <https://chesstempo.com/pgn-viewer.html>
  * <http://www.apronus.com/chess/wbeditor.php>
  
  You may find additional information on the standards of PGN [here](http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm). I will get back to you if I find other potentially useful elements.
  
  ---
  
  [\*]: just to make sure, I also quickly parsed the text to make sure all parentheses are properly closed.
  
  [\*\*]: for example, the `chess.pgn.read_game` method from `python-chess` fails to fully read this PGN, it only sees the main variation upto move `8... e5.`
  
  
  ---
  
  