next up previous contents
Next: What You Should Up: Parsing in Prolog Previous: How to Extract

Adding Arbitrary Prolog Goals

Grammar rules are simply expanded to Prolog goals. We can also insert arbitrary Prolog subgoals on the right hand side of a grammar rule but we must tell Prolog that we do not want them expanded. This is done with the help of braces --- i.e. { }. For example, here is a grammar rule which parses a single character input as an ASCII code and succeeds if the character represents a digit. It also returns the digit found.

 
digit(D) -->

[X],

SPM_quot" X >= 48,

X =< 57,

D is X-48 SPM_quot".

The grammar rule looks for a character at the head of a list of input characters and succeeds if the Prolog subgoals

 
		       SPM_quot" 		 X >= 48,

X =< 57,

D is X-48 SPM_quot".

succeed. Note that we assume we are working with ASCII codes for the characters and that the ASCII code for ``0'' is 48 and for ``9'' is 57. Also note the strange way of signifying ``equal to or less than'' as ``=<''.



Paul Brna
Mon May 24 20:14:48 BST 1999