• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Not all context-free grammars are equally useful for parsing. Some grammars are ambiguous (producing multiple parse trees), some contain left recursion (breaking top-down parsers), and some require unbounded lookahead. A classification system helps parser designers choose or transform grammars for the target parsing strategy.

Core Idea

Context-free grammars are classified by their properties relevant to parsing: ambiguous vs unambiguous, left-recursive vs non-left-recursive, LL(k) (parseable with k lookahead top-down), and LR(k) (parseable with k lookahead bottom-up). Every LL grammar is also LR, but not vice versa. Grammars can often be transformed (left-recursion elimination, left-factoring) to fit a desired class.

How It Works

A grammar is ambiguous if some string has more than one leftmost derivation. A grammar is left-recursive if a non-terminal derives a string starting with itself (direct: A → Aα, or indirect: A → Bβ, B → Aγ). A grammar is LL(k) if for every non-terminal and every k-token lookahead, exactly one production can be chosen. A grammar is LR(k) if handle identification can be done with k lookahead. The class hierarchy: LL ⊂ LR ⊂ unambiguous CFG ⊂ CFG.

Visual Explanation

cfg_classification CFG All CFGs Unamb Unambiguous CFG->Unamb no ambiguity LR LR(k) (bottom-up) Unamb->LR deterministic LL LL(k) (top-down) LR->LL left-to-right parseable Amb Ambiguous (multiple trees) Amb->CFG NonDet Non-deterministic (shift/reduce conflicts) NonDet->Unamb

Semantic Network

semantic_cfg_class THIS CFG Classification PRE1 Context-Free Grammar THIS--PRE1 built from OUT1 Top-Down Parsing THIS--OUT1 LL grammars used here OUT2 Bottom-Up Parsing THIS--OUT2 LR grammars used here OUT3 LR Parsers THIS--OUT3 builds into REL1 Ambiguous Grammar THIS--REL1 contrasts with REL2 FIRST and FOLLOW Sets THIS--REL2 related

Key Properties

  • Ambiguous: Multiple parse trees for the same string — problematic for deterministic parsing
  • Left-recursive: Direct or indirect recursion on the left — fatal for top-down parsers
  • LL(k): Deterministic top-down parseable with k-token lookahead — requires left-factoring
  • LR(k): Deterministic bottom-up parseable with k-token lookahead — more powerful than LL
  • Hierarchy: Every LL grammar is LR, but LR grammars include non-LL languages (e.g., left-recursive expressions)

Connections

Edge Cases & Gotchas

  • LL(1) ≠ LL(k): A grammar may not be LL(1) but may be LL(2) — increasing lookahead increases power
  • LR(0) < SLR < LALR < CLR: Within LR family, each subclass handles a larger set of grammars
  • Grammar transformation: Left-recursive grammars can be mechanically transformed to non-left-recursive, but the resulting grammar may be harder to read
  • Inherently ambiguous languages: Some languages are inherently ambiguous — no unambiguous grammar exists for them