Plan for Today Context Free s models for specifying programming languages syntax semantics example grammars derivations Parse trees yntax-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? DF 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 a recursive definition mechanism: : ( ) C453 Lecture Context Free Intro 1 C453 Lecture Context Free Intro 2 Context Free s CFG: set of productions of the form yntax and emantics Regular Expressions define what correct tokens are 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 Context Free s define what correctly formed programs are ut are all correctly formed programs meaningful? Example: stmt! iftmt whiletmt iftmt! IF OPEN boolexpr CLOE tmt whiletmt! WHILE OPEN boolexpr CLOE tmt C453 Lecture Context Free Intro 3 C453 Lecture Context Free Intro 4
yntax and emantics Our Next Class of Languages Regular Expressions define what correct tokens are Context Free s define what correctly formed programs are ut 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 Context-Free Languages R { a n b n } { ww } Regular Languages a *b* (a b)* C453 Lecture Context Free Intro 5 Context-Free Languages Example Context-Free s Recursive definitions We will start here Pushdown utomata F + stack context-free grammar G: ab derivation: ab aabb aabb nother derivation: ab aabb aaabbb aaabbb
n pplication of this Language Deriving another grammar L(G) = ab Describes parentheses: n n { a b : n 0} (((( )))) Gave a grammar for: Context-Free Languages n b n Can we derive a for: { a } R { ww } Regular Languages context-free grammar G : derivation: Example aa bb aa abba abba Representing ll Properly Nested Parentheses L(G) = ab n n { a b : n 0} nother derivation: aa abba abaaba abaaba Describes parentheses: (((( )))) Can we build a grammar to include any valid combination of ( )? For example ( ( ) ( ( ) ) )
Possible context-free grammar G : derivation: () () () () nother derivation: () () ()() ()() Context-Free s Nonterminals G = ( V, T,, P) Terminals tart symbol Productions of the form: Nonterminal x tring of symbols, Nonterminals and terminals Derivation, Language : G=(V,T,,P) Derivation: tart with start symbol Keep replacing non-terminals by their RH 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 Derivation Order Given a grammar with rules: 1. 2. aa 4. b 3. 5. 1 lways expand the leftmost non-terminal Leftmost derivation: 2 3 aa aa aab aab 4 5 C453 Lecture Context Free Intro 15
Derivation Order tring Given a grammar with rules: 1. 2. aa 3. lways expand the rightmost non-terminal 4. b 5. tm --> id := Exp Exp --> num Exp --> ( tm, Exp ) Leftmost derivation: a := ( b := ( c := 3, 2 ), 1 ) tm ==> a := Exp ==> a := ( tm, Exp ) ==> a := ( b := Exp, Exp ) ==> a := ( b := ( tm, 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) Rightmost derivation: 1 4 5 b b aab aab 2 3 Rightmost derivation: tm ==> a := Exp ==> a := ( tm, Exp ) ==> a := ( tm, 1) ==> Parse Trees aa b Parse Trees aa b a a aa
Parse Trees aa b aa aab Parse Trees aa b aa aab aab a a b a a b Parse Trees aa b aa aab aab aab a a b yield aab = aab entential forms aa b Partial parse tree
aa Partial parse tree a a sentential form ometimes, derivation order doesn t matter Leftmost: aa aa aab aab Rightmost: ame parse tree b b aab aab a a b Does it matter here? Parse Tree How about here? tm --> id := Exp Exp --> num Exp --> ( tm, Exp ) tring a := ( b := ( c := 3, 2 ), 1 ) (1) exp --> exp * exp (2) exp --> exp + exp (3) exp --> NUM tring 42 + 7 * 6 What does this question mean? C453 Lecture Context Free Intro 27 C453 Lecture Context Free Intro 28
yntax 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 Interpret this program tm --> id := Exp Exp --> num Exp --> ( tm, Exp ) tring a := ( b := ( c := 3, 2 ), 1 ) Parse Tree C453 Lecture Context Free Intro 29 C453 Lecture Context Free Intro 30 How about here? emantic Rules for Expression Example (1) exp --> exp * exp (2) exp --> exp + exp (3) exp --> NUM tring 42 + 7 * 6 C453 Lecture Context Free Intro 31 C453 Lecture Context Free Intro 32