Understanding Abstract Syntax and Inductive Definitions in Programming Languages

abstract syntax n.w
1 / 20
Embed
Share

Explore the concept of abstract syntax and inductive definitions in programming languages, focusing on the representation of programs as trees and the definition of language features through induction. Learn about context-free languages, expression definitions, and mathematical induction in this informative content.

  • Programming Languages
  • Abstract Syntax
  • Inductive Definitions
  • Context-Free Languages
  • Mathematical Induction

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Abstract Syntax Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc20.html 1

  2. Abstract Syntax Intermediate program representation Defines a tree - Preserves program hierarchy Generated by the parser Declared using an (ambiguous) context free grammar (relatively flat) Not meant for parsing Keywords and punctuation symbols are not stored (Not relevant once the tree exists) 2

  3. Topics Inductive Definitions Context Free Languages Example: Expressions 3

  4. Inductive Definitions Many programming langue features are defined by induction 4

  5. Induction in Programming Languages The syntax of programing languages is inductively defined A number is expression If e1 and e2 are expressions to so is e1 + e2 Types are inductively defined int is a type if t1, t2, , tk are types then struct { t1 i1; t2 i2; , tk ik;} is a type where i1, i2, , ik are identifiers Recursive functions fac(n) = if n = 1 then 1 else n * fac(n-1) fac1=1, facn=n*facn-1

  6. Mathematical Induction P(n) is a property of natural number n To show that P(n) holds for every n, it suffices to show that: P(0) is true If P(m) is true then P(m+1) is true for every number m In logic (P(0) m N. P(m) P(m+1)) n N. P(n) P(0) P(m) P(m+1) 0 m+1 m

  7. Course of values Induction P(n) is a property of natural number n To show that P(n) holds for every n, it suffices to show that for all m if for all k <m, P(k) holds then P(m) In logic m N. ( k < m. P(k)) P(m)) n N. P(n) P(m) P(0) P(1) P(k) 0 1 m k

  8. Context Free Languages Inductively define a set of words Terminals Non-Terminals Start Non-terminal Rules Non-Terminal Var1 Var2 Varn 8

  9. Expression Definition (take 1) exp id exp num exp exp Binop exp // binary expression exp Unop exp // unary expression Binop + Binop - Binop * Unop - 9

  10. Questions How to show that x + 5 * y is an expression How to show that v v is not an expression for any v? 10

  11. x + 5 * y L(exp) exp exp Binop exp + id x exp Binop exp num id y * 5 11

  12. v v L(exp) 12

  13. Ambiguous Context Free Grammars Two syntax trees [Two leftmost/rightmost derivations] 13

  14. Ambiguity in Expressions exp exp Binop exp Binop exp exp exp exp Binop exp Binop exp exp

  15. Non-Ambiguous Expression Grammar exp term exp exp + term exp exp - term factor -factor factor id factor num factor (exp) term factor term term * factor 15

  16. Non-Ambiguous Expression Grammar (BNF) exp term | exp + term | exp - term term factor | term term * factor factor -factor | id | num | (exp) 16

  17. Abstract Syntax Tree Intermediate program representation Not meant for parsing Hides irrelevant details Defined by an ambiguous grammar 17

  18. Abstract Syntax for Arithmetic Expressions Exp id Exp num Exp Exp Binop Exp (IdExp) (NumExp) (BinOpExp) Exp Unop Exp (UnOpExp) Binop + Binop - (Plus) (Minus) Binop * (Times) Binop / Unop - (Div) (UnMin) 18

  19. package Absyn; abstract public class Absyn { public int pos ;} Exp extends Absyn {} ; class IdExp extends Exp { String rep ; IdExp(r) { rep = r ;} } class NumExp extends Exp { int number ; NumExp(int n) { number = n ;} } class OpExp { public final static int PLUS=1; public final static int Minus=2; public final static int Times=3; public final static int Div=4; } final static int OpExp.PLUS, OpExp.Minus, OpExp.Times, OpExp.Div; class BinExp extends Exp { Exp left, right; OpExp op ; BinExp(Exp l, OpExp o, Bin Exp r) { left = l ; op = o; right = r ; } } 19

  20. Summary Abstract syntax provides a clear interface with other compiler phases Supports general programming languages Can be stored for large programs Automatically generated during parsing But the design of an abstract syntax for a given PL may take some time 20

More Related Content