Plan for Today Regular Expressions: repetition and choice Context - - PowerPoint PPT Presentation

plan for today regular expressions repetition and choice
SMART_READER_LITE
LIVE PREVIEW

Plan for Today Regular Expressions: repetition and choice Context - - PowerPoint PPT Presentation

Plan for Today Regular Expressions: repetition and choice Context Free Grammars let : a | b | c word : let + models for specifying programming languages syntax semantics What regular expressions cannot express:


slide-1
SLIDE 1

CS453 Lecture Context Free Grammar Intro 1

Plan for Today

Context Free Grammars

– models for specifying programming languages syntax semantics – example grammars – derivations

Parse trees Syntax-directed translation

– Used syntax-directed translation to interpret expression language

Top-down Predictive Parsing

Regular Expressions: repetition and choice

let : “a” | “b” | “c” word : let+ What regular expressions cannot express:

nesting, e.g. matching parentheses: ( ) | (( )) | ((( ))) | … to any depth Why? A DFA has only a finite # states and thus cannot encode that it has seen N “(“-s and thus now must see N “)”-s for the parentheses to match (for any N). For that we need some form of recursion:

S : “(“ S “)” | ε

CS453 Lecture Context Free Grammar Intro 2

Context Free Grammars

CFG: set of productions of the form Non-terminal  phrase | phrase | phrase … phrase: string of terminals and non-terminals terminals: tokens of the language non-terminals represent sets of strings of tokens of the language Example: stmt  ifStmt | whileStmt ifStmt  IF OPEN boolExpr CLOSE Stmt whileStmt  WHILE OPEN boolExpr CLOSE Stmt

CS453 Lecture Context Free Grammar Intro 3

Syntax and Semantics

Regular Expressions define what correct tokens are Context Free Grammars define what correctly formed programs are But… are all correctly formed programs meaningful?

CS453 Lecture Context Free Grammar Intro 4

slide-2
SLIDE 2

Syntax and Semantics

Regular Expressions define what correct tokens are Context Free Grammars define what correctly formed programs are But… are all correctly formed programs meaningful? NO: the program can have semantic errors some can be detected by the compiler: type errors, undefined errors some cannot: run-time errors, program does not compute what it is supposed to The semantics of a program defines its meaning. Here, we do syntax directed translation / interpretation

CS453 Lecture Context Free Grammar Intro 5

Our Next Class of Languages

Regular Languages

} {

n nb

a } {

R

ww

Context-Free Languages * *b a

(a | b)*

Context-Free Languages Pushdown Automata Context-Free Grammars Recursive definitions stack FSA +

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

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

S →(S) S →SS S →ε

S ⇒ SS ⇒ (S)S ⇒ ()S ⇒ ()

A context-free grammar :

G

A derivation:

A 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, Language

Grammar: G=(V,T,S,P) Derivation: Start with start symbol S Keep replacing non-terminals A by their RHS x, until no non-terminals are left The resulting string (sentence) is part of the language L(G) The Language L(G) defined by the CFG G: L(G) = the set of all strings of terminals that can be derived this way

CS453 Lecture Context Free Grammar Intro 15

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

slide-5
SLIDE 5

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

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

slide-6
SLIDE 6

aaABb aaAB AB S ⇒ ⇒ ⇒

S B A a a A B b

Parse Trees

AB S → A →aaA |ε B →Bb |ε 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 |ε

slide-7
SLIDE 7

aaAB AB S ⇒ ⇒

S B A a a A

Partial parse tree sentential form

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 doesnt matter Leftmost: Rightmost: ε ε

CS453 Lecture Context Free Grammar Intro 27

Does it matter here? a := ( b := ( c := 3, 2 ), 1 ) Stm --> id := Exp Exp --> num Exp --> ( Stm, Exp ) Grammar String Parse Tree

What does this question mean?

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

CS453 Lecture Context Free Grammar Intro 29

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

CS453 Lecture Context Free Grammar Intro 30

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 31

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 32

Semantic Rules for Expression Example

slide-9
SLIDE 9

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

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

For general context-free grammars: The exhaustive search approach is extremely costly: O(|P||w|) There exists a parsing algorithm that parses a string w in time 3

| | w

For LL(1) grammars, a simple type of CFGs that we will meet soon, we can use Predictive parsing and parse in time

| | w

slide-11
SLIDE 11

CS453 Lecture Context Free Grammar Intro 41

Example Predictive Parser: Recursive Descent

void start() { switch(m_lookahead) { case IF, WHILE: stmts(); match(Token.Tag.EOF); break; default: throw new ParseException(…); }} void stmts() { switch(this.m_lookahead) { case IF,WHILE: stmt(); stmts(); break; case EOF: break; default: throw new ParseException(…); }} void stmt() { switch(m_lookahead) { case IF: ifStmt();break; case WHILE: whileStmt(); break; default: throw new ParseException(…); }} void ifStmt() {switch(m_lookahead) { case IF: Check_next_Token(id); Check_next_Token(OPENBRACE); stmts(); Check_next_Token(CLOSEBRACE); break; default: throw new ParseException(…); }} start -> stmts EOF start -> stmts EOF stmts -> stmts -> stmt | stmt stmts | stmt stmts stmt -> ifStmt | whileStmt stmt -> ifStmt | whileStmt ifStmt -> IF id { stmts } ifStmt -> IF id { stmts } whileStmt -> WHILE id { stmts } whileStmt -> WHILE id { stmts }

Recursive Descent Parsing

Each non-terminal becomes a method that mimics the RHSs of the productions associated with it and choses a particular RHS: an alternative based on a look-ahead symbol and throws an exception if no alternative applies When does this work?

CS453 Lecture Context Free Grammar Intro 42