CS 3240 Languages and Computation Course Overview Sasha

  • Slides: 72
Download presentation
CS 3240: Languages and Computation Course Overview Sasha Boldyreva

CS 3240: Languages and Computation Course Overview Sasha Boldyreva

Personnel n Instructor: Alexandra (Sasha) Boldyreva Email: aboldyre@cc. gatech. edu ¨ Office: Klaus 3444

Personnel n Instructor: Alexandra (Sasha) Boldyreva Email: aboldyre@cc. gatech. edu ¨ Office: Klaus 3444 ¨ Office Hours: ¨ n n n Tue. & Wed. at 2: 00 to 3: 00 pm Or by appointment TAs: TBA Email: TBA ¨ Office Hours: TBA ¨

Required Textbooks Bundle ISBN# 1418879746, including n “Compiler Construction Principles and Practice” by Kenneth

Required Textbooks Bundle ISBN# 1418879746, including n “Compiler Construction Principles and Practice” by Kenneth C. Louden, Thompson Course Technology, 1997, ISBN 0534939724 n “Introduction to Theory of Computation, Second Edition” by Michael Sipser, Thompson Course Technology, 2005, ISBN 0534950973

Course Objectives n Formal languages Understand definitions of regular and context-free languages and their

Course Objectives n Formal languages Understand definitions of regular and context-free languages and their corresponding “machines” ¨ Understand their computational powers and limitations ¨ n Compiler concepts Understand their applications in compilers ¨ Front-end of compiler ¨ Lexical analysis, parsing, semantic analysis ¨ n Theory of computation Understand Turing machines ¨ Understand decidability ¨

Course Syllabus n n n n n Lexical analysis, scanners, pattern matching Regular expressions,

Course Syllabus n n n n n Lexical analysis, scanners, pattern matching Regular expressions, DFAs, NFAs and automata Limits on regular expressions, pumping lemma Practical parsing, LL and LR parsing Context-free languages, grammars, Chomsky Hierarchy Pushdown automata, deterministic vs. non-deterministic Attribute grammars, type inferencing Context-free vs. context-sensitive grammars Decidable vs. Undecidable problems, Turing Machines, Halting Problem Complexity of computation, classes of languages P/NP, space and time completeness

Grading n n n n Homeworks: 25% Mini-project: 15% Midterm : 30% Final: 30%

Grading n n n n Homeworks: 25% Mini-project: 15% Midterm : 30% Final: 30% Homeworks to be submitted in class - hardcopy No late homework or assignments Homework should be concise, complete, and precise Tests will be in class

Class Policies Students must write solutions to assignments completely independently n General discussions are

Class Policies Students must write solutions to assignments completely independently n General discussions are allowed on assignments among students, but names of collaborators must be reported n Cell phones off, silence please n

Resources n Class webpage: see T-Square n Check for schedule changes.

Resources n Class webpage: see T-Square n Check for schedule changes.

Introduction to Compiler Concepts

Introduction to Compiler Concepts

Compilers n What is a compiler? ¨A program that translates an executable program from

Compilers n What is a compiler? ¨A program that translates an executable program from source language into target language ¨ Usually source language is high-level language, and target language is object (or machine) code ¨ Related to interpreters n Why compilers? ¨ Programming in machine (or assembly) language is tedious, error prone, and machine dependent ¨ Historical note: In 1954, IBM started developing FORTRAN language and its compiler

Why study theory of compiler? n n n Besides it is required… Prerequisite for

Why study theory of compiler? n n n Besides it is required… Prerequisite for developing advanced compilers, which continues to be active as new computer architectures emerge Useful to develop software tools that parse computer codes or strings ¨ E. g. , editors, debuggers, interpreters, preprocessors, … n Important to understand how compliers work to program more effectively

How Does Compiler Work? Request Token Start • Front End: Analysis of program syntax

How Does Compiler Work? Request Token Start • Front End: Analysis of program syntax and semantics Scanner Get Token Parser Semantic Action Checking Intermediate Representation Semantic Error

Parts of Compilers Front End 1. Lexical Analysis 2. Syntax Analysis 3. Semantic Analysis

Parts of Compilers Front End 1. Lexical Analysis 2. Syntax Analysis 3. Semantic Analysis Back End Focus of this class. 4. Code Generation 5. Optimization Synthesis

The Big Picture n n Parsing: Translating code to rules of grammar. Building representation

