Docsity

Understanding Context-Free Languages and Pushdown Automata - Prof. Andrew P. Tolmach, Study notes of Computer Science

Partial preview of the text

Download Understanding Context-Free Languages and Pushdown Automata - Prof. Andrew P. Tolmach and more Study notes Computer Science in PDF only on Docsity!

CS311 Computational Structures

Context-free Languages

Andrew P. Black Lecture 7

Chomsky hierarchy

In 1957, Noam Chomsky published Syntactic Structures , an landmark book that defined the so- called Chomsky hierarchy of languages original name language generated productions: Type-3 Grammars Regular Type-2 Grammars Contex-free Type-1 Grammars Context-sensitive Type-0 Grammars Recursively-enumerable no restriction A → γ A → a and A → aB αAβ → αγβ A, B: terminals, a, b non-terminal, α, β sequences of terminals and non-terminals

Context-free Languages

Context-free Languages

More general productions than regular

grammars

S → w where w is any string of terminals and non-terminals

Context-free Languages

More general productions than regular

grammars

S → w where w is any string of terminals and non-terminals

What languages do these grammars

generate?

S →(A)

A → Λ | aA | ASA

Context-free Languages

More general productions than regular

grammars

S → w where w is any string of terminals and non-terminals

What languages do these grammars

generate?

S →(A)

A → Λ | aA | ASA

S → Λ | aSb

Context-free languages more general than regular languages

  • {a n b n | n ≥ 0} is not regular

Context-free languages more general than regular languages

  • {a n b n | n ≥ 0} is not regular ‣ but it^ is^ context-free

Context-free languages more general than regular languages

  • {a n b n | n ≥ 0} is not regular ‣ but it^ is^ context-free
  • Why are they called “context-free”? ‣ Context-sensitive grammars allow more than one symbol on the lhs of productions

Context-free languages more general than regular languages

  • {a n b n | n ≥ 0} is not regular ‣ but it^ is^ context-free
  • Why are they called “context-free”? ‣ Context-sensitive grammars allow more than one symbol on the lhs of productions ° xAy^ →^ x(S)y can only be applied to the non-terminal A when it is in the context of x and y

Example

adding_operator ::= "+" | "−". multiplying_operator ::= "×" | "/" | "÷". primary ::= unsigned_number | variable | function_designator | "(" arithmetic_expression ")". factor ::= primary | factor | factor power primary. term ::= factor | term multiplying_operator factor. simple_arithmetic_expression ::= term | adding_operator term | simple_arithmetic_expression adding_operator term. if_clause ::= if Boolean_expression then. arithmetic_expression ::= simple_arithmetic_expression | if_clause simple_arithmetic_expression else arithmetic_expression. if a < 0 then U+V else if a * b < 17 then U/V else if k <> y then V/U else 0

Context-free languages

Closed under ∪, ∗ and ·, and under

∩ with a regular language

‣ How do we prove these properties?

Not closed under intersection,

complement or difference

Recognizable by pushdown automata

‣ A pushdown automaton is a generalization of a

finite-state automaton

Executing a PDA

• The configuration for a PDA (PDA +

Heinʼs ID ) consists of

  1. The PDA,
  2. The current state of the PDA
  3. The contents of its stack,
  4. the remaining input.

We defined

‣ The transitions^ are applicable iff

° is the current state, ° , or the next character on the input tape is^ , and ° , or the top of the stack is

‣ If you select a transition^ , then

° The new state is ° if^ ,^ is popped off of the stack, and ° the (possibly empty) sequence of symbols^ is pushed onto the stack

Transitions

δ(q, a, γ)

q

a

q

′

δ : (Q × A

Λ

× Γ

Λ

) → P{Q × Γ

∗

(q

′

a = Λ