Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator precedence and AST’s

Similar presentations


Presentation on theme: "Operator precedence and AST’s"— Presentation transcript:

1 Operator precedence and AST’s
Module 06.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

2 topics Operator precedence and associativity Building Derivation Trees
Abstract Syntax Trees (ASTs) String-to-Tree Transduction

3 Operator precedence Let’s build a CFG for expressions consisting of:
E → E + T E consists of T's, → E - T separated by –’s and +'s → T (lowest precedence). T → F * T T consists of F's, → F / T separated by *'s and /'s → F (next precedence). F → - F F consists of a single P, → + F preceded by +'s and -'s. → P (next precedence). P → '(' E ')' P consists of (E), → i or a single i (highest precedence). Let’s build a CFG for expressions consisting of: elementary identifier i. + and - (binary ops) have lowest precedence, and are left associative . * and / (binary ops) have middle precedence, and are right associative. + and - (unary ops) have highest precedence, and are right associative.

4 Operator Precedence and Associativity
The lower in the grammar, the higher the precedence. Operator Associativity: Tie breaker for precedence. Left recursion in the grammar means left associativity of the operator, left branching in the tree. Right recursion in the grammar means right associativity of the operator, right branching in the tree.

5 Building Derivation Trees
Sample Input : - + i - i * ( i + i ) / i + i (Human) derivation tree construction: Bottom-up. First pass: scan entire expression, process operators with highest precedence (parentheses are highest). Each subsequent pass: process operators on next level. Lowest precedence operators are last, at the top of tree.

6 E → E + T → E - T → T T → F * T → F / T → F F → - F → + F → P P → '(' E ')' → i

7 The abstract syntax tree (AST)
E → E + T => + → E -T => - → T T → F * T => * → F / T => / → F F → - F => neg → + F => + → P P → '(' E ')' → i => i AST is a condensed version of the derivation tree. No noise (intermediate nodes). String-to-tree transduction grammar: rules of the form A → ω => 's'. Build 's' tree node, with one child per tree from each nonterminal in ω.

8 The abstract syntax tree (AST)
E → E + T => + → E -T => - → T T → F * T => * → F / T => / → F F → - F => neg → + F => + → P P → '(' E ')' → i => i Sample Input : - + i - i * ( i + i ) / i + i

9 String-to-Tree Transduction
T → F * T => * → F / T => / → F F → - F => neg → + F => + → P P → '(' E ')' → i => i Transduce from vocabulary of input symbols, to vocabulary of tree node names. Could eliminate construction of unary + node, anticipating semantics. F → - F => neg → + F // no more unary + node → P A “string-to-tree transduction grammar ”, or “Syntax-Directed Translation Scheme”

10 summary Operator precedence and associativity
Building Derivation Trees Abstract Syntax Trees (ASTs) String-to-Tree Transduction


Download ppt "Operator precedence and AST’s"

Similar presentations


Ads by Google