1 / 10

Operator precedence and AST’s

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,

swett
Download Presentation

Operator precedence and AST’s

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operator precedence and AST’s Module 06.2COP4020 – 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 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.

  4. 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

  5. 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

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

  7. 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

  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 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”

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

More Related