TRALE: Quick Start
TRALE is a programming language designed for defining and processing type-feature structure rules, commonly used in computational linguistics for constructing grammars. To build a functional grammar in TRALE, you need to define four core components: types, features, lexicons, and rules. Each of these components plays a crucial role in structuring how language elements (like words and phrases) combine and interact to form valid sentences. Let’s break down each part:
- Types define categories or classes of syntactic structures (like nouns, verbs, phrases or clauses), linguistic feature primitives (such as number (singular, plural), gender (masculine, feminine, neuter), or person (first, second, third) and tense (present, past)).
- Features specify the properties or attributes of those types (such as nouns have number, verbs have tense).
- Lexicons represent the actual words and their corresponding types and features.
- Rules define how types and features combine to form grammatical structures, allowing you to model sentence formation.
By combining these four components, you can build complex grammars that can accept well-formed sentences and reject ungrammatical ones.
Table of Contents
Open Table of Contents
1. Types
In TRALE, types are used to create hierarchical structures that represent different linguistic categories. Types can be broken down into subtypes, and each subtype inherits the characteristics of its supertype.
Syntax:
supertype sub [subtype1, subtype2, subtype3].
Example:
bot sub [cat, person].
Here, bot
is the supertype, and cat
(category) and person
are subtypes. All other types in the grammar will be descendants of bot
.
In TRALE, bot
is a special supertype that stands for “bottom.” It represents the root or base of the type hierarchy tree. All types in your grammar ultimately trace their lineage back to bot
. This makes it the foundation of any type structure in TRALE.
2. Features
Features are attributes or properties that further define a type. For instance, you might want to specify the person of a pronoun or the tense of a verb. Features help you capture these distinctions within your grammar.
Syntax:
type intro [
feature_name_1:type_of_value_of_feature_1,
feature_name_2:type_of_value_of_feature_2].
Example:
pn intro [pn_person:person].
In this example, the type pn
(pronoun) introduces a feature called pn_person
, which has the type person
. This helps categorize pronouns according to their person (e.g., first, second, third).
3. Lexicons
Lexicons are where you define the actual words in your language and link them to their respective types and features. In TRALE, you use tokens to specify which word belongs to which type and what values its features should have.
Syntax:
token ---> (type, feature1:value1, feature2:value2).
Example:
i ---> (pn, pn_person:first).
This means that the word i
is a pronoun (pn
), and it refers to the first person (feature pn_person:first
).
4. Rules
Rules are essential for defining how the elements of your grammar combine. They specify how different types (with their corresponding features) should appear together to form valid grammatical constructions.
Syntax:
rule_name rule
(product_type, feature:value) ===>
cat> (type1, feature1:value1),
cat> (type2, feature2:value2).
product_type
: The resulting structure (or type) after the rule is applied.feature:value
: The features and their values for the resulting type.cat>
: Specifies a category (type + features) that must appear in a specific order.
Example:
first_person rule
s ===>
cat> (pn, pn_person:first),
cat> (v, v_person:first).
This rule describes how a valid sentence (s
) can be formed: a first-person pronoun (pn
) must be followed by a verb that agrees in person (v_person:first
).
5. Important Notes on TRALE Syntax
-
Whitespace & Indentation: TRALE ignores spaces and indentation. You can format your rules for readability, but they don’t affect the code’s functionality.
Example:
rule_name rule (product_type, feature:(((something)))) ===> cat> (type1, feature1: value1, feature2: value2), cat> (type2, feature2:value2).
-
Brackets: Brackets can be omitted where there is no ambiguity. However, you can also add more brackets for clarity when necessary.
This example will work with no problem in TRALE:
token ---> type, feature1:(((value1))), feature2:value2.
-
Full Stop: In TRALE syntax, a full stop (
.
) is used to mark the end of a statement or expression, similar to the semicolon (;
) in C or Java. Every definition or rule must end with a period. -
Comment:
%
is the marker for comment in TRALE, just like#
in Python.
6. Your First Grammar
Now that you understand the basic components, let’s create a simple grammar that models subject-verb agreement based on person. This grammar will include the necessary types, features, lexicons, and rules.
% Type Feature Structure
bot sub [cat, person].
cat sub [pn, v, s].
pn intro [pn_person:person].
v intro [v_person:person].
person sub [first, second, third].
% Lexicon
i ---> (pn, pn_person:first).
you ---> (pn, pn_person:second).
he ---> (pn, pn_person:third).
she ---> (pn, pn_person:third).
sleep ---> (v, v_person:first).
sleep ---> (v, v_person:second).
sleeps ---> (v, v_person:third).
% Rules
first_person rule
s ===>
cat> (pn, pn_person:first),
cat> (v, v_person:first).
second_person rule
s ===>
cat> (pn, pn_person:second),
cat> (v, v_person:second).
third_person rule
s ===>
cat> (pn, pn_person:third),
cat> (v, v_person:third).
Type Feature Structure:
- The types
pn
(pronouns),v
(verbs), ands
(sentences) as subtypes ofcat
. - The
person
type, withfirst
,second
, andthird
as its subtypes. - Pronouns (
pn
) and verbs (v
) introduce features to denote their person (pn_person
for pronouns,v_person
for verbs).
Lexicon: This lexicon defines first-, second-, and third-person pronouns and their corresponding verbs (with proper person agreement).
Rules: These rules ensure that:
- For a valid sentence (
s
), a first-person pronoun must be followed by a first-person verb. - The same applies for second-person and third-person combinations.
7. Running Your Grammar
Once you’ve written your grammar, save it as a .pl
file (e.g., your_first_grammar.pl
) and use the following command to run it in TRALE:
trale -fusg -c your_first_grammar.pl
Getting the TRALE Prompt
After running the command, you’ll enter the TRALE prompt, which functions similarly to an interactive shell (like Python’s REPL). From here, you can test your grammar and validate sentences.
Commands at the Prompt:
halt.
: Exits the TRALE prompt.compile_gram('grammar_name.pl').
: Compiles a grammar file, which is equivalent to using the-c
flag when launching TRALE.rec([a, sentence, like, this]).
: Parse a sentence. The bracket is optional.
Here’s what a sample session might look like:
| ? rec[i, sleep].
yes
| ? rec[you, sleeps].
no
| ? rec[he, sleeps].
yes
| ? rec[she, sleep].
no
rec[i, sleep].
returnsyes
because the sentence is grammatically correct.rec[you, sleeps].
returnsno
because of subject-verb disagreement.rec[he, sleeps].
returnsyes
as the sentence is grammatical.
By defining types, features, lexicons, and rules, you can build more complex grammars that reflect the syntax of any language you’re working on. This Quick Start guide covers the basics, but TRALE’s flexible framework allows you to extend your grammar to handle more intricate linguistic phenomena.