Skip to content

3 2 normalization and value parsing

TransCompilator edited this page Nov 26, 2026 · 1 revision

3.2 Normalization and Value Parsing

Source: https://deepwiki.com/PonvasanthPon/TransCompilator/3-2-normalization-and-value-parsing

Normalization and Value Parsing

Relevant source files

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

Purpose and Scope

Section 3.2 details the pre-processing, deterministic normalization, number-word translation, and recursive value expression parsing mechanisms located within tc-semantics. The pipeline transforms unstructured or casually phrased English expressions, idioms, contractions, conversational politeness openers, and spelled-out cardinal numbers into strict, typed AST structures (AstExpr) suitable for semantic analysis and downstream lowering.

Sources: crates/tc-semantics/src/value.rs, crates/tc-semantics/src/normalize.rs, crates/tc-semantics/src/number_words.rs


1. Linguistic Normalization Layer

The normalizer cleans sentence input without altering string literals, code spans, or control-flow markers (Then, Otherwise, Else). It operates via contraction expansion and casual-opener stripping.

Contraction Expansion

Contractions are expanded by tokenizing the input text using tokenize [crates/tc-semantics/src/normalize.rs:68] and inspecting token pairs for clitic suffixes ('t, 's, 're, 'm, 'll, 've). The function contraction_replacement matches stem-suffix pairs and substitutes them with canonical uncontracted forms (e.g., isn + t becomes is not, I + m becomes I am) [crates/tc-semantics/src/normalize.rs:9-63]. The expansion logic ensures that substitutions only occur outside quoted strings by validating token positions [crates/tc-semantics/src/normalize.rs:65-112].

Casual-Opener Stripping

The function strip_casual_openers iteratively strips common conversational prefixes, politeness wrappers, and discourse openers [crates/tc-semantics/src/normalize.rs:114-148]. Examples of normalized prefixes include:

  • start by setting $\rightarrow$ set [crates/tc-semantics/src/normalize.rs:120]
  • go ahead and $\rightarrow$ "" [crates/tc-semantics/src/normalize.rs:123]
  • tell me or show me $\rightarrow$ print [crates/tc-semantics/src/normalize.rs:134-135]

Sentence-level transformation is coordinated by sentence(), which combines contraction expansion, opener stripping, and relative directional adjustments [crates/tc-semantics/src/normalize.rs:150-185].

Title: "Natural Language Space to Code Entity Space: Normalization Pipeline"

graph TD
    RawText[""Raw English Source Sentence""] --> Tokenize[""tokenize (crates/tc-semantics/token.rs)""]
    Tokenize --> Expand[""expand_contractions (crates/tc-semantics/normalize.rs:65)""]
    Expand --> Strip[""strip_casual_openers (crates/tc-semantics/normalize.rs:114)""]
    Strip --> SentenceNorm[""sentence (crates/tc-semantics/normalize.rs:150)""]
    SentenceNorm --> CanonicalSentence[""Normalized Canonical Sentence""]
Loading

Sources: crates/tc-semantics/src/normalize.rs


2. English Number Word Parsing

Spoken-word integers (zero through ninety-nine, hundred, thousand, and million) are parsed deterministically into signed 64-bit integers (i64) by crates/tc-semantics/src/number_words.rs.

Hyphen Normalization and Phrase Parsing

Before parsing, normalize_hyphens scans token sequences for word-hyphen-word patterns (e.g., twenty-three) and replaces hyphen tokens with spaces [crates/tc-semantics/src/number_words.rs:8-38].

The core parsing routine parse() splits the cleaned string into whitespace-separated words, filters out illegal leading articles (a, an), and accumulates values across units, tens, and multiplier magnitude words (hundred, thousand, million) [crates/tc-semantics/src/number_words.rs:40-95]. is_word() acts as a closed-class membership gate confirming valid lexical items [crates/tc-semantics/src/number_words.rs:97-135].

Title: "Natural Language Space to Code Entity Space: Number Word Parsing"

graph TD
    NumText[""Spoken Number Text: 'twenty-three' / 'one hundred and one'""] --> HyphenNorm[""normalize_hyphens (crates/tc-semantics/number_words.rs:8)""]
    HyphenNorm --> TokenSplit[""Whitespace Split & Filter (crates/tc-semantics/number_words.rs:51)""]
    TokenSplit --> Accumulate[""parse (crates/tc-semantics/number_words.rs:40)""]
    Accumulate --> IntValue[""Signed Integer Value (i64)""]
Loading

Sources: crates/tc-semantics/src/number_words.rs


3. Recursive Value Parsing and Expression Lowering

Value positions across assignments, print statements, and conditions rely on parse_value and the ValueContext trait [crates/tc-semantics/src/value.rs:77-124].

Value Classification

The ValueSpec struct classifies expressions prior to deep lowering using ValueSpec::classify [crates/tc-semantics/src/value.rs:24-74]. Expressions are categorized into coarse kinds via ValueKind:

  • Literal: Quoted strings, boolean constants (true, false), null, no address, and numeric sequences [crates/tc-semantics/src/value.rs:37-48].
  • Call: Function/phrase invocations containing parentheses or starting with create [crates/tc-semantics/src/value.rs:49-50].
  • Place: Variable references, field lookups, or index expressions containing ., ', of, or [ [crates/tc-semantics/src/value.rs:51-56].
  • Expression: Arithmetic, bitwise, comparison, or pointer operations [crates/tc-semantics/src/value.rs:57-66].

The Value Parser Routine

parse_value handles recursive composition by:

  1. Normalizing number-word hyphens [crates/tc-semantics/src/value.rs:125].
  2. Resolving anaphora (their ..., the result) [crates/tc-semantics/src/value.rs:128-143].
  3. Parsing aggregate length/count wrappers (the number of items in ..., length of ..., size of ...) [crates/tc-semantics/src/value.rs:144-179].
  4. Delegating atomic expressions back to ValueContext callbacks [crates/tc-semantics/src/value.rs:77-117].

Sources: crates/tc-semantics/src/value.rs


4. Anaphora Resolution & Special Reference Handling

The value parser resolves context-dependent references without machine learning models, relying strictly on lexical scoping rules maintained by the compilation context.

Plural References (their ...)

When an expression begins with their , parse_value queries context.plural_names() to retrieve active plural referent identifiers from preceding compound bindings [crates/tc-semantics/src/value.rs:128-137]. It emits an anaphora trace note (note_anaphor) recording the resolution mapping for diagnostics and paraphrase echo [crates/tc-semantics/src/value.rs:130].

Result References (the result)

References to the result evaluate to the variable name returned by context.result_name(), representing the most recent assignment target in the active discourse scope [crates/tc-semantics/src/value.rs:138-143].

Title: "Data Flow for Value Parsing and Expression Lowering"

graph TD
    ValText[""Raw Value Text String""] --> NormHyphen[""normalize_hyphens (crates/tc-semantics/value.rs:125)""]
    NormHyphen --> CheckAnaph[""Anaphora Check: their / the result (crates/tc-semantics/value.rs:128)""]
    CheckAnaph --> CheckAgg[""Length/Count Aggregate Prefix (crates/tc-semantics/value.rs:144)""]
    CheckAgg --> ClassifyVal[""ValueSpec::classify (crates/tc-semantics/value.rs:34)""]
    ClassifyVal --> LowerContext[""ValueContext Callbacks (crates/tc-semantics/value.rs:77)""]
    LowerContext --> AstExprOut[""Typed AstExpr (tc-typeck)""]
Loading

Sources: crates/tc-semantics/src/value.rs


Clone this wiki locally