Context Free Language

Context Free Language

Context Free Grammars

intro

  • Example

    alt text

      • 這個集合表示所有形如 “a” 重複 n 次,然後 “b” 重複 n 次的字串。例如,”ab”, “aabb”, “aaabbb” 都屬於這個集合。
      • 儘管看起來簡單,但這個語言不是正規語言。這是一個經典的例子,用來展示有些看起來很簡單的語言,其實並非正規的。
      • 這個集合表示所有形如 “w” 是一個字串,然後 “w^R” 是 “w” 的逆序的字串。例如,”ababa”,因為它的逆序也是 “ababa”,所以屬於這個集合。類似於上一個例子,這個語言也不是正規語言。
    • Regular Languages:
      • 這是一個橢圓形,表示所有正規語言的集合。
    • $a^b^$:
      • 這個表示所有由 “a” 和 “b” 組成的字串,其中 “a” 和 “b” 可以重複任意次數(包括 0 次),且 “a” 必須在 “b” 前面。例如,”a”, “bb”, “aabbb” 都屬於這個集合。
      • 這個表示所有由 “a” 和 “b” 組成的字串,其中 “a” 和 “b” 可以重複任意次數(包括 0 次),且可以任意交錯。例如,”a”, “bb”, “ababab” 都屬於這個集合。
  • 可以如何描述context-free language

    alt text

    • Context-Free Languages: 一種上下文無關的語言
    • Context-Free Grammars: 一種能夠生成、描述CFL的文法
    • Pushdown Automata: 能夠辨識CFL的自動機,通常是stack

Def 5.1

  • CFG的定義

    alt text

    • 上下文無關文法的組成
      • 文法 G:整個文法的符號,由以下四個部分組成:
        • V:變數(或非終結符)的集合。這些變數代表語言中的語法結構。
        • T:終結符的集合。這些終結符是語言中不可再分割的最小單位,也就是實際的字元。
        • S:開始變數,所有推導都從這個變數開始。
        • P:產生式(或產生規則)的集合。這些規則定義了變數如何替換成其他變數或終結符。
      • 產生式:
        • 產生式的形式是:A -> x
        • A 是一個變數。
        • x 是一個由變數和終結符組成的字串。
      • 產生式的意思是:變數 A 可以被替換成字串 x。
    • 上下文無關的特點
      • 上下文無關:產生式中的變數 A 可以被替換,而不需要考慮它在字串中的上下文。也就是說,不管 A 出現在什麼位置,都可以直接替換。
    • Regular and linear grammars are clearly context-free
    • But a context-free grammar is not necessarily linear

      alt text

  • CF language的定義

    alt text

線性文法 vs 非線性文法

  • 線性文法:
    • 在線性文法中,產生規則的形式是每個產生式最多只有一個變量(非終結符號)。例如: A → aB | b
    • 這種文法的每個推導步驟中,句法形式只包含一個變量。
  • 非線性文法:
    • 在非線性文法中,產生規則可以同時包含多個變量。這使得推導過程中的句法形式可能同時包含多個非終結符號。例如: A → BC | a
    • 這樣的推導可以導致包含多個非終結符號的句法形式,如 BC。

Derivation other

  • 推導順序

    alt text

Parse Tree

  • 用樹來表示 Parser

    alt text

  • 樹的結構

    • 左側標籤(Left Side of Productions):樹的每個節點標籤為產生式的左側,即非終結符號。例如,如果有一條產生式 S → AB,則樹的根節點 S 代表這個產生式的左側。
    • 右側子節點(Right Side Children):每個節點的子節點對應於其產生式的右側部分。也就是說,節點的子節點表示該非終結符號可以被替換成的符號。例如,對於 S → AB,A 和 B 將作為 S 的子節點。
    • 有序樹(Ordered Tree):一種樹結構,其中每個節點的子節點具有特定的順序。這意味著在樹中的每個節點,其子節點的排列是有意義的,可能影響生成的結果。

Def 5.3

  • Derivative tree: 什麼樣的有序樹 是一個G的Derivation tree

    alt text

  • Depth-first: 深度優先搜尋

    • left most

      alt text

  • Partial Derivative tree: 部分推導樹

    alt text

    • 保留 3, 4, 5的性質
  • diff.: 值得注意的不同之處

    • Derivative tree != Partial derivative tree

      alt text

Theorem 5.1

  • CFG and derivation tree的關係

    alt text

    • 語言和推導樹的關係:
      • alt text
    • 部分推導樹:
      • alt text
    • 有些時候推導順序,可能不存在差異

      alt text

Parsing and Ambiguity

Parsing

  • 成員問題

    • Given a string w of terminals, we want to know whether or not w is in L(G) (membership question)
  • 什麼是parsing

    • Parsing describes finding a sequence of productions by which a w ϵ L(G) is derived
    • alt text

Exhaustive Search Parsing (Brute Force Parsing)

  • 暴力法 (暴搜)

    • top-down pasing

      alt text
      alt text
      alt text
      alt text

  • 缺點

    • Flaws of Exhaustive Search Parsing
      • Tediousness (bad for efficiency)
      • It is possible that it never terminates for strings not in L(G)

Theorem 5.2

  • 如果文法不包含特定形式: lambda or 自身

    alt text
    alt text

  • 則存在字串長度限制

    alt text
    alt text
    alt text

EX. Big-O

  • 暴力法

    alt text
    alt text

  • 一般方式

    alt text

s-grammar

  • 定義新的文法形式

    alt text
    alt text

    • 每個phase 只有一種選擇
  • Ex.

    alt text

Ambiguity

  • 存在兩種推導順序

    alt text

  • left most

    alt text
    alt text

    • 中間的計算過程 順序會不同 導致計算結果不同

Def 5.5

  • 甚麼樣的CFG 是ambiguous

    alt text
    alt text

why care?

  • different result: 導致不同結果

    alt text

    • Ambiguity is bad for programming languages

fix

  • 修正

    alt text
    alt text

    • Unique derivation tree
  • unique

    alt text

if-elase ambiguous

  • 代換成if else

    alt text
    alt text

Inherent Ambiguity

  • 有些東西天生就只有ambiguous: Some context free languages have only ambiguous grammars

    alt text
    alt text

Context-Free Grammars and Programming Languages

  • 編譯器在做甚麼

    alt text

    • lexer
    • parser
    • code generation

Context Free Language
https://z-hwa.github.io/webHome/[object Object]/Theory of Computation/Context-Free-Language/
作者
crown tako
發布於
2024年11月1日
許可協議