100 likes | 152 Views
Operator precedence and AST’s. Module 06.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez. topics. Operator precedence and associativity Building Derivation Trees Abstract Syntax Trees (ASTs) String-to-Tree Transduction. Operator precedence. E → E + T E consists of T's,
E N D
Operator precedence and AST’s Module 06.2COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez
topics • Operator precedence and associativity • Building Derivation Trees • Abstract Syntax Trees (ASTs) • String-to-Tree Transduction
Operator precedence E → E + TE consists of T's, → E - Tseparated by –’s and +'s → T(lowest precedence). T → F * TT consists of F's, → F / Tseparated by *'s and /'s → F(next precedence). F → -FF consists of a single P, → + Fpreceded by +'s and -'s. → P(next precedence). P → '(' E ')'P consists of (E), → ior 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.
Operator precedence: 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. Operator Precedence and Associativity
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. Building Derivation Trees
E → E + T → E -T → T T → F * T → F / T → F F → -F → + F → P P → '(' E ')' → 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 ω. 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
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
String-to-Tree Transduction E → E + T => + → E -T => - → T 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”
summary • Operator precedence and associativity • Building Derivation Trees • Abstract Syntax Trees (ASTs) • String-to-Tree Transduction