Comprehensive Guide to Semantic Analysis in Programming
E N D
Presentation Transcript
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 • What is a program?
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
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.
Environment • Mapping from a symbol to an entry • Keep all information about variables, functions and types. • VarEntry, FuncEntry, TypeEntry in one env • Table.java
What is an entry • An entry keeps information about specific variable/function/type • Must exists in some environment
Type checking • +-*/ • Lvalue • Return value • Make sure you have filled expr.type after checking
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.
Semantic semantic = new Semantic(program); • for (inti = 0; i<program.declarationlist.size(); i++) • { • checkDeclarationlist(program.declarationlist[i]); • }
void checkStmt(Stmtstmt) • { • if (stmtinstanceofIfstmt) • checkIfstmt(stmt); • … • } • Funtion Overloading
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) …
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; • }
Types • final class Name extends Type { • String name; • } • IT IS USED WHEN YOU SEE THIS: • Struct node{ • Int data; • Struct node *next; • };
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; • }
Boolean IsEqual(Type a, Type b) • { • If… • }
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()
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; • } • }