title: NLTK: Parsing sentences using a simple grammar and part of speech tags
body: <p>For a sentence like "This is a simple sentence" which has been part of speech tagged to:</p>
  
  <pre><code>[('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('simple', 'JJ'), ('sentence', 'NN')]
  </code></pre>
  
  <p>And using the following grammar:</p>
  
  <pre><code>    my_grammar = nltk.CFG.fromstring("""
  ... S -&gt; DP VP
  ... DP -&gt; Det NP
  ... NP -&gt; Adj N
  ... VP -&gt; V DP
  ... Det -&gt; 'DT'
  ... N -&gt; 'NN'
  ... V -&gt; 'VBZ'
  ... Adj -&gt; 'JJ'
  ... """)
  </code></pre>
  
  <p>How do I output a tree structure. I am attempting to use the part of speech tags in place of the word that has been tagged, but still display the word itself in the tree to get something similar to:</p>
  
  <pre><code>    (S
       (DP This)
       (VP
        (V is)
        (DP (Det a)
         (NP
          (Adj simple) (N sentence))))
  </code></pre>
  
  <p>EDIT: I've tried the answer <a href="https://stackoverflow.com/questions/4858467/combining-a-tokenizer-into-a-grammar-and-parser-with-nltk?rq=1">here</a>, but when I get to using the command:</p>
  
  <pre><code>for tree in parser.parse(pos_tags):
  ...     print(tree)
  </code></pre>
  
  <p>Nothing is returned</p>