from coursepy.lang.parsing import make_parser, make_cfg

Part 1: Extend your grammar so that you can parse sentences like Every student saw a dog with a telescope, with its two readings (i.e., the one in which the dog has a telescope, and the one in which the student has a telescope).

grammar1 = make_cfg("""
S  -> NP VP

NP -> Det N
NP -> NP PP

VP -> V
VP -> V NP
VP -> VP PP

PP -> P NP

Det -> 'every' | 'a' | 'the' | 'some'
N   -> 'student' | 'professor' | 'dog' | 'telescope'
V   -> 'walks' | 'saw' | 'talks'
P   -> 'with'
""")

parser1 = make_parser(grammar1)
trees1 = parser1("every student saw a dog with a telescope")
[t.pretty_print() for t in trees1]
                       S                                
        _______________|_______                          
       |                       VP                       
       |                _______|________                 
       |               VP               PP              
       |            ___|___         ____|___             
       NP          |       NP      |        NP          
   ____|_____      |    ___|___    |     ___|______      
 Det         N     V  Det      N   P   Det         N    
  |          |     |   |       |   |    |          |     
every     student saw  a      dog with  a      telescope

                       S                                
        _______________|_______                          
       |                       VP                       
       |            ___________|___                      
       |           |               NP                   
       |           |        _______|____                 
       |           |       |            PP              
       |           |       |        ____|___             
       NP          |       NP      |        NP          
   ____|_____      |    ___|___    |     ___|______      
 Det         N     V  Det      N   P   Det         N    
  |          |     |   |       |   |    |          |     
every     student saw  a      dog with  a      telescope
[None, None]

Part 2: Your grammar generates ungrammatical sentences like *Every student saw (English requires an explicit object in this case). Modify your grammar so that it does not generate such sentences. But, be careful, both Every student walks and Every student walks a dog are grammatical, you should do justice to this fact as well.

grammar2 = make_cfg("""
S  -> NP VP

NP -> Det N
NP -> NP PP

VP -> IV
VP -> TV NP
VP -> VP PP

PP -> P NP

Det -> 'every' | 'a' | 'the' | 'some'
N   -> 'student' | 'professor' | 'dog' | 'telescope'
IV   -> 'walks' | 'talks'
TV   -> 'walks' | 'saw'
P   -> 'with'
""")
parser2 = make_parser(grammar2)
parser2("every student saw")
[]
parser2("every student walks a dog")[0].pretty_print()
parser2("every student walks")[0].pretty_print()
                    S              
        ____________|____           
       |                 VP        
       |             ____|___       
       NP           |        NP    
   ____|_____       |     ___|___   
 Det         N      TV  Det      N 
  |          |      |    |       |  
every     student walks  a      dog

             S         
        _____|______    
       NP           VP 
   ____|_____       |   
 Det         N      IV 
  |          |      |   
every     student walks

Part 3: Extend your grammar so that your grammar accepts sentences like Every student talks to a professor and rejects *Every student talks a professor.

grammar3 = make_cfg("""
S  -> NP VP

NP -> Det N
NP -> NP PP
DNP -> Dat NP


VP -> IV
VP -> TV NP
VP -> VP PP
VP -> OV DNP

PP -> P NP

Det -> 'every' | 'a' | 'the' | 'some'
N   -> 'student' | 'professor' | 'dog' | 'telescope'
IV   -> 'walks' | 'talks'
TV   -> 'walks' | 'saw'
OV   -> 'talks'
P   -> 'with'
Dat -> 'to'

""")
parser3 = make_parser(grammar3)
parser3("every student talks to a professor")[0].pretty_print()
                    S                            
        ____________|________                     
       |                     VP                  
       |             ________|___                 
       |            |           DNP              
       |            |     _______|___             
       NP           |    |           NP          
   ____|_____       |    |        ___|______      
 Det         N      OV  Dat     Det         N    
  |          |      |    |       |          |     
every     student talks  to      a      professor
[t.pretty_print() for t  in parser3("every student talks to a professor with a telescope")]
                                 S                                      
        _________________________|__________                             
       |                                    VP                          
       |                      ______________|___________                 
       |                     VP                         |               
       |             ________|___                       |                
       |            |           DNP                     PP              
       |            |     _______|___               ____|___             
       NP           |    |           NP            |        NP          
   ____|_____       |    |        ___|______       |     ___|______      
 Det         N      OV  Dat     Det         N      P   Det         N    
  |          |      |    |       |          |      |    |          |     
every     student talks  to      a      professor with  a      telescope

             S                                                      
        _____|___________                                            
       |                 VP                                         
       |             ____|______________                             
       |            |                  DNP                          
       |            |     ______________|______                      
       |            |    |                     NP                   
       |            |    |        _____________|____                 
       |            |    |       |                  PP              
       |            |    |       |              ____|___             
       NP           |    |       NP            |        NP          
   ____|_____       |    |    ___|______       |     ___|______      
 Det         N      OV  Dat Det         N      P   Det         N    
  |          |      |    |   |          |      |    |          |     
every     student talks  to  a      professor with  a      telescope
[None, None]

Another solution for talk to would be to treat it as a single lexical item. However, there is a subtle distinction between verbal complexes like talk to on one hand and those like look at on the other.

look allows many prepositions with clear shifts in meaning:

  1. look at (direct gaze toward),
  2. look for (search),
  3. look into (investigate),
  4. look after (take care of), etc.

These are (diachronically) prepositional or phrasal-verb constructions with largely idiosyncratic semantics.

talk has a rather narrower set of productive patterns:

  1. talk to/with NP (addressee),
  2. talk about NP (topic),
  3. talk of NP (more formal/literary topic),

In this sense talk + P has a more systematic/transparent semantics. Compare the above types of talk + P with lexicalized usages like talk back, talk down to, etc., which have negative connotations.

Both look at and talk to syntactically differ from phrasal verbs like look up.

  1. I looked the word up.
  2. *I looked the word at.
  3. I looked it up.
  4. *I looked up it.
  5. * I looked it at.
  6. I looked at it.

Download .ipynb Download .py