Knowledge Sprint SeriesMT24 Learning

Phase 8 · Sprint 24/90 · Tokenization & ParsingChurch · Patil · Jurafsky · de Marneffe · Bray

Parsing Structured Meaning from Text

Text has an order. Meaning has a shape. A parser finds the shape, or says it cannot.

In short. From a string to a tree, how ambiguity multiplies, why precedence matters, and parsing JSON strictly.

Sprint 24 of 90 infographic: Parsing Structured Meaning from Text. Text has an order. Meaning has a shape. A parser finds the shape, or says it cannot. The full text of the image is on this page.
Full resolution2160 × 2700 PNGPhase summary

The part of the image you are reading lights up. Tap a part to jump to its text.

Fig. 1 in words

Two trees · ambiguity grows · a duplicate key · same data as code 05

  1. 1 Same 5 tokens · two trees

    • 2 + 3 * 4
    • + at the root, over 2 and a * node over 3 and 4: = 14 · grammar's tree
    • * at the root, over a + node (2, 3) and 4: = 20 · needs ( )
  2. 2 Trees per n operators

    • n = 1 to 8: 1 · 2 · 5 · 14 · 42 · 132 · 429 · 1 430
    • log scale
    • legend: the bar at 2 is 2 + 3 * 4 · the bar at 3 is 3 PPs
  3. 3 One record · two readers

    • {"dose": 5, "dose": 50}
    • json.loads: {'dose': 50} · no warning
    • strict hook: rejected: duplicate key
    • RFC 8259: names SHOULD be unique. Readers differ.

01 From String to Tree Grammar

  • A grammar lists the allowed forms and how parts group
  • Parsing turns a string into a tree: leaves keep the order, the shape holds the meaning
  • Constituency trees group phrases. Dependency trees link words to heads

Universal Dependencies: one scheme, 200+ treebanks in 150+ languages (de Marneffe et al. 2021).

02 Ambiguity Multiplies Trees

  • “I shot an elephant in my pajamas”: the phrase can attach to elephant or to shot
  • Each added prepositional phrase multiplies the readings: 2, 5, 14, 42…
  • A parser must choose, by rules or by probabilities

Church & Patil (1982) found 4 862 trees for nine such phrases. Example: Jurafsky & Martin.

03 Precedence Matters Rules

  • “*” sits one level deeper, so 2 + 3 * 4 = 14
  • The same tokens as (2 + 3) * 4 give 20
  • The loop groups from the left: 1 + 2 + 3 parses as (1 + 2) + 3

Precedence and grouping belong in the grammar, not in the reader’s head.

04 Parse Strictly Schema

  • JSON object names SHOULD be unique, and readers differ on duplicates (RFC 8259)
  • Python keeps the last one: “dose” 5 then 50 gives 50, with no warning
  • Python also reads NaN and Infinity, which JSON does not allow

object_pairs_hook sees every pair and can refuse the record (Python docs).

05 Try It Yourself PYTHON 3.11.15

sprint24_parsing_structured_meaning_from_text.py

import json, re
def parse(src):      # "*" binds tighter than "+"
    toks = re.findall(r"\d+|\S", src)
    def atom():
        t = toks.pop(0)
        if t != "(": return int(t)
        e = level("+"); toks.pop(0); return e
    def level(op):
        sub = atom if op == "*" else (
            lambda: level("*"))
        e = sub()
        while toks and toks[0] == op:
            toks.pop(0); e = (op, e, sub())
        return e
    return level("+")
def ev(e):
    if isinstance(e, int): return e
    a, b = ev(e[1]), ev(e[2])
    return a + b if e[0] == "+" else a * b
t = parse("2 + 3 * 4"); print(t, ev(t))
print(ev(parse("(2 + 3) * 4")))
def trees(n):        # bracketings of n operators
    return 1 if n == 0 else sum(trees(k) *
        trees(n - 1 - k) for k in range(n))
print([trees(n) for n in range(1, 9)])
def strict(pairs):
    if len({k for k, _ in pairs}) < len(pairs):
        raise ValueError("duplicate key")
    return dict(pairs)
raw = '{"dose": 5, "dose": 50}'
print(json.loads(raw))
try: json.loads(raw, object_pairs_hook=strict)
except ValueError as e: print("rejected:", e)

Output, as printed on the image

('+', 2, ('*', 3, 4)) 14
20
[1, 2, 5, 14, 42, 132, 429, 1430]
{'dose': 50}
rejected: duplicate key

Run it. sprint24_parsing_structured_meaning_from_text.py · versions, data and steps

Check: 3 × 4 = 12, 2 + 12 = 14 · 2 + 3 = 5, 5 × 4 = 20 · 5 + 2 + 2 + 5 = 14

06 General Notes Practice

  • Parse once at the boundary, then pass typed records along
  • Reject what does not fit. Log the raw text with the reason
  • Keep the raw input beside the parsed record

Test with ambiguous, malformed and duplicated inputs, not only clean ones.

07 Mini Project Hands-on

  • Add “-” beside “+”: 8 - 3 - 2 must give 3, not 7
  • Feed “2 + * 3” and make the error name the bad token
  • Pass parse_constant to json.loads to refuse NaN

Parse 10 recipe lines like “2 eggs” into JSON records and validate every key.

R Trusted Sources

  1. Church & Patil (1982). Coping with Syntactic Ambiguity… AJCL 8(3–4)
  2. Jurafsky & Martin. Speech and Language Processing, 3rd ed. draft, ch. 17
  3. de Marneffe et al. (2021). Universal Dependencies. CL 47(2), 255–308
  4. Bray, ed. (2017). RFC 8259: The JavaScript Object Notation… IETF
  5. Python docs: json · repeated names, NaN, object_pairs_hook

Cite this phase

Tahri, M. (2026). MT24 Learning — Knowledge Sprint Series, Phase 8: Tokenization & Parsing (Version 1.0) [Educational resource]. Zenodo. https://doi.org/10.5281/zenodo.23213765

Version DOI 10.5281/zenodo.23213765 · all versions 10.5281/zenodo.23213764 · first published 2 October 2026