The Big Picture n n Parsing: Translating code to rules of grammar. Building representation of code. Scanning: Converting input text into stream of known objects called tokens. Simplifies parsing process. n Grammar dictates syntactic rules of language i. e. , how legal sentence could be formed n Lexical rules of language dictate how legal word is formed by concatenating alphabet.

Overall Operation n Parser is in control of the overall operation ¨ Demands n

Overall Operation n Parser is in control of the overall operation ¨ Demands n Scanner reads input file into token buffer & forms a token (How? ) ¨ Token n scanner to produce a token is returned to parser Parser attempts to match the token (How? ) ¨ Failure: Syntax Error! ¨ Success: n n Does nothing and returns to get next token, or Takes semantic action

Overall Operation n Semantic action: look up variable name ¨ If found okay ¨

Overall Operation n Semantic action: look up variable name ¨ If found okay ¨ If not: put in symbol table If semantic checks succeed, do codegeneration (How? ) n Continue to get next token n No more tokens? Done! n

Scanning/Tokenization Input File n n Token Buffer What does the Token Buffer contain? ¨

Scanning/Tokenization Input File n n Token Buffer What does the Token Buffer contain? ¨ Token being identified Why a two-way ( ) street? ¨ Characters can be read ¨ and unread ¨ Termination of a token

Example main() m

Example main() m

Example main() am

Example main() am

Example main() iam

Example main() iam

Example main() niam

Example main() niam

