29
CFG 1 CSC 8505 Compiler Construction Context-Free Grammars Using grammars in parsers

CSC 8505 Compiler Construction Context-Free Grammars

  • Upload
    garran

  • View
    69

  • Download
    0

Embed Size (px)

DESCRIPTION

CSC 8505 Compiler Construction Context-Free Grammars. Using grammars in parsers. Parsing Process. Call the scanner to get tokens Build a parse tree from the stream of tokens A parse tree shows the syntactic structure of the source program. - PowerPoint PPT Presentation

Citation preview

Page 1: CSC 8505 Compiler Construction Context-Free Grammars

CFG 1

CSC 8505Compiler Construction

- Context Free Grammars Using grammars in parsers

Page 2: CSC 8505 Compiler Construction Context-Free Grammars

CFG 2

Parsing Process Call the scanner to get tokens Build a parse tree from the stream of to

kens A parse tree shows the syntactic structure

of the source program. Add information about identifiers in the

symbol table Report error, when found, and recover f

rom thee error

Page 3: CSC 8505 Compiler Construction Context-Free Grammars

CFG 3

Grammar a tuple ( V, T, P, S ) where

V is a finite set of nonterminals, containing S, T is a finite set of terminals, P is a set of production rules in the form of β wh

ere and β are strings over V U T , and S is the start symbol.

Example G= ({S, A, B, C}, {a, b, c}, P, S)

P= { SSABC, BA AB, CB BC, CA AC, SA a, aA aa, aB ab, bB bb, bC bc, cC cc}

Page 4: CSC 8505 Compiler Construction Context-Free Grammars

CFG 4

- Context Free Grammar a tuple ( V, T, P, S ) where

V is a finite set of nonterminals, containingS,

T is a finite set of terminals, P is a set of production rules in the form of

β where is in V and β is in (V U T )*, and S is the start symbol.

Any string in (V U T)* is called a sententi al form.

Page 5: CSC 8505 Compiler Construction Context-Free Grammars

CFG 5

Examples E E O E E (E) E id O + O - O * O /

S SS S (S)S S () S

Page 6: CSC 8505 Compiler Construction Context-Free Grammars

CFG 6

- Backus Naur Form (BNF) Nonterminals are in < >. Terminals are any other symbols. ::= means . | means or. Examples:

<E> ::= <E><O><E>| (<E>) | ID <O> ::= - + | | * | /

<S> ::= <S><S> | (<S>)<S> | () |

Page 7: CSC 8505 Compiler Construction Context-Free Grammars

CFG 7

Derivation A sequence of replacement of a substri

ng in a sentential form.Definition Let G = (V , T , P , S ) be a CFG, , , be

strings in (V ( T)* and A is in V.A G if A is in P.

*G denotes a derivation in zero step ormore.

Page 8: CSC 8505 Compiler Construction Context-Free Grammars

CFG 8

Examples S SS | (S)S | () | S

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

E E O E | (E) | id O - *+ | | | / E

E O E (E) O E (E O E) O E* ((E O E) O E) O E ((id O E)) O E) O E ((id + E)) O E) O E ((id + id)) O E) O E * ((id + id)) * id) + id

Page 9: CSC 8505 Compiler Construction Context-Free Grammars

CFG 9

Leftmost Derivation Rightmost Derivation Each step of the derivati

on is a replacement of t he leftmost nonterminal

s in a sentential form.E E O E (E ) O E (E O E) O E (id O E) O E (id + E ) O E (id + id) O E (id + id) * E (id + id) * id

Each step of the derivati on is a replacement of th

e rightmost nonterminal s in a sentential form.

