Skip to content

2 2 link grammar integration and clause trees

TransCompilator edited this page Nov 26, 2026 · 1 revision

2.2 Link Grammar Integration and Clause Trees

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/2-2-link-grammar-integration-and-clause-trees

Link Grammar Integration and Clause Trees

Relevant source files

The following files were used as context for generating this wiki page:

Purpose and Scope

This page details the syntactic front-end integration of the vendored CMU Link Grammar parser within the TransCompilator (tc_r1) architecture. It covers the build-time static configuration, the safe Foreign Function Interface (FFI) wrapper layer, and the deterministic normalization algorithms that transform raw Link Grammar dependency linkages into structured ClauseTree representations featuring active, passive, and imperative grammatical voices.

Sources: vendor/link-grammar/README.md:1-17, crates/tc-syntax/src/lg_ffi.rs:1-6, crates/tc-syntax/src/clause.rs:1-2


Vendored CMU Link Grammar and Build Configuration

TransCompilator vendors an unmodified runtime copy of CMU Link Grammar version 5.13.2 under vendor/link-grammar/ to guarantee zero-ML determinism and offline build constraints. The package includes public headers, dictionary data files (share/link-grammar/en/4.0.dict), and precompiled shared libraries (lib/liblink-grammar.so.5.13.2) under the LGPL-2.1 license.

The build script crates/tc-syntax/build.rs locates these resources relative to CARGO_MANIFEST_DIR, configures native library search paths, and injects relocatable $ORIGIN-relative runpaths (-Wl,-rpath) at varying directory depths (2..=6). This ensures test binaries and the main compiler driver can load the shared library dynamically without requiring system-wide administrative installations or LD_LIBRARY_PATH exports.

// Example build script configuration snippet
// crates/tc-syntax/build.rs:17-31

Sources: vendor/link-grammar/README.md:1-17, crates/tc-syntax/build.rs:1-37, vendor/link-grammar/LICENSE:1-10


Safe FFI Wrapper and Memory Management (lg_ffi.rs)

The crates/tc-syntax/src/lg_ffi.rs module encapsulates all raw C pointers behind safe Rust abstractions. It defines opaque structures mirroring the C ABI (DictionaryOpaque, ParseOptionsOpaque, SentenceOpaque, and LinkageOpaque) and exposes safe structs such as LgParse and LgLink.

+-------------------------------------------------------------+
| Natural Language Space: Raw English Sentence Input           |
+-------------------------------------------------------------+
                             |
                             v
+-------------------------------------------------------------+
| FFI Boundary: crates/tc-syntax/src/lg_ffi.rs                |
| - initialize() [crates/tc-syntax/src/lg_ffi.rs:156-174]     |
| - sentence_create() & sentence_parse()                      |
| - SentenceGuard & LinkageGuard (Drop impls)                 |
+-------------------------------------------------------------+
                             |
                             v
+-------------------------------------------------------------+
| Code Entity Space: LgParse & LgLink Structs                 |
+-------------------------------------------------------------+

Sources: crates/tc-syntax/src/lg_ffi.rs:1-195

Initialization and Resource Guards

Dictionary initialization is governed by initialize(), which resolves the path to en/4.0.dict using /proc/self/exe inspection or manifest fallback. It utilizes OnceLock to ensure process-global dictionary loading and suppresses noisy C-level error channels via lg_error_set_handler().

To prevent memory leaks across the FFI boundary, RAII drop guards—SentenceGuard and LinkageGuard—automatically invoke sentence_delete() and linkage_delete() when pointer wrappers leave scope.

Sources: crates/tc-syntax/src/lg_ffi.rs:137-194


Clause Tree Normalization (clause.rs)

The crates/tc-syntax/src/clause.rs module converts raw dependency linkages into semantic ClauseTree instances. Link Grammar output annotations (such as part-of-speech suffixes and bracketed tags) are cleaned via strip_annotations() and split_tag(), allowing the engine to extract grammatical voice (Voice), main verb heads (VerbHead), subjects, direct objects, and prepositional phrases (PrepPhrase).

Grammatical Voice and Role Resolution

The normalization engine classifies clauses into three distinct voices:

  • Active: Subject performs the action, identified via subject-verb linkage patterns (Sp).
  • Passive: Subject receives the action, detected via passive auxiliary markers (Pv, PV).
  • Imperative: Subject is omitted, defaulting to a wall-connected verb head or first tagged verb.
+-------------------------------------------------------------+
| Natural Language Space: Parse Linkage (`LgParse`)           |
+-------------------------------------------------------------+
                             |
                             v
+-------------------------------------------------------------+
| Normalization Engine: crates/tc-syntax/src/clause.rs        |
| - strip_annotations() [crates/tc-syntax/src/clause.rs:80-92]  |
| - sentence_verb_index() [crates/tc-syntax/src/clause.rs:190-206]|
| - Voice Classification [crates/tc-syntax/src/clause.rs:13-21] |
+-------------------------------------------------------------+
                             |
                             v
+-------------------------------------------------------------+
| Code Entity Space: ClauseTree, VerbHead, PrepPhrase         |
+-------------------------------------------------------------+

Sources: crates/tc-syntax/src/clause.rs:1-218

Core Data Structures

  • Voice: Enum representing Active, Passive, or Imperative scopes.
  • VerbHead: Struct containing raw spelling, deterministic lemma (lemma), and past-tense flags (is_past).
  • PrepPhrase: Struct pairing normalized prepositions (such as out_of) with trailing token sequences.
  • ClauseTree: The primary syntax-layer record consumed by downstream intent gates and semantic lowering phases.

Sources: crates/tc-syntax/src/clause.rs:13-68


Clone this wiki locally