Please forgive the potentially noob question, but I'm trying to get started with semantic text analysis, particularly in the legal space.

I found a very good paper which describes a context free grammar for legal argument (pp. 10-12).

I also found that Python's NLTK library allows context free grammars to be defined like this:

>>> groucho_grammar = nltk.parse_cfg("""
... S -> NP VP
... PP -> P NP
... NP -> Det N | Det N PP | 'I'
... VP -> V NP | VP PP
... Det -> 'an' | 'my'
... N -> 'elephant' | 'pajamas'
... V -> 'shot'
... P -> 'in'
... """)

Is it even feasible/practical to use NLTK to define the grammar described in the paper? If so, how should I attack the problem? And if not so, what are the appropriate lines of inquiry I should follow in order to reproduce (part of) the context free grammar for legal argument?

2 Answers 2


Any context-free parser can be used. However "pure" CF grammars aren't practical for real applications. I'd recommend to use LFG or a similar tool that generates more useful underlying representations.

  • 1
    True, but the way people learn that is by watching them work and learning why, so trying it out would be a good exercise. I can't advise on the suitability of any parser, though.
    – jlawler
    Nov 1, 2013 at 16:36
  • @jlawler You are right. In this case NLTK is a feasible option.
    – Atamiri
    Nov 1, 2013 at 18:34
  • Thanks for the insight. How would I go about understanding the formal notation from the article to the type notation used in NLTK?
    – user94154
    Nov 2, 2013 at 4:56
  • 1
    LFG is a grammar formalism, not a tool. Which tool do you have in mind, precisely? Perhaps you could link to that in your answer?
    – prash
    Nov 2, 2013 at 10:24
  • 1
    LFG uses this kind of rule templates so Kaplan & Bresnan (1982) is a good paper to start. In general, what you need is a chart parser capable of interpreting ambiguous grammars. A very easy to use tool are Q-systems. This formalism is quite old and in fact Turing complete but it uses a chart in order to run CF grammars efficiently (the language is a forerunner of Prolog). There's an online implementation. Any CF grammar can be written as a Q-system in a straightforward way. As for documentation, google for "The birth of Prolog", one section is dedicated to the description of Q-systems.
    – Atamiri
    Nov 4, 2013 at 20:41

This grammar in the article is ambiguous, and the article says that ambiguity is part of the design. Hence you need a parser that can handle ambiguity. Many context-free parsers will not do that.

However, NLTK does offer a chart parser, i.e., an algorithm (based on dynamic programming) that can parse any context-free grammar and give you ways of dealing with the multiple possible parses that a sentence may have when the grammar is ambiguous.

However I have no experience with it, and I do not know (for lack of a formal presentation that I did not find in the time I had available) how easily and effectively it deals with multiple parse trees, of which there can be a large number.

Another problem that I see is that the grammar you wish to use is a grammar for discourse structure, rather than for sentences. This is not inherently a problem but has its own specificity.

When parsing an English sentence, you need a first phase that recognizes the various lexical elements of your language, and identifies their possible lexical categories (parts of speech).

Here, you obviously have to identify "parts of discourse" which are the terminal of the discourse context free syntax you want to parse. I guess they are denoted in figure 2 by lower case letters.

Hence, you may have to cascade two parsers, an English parser to recognize your "parts of discourse", which are used by the second parser to analyze your discourse structure.

Most likely, the NLTK parser can handle both. But I do not have hands on experience with it, and the description provided is far too long for me to have time to grasp its structure without a more condensed formal presentation.

Regarding extended rules, such as "T => A+ D", if the parser cannot handle them directly, they can easily be changed into the usual context-free rules. This example can be rewritten as:

T => A T and T=> A D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.