E E O E E O id E * id (E *) id (E O E *) id (E O *id) id (E (( + ) * (id + id) * id

Page 10: CSC 8505 Compiler Construction Context-Free Grammars

CFG 10

Language Derived from Grammar Let G = (V , T , P , S ) be a CFG. A string w in T * is derived from G if S *

Gw. A language generated by G , denoted by

L(G ), is a set of strings derived from G. L(G) = {w| S *

Gw}.

Page 11: CSC 8505 Compiler Construction Context-Free Grammars

CFG 11

Right/Left Recursive A grammar is a left r

ecursive if its produc tion rules can gener

ate a derivation of th e form A * A X.

Examples: E E O id | (E) | id E F + id | (E) | id

F E * id | id E F + id

E * id + id

A grammar is a rightrecursive if its produ

ction rules can gener ate a derivation of th

e form A * X A. Examples:

E id O E | (E) | id E id + F | (E) | id

F id * E | id E id + F

id + id * E

Page 12: CSC 8505 Compiler Construction Context-Free Grammars

CFG 12

Parse Tree A labeled tree in which

the interior nodes are labeled by nonterminals

leaf nodes are labeled by terminals the children of an interior node represent a

replacement of the associated nonterminal in a derivation

corresponding to a derivationE(id

EF

*id i

d

Page 13: CSC 8505 Compiler Construction Context-Free Grammars

CFG 13

Parse Trees and Derivations E E + E 1( ) id + E (2 ) id + E * E (3 ) id + id * E (4 ) id + id * id (5 )

E E + E 1( ) E + E * E 2( ) E + E * id 3( ) E + id * id 4( ) id + id * id 5( )

Preorder numbering

Reverse or postorder numbering

(

E 1

E 2 E 5

E 3

* E4

id

id

id

(

E 1

E 5 E 3

E 2

* E4

id

id

id

Page 14: CSC 8505 Compiler Construction Context-Free Grammars

CFG 14

Grammar: ExampleList of parameters in: Function definition

function sub(a,b,c) Function call

sub(a,1,2)

<Fdef> function id ( <argList> )<argList> id , <arglist> | id<Fcall> id ( <parList> )<parList> <par> ,<parlist>| <par><par> id | const

<Fdef> function id ( <argList> )<argList> <arglist> , id | id<Fcall> id ( <parList> )<parList> <parlist> ,<par>| <par><par> id | const

<argList> id , <arglist> id, id , <arglist> … (id ,)* id<argList> <arglist> , id<arglist> , id, id … id (, id )*

Page 15: CSC 8505 Compiler Construction Context-Free Grammars

CFG 15

Grammar: ExampleList of parameters If zero parameter is

allowed, then ?

<Fdef> function id ( <argList> )| function id ( )

<argList> id , <arglist> | id<Fcall> id ( <parList> ) | id ( )<parList> <par> ,<parlist>| <par><par> id | const

<Fdef> function id ( <argList> )<argList> id , <arglist> | id | <Fcall> id ( <parList> )<parList> <par> ,<parlist>| <par><par> id | const

Work ?

NO! Generateid , id , id ,

Page 16: CSC 8505 Compiler Construction Context-Free Grammars

CFG 16

Grammar: ExampleList of statements: No statement One statement:

s; More than one

statement: s; s; s;

A statement can be a block of statements. {s; s; s;}

Is the following correct?{ {s; {s; s;} s; {}} s; }

<St> ::= | s; | s; <St> | { <St> } <St><St> { <St> } <St> { <St> } { { <St> } <St>} { { <St> } s; <St>} { { <St> } s; } { { s; <St> } s;} { { s; { <St> } <St> } s;} { { s; { <St> } s; <St> } s;} { { s; { <St> } s; { <St> } <St> } s;} { { s; { <St> } s; { <St> } } s;} { { s; { <St> } s; {} } s;} { { s; { s; <St> } s; {} } s;} { { s; { s; s;} s; {} } s;}

Page 17: CSC 8505 Compiler Construction Context-Free Grammars

CFG 17

Abstract Syntax Tree Representation of actual source tokens Interior nodes represent operators. Leaf nodes represent operands.

Page 18: CSC 8505 Compiler Construction Context-Free Grammars

CFG 18

Abstract Syntax Tree for Expression

( E

E E

E

*E

id3

id2

id1

(

id1id3

*id2

Page 19: CSC 8505 Compiler Construction Context-Free Grammars

CFG 19

Abstract Syntax Tree for If Statement

st

ifStatement

)exptrue

(if

else st

st elsePart

return

if

true

st return

Page 20: CSC 8505 Compiler Construction Context-Free Grammars

CFG 20

Ambiguous Grammar A grammar is ambiguous if it can gener

ate two different parse trees for one string.

Ambiguous grammars can cause incons istency in parsing.

Page 21: CSC 8505 Compiler Construction Context-Free Grammars

CFG 21

Example: Ambiguous Grammar E E + E E - E E E E * E E E / E E ((

+

E

E

E

E

*E

id3

id2

id1

*

E

E

id2

E

E

+E

id1

id3

Page 22: CSC 8505 Compiler Construction Context-Free Grammars

CFG 22

Ambiguity in Expressions Which operation is to be done first?

solved by precedence An operator with higher precedence is done bef

ore one with lower precedence. An operator with higher precedence is placed in

a rule (logically) further from the start symbol. solved by associativity

- -If an operator is right associative (or left associa tive), an operand in between2 operators is asso ciated to the operator to the right (left).

- Right associated : W + ( X+(Y+Z)) ((((-(((((((((( ( ((( ( (( ( (( ( (

Page 23: CSC 8505 Compiler Construction Context-Free Grammars

CFG 23

Precedence E E + E E - E E E E * E E E / E E id E E + E E - E E E F F F * F

F F / FF id

+

E

E

E

E

*E

id3

id2

id1

*

E

E

id2

E

E

+E

id1

id3

E

+ E E

*F

id3id2

id1 F

F F

Page 24: CSC 8505 Compiler Construction Context-Free Grammars

CFG 24

Precedence (cont’d) E - E + E | E E | F F F * F | F / F | X X ( E ) | id

(id1 + id2) * id3* id4

* F

X

F

+E id4

id3

E

X

E X

E

F

) (

*F F

id1

X

F

id2

X

F

Page 25: CSC 8505 Compiler Construction Context-Free Grammars

CFG 25

Associativity - Left associative operato

rs E - E + F | E F | F F F * X | F / X | X X ( E ) | id

(id1 + id2) * id3 / id4 = (( (id1 + id2) * id3) / id4)

/ F X

(E

id3 X E

E F

) (

*F X

id1XF

id4

XF

id2

Page 26: CSC 8505 Compiler Construction Context-Free Grammars

CFG 26

Ambiguity in Dangling Else St IfSt | ...

IfSt if ( E ) St | if ( E ) St else St E 0 | 1 | …

IfSt

)if ( elseE StSt

IfSt

)if ( E St

0

1

0 IfSt

)if ( elseE StSt

IfSt

)if ( E St

1

0{ if ( ) { (1) }if St

SS S} 0{ if ( ) 1{ if ( ) St else St } }

Page 27: CSC 8505 Compiler Construction Context-Free Grammars

CFG 27

Disambiguating Rules for Dangling El se

St MatchedSt | UnmatchedSt

UnmatchedSt if (E) St | if (E) MatchedSt else Unmatched

St MatchedSt

if (E) MatchedSt else MatchedSt| ...

E 0 | 1

if (0) if (1) St else St= if (0)

if (1) St else St

UnmatchedSt

)if ( E

MatchedSt

)if ( elseE MatchedStMatchedSt

St

St

Page 28: CSC 8505 Compiler Construction Context-Free Grammars

CFG 28

- Extended Backus Naur Form (EBNF) Kleene’s Star/ Kleene’s Closure

Seq ::= St {; St} Seq ::= {St ;} St

Optional Part IfSt ::= if ( E ) St [else St] - E ::= F [+ E ] | F [ E]

Page 29: CSC 8505 Compiler Construction Context-Free Grammars

CFG 29

Syntax Diagrams Graphical representation of EBNF rules

nonterminals: terminals: sequences and choices:

Examples X ::= ( E) | id

Seq ::= {St ;} St

E ::= F [+ E ]

IfStid

( )Eid

StSt;

F ( E