For Programmers: Free Programming Magazines  


Home > Archive > Prolog > July 2006 > Problem with Parsing









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with Parsing
Advait

2006-07-16, 3:59 am

Hi,

I am creating a parser for alphabetical sentences.
1. Assuming the sentence contains only spaces and
alphabets (no symbols like . ' , : ; ! $ etc.),
also space =\= alphabet

2. eg.
"I am not mad"
" I am not mad"
" I am not mad "
""
" "
"I"

3. Parser integrates the alphabets into atom and
generates a list of words.

4. I assume that the grammer is (alphabet*space*)*

5. Here a code that I wrote,

%%%%%%%% start %%%%%%%%%%%
s([]) --> [].
s([H|T]) --> word(W),spaces,s(T), { atom_chars(H,W) }.
s(L) --> spaces,s(L).

spaces --> [' '],spaces.
spaces --> [].

word([X|T]) --> [X],{X\=' '},!,word(T).
word([]) --> [].

%%%%%%%% end %%%%%%%%%%%%%

6. I meet *null atoms* in the list while backtracking.
?- atom_chars('i am mad',Chars), s(L,Chars,[]).

L = [i,am,mad];

L = [i,am,mad,''];

L = [i,am,mad,'',''];
...
...

I want to use minimum cuts to see the program more declarative.
Do I need to change the structure of grammer or parser clauses ?
Please help me.

yours
Advait

Markus Triska

2006-07-16, 7:00 pm

"Advait" <advait_raut@indiatimes.com> writes:


> Do I need to change the structure of grammer or parser clauses ?



s([]) --> [].
s(Toks) -->
space,
s(Toks).
s([Tok|Toks]) -->
word(W),
!, % single solution: longest input match
{ name(Tok, W) },
s(Toks).


space --> " ".

word([L|Ls]) --> letter(L), wordr(Ls).

wordr([L|Ls]) --> letter(L), wordr(Ls).
wordr([]) --> [].

letter(L) --> [L], {0'A =< L, L =< 0'Z ; 0'a =< L, L =< 0'z }.




?- phrase(s(Words), "these are the words").

Words = [these, are, the, words]

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com