Example main() (niam

Example main() (niam

Example main() niam Keyword: main

Example main() niam Keyword: main

Parser Translating code to rules of a grammar n Control the overall operation n

Parser Translating code to rules of a grammar n Control the overall operation n Demands scanner to produce a token n Failure: Syntax Error! n Success: n ¨ Does nothing and returns to get next token, or ¨ Takes semantic action

Grammar Rules <C-PROG> MAIN OPENPAR <PARAMS> CLOSEPAR <MAIN-BODY> <PARAMS> NULL <PARAMS> VAR <VAR-LIST> <VARLIST>

Grammar Rules <C-PROG> MAIN OPENPAR <PARAMS> CLOSEPAR <MAIN-BODY> <PARAMS> NULL <PARAMS> VAR <VAR-LIST> <VARLIST> , VAR <VARLIST> NULL <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE> VAR <VAR-LIST>; <ASSIGN-STMT> VAR = <EXPR>; <EXPR> VAR<OP><EXPR> <OP> + <OP> <TYPE> INT <TYPE> FLOAT

Demo main() { int a, b; a = b; } Scanner Parser Token Buffer

Demo main() { int a, b; a = b; } Scanner Parser Token Buffer

Demo main() { int a, b; a = b; } Scanner Token Buffer "Please,

Demo main() { int a, b; a = b; } Scanner Token Buffer "Please, get me the next token" Parser

Demo main() { int a, b; a = b; } Scanner Parser m

Demo main() { int a, b; a = b; } Scanner Parser m

Demo main() { int a, b; a = b; } Scanner Parser am

Demo main() { int a, b; a = b; } Scanner Parser am

Demo main() { int a, b; a = b; } Scanner Parser iam

Demo main() { int a, b; a = b; } Scanner Parser iam

Demo main() { int a, b; a = b; } Scanner Parser niam

Demo main() { int a, b; a = b; } Scanner Parser niam

Demo main() { int a, b; a = b; } Scanner Parser (niam

Demo main() { int a, b; a = b; } Scanner Parser (niam

Demo main() { int a, b; a = b; } Scanner Parser niam

Demo main() { int a, b; a = b; } Scanner Parser niam

Demo main() { int a, b; a = b; } Scanner Token Buffer Token:

Demo main() { int a, b; a = b; } Scanner Token Buffer Token: main Parser

Demo main() { int a, b; a = b; } Scanner Token Buffer Parser

Demo main() { int a, b; a = b; } Scanner Token Buffer Parser "I recognize this"

Parsing (Matching) n n n Start matching using a rule When match takes place

Parsing (Matching) n n n Start matching using a rule When match takes place at certain position, move further (get next token & repeat) If expansion needs to be done, choose appropriate rule (How to decide which rule to choose? ) If no rule found, declare error If several rules found, the grammar (set of rules) is ambiguous

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner "Please, get me the next token" Parser

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: MAIN Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY>

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner "Please, get me the next token" Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY>

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: OPENPAR Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY>

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: CLOSEPAR Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> <PARAMETERS> NULL

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: CLOSEPAR Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> <PARAMETERS> NULL

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: CLOSEPAR Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY>

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: CURLYOPEN Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: INT Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>; <TYPE> INT

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: INT Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>; <TYPE> INT

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: INT Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>; <TYPE> INT

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: VAR Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>; <VARLIST> , VAR <VARLIST> NULL

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: ', ' [COMMA] Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>; <VARLIST> , VAR <VARLIST> NULL

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: VAR Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>; <VARLIST> , VAR <VARLIST> NULL

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: '; ' Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>; <VARLIST> , VAR <VARLIST> NULL

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: '; ' Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>; <VARLIST> , VAR <VARLIST> NULL

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: '; ' Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>; <VARLIST> , VAR <VARLIST> NULL

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: '; ' Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>; <VARLIST> , VAR <VARLIST> NULL

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: '; ' Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>;

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: '; ' Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <DECL-STMT> <TYPE>VAR<VAR-LIST>;

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: VAR Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <ASSIGN-STMT> VAR = <EXPR>; <EXPR> VAR

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: '=' Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <ASSIGN-STMT> VAR = <EXPR>; <EXPR> VAR

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: VAR Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <ASSIGN-STMT> VAR = <EXPR>; <EXPR> VAR

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: VAR Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <ASSIGN-STMT> VAR = <EXPR>; <EXPR> VAR

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: VAR Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <ASSIGN-STMT> VAR = <EXPR>; <EXPR> VAR

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: '; ' Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <ASSIGN-STMT> VAR = <EXPR>;

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: '; ' Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE <ASSIGN-STMT> VAR = <EXPR>;

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner

Scanning & Parsing Combined main() { int a, b; a = b; } Scanner Token: CURLYCLOSE Parser <C-PROG> MAIN OPENPAR <PARAMETERS> CLOSEPAR <MAIN-BODY> CURLYOPEN <DECL-STMT> <ASSIGN-STMT> CURLYCLOSE

What Is Happening? n During/after parsing? ¨ Tokens n get gobbled Symbol tables ¨

What Is Happening? n During/after parsing? ¨ Tokens n get gobbled Symbol tables ¨ Variables have attributes ¨ Declaration attached attributes to variables n Semantic actions ¨ What n are semantic actions? Semantic checks

Symbol Table n n int a, b; Declares a and b ¨ Within current

Symbol Table n n int a, b; Declares a and b ¨ Within current scope ¨ Type integer n Use of a and b now legal Name a b Basic Symbol Table Type Scope int "main"

Typical Semantic Actions n n n Enter variable declaration into symbol table Look up

Typical Semantic Actions n n n Enter variable declaration into symbol table Look up variables in symbol table Do binding of looked-up variables (scoping rules, etc. ) Do type checking for compatibility Keep the semantic context of processing a + b + c t 1 = a + b t 2 = t 1 + c Semantic Context

How Are Semantic Actions Called? n Action symbols embedded in the grammar ¨ Each

How Are Semantic Actions Called? n Action symbols embedded in the grammar ¨ Each action symbol represents a semantic procedure ¨ These procedures do things and/or return values Semantic procedures are called by parser at appropriate places during parsing n Semantic stack implements & stores semantic records n

Semantic Actions <decl-stmt> <type>#put-type<var-list>#do-decl <type> int | float <var-list> <var>#add-decl <var> ID#proc-decl #put-type puts

Semantic Actions <decl-stmt> <type>#put-type<var-list>#do-decl <type> int | float <var-list> <var>#add-decl <var> ID#proc-decl #put-type puts given type on semantic stack #proc-decl builds decl record for var on stack #add-decl builds decl-chain #do-decl traverses chain on semantic stack using backwards pointers entering each var into symbol table id 3 id 2 id 1 type decl record #do-decl Name Type Scope id 1 1 3 id 2 1 3 id 3 1 3

Semantic Actions What else can semantic actions do in addition to storing and looking

Semantic Actions What else can semantic actions do in addition to storing and looking up names in a symbol table? n Two type of semantic actions n ¨ Checking (binding, type compatibility, scoping, etc. ) ¨ Translation (generate temporary values, propagate them to keep semantic context).

Full Compiler Structure Scanner Start Parser Semantic Action Code Generation • Most compilers have

Full Compiler Structure Scanner Start Parser Semantic Action Code Generation • Most compilers have two pass Semantic Error CODE

Summary Front-end of compiler: scanner and parser n Translation takes place in back end

Summary Front-end of compiler: scanner and parser n Translation takes place in back end n Scanner, parser and code generator are automated n ¨ How? class We will answer this question in this