Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wu Hang.  Introduction  Requirements  Checking  Environment  How to start  Tips.

Similar presentations


Presentation on theme: "Wu Hang.  Introduction  Requirements  Checking  Environment  How to start  Tips."— Presentation transcript:

1 Wu Hang

2  Introduction  Requirements  Checking  Environment  How to start  Tips

3  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  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  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  Mapping from a symbol to an entry  Keep all information about variables, functions and types.  VarEntry, FuncEntry, TypeEntry in one env  Table.java

7  An entry keeps information about specific variable/function/type  Must exists in some environment

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

9  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 (int i = 0; i <program.declarationlist.size(); i++)  { ◦ checkDeclarationlist(program.declarationlist[i]);  }

11  void checkStmt(Stmt stmt)  {  if (stmt instanceof Ifstmt)  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  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  final class Name extends Type {  String name;  }  IT IS USED WHEN YOU SEE THIS:  Struct node{  Int data;  Struct node *next;  };

15  abstract class Record extends Type {  class RecordField {  Type type;  String name;  }  List 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  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 dict = new java.util.Hashtable ();  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!


Download ppt "Wu Hang.  Introduction  Requirements  Checking  Environment  How to start  Tips."

Similar presentations


Ads by Google