Parse Trees Applications of the Tree Structure Creative

  • Slides: 19
Download presentation
Parse Trees Applications of the Tree Structure Creative Commons License – Curt Hill.

Parse Trees Applications of the Tree Structure Creative Commons License – Curt Hill.

Expression Trees • We have previously seen expression trees • A parse tree is

Expression Trees • We have previously seen expression trees • A parse tree is a generalization of an expression tree • The grammar of expressions is intuitive to most of us • The grammar of languages is less intuitive Creative Commons License – Curt Hill.

Parse Trees • A parse tree is a construction that represents the parse of

Parse Trees • A parse tree is a construction that represents the parse of a sentence • Parse trees are often built by compilers and other programs that scan source code • A parse tree is one acceptable parse of this source code based on the grammar • First some definitions and background Creative Commons License – Curt Hill.

A grammar consists of • Terminals • • Keywords Constants Words Symbols or operators

A grammar consists of • Terminals • • Keywords Constants Words Symbols or operators • Non-terminals • Named constructs where the name never actually appears • Such as statements and expression • Distinguished symbol • The starting point • Usually represents whole program • Productions • Rules for going from distinguished symbol to non terminals Creative Commons License – Curt Hill.

Example • Simplified grammar for expression – Terminals: • Constant • Ident • +-()*/

Example • Simplified grammar for expression – Terminals: • Constant • Ident • +-()*/ – Productions in EBNF: • • Expression : : = Term + term Expression : : = term - term Expression : : = term Term : : = Factor [ { * / } factor ] Factor : : = Constant Factor : : = Ident Factor : : = ( Expression ) Creative Commons License – Curt Hill.

Productions Again • The productions are all replacement rules • They may also be

Productions Again • The productions are all replacement rules • They may also be visually denoted in terms of syntax graphs • These are usually as easy to understand but represent same information Creative Commons License – Curt Hill.

Syntax Graphs • A circle represents a terminal – Reserved word or operator –

Syntax Graphs • A circle represents a terminal – Reserved word or operator – No further definition • A rectangle represents a non-terminal – For statement or expression – Must be defined else where • An arrow represents the path between one item and another – The arrows may branch indicating alternatives • Recursion is also allowed Creative Commons License – Curt Hill.

Simple Expressions expression term + - term factor * / factor constant ident (

Simple Expressions expression term + - term factor * / factor constant ident ( expression Creative Commons License – Curt Hill. )

Parse tree example • In a parse tree – Leaves are terminals – Nodes

Parse tree example • In a parse tree – Leaves are terminals – Nodes are non-terminals • Usually evaluated from bottom to top • Consider the parse of: 2+5*(3 -4) Creative Commons License – Curt Hill.

Expression: 2 + 5 * (3 – 4) expression term factor 2 term +

Expression: 2 + 5 * (3 – 4) expression term factor 2 term + factor 5 factor * ) ( expression term - term factor 3 4 Creative Commons License – Curt Hill.

Notes • Parse trees are partially built by the parser of the compiler •

Notes • Parse trees are partially built by the parser of the compiler • Then used by the code generator • Once used the sub-trees are discarded • Whole tree never exists Creative Commons License – Curt Hill.

C Subset as an Example • V – set of non-terminals – Statement –

C Subset as an Example • V – set of non-terminals – Statement – Declaration – For-statement • T – set of terminals – Reserved words – Punctuation – Identifiers Creative Commons License – Curt Hill.

C example again • S – Start symbol – Independently compilable part – Program

C example again • S – Start symbol – Independently compilable part – Program – Function – Constant • P – set of productions – Rewrite rules – Start at the start symbol – End at terminals Creative Commons License – Curt Hill.

C For Production • For-statement for ( expression; expression) statement • This contains the

C For Production • For-statement for ( expression; expression) statement • This contains the terminals: – For ( ; ) • Non-terminals – Expression – Statement Creative Commons License – Curt Hill.

Productions Again • Each non-terminal should have one or more productions that define it

Productions Again • Each non-terminal should have one or more productions that define it – Every non-terminal must have one or more productions • Multiple productions usually signify alternation • Recursion is allowed Creative Commons License – Curt Hill.

Recursion • • Productions may be recursive Recall for-statement, here is Statement expression ;

Recursion • • Productions may be recursive Recall for-statement, here is Statement expression ; Statement for-statement ; Statement if-statement ; Statement while-statement ; Statement compound-statement Etc. Creative Commons License – Curt Hill.

For Example • Suppose the following statement – for(i=0; i<5; i++) cout << i

For Example • Suppose the following statement – for(i=0; i<5; i++) cout << i << ‘n’; • What would the parse tree look like? Creative Commons License – Curt Hill.

Part of Tree For-statement for statement ( ) expr ; Creative Commons License –

Part of Tree For-statement for statement ( ) expr ; Creative Commons License – Curt Hill. ;

Commentary • The three expressions and the statement would need sub-trees • This shows

Commentary • The three expressions and the statement would need sub-trees • This shows that parse trees can be Nway better than the expression trees did Creative Commons License – Curt Hill.