1 / 20

Semantic Analysis

Semantic Analysis. Wu Hang. Outline. Introduction Requirements Checking Environment How to start Tips. Introduction. Purpose Is syntactic analysis sufficient for program checking? What’s the differences between syntax and semantics ? Approach After syntactic analysis

duane
Download Presentation

Semantic Analysis

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. Semantic Analysis Wu Hang

  2. Outline • Introduction • Requirements • Checking • Environment • How to start • Tips

  3. Introduction • Purpose • Is syntactic analysis sufficient for program checking? • What’s the differences between syntax and semantics? • Approach • After syntactic analysis • What is a program?

  4. Requirements • See wiki • Refer to gcc for any semantic issues not mentioned. • Discuss when you find sth. strange. • May test many cases • Take everything into consideration

  5. Checking • Type checking • Use before declaration • Duplicate variables • Subscript checking • Pointer checking (return value, parameters, assignment, etc.) • Mismatch parameters of function or fields of record • Break/continue out of a loop • Struct checking (return value, parameters, assignment, etc.) • Etc.

  6. Environment • Mapping from a symbol to an entry • Keep all information about variables, functions and types. • VarEntry, FuncEntry, TypeEntry in one env • Table.java

  7. What is an entry • An entry keeps information about specific variable/function/type • Must exists in some environment

  8. Type checking • +-*/ • Lvalue • Return value • Make sure you have filled expr.type after checking

  9. How to start • Top down/bottom up • Deep understanding about the “program” you get after syntactic analysis • Keep in mind what you NEED to do • Try to write a program and run by gcc when you’re not sure whether it’s right or wrong.

  10. Semantic semantic = new Semantic(program); • for (inti = 0; i<program.declarationlist.size(); i++) • { • checkDeclarationlist(program.declarationlist[i]); • }

  11. void checkStmt(Stmtstmt) • { • if (stmtinstanceofIfstmt) • checkIfstmt(stmt); • … • } • Funtion Overloading

  12. Get a variable to record loop • Loop=0 • When enter a loop • Loop++; • When leave a loop • Loop--; • When there is a continue/break • If (loop>0) …

  13. Types • abstract class Type {} • • final class Void extends Type {} • • final class Char extends Type {} • • final class Int extends Type {} • • class Pointer extends Type { • Type elementType; • } • • final class Array extends Pointer { • int capacity; • }

  14. Types • final class Name extends Type { • String name; • } • IT IS USED WHEN YOU SEE THIS: • Struct node{ • Int data; • Struct node *next; • };

  15. Types • abstract class Record extends Type { • class RecordField { • Type type; • String name; • } • List<RecordField> fields; • } • • final class Struct extends Record {} • • final class Union extends Record {} • • final class Function extends Type { • Type argumentType; • Type returnType; • }

  16. Boolean IsEqual(Type a, Type b) • { • If… • }

  17. Tips • What is the node’s class? • instanceof • Specify type • getInstance() and equals(); static factory • Return specific string • Intern() • Put attributes (lvalue, type, etc.) into the base class • Simplify your code • Copy a HashMap • clone()

  18. public class Symbol { • private String name; • private Symbol(String n) { • name = n; • } • public String toString() { • return name; • } • private static java.util.Dictionary<String, Symbol> dict = new java.util.Hashtable<String, Symbol>(); • public static Symbol symbol(String n) { • String u = n.intern(); • Symbol s = dict.get(u); • if (s == null) { • s = new Symbol(u); • dict.put(u, s); • } • return s; • } • }

  19. Q & A?

  20. Thanks!

More Related