diff --git a/notes/5.md b/notes/5.md new file mode 100644 index 0000000..acc1f3a --- /dev/null +++ b/notes/5.md @@ -0,0 +1,58 @@ +# Semantic Analysis + +* after passing the lexical and syntax analysis, there are still errors. so correcting usage of variables, objects and function... are needed. + +**Semantic Analysis** ensures the program satisfies a set of rules regarding the usage of programming constructs: +* identifiers declared before used +* types +* inheritance relationships +* single definition + +## Categories of Semantic Analysis + +* Scopes +* Types + +### Scope + +**Lexical Scope**: textual region in the program + +**Symbol Tables** + +Symantic checks refer to properties of identifier in the program; it need an environment to store identifier info: **symbol table**. + +| name | kind | type | +| ---- | ---- | ------------ | +| foo | func | `int -> int` | +| m | arg | `int` | +| n | arg | `int` | +| tmp | var | char | + +Each scope has symbol tables. +And program has hierachy of symbol tables(scope). + +if the identifier is used traverse the hierachy of symbol tables upward until finding the identifier with the same name to determine the declaration from the current scope. + +#### Build a Symbol Table + +there are five operations: +* insert scope +* exit scope +* find symbol(x) +* add symbol(x) +* check scope(x) + +### Type + +* Type checking +* Type inferencing + +Three Language Types: +* Statically typed +* Dynamically typed +* Untyped(machine code) + +**Static Type Checking** +Does not require additional type checking instructions at runtime. +and guarantees that the executions are safe at compile time. +modern languages require both static and dynamic type checking(union, void ptr) \ No newline at end of file diff --git a/pdf/L5.pdf b/pdf/L5.pdf new file mode 100644 index 0000000..52862f0 Binary files /dev/null and b/pdf/L5.pdf differ