| 458 | | In the body of a grammar rule, we can call rules that are inherited from ancestor objects (or imported categories) or contained in other objects. This is accomplished by using non-terminals as messages. Using a non-terminal as a message to self allows us to call grammar rules in categories and ancestor objects. To call grammar rules encapsulated in other objects, we use a non-terminal as a message to those objects. Along with the message sending operators (<code>::/1</code> and <code>::/2</code>), we may also use other control constructs such as <code>\+/1</code>, <code>!/0</code>, <code>;/2</code>, <code>->/2</code>, and <code>{}/1</code> in the body of a grammar. In addition, grammar rules may contain metacalls (a variable taking the place of a non-terminal), which are translated to calls of the built-in method <code>phrase/3</code>. |
| | 458 | In the body of a grammar rule, we can call rules that are inherited from ancestor objects, imported from categories, or contained in other objects. This is accomplished by using non-terminals as messages. Using a non-terminal as a message to self allows us to call grammar rules in categories and ancestor objects. To call grammar rules encapsulated in other objects, we use a non-terminal as a message to those objects. Consider the following example, containing grammar rules for parsing natural language sentences: |
| | 459 | </p> |
| | 460 | <pre> |
| | 461 | :- object(sentence, |
| | 462 | imports(determiners, nouns, verbs)). |
| | 463 | |
| | 464 | :- public(parse/2). |
| | 465 | |
| | 466 | parse(List, true) :- |
| | 467 | phrase(sentence, List). |
| | 468 | parse(_, false). |
| | 469 | |
| | 470 | sentence --> noun_phrase, verb_phrase. |
| | 471 | |
| | 472 | noun_phrase --> ::determiner, ::noun. |
| | 473 | noun_phrase --> ::noun. |
| | 474 | |
| | 475 | verb_phrase --> ::verb. |
| | 476 | verb_phrase --> ::verb, noun_phrase. |
| | 477 | |
| | 478 | :- end_object. |
| | 479 | </pre> |
| | 480 | <p> |
| | 481 | The object imported categories would contain the necessary grammar rules for parsing determiners, nouns, and verbs. For example: |
| | 482 | </p> |
| | 483 | <pre> |
| | 484 | :- category(determiners). |
| | 485 | |
| | 486 | :- private(determiner/2). |
| | 487 | |
| | 488 | determiner --> [the]. |
| | 489 | determiner --> [a]. |
| | 490 | |
| | 491 | :- end_category. |
| | 492 | </pre> |
| | 493 | <p> |
| | 494 | Along with the message sending operators (<code>::/1</code> and <code>::/2</code>), we may also use other control constructs such as <code>\+/1</code>, <code>!/0</code>, <code>;/2</code>, <code>->/2</code>, and <code>{}/1</code> in the body of a grammar. In addition, grammar rules may contain metacalls (a variable taking the place of a non-terminal), which are translated to calls of the built-in method <code>phrase/3</code>. |