S " # Context-Free Pushdown Grammars Automata A derivation: - - PowerPoint PPT Presentation

s
SMART_READER_LITE
LIVE PREVIEW

S " # Context-Free Pushdown Grammars Automata A derivation: - - PowerPoint PPT Presentation

Plan for This Today Our Next Class of Languages Context Free Grammars model for specifying programming languages Context-Free Languages why not just use regular expressions? example grammar derivations R n b n { ww } { a }


slide-1
SLIDE 1

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 nb

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:

slide-2
SLIDE 2

! " " S aSb S = ) (G L (((( )))) } : { ! n b a

n n Describes parentheses:

An Application of This Language

Deriving another grammar

Regular Languages

} {

n nb

a } {

R

ww

Context-Free Languages

Can we derive a Grammar for: Gave a grammar for:

S "aSa S "bSb S "# abba abSba aSa S ! ! !

A context-free grammar :

G

A derivation:

Example

abaaba abaSaba abSba aSa S ! ! ! !

Another derivation:

! " " S aSb S = ) (G L (((( )))) } : { ! n b a

n n Describes parentheses:

Representing All Properly Nested Parentheses

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

slide-3
SLIDE 3

S "(S) S "SS S "#

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:

x A !

String of symbols,

Nonterminals and terminals

) , , , ( P S T V G =

Nonterminals Terminals Start

symbol Nonterminal

Derivation Order

AB S ! . 1

  • 2. A "aaA
  • 3. A "#
  • 4. B "Bb
  • 5. B "#

aab aaBb aaB aaAB AB S

5 4 3 2 1

! ! ! ! !

Leftmost derivation:

Given a grammar with rules: Always expand the leftmost non-terminal

Derivation Order

aab aaAb Ab ABb AB S

3 2 5 4 1

! ! ! ! !

Rightmost derivation:

Always expand the rightmost non-terminal Given a grammar with rules:

AB S ! . 1

  • 2. A "aaA
  • 3. A "#
  • 4. B "Bb
  • 5. B "#
slide-4
SLIDE 4

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) ==>

AB S ! AB S ! A "aaA |# B "Bb |#

S B A

Parse Trees

aaAB AB S ! !

a a A S B A

AB S ! A "aaA |# B "Bb |#

Parse Trees

aaABb aaAB AB S ! ! !

S B A a a A B b

Parse Trees

AB S ! A "aaA |# B "Bb |#

slide-5
SLIDE 5

aaBb aaABb aaAB AB S ! ! ! !

S B A a a A B b

"

Parse Trees

AB S ! A "aaA |# B "Bb |# aab aaBb aaABb aaAB AB S ! ! ! ! !

S B A a a A B b

" "

AB S ! A "aaA |# B "Bb |#

yield

aa""b = aab

Parse Trees Sentential forms

AB S !

S B A

Partial parse tree

AB S ! A "aaA |# B "Bb |# aaAB AB S ! !

S B A a a A

Partial parse tree sentential form

slide-6
SLIDE 6

aab aaBb aaB aaAB AB S ! ! ! ! ! aab aaAb Ab ABb AB S ! ! ! ! !

S B A 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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

grammar Parser input string derivation Example: Parser derivation

S "SS S "aSb S "bSa S "#

input ?

aabb

Exhaustive Search

S "SS | aSb |bSa |#

Phase 1:

S " SS S " aSb S " bSa S " #

aabb

All possible derivations of length 1 Find derivation of

aabb

S " SS S " aSb S " bSa S " #

slide-9
SLIDE 9

Phase 2

aSb S SS S ! ! aabb S SS S bSaS SS S aSbS SS S SSS SS S ! ! ! ! ! ! ! !

Phase 1

ab aSb S abSab aSb S aaSbb aSb S aSSb aSb S ! ! ! ! ! ! ! !

S "SS | aSb |bSa |#

Phase 2

S SS S aSbS SS S SSS SS S ! ! ! ! ! ! aaSbb aSb S aSSb aSb S ! ! ! !

Phase 3

aabb aaSbb aSb S ! ! ! aabb

S "SS | aSb |bSa |#

Final result of exhaustive search Parser derivation input

aabb aabb aaSbb aSb S ! ! !

(top-down parsing)

S " SS S " aSb S " bSa S " #

Time complexity of exhaustive search Suppose there are no productions of the form

A "# B A !

Number of phases for string :

w | | 2 w

slide-10
SLIDE 10

Time for phase 1: k

k

possible derivations For grammar with rules

k

Time for phase 2: 2

k

possible derivations 2

k

Time for phase : | | 2 w

k

possible derivations | | 2 w

k | | 2 w

Total time needed for string :

w

| | 2 2 w

k k k + + + !

Extremely bad!!!

phase 1 phase 2 phase 2|w|

slide-11
SLIDE 11

For general context-free grammars: There exists a parsing algorithm that parses a string in time

| | w

3

| | 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