11
CS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars model for specifying programming languages why not just use regular expressions? example grammar – derivations Parse trees Syntax-directed translation using syntax-directed translation to interpret MiniSVG Top-down Predictive Parsing Our Next Class of Languages Regular Languages } { n n b a } { R ww Context-Free Languages * * b a * ) ( b a + Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton We will start here Example A context-free grammar : S " aSb S "# aabb aaSbb aSb S ! ! ! G A derivation: aaabbb aaaSbbb aaSbb aSb S ! ! ! ! Another derivation:

Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

CS453 Lecture Context Free Grammar Intro 1

Plan for This Today

 Context Free Grammars –  model for specifying programming languages –  why not just use regular expressions? –  example grammar –  derivations

 Parse trees

 Syntax-directed translation –  using syntax-directed translation to interpret MiniSVG

 Top-down Predictive Parsing

Our Next Class of Languages

Regular Languages

}{ nnba }{ Rww

Context-Free Languages

**ba *)( ba +

Context-Free Languages

Pushdown Automata

Context-Free Grammars

stack

automaton

We will start here

Example

A context-free grammar :

!

S"aSbS"#

aabbaaSbbaSbS !!!

G

A derivation:

aaabbbaaaSbbbaaSbbaSbS !!!!Another derivation:

Page 2: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

!"

"

SaSbS

=)(GL

(((( ))))

}0:{ !nba nn

Describes parentheses:

An Application of This Language Deriving another grammar

Regular Languages

}{ nnba }{ Rww

Context-Free Languages Can we derive a Grammar for:

Gave a grammar for:

!

S"aSaS"bSbS"#

abbaabSbaaSaS !!!

A context-free grammar : G

A derivation:

Example

abaabaabaSabaabSbaaSaS !!!!Another derivation:

!"

"

SaSbS

=)(GL

(((( ))))

}0:{ !nba nn

Describes parentheses:

Representing All Properly Nested Parentheses

Can we build a grammar to include any valid combination of ()? For example ( () (()) )

Page 3: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

!

S"(S)S"SSS"#

!

S" SS" (S)S" ()S" ()

A context-free grammar : G

A derivation:

The Possible Grammar

!

S" SS" (S)S" ()S" ()(S)" ()()Another derivation:

Context-Free Grammars

Grammar

Productions of the form: xA!

String of symbols, Nonterminals and terminals

),,,( PSTVG =

Nonterminals Terminals Start symbol

Nonterminal

Derivation Order

ABS!.1

!

2. A"aaA3. A"#

!

4. B"Bb5. B"#

aabaaBbaaBaaABABS54321!!!!!

Leftmost derivation:

Given a grammar with rules:

Always expand the leftmost non-terminal

Derivation Order

aabaaAbAbABbABS32541!!!!!

Rightmost derivation:

Always expand the rightmost non-terminal

Given a grammar with rules:

ABS!.1

!

2. A"aaA3. A"#

!

4. B"Bb5. B"#

Page 4: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

Leftmost derivation:

Rightmost derivation:

Stm --> id := Exp Exp --> num Exp --> ( Stm, Exp )

Grammar

a := ( b := ( c := 3, 2 ), 1 )

String

Stm ==> a := Exp ==> a := ( Stm, Exp ) ==> a := ( b := Exp, Exp ) ==> a := ( b := ( Stm, Exp ), Exp ) ==> a := ( b := ( c := Exp, Exp ), Exp ) ==> a := ( b := ( c := 3, Exp ), Exp ) ==> a := ( b := ( c := 3, 2), Exp ) ==> a := ( b := ( c := 3, 2), 1)

Stm ==> a := Exp ==> a := ( Stm, Exp ) ==> a := ( Stm, 1) ==>

ABS!

ABS!

!

A"aaA |#

!

B"Bb |#

S

BA

Parse Trees

aaABABS !!

a a A

S

BA

ABS!

!

A"aaA |#

!

B"Bb |#Parse Trees

aaABbaaABABS !!!S

BA

a a A B b

Parse Trees

ABS!

!

A"aaA |#

!

B"Bb |#

Page 5: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

aaBbaaABbaaABABS !!!!S

BA

a a A B b

!

"

Parse Trees

ABS!

!

A"aaA |#

!

B"Bb |#aabaaBbaaABbaaABABS !!!!!

S

BA

a a A B b

!

"

!

"

ABS!

!

A"aaA |#

!

B"Bb |#

yield

!

aa""b= aab

Parse Trees

Sentential forms

ABS!

S

BA

Partial parse tree

ABS!

!

A"aaA |#

!

B"Bb |# aaABABS !!

S

BA

a a A

Partial parse tree

sentential form

Page 6: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

aabaaBbaaBaaABABS !!!!!

aabaaAbAbABbABS !!!!!S

BA

a a A B b

Same parse tree

Sometimes, derivation order doesn’t matter

Leftmost:

Rightmost:

!

"

!

" CS453 Lecture Context Free Grammar Intro 22

Does it matter here?

a := ( b := ( c := 3, 2 ), 1 )

Stm --> id := Exp Exp --> num Exp --> ( Stm, Exp )

Grammar

String

Parse Tree

CS453 Lecture Context Free Grammar Intro 23

How about here?

42 + 7 * 6

(1) exp --> exp * exp (2) exp --> exp + exp (3) exp --> NUM

Grammar

String

CS453 Lecture Context Free Grammar Intro 24

Syntax Directed Translation or Interpretation

 Use parse tree to … –  Translate one language to another –  Create a data structure of the program –  Interpret, or evaluate, the program

 Works conceptually by… –  Parser discovers the parse tree –  Parser executes certain actions while “traversing” the parse tree using a

depth-first, post-order traversal

Page 7: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

CS453 Lecture Context Free Grammar Intro 25

MiniSVG Grammar

 svg -> SVG_START elem_list SVG_END

 elem_list -> elem elem_list | epsilon

 elem -> RECT_START KW_X EQ NUM KW_Y EQ NUM KW_WIDTH EQ NUM KW_HEIGHT EQ NUM KW_FILL EQ COLOR ELEM_END

 | CIRCLE_START KW_CX EQ NUM KW_CY EQ NUM KW_R EQ NUM KW_FILL EQ COLOR ELEM_END

 | LINE_START KW_X1 EQ NUM KW_Y1 EQ NUM KW_X2 EQ NUM KW_Y2 EQ NUM KW_STROKE EQ COLOR ELEM_END

CS453 Lecture Context Free Grammar Intro 26

Example Parse Tree for MiniSVG

CS453 Lecture Context Free Grammar Intro 27

Interpret this program

a := ( b := ( c := 3, 2 ), 1 )

Stm --> id := Exp Exp --> num Exp --> ( Stm, Exp )

Grammar

String

Parse Tree

CS453 Lecture Context Free Grammar Intro 28

How about here?

42 + 7 * 6

(1) exp --> exp * exp (2) exp --> exp + exp (3) exp --> NUM

Grammar

String

Page 8: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

grammar

Parser input string

derivation

Example:

Parser derivation

!

S"SSS"aSbS"bSaS"#

input

? aabb

Exhaustive Search

!

S"SS | aSb |bSa |#

Phase 1:

!

S" SSS" aSbS" bSaS"#

aabb

All possible derivations of length 1

Find derivation of aabb

!

S" SSS" aSbS" bSaS"#

Page 9: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

Phase 2

aSbSSSS

!

!

aabb

SSSSbSaSSSSaSbSSSSSSSSSS

!!

!!

!!

!!

Phase 1

abaSbSabSabaSbSaaSbbaSbSaSSbaSbS

!!

!!

!!

!!

!

S"SS | aSb |bSa |# Phase 2

SSSSaSbSSSSSSSSSS

!!

!!

!!

aaSbbaSbSaSSbaSbS

!!

!!

Phase 3 aabbaaSbbaSbS !!!

aabb

!

S"SS | aSb |bSa |#

Final result of exhaustive search

Parser

derivation

input

aabb

aabbaaSbbaSbS !!!

(top-down parsing)

!

S" SSS" aSbS" bSaS"#

Time complexity of exhaustive search

Suppose there are no productions of the form

!

A"#

BA!

Number of phases for string : w ||2 w

Page 10: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

Time for phase 1: k

k possible derivations

For grammar with rules k

Time for phase 2: 2k

possible derivations 2k

Time for phase : ||2wk

possible derivations ||2wk

||2 w

Total time needed for string : w

||22 wkkk +++ !

Extremely bad!!!

phase 1 phase 2 phase 2|w|

Page 11: Our Next Class of Languages - Colorado State Universitycs453/yr2011/Slides/04-CFG-Intro.ppt.pdfCS453 Lecture Context Free Grammar Intro 1 Plan for This Today Context Free Grammars

For general context-free grammars:

There exists a parsing algorithm that parses a string in time

||w3||w

For LL(1) grammars we can use Predictive parsing and parse in ||w

CS453 Lecture Context Free Grammar Intro 42

Example Predictive Parser

void start() { switch(m_lookahead) { case NUM: mesh(); match(Token.Tag.EOF); break; default: throw new ParseException(…); }} void mesh() { switch(this.m_lookahead) { case NUM: num_nodes = ((Num)m_lookahead).value; match(NUM); nodelist(); num_elem = ((Num)m_lookahead).value; match(NUM); elemlist(); break; default: throw new ParseException(…); }} void nodelist() { switch(m_lookahead) { case NUM: break; // nodelist -> epsilon case NODE: node(); nodelist(); break; // nodelist -> node nodelist default: throw new ParseException(…); }}

(1) start -> mesh EOF(2) mesh -> NUM nodelist NUM elemlist(3a & b) nodelist -> ϵ | node nodelist(4) node -> NODE NUM REAL REAL // node_id, x, y(5a & b) elemlist -> ϵ | elem elemlist(6a) elem -> TRI NUM NUM NUM NUM // elem_id, 3 node ids(6b) elem -> SQR NUM NUM NUM NUM NUM //elem_id,4 node ids

CS453 Lecture Context Free Grammar Intro 43