2

Why do I get "invalid syntax" in the line with the % start S?

nltk.data.show_cfg('grammars/book_grammars/feat0.fcfg')
% start S 
S -> NP[NUM=?n] VP[NUM=?n]
# NP expansion productions
NP[NUM=?n] -> PropN[NUM=?n]
NP[NUM=?n] -> Det[NUM=?n] N[NUM=?n]
NP[NUM=pl] -> N[NUM=pl]
alvas
  • 115,346
  • 109
  • 446
  • 738
nefeli
  • 31
  • 1
  • 1
    Are you attempting to run these lines with the Python interpreter? That's not correct.. in that example in the book, they run the first line, and the remaining lines are the output of the `show_cfg` function. – cody Dec 21 '18 at 11:37
  • yes, I was doing it in a wrong way. Thank you! – nefeli Dec 21 '18 at 11:42

1 Answers1

1

As @cody pointed out, this is the code part:

>>> import nltk
>>> nltk.data.show_cfg('grammars/book_grammars/feat0.fcfg')

That outputs these:

% start S
# ###################
# Grammar Productions
# ###################
# S expansion productions
S -> NP[NUM=?n] VP[NUM=?n]
# NP expansion productions
NP[NUM=?n] -> N[NUM=?n] 
NP[NUM=?n] -> PropN[NUM=?n] 
NP[NUM=?n] -> Det[NUM=?n] N[NUM=?n]
NP[NUM=pl] -> N[NUM=pl] 
# VP expansion productions
VP[TENSE=?t, NUM=?n] -> IV[TENSE=?t, NUM=?n]
VP[TENSE=?t, NUM=?n] -> TV[TENSE=?t, NUM=?n] NP
# ###################
# Lexical Productions
# ###################
Det[NUM=sg] -> 'this' | 'every'
Det[NUM=pl] -> 'these' | 'all'
Det -> 'the' | 'some' | 'several'
PropN[NUM=sg]-> 'Kim' | 'Jody'
N[NUM=sg] -> 'dog' | 'girl' | 'car' | 'child'
N[NUM=pl] -> 'dogs' | 'girls' | 'cars' | 'children' 
IV[TENSE=pres,  NUM=sg] -> 'disappears' | 'walks'
TV[TENSE=pres, NUM=sg] -> 'sees' | 'likes'
IV[TENSE=pres,  NUM=pl] -> 'disappear' | 'walk'
TV[TENSE=pres, NUM=pl] -> 'see' | 'like'
IV[TENSE=past] -> 'disappeared' | 'walked'
TV[TENSE=past] -> 'saw' | 'liked'

When attempting to type the following:

>>> % start S
  File "<stdin>", line 1
    % start S
    ^
SyntaxError: invalid syntax

You'll see a SyntaxError because it's not Python, it's the NLTK specific sublanguage to write the .cfg/.fcfg grammar.

alvas
  • 115,346
  • 109
  • 446
  • 738