Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Context-Free Grammars: Description, Natural Language Examples, and Programming Languages, Slides of Theory of Automata

Context-free grammars (cfgs), their components, natural language examples, and their use in describing parts of programming languages. Cfgs are essential for understanding the meaning of computer programs and are used in compilers.

Typology: Slides

2012/2013

Uploaded on 04/29/2013

juni
juni ๐Ÿ‡ฎ๐Ÿ‡ณ

4

(17)

122 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Context-free languages
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Context-Free Grammars: Description, Natural Language Examples, and Programming Languages and more Slides Theory of Automata in PDF only on Docsity!

Context-free languages

Context-free grammar

โ€ข This is an a different model for describing

languages

โ€ข The language is specified by productions

(substitution rules) that tell how strings can be

obtained, e.g.

โ€ข Using these rules, we can derive strings like this:

A โ†’ 0A

A โ†’ B

B โ†’

A, B are variables 0, 1, # are terminals A is the start variable

A โ‡’ 0A1 โ‡’ 00A11 โ‡’ 000A111 โ‡’ 000B111 โ‡’ 000#

Natural languages

โ€ข We can describe (some fragments) of the

English language by a context-free grammar:

SENTENCE โ†’ NOUN-PHRASE VERB-PHRASE NOUN-PHRASE โ†’ CMPLX-NOUN NOUN-PHRASE โ†’ CMPLX-NOUN PREP-PHRASE VERB-PHRASE โ†’ CMPLX-VERB VERB-PHRASE โ†’ CMPLX-VERB PREP-PHRASE PREP-PHRASE โ†’ PREP CMPLX-NOUN CMPLX-NOUN โ†’ ARTICLE NOUN CMPLX-VERB โ†’ VERB NOUN-PHRASE CMPLX-VERB โ†’ VERB

ARTICLE โ†’ a ARTICLE โ†’ the NOUN โ†’ boy NOUN โ†’ girl NOUN โ†’ flower VERB โ†’ likes VERB โ†’ touches VERB โ†’ sees PREP โ†’ with

variables: SENTENCE, NOUN-PHRASE, โ€ฆ terminals: a, the, boy, girl, flower, likes, touches, sees, with start variable: SENTENCE

Programming languages

โ€ข Context-free grammars are also used to

describe (parts of) programming languages

โ€ข For instance, expressions like (2 + 3) * 5 or

3 + 8 + 2 * 7 can be described by the CFG

โ†’ + โ†’ * โ†’ () โ†’ 0 โ†’ 1 โ€ฆ โ†’ 9

Variables: Terminals: +, *, (, ), 0, 1, โ€ฆ, 9

Definition of context-free grammar

โ€ข A context-free grammar (CFG) is a 4-tuple

( V , T , P , S) where

  • V is a finite set of variables or non-terminals
  • T is a finite set of terminals ( V โˆฉ T = โˆ…)
  • P is a set of productions or substitution rules of the

form

where A is a symbol in V and ฮฑ is a string over V โˆช T

  • S is a variable in V called the start variable

A โ†’ ฮฑ

Shorthand notation for productions

  • When we have multiple productions with the

same variable on the left like

we can write this in shorthand as

E โ†’ E + E E โ†’ E * E E โ†’ (E) E โ†’ N

E โ†’ E + E | E * E | (E) | 0 | 1 N โ†’ 0N | 1N | 0 | 1

Variables: E, N Terminals: +, *, (, ), 0, 1 Start variable: E

N โ†’ 0N N โ†’ 1N N โ†’ 0 N โ†’ 1

Language of a CFG

โ€ข The language of a CFG ( V , T , P , S) is the set of

all strings containing only terminals that can be

derived from the start variable S

โ€ข This is a language over the alphabet T

โ€ข A language L is context-free if it is the

language of some CFG

L = {ฯ‰ | ฯ‰ โˆˆ T* and S โ‡’* ฯ‰ }

Example 1

โ€ข Is the string 00#11 in L?

โ€ข How about 00#111, 00#0#1#11?

โ€ข What is the language of this CFG?

A โ†’ 0A1 | B

B โ†’

variables: A, B terminals: 0, 1, # start variable: A

L = {0 n #1 n : n โ‰ฅ 0}

Examples: Designing CFGs

โ€ข Write a CFG for the following languages

  • Linear equations over x, y, z, like: x + 5y โ€“ z = 9 11x โ€“ y = 2
  • Numbers without leading zeros, e.g., 109, 0 but not 019
  • The language L = {a n b n c m d m^ | n โ‰ฅ 0, m โ‰ฅ 0}

n m m n Docsity.com

Context-free versus regular

โ€ข Write a CFG for the language (0 + 1)*

โ€ข Can you do so for every regular language?

โ€ข Proof:

S โ†’ A

A โ†’ ฮต | 0A | 1A

Every regular language is context-free

regular expression NFA DFA

Context-free versus regular

โ€ข Is every context-free language regular?

โ€ข No! We already saw some examples:

โ€ข This language is context-free but not regular

A โ†’ 0A1 | B

B โ†’

L = {0 n #1 n : n โ‰ฅ 0}

Parse tree

โ€ข Derivations can also be represented using

parse trees

E โ‡’ E + E โ‡’ V + E โ‡’ x + E โ‡’ x + (E) โ‡’ x + (E โˆ’ E) โ‡’ x + (V โˆ’ E) โ‡’ x + (y โˆ’ E) โ‡’ x + (y โˆ’ V) โ‡’ x + (y โˆ’ z)

E

E + E

V ( E )

E โˆ’^ E

V V

x

y z

E โ†’ E + E | E - E | (E) | V V โ†’ x | y | z

Left derivation

โ€ข Always derive the leftmost variable first:

โ€ข Corresponds to a left-to-right traversal of parse

tree

E โ‡’ E + E โ‡’ V + E โ‡’ x + E โ‡’ x + (E) โ‡’ x + (E โˆ’ E) โ‡’ x + (V โˆ’ E) โ‡’ x + (y โˆ’ E) โ‡’ x + (y โˆ’ V) โ‡’ x + (y โˆ’ z)

E

E + E

V ( E )

E โˆ’^ E

V V

x

y z

Ambiguity

โ€ข A grammar is ambiguous if some strings have

more than one parse tree

โ€ข Example: E^ โ†’^ E + E | E^ โˆ’^ E | (E) | V

V โ†’ x | y | z

x + y + z

E

E + E

V E + E

x V V y z

E

E + E

E + E V

V V

x y

z