DCG parsing - Natural Language in Prolog

From: Neil (neilyork82_at_yahoo.com)
Date: 05/17/04


Date: Mon, 17 May 2004 16:56:47 +1000

Hi,

I am learning grammar and parsing. Here I can parse a very simple sentence
"John eats the cat". BUT HOW CAN I EXTEND IT TO PARSE A MORE COMPLICATED
SENTENCE LIKE " The wombat on Jane give him". The parts of speech to be
covered are: art noun prep pro proper v
("pro" signifies pronoun, "proper" signifies proper noun, "prep" signifies
preposition).
The grammar rules to be covered are:

    a.. s -> np vp
    b.. vp -> v np
    c.. np -> proper
    d.. np -> art cnp2
    e.. cnp2 -> cnp
    f.. cnp -> noun
(s=sentence,np=noun phrase,vp=verb phrase,proper=proper
noun,art=article,cnp=common noun phrase,v=verb)
Current simple parsing in prolog
% grammar rules: (the first four only yet)

s(P1,P3,s(NP,VP)) :- np(P1,P2,NP), vp(P2,P3,VP).
vp(P1,P3,vp(v(Verb),NP)) :- v(P1,P2,Verb), np(P2,P3,NP).
np(P1,P2,np(name(Name))) :- proper(P1,P2,Name).
np(P1,P3,np(art(Art),noun(Noun))) :- art(P1,P2,Art), noun(P2,P3,Noun).

% lexicon entries:

isname(john).
isverb(ate).
isart(the).
isnoun(cat).

% rules to do lexical analysis - that is to classify words according
% to their part of speech:

art(From, To, Word) :- word(Word, From, To), isart(Word).
noun(From, To, Word) :- word(Word, From, To), isnoun(Word).
v(From, To, Word) :- word(Word, From, To), isverb(Word).
proper(From, To, Word) :- word(Word, From, To), isname(Word).

% An example sentence to parse:

word(john, 1, 2).
word(ate, 2, 3).
word(the, 3, 4).
word(cat, 4, 5).

____________
Lexical entries for more complicated sentence
isart(the, the1).
isname(jane, "Jane").
isnoun(wombat, wombat1).
isprep(on, on_loc1).
ispro(him, he1).
isverb(give, give1).
Regards,Neil



Relevant Pages

  • Re: Parsing Expression Grammar
    ... problem with your grammar, it will simply not parse some inputs. ... The exception being, if the grammar is LL, then a PEG will ... I think it is possible to get a parser which nearly succeeds on all ...
    (comp.compilers)
  • Re: my ears are burning... ;)
    ... There is some really amazing jiggery-pokery in the grammar analyzer. ... At that point, we must backtrack. ... ANTLR generates, especially with selective memoization. ... ANTLRWill generate a parsing ...
    (comp.lang.ruby)
  • misc: my effort, and a new ambiguity...
    ... today I actually got around to writing most of the new parser. ... the final x in 'tx;' would need to change the relative precedence ... ordering after parsing 't' in order to parse as expected. ...
    (comp.lang.misc)
  • Re: my ears are burning... ;)
    ... There is some really amazing jiggery-pokery in the grammar analyzer. ... a finite maximum determined at parser generation time. ... ANTLRWill generate a parsing ... Also, if you are always backtracking, the error message is ...
    (comp.lang.ruby)
  • Re: bison and/or antlr ?
    ... Learing antlr (recursive descent parsing) is considerably easier. ... programming language grammar. ... but regular expressions plus back-references are ...
    (comp.compilers)