Presentation is loading. Please wait.

Presentation is loading. Please wait.

Semantic Analysis Pu Youer 2013.3.30.

Similar presentations


Presentation on theme: "Semantic Analysis Pu Youer 2013.3.30."— Presentation transcript:

1 Semantic Analysis Pu Youer

2 Outline Introduction Requirements Checking Environment How to start

3 Introduction Real Java coding Purpose Approach
Is syntactic analysis sufficient for program checking? What is the difference between Syntax and Semantics? Approach What did you get after syntactic analysis? i.e. What is the “program”.

4 Requirements Check constraints on the wiki
Refer to gcc 4.7.1 for any semantic issues not mentioned above. Discuss on the group if there is anything strange Type of Expr should be specified Expr.type Generate Env

5 Checking Type Checking Use before Declaration
Unmatched parameters of a function or fields of a record break/continue out of a loop etc

6 Type Checking Make sure you have filled Expr.type after checking them.
Be careful with +- Pay attention to lvalue Return value should be consistent with function declaration.

7 Environment Mapping from a Symbol to an Entry.
Keeping all the information about Variables, Functions and Types VarEntry, FuncEntry, TypeEntry in one env. Two Layer Environment, Global Environment vs Local Environment. You may visit AST twice to make it right. Constraints about names.

8 What is an Entry? An Entry keeps information about variables, functions, and types. Entries exist in some environment. There may be three kinds of entries, for Variables, for Functions and for Types. Through an entry, you should get type of that entry, parameter list etc.

9 How to start Read the framework, build your own
Deep understanding about the “program” you get after syntactic analysis. Keep in mind what you are doing. Visit nodes of the “program” recursively. Specify types. Check if the types are alright. Check if the stmts are right. Build Env.(Unique name)

10 What you may do in Main Semantic1 semantic1 = new Semantic(program)

11 What you may do in Semantic1
for (int i = 0; i < program.declarationlist.size(); i++) { visit_declarationlist( program.declarationlist[i]); }

12 What you may do in Semantic2
void visit_stmt(Stmt stmt) { if (stmt instanceof Ifstmt) visit_Ifstmt(stmt); … }

13 What you may still need Get a variable to record loop
When enters a for/while loop++; When leave a for/while loop--; When there’s a break/continue judge if loop>1;

14 Useful tips What is the node’s class? Specify type
instanceof Specify type Use getIntance() and equals() Mental preparation for rewriting As Xiao Jia said


Download ppt "Semantic Analysis Pu Youer 2013.3.30."

Similar presentations


Ads by Google