250 likes | 379 Views
Lecture 1 1: Semester Review. COMP 310 0 Dept. Computer Science and Technology United International College. Introduction. Compilation vs. Interpretation Compilation stages. DFA and its equivalents. DFA&NFA Definition Accepted string Regular expression Notation Accepted string
E N D
Lecture 11: Semester Review COMP3100 Dept. Computer Science and Technology United International College
Introduction • Compilation vs. Interpretation • Compilation stages
DFA and its equivalents • DFA&NFA • Definition • Accepted string • Regular expression • Notation • Accepted string • Convert to NFA and DFA • Regular grammar • Definition • DFA=NFA=Regular expression=Regular grammar
Regular expression->DFA • Start with a regular expression. • Construct an NFA from it. • Use a set of subsets construction to obtain an equivalent DFA. • Construct the minimal equivalent DFA.
Convert the following regular expression to a DFA • 1(0|1)*101
PDA and its relevant • Context free languages • Definition • PDA • Definition • Difference between PDA and DFA • How does it work? • A language is context-free iff a pushdown automata recognizes it • Non-deterministic PDA are more powerful than deterministic ones
Parser-Top down parsing • Predicts a derivation • Matches non-terminal against token observed in input • How to parse given a parse table? • First set, follow set • Parse table construction • LL(1) grammar • Convert non-LL(1) grammar to LL(1) grammar
Exercise • Given the following grammar S->a|^|(T) T->T,S|S • Eliminate the left-recursion in the grammar. • Build the LL(1) parsing table
Parser-Bottom up parsing • Right-most derivation – Backward • Start with the tokens • End with the start symbol • Match substring on RHS of production, replace by LHS • Parsing given parsing table for LR(0) • Parsing table construction for LR(0) • Right-associative is not LR(0)
Parser-Bottom up parsing (2) • SLR • Apply reduction only if C is in FOLLOW(X) • LR(1) • LR(1) parsing table construction • LALR • Constructs LR(1) DFA and then merge any 2 LR(1) states whose items are identical except lookahead • Classification of grammar
Given the following grammar S->AS|b A->SA|a (1) Build the full LR(0) DFA (2) Is it an SLR(1) grammar?
For state I1, note that Follow(S’)={$}. Hence, the shift-reduce conflict in state I1 can be solved in SLR(1). • For state I6, note that Follow(A)={a,b}. Hence, there is a shift-reduce conflict in state I6 which cannot solved in SLR(1). • For state I7, note that Follow(S)={a,b,$}. Hence, there is a shift-reduce conflict in state I7 which cannot solved in SLR(1).
Semantic Analysis • Syntax-directed definition • Semantic actions • Synthesized attribute • Inherited attribute • lexical, syntax, semantic errors • Symbol table construction • Type derivation
Intermediate Code Generation • Translate high-level to low-level sentences • 4 Distinct Regions of Memory • Execution Stack • Activation record/frame
Running time organization • Parameter passing modes • Value • Reference • Value/result • Name
Parameter Passing Modes • int i; • int A[3]; • void Q(int B) { • A[1] = 3; • i = 2; • write(B); • B = 5; • } • int main() { • i = 1; • A[1] = 2; • A[2] = 4; • Q(A[i]); • }