semantic analysis
play

SEMANTIC ANALYSIS PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh - PowerPoint PPT Presentation

SEMANTIC ANALYSIS PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/28 PROGRAM TRANSLATION FLOW CHART code improvement Symbol table other intermediate form Abstract syntax tree or Back end target language


  1. SEMANTIC ANALYSIS PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/28

  2. PROGRAM TRANSLATION FLOW CHART code improvement Symbol table other intermediate form Abstract syntax tree or Back end target language Modified (e.g., assembly) Target language intermediate form Modified Machine-specific Scanner (lexical analysis) Target code generation code improvement Machine-independent Front end Parse tree Token stream (Character stream) Source program code generation Semantic analysis and Parser (syntactic analysis) 2/28

  3. ROAD MAP • Syntax, semantics, and semantic analysis • Attribute grammars • Action routines • Abstract syntax trees 3/28

  4. ROAD MAP • Syntax, semantics, and semantic analysis • Attribute grammars • Action routines • Abstract syntax trees 3/28

  5. SYNTAX AND SEMANTICS Syntax • Describes form of a valid program • Can be described by a context-free grammar Semantics • Describes meaning of a program • Cannot be described by a context-free grammar Some constraints that may appear syntactic are enforced by semantic analysis! Example: Use of identifier only after its declaration 4/28

  6. SYNTAX AND SEMANTICS Syntax • Describes form of a valid program • Can be described by a context-free grammar Semantics • Describes meaning of a program • Cannot be described by a context-free grammar Some constraints that may appear syntactic are enforced by semantic analysis! Example: Use of identifier only after its declaration 4/28

  7. SYNTAX AND SEMANTICS Syntax • Describes form of a valid program • Can be described by a context-free grammar Semantics • Describes meaning of a program • Cannot be described by a context-free grammar Some constraints that may appear syntactic are enforced by semantic analysis! Example: Use of identifier only after its declaration 4/28

  8. SEMANTIC ANALYSIS Semantic analysis • Enforces semantic rules • Builds intermediate representation (e.g., abstract syntax tree) • Fills symbol table • Passes results to intermediate code generator Two approaches • Interleaved with syntactic analysis • As a separate phase Formal mechanism • Attribute grammars 5/28

  9. SEMANTIC ANALYSIS Semantic analysis • Enforces semantic rules • Builds intermediate representation (e.g., abstract syntax tree) • Fills symbol table • Passes results to intermediate code generator Two approaches • Interleaved with syntactic analysis • As a separate phase Formal mechanism • Attribute grammars 5/28

  10. SEMANTIC ANALYSIS Semantic analysis • Enforces semantic rules • Builds intermediate representation (e.g., abstract syntax tree) • Fills symbol table • Passes results to intermediate code generator Two approaches • Interleaved with syntactic analysis • As a separate phase Formal mechanism • Attribute grammars 5/28

  11. ENFORCING SEMANTIC RULES Static semantic rules • Enforced by compiler at compile time • Example: Do not use undeclared variable. Dynamic semantic rules • Compiler generates code for enforcement at runtime. • Examples: Division by zero, array index out of bounds • Some compilers allow these checks to be disabled. 6/28

  12. ENFORCING SEMANTIC RULES Static semantic rules • Enforced by compiler at compile time • Example: Do not use undeclared variable. Dynamic semantic rules • Compiler generates code for enforcement at runtime. • Examples: Division by zero, array index out of bounds • Some compilers allow these checks to be disabled. 6/28

  13. ROAD MAP • Syntax, semantics, and semantic analysis • Attribute grammars • Action routines • Abstract syntax trees 7/28

  14. ROAD MAP • Syntax, semantics, and semantic analysis • Attribute grammars • Action routines • Abstract syntax trees 7/28

  15. ATTRIBUTE GRAMMARS Attribute grammar An augmented context-free grammar: • Each symbol in a production has a number of attributes. • Each production is augmented with semantic rules that • Copy attribute values between symbols, • Evaluate attribute values using semantic functions, • Enforce constraints on attribute values. 8/28

  16. E 1 val add E 2 val T val E 1 val sub E 2 val T val T 1 val mul T 2 val F val T 1 val div T 2 val F val F 1 val neg F 2 val F const val T 1 T 2 F T F F val F val T val E val F 1 const F 2 F E F val ATTRIBUTE GRAMMAR: EXAMPLE T 2 F T E 1 T E 1 E 2 E 2 E T E val T val T 1 9/28 E → E + T E → E − T E → T T → T ∗ F T → T / F T → F F → − F F → ( E ) F → const

  17. ATTRIBUTE GRAMMAR: EXAMPLE 9/28 E → E + T E 1 → E 2 + T { E 1 . val = add ( E 2 . val , T . val ) } E → E − T E 1 → E 2 − T { E 1 . val = sub ( E 2 . val , T . val ) } E → T E → T { E . val = T . val } T → T ∗ F T 1 → T 2 ∗ F { T 1 . val = mul ( T 2 . val , F . val ) } T → T / F T 1 → T 2 / F { T 1 . val = div ( T 2 . val , F . val ) } T → F T → F { T . val = F . val } F → − F F 1 → − F 2 { F 1 . val = neg ( F 2 . val ) } F → ( E ) F → ( E ) { F . val = E . val } F → const F → const { F . val = const . val }

  18. SYNTHESIZED AND INHERITED ATTRIBUTES Synthesized attributes Attributes of LHS of production are computed from attributes of RHS of production. Inherited attributes Attributes flow from left to right: • From LHS to RHS, • From symbols on RHS to symbols later on the RHS. 10/28

  19. SYNTHESIZED AND INHERITED ATTRIBUTES Synthesized attributes Attributes of LHS of production are computed from attributes of RHS of production. Inherited attributes Attributes flow from left to right: • From LHS to RHS, • From symbols on RHS to symbols later on the RHS. 10/28

  20. A 2 a A 1 count A 2 count B 2 b B 1 count B 2 count C 2 c C 1 count C 2 count SYNTHESIZED ATTRIBUTES: EXAMPLE c 1 C 1 C count 1 C 1 1 B 1 B B count C count is not context-free but can be defined using an attribute grammar: S A B C Condition: A count B count A b a A count 1 A 1 1 The language 11/28 L = { a n b n c n | n > 0 } = { abc , aabbcc , aaabbbccc , . . . }

  21. SYNTHESIZED ATTRIBUTES: EXAMPLE The language 11/28 is not context-free but can be defined using an attribute grammar: L = { a n b n c n | n > 0 } = { abc , aabbcc , aaabbbccc , . . . } S → A B C { Condition: A . count = B . count = C . count } A → a { A . count = 1 } A 1 → A 2 a { A 1 . count = A 2 . count + 1 } B → b { B . count = 1 } B 1 → B 2 b { B 1 . count = B 2 . count + 1 } C → c { C . count = 1 } C 1 → C 2 c { C 1 . count = C 2 . count + 1 }

  22. SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1) 2 C S 1 2 3 1 3 C 1 2 3 3 3 3 c c Input: aaabbbccc A Parse tree: A a a A a B c b b B b B C 12/28

  23. SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1) 2 C S 1 2 3 1 3 C 1 2 3 3 3 3 c c Input: aaabbbccc A Parse tree: A a a A a B c b b B b B C 12/28

  24. SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1) 2 C S 1 2 3 1 3 C 1 2 3 3 3 3 c c Input: aaabbbccc A Parse tree: A a a A a B c b b B b B C 12/28

  25. SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1) c 3 2 1 3 2 1 3 2 1 S C c C c Input: aaabbbccc a Parse tree: A a a A A C B b b B b B 12/28 3 = 3 = 3

  26. SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2) c 3 2 3 3 2 1 2 1 3 2 1 S C C Input: aaabbccc c c C B b b B A a A a a A Parse tree: 13/28

  27. SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2) c 3 2 3 3 2 1 2 1 3 2 1 S C C Input: aaabbccc c c C B b b B A a A a a A Parse tree: 13/28

  28. SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2) c 3 2 3 3 2 1 2 1 3 2 1 S C C Input: aaabbccc c c C B b b B A a A a a A Parse tree: 13/28

  29. SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2) Input: aaabbccc 3 2 1 2 1 3 2 1 S C c C c c C B b b B A a A a a A Parse tree: 13/28 3 ̸ = 2 ̸ = 3

  30. A 2 a A 1 count A 2 count B 2 b B 2 inhCount B 1 inhCount C 2 c C 2 inhCount C 1 inhCount INHERITED ATTRIBUTES: EXAMPLE 1 c C 1 Condition C inhCount 1 C 1 1 B 1 Condition B inhCount A S A B C B inhCount A count C inhCount A count a Again, we consider the language A count 1 A 1 1 B b 14/28 L = { a n b n c n | n > 0 } = { abc , aabbcc , aaabbbccc , . . . } .

  31. INHERITED ATTRIBUTES: EXAMPLE Again, we consider the language 14/28 L = { a n b n c n | n > 0 } = { abc , aabbcc , aaabbbccc , . . . } . S → A B C { B . inhCount = A . count ; C . inhCount = A . count } A → a { A . count = 1 } A 1 → A 2 a { A 1 . count = A 2 . count + 1 } B → b { Condition : B . inhCount = 1 } B 1 → B 2 b { B 2 . inhCount = B 1 . inhCount − 1 } C → c { Condition : C . inhCount = 1 } C 1 → C 2 c { C 2 . inhCount = C 1 . inhCount − 1 }

  32. INHERITED ATTRIBUTES: PARSE TREE DECORATION (1) 2 S 1 2 3 3 3 2 c 1 1 1 1 1 1 C C Input: aaabbbccc A Parse tree: A a a A a B c b b B b B C c 15/28

  33. INHERITED ATTRIBUTES: PARSE TREE DECORATION (1) 2 S 1 2 3 3 3 2 c 1 1 1 1 1 1 C C Input: aaabbbccc A Parse tree: A a a A a B c b b B b B C c 15/28

  34. INHERITED ATTRIBUTES: PARSE TREE DECORATION (1) 2 S 1 2 3 3 3 2 c 1 1 1 1 1 1 C C Input: aaabbbccc A Parse tree: A a a A a B c b b B b B C c 15/28

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend