Title: CS 3240: Languages and Computation
1CS 3240 Languages and Computation
- Course Overview
- Santosh Pande
2Personnel
- Instructor Prof. Santosh Pande
- Email santosh_at_cc.gatech.edu
- Office KACB 2338
- Office Hours
- Tue. Thur. at 300 to 400 pm
- Or by appointment
- TAs Philip Wright and Aditya Devurkar
- Email pwright_at_gatech.edu and agdevurkar_at_gmail.co
m - Office Hours TBA
- Subject to change
3Required Textbooks
- Bundle ISBN 1418879746, including
- Compiler Construction Principles and Practice
by Kenneth C. Louden, Thompson Course Technology,
1997, ISBN 0534939724 - Introduction to Theory of Computation, Second
Edition by Michael Sipser, Thompson Course
Technology, 2005, ISBN 0534950973 - Both available on reserve in the library
4Course Objectives
- Formal languages
- Understand definitions of regular and
context-free languages and their corresponding
machines - Understand their computational powers and
limitations - Compiler concepts
- Understand their applications in compilers
- Front-end of compiler
- Lexical analysis, parsing, semantic analysis
- Theory of computation
- Understand Turing machines
- Understand decidability
5Course Syllabus
- 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 - Future topics Quantum
6Grading
- Homeworks 25
- Pop-quizzes in class 5
- Mini-project 15
- Midterm 25
- Final 30
- Homeworks to be submitted in class - hardcopy
- No late homework or assignments without prior
approval of instructor - Homework should be concise, complete, and precise
- Tests will be in class. Closed book, closed notes
7Class Policies
- Students must write solutions to assignments
completely independently - General discussions are allowed on assignments
among students, but names of collaborators must
be reported - No walking in or out during the lecture
- Cell phones off, silence please
- All absences must be vetted by Dean of Students
5 credit pop quizzes!
8Resources
- Class webpage and Swiki
- http//www.cc.gatech.edu/classes/AY2009/cs3240_spr
ing/ - Newsgroup git.cc.class.cs3240
- Check often for announcements and schedule
changes.
9Introduction toCompiler Concepts
10Compilers
- 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
- 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
11Why study theory of compiler?
- 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, - Important to understand how compliers work to
program more effectively
12How Does Compiler Work?
Scanner
Request Token
Get Token
Parser
Start
- Front End Analysis of program syntax and
semantics
Semantic Action
Semantic Error
Checking
Intermediate Representation
13Parts of Compilers
Focus of this class.
Analysis
1. Lexical Analysis 2. Syntax Analysis 3.
Semantic Analysis
Front End
Synthesis
4. Code Generation 5. Optimization
Back End
14The Big Picture
- 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. - Grammar dictates syntactic rules of language
i.e., how legal sentence could be formed - Lexical rules of language dictate how legal word
is formed by concatenating alphabet.
15Overall Operation
- Parser is in control of the overall operation
- Demands scanner to produce a token
- Scanner reads input file into token buffer
forms a token (How?) - Token is returned to parser
- Parser attempts to match the token (How?)
- Failure Syntax Error!
- Success
- Does nothing and returns to get next token, or
- Takes semantic action
16Overall Operation
- Semantic action look up variable name
- If found okay
- If not put in symbol table
- If semantic checks succeed, do code-generation
(How?) - Continue to get next token
- No more tokens? Done!
17Scanning/Tokenization
Input File
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
18Example
main()
m
19Example
main()
am
20Example
main()
iam
21Example
main()
niam
22Example
main()
(niam
23Example
main()
niam
Keyword main
24Parser
- Translating code to rules of a grammar
- Control the overall operation
- Demands scanner to produce a token
- Failure Syntax Error!
- Success
- Does nothing and returns to get next token, or
- Takes semantic action
25Grammar Rules
- ltC-PROGgt ? MAIN OPENPAR ltPARAMSgt CLOSEPAR
ltMAIN-BODYgt - ltPARAMSgt ? NULL
- ltPARAMSgt ? VAR ltVAR-LISTgt
- ltVARLISTgt ? , VAR ltVARLISTgt
- ltVARLISTgt ? NULL
- ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt ltASSIGN-STMTgt
CURLYCLOSE - ltDECL-STMTgt ? ltTYPEgt VAR ltVAR-LISTgt
- ltASSIGN-STMTgt ? VAR ltEXPRgt
- ltEXPRgt ? VAR
- ltEXPRgt ? VARltOPgtltEXPRgt
- ltOPgt ?
- ltOPgt ? -
- ltTYPEgt ? INT
- ltTYPEgt ? FLOAT
26Demo
main() int a,b a b
Scanner
Token Buffer
Parser
27Demo
main() int a,b a b
Scanner
Token Buffer
"Please, get me the next token"
Parser
28Demo
main() int a,b a b
Scanner
m
Parser
29Demo
main() int a,b a b
Scanner
am
Parser
30Demo
main() int a,b a b
Scanner
iam
Parser
31Demo
main() int a,b a b
Scanner
niam
Parser
32Demo
main() int a,b a b
Scanner
(niam
Parser
33Demo
main() int a,b a b
Scanner
niam
Parser
34Demo
main() int a,b a b
Scanner
Token Buffer
Token main
Parser
35Demo
main() int a,b a b
Scanner
Token Buffer
Parser
"I recognize this"
36Parsing (Matching)
- 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
37Scanning Parsing Combined
main() int a,b a b
Scanner
"Please, get me the next token"
Parser
38Scanning Parsing Combined
main() int a,b a b
Scanner
Token MAIN
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt
39Scanning Parsing Combined
main() int a,b a b
Scanner
"Please, get me the next token"
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt
40Scanning Parsing Combined
main() int a,b a b
Scanner
Token OPENPAR
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt
41Scanning Parsing Combined
main() int a,b a b
Scanner
Token CLOSEPAR
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltPARAMETERSgt ? NULL
42Scanning Parsing Combined
main() int a,b a b
Scanner
Token CLOSEPAR
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltPARAMETERSgt ? NULL
43Scanning Parsing Combined
main() int a,b a b
Scanner
Token CLOSEPAR
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt
44Scanning Parsing Combined
main() int a,b a b
Scanner
Token CURLYOPEN
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE
45Scanning Parsing Combined
main() int a,b a b
Scanner
Token INT
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt ltTYPEgt ? INT
46Scanning Parsing Combined
main() int a,b a b
Scanner
Token INT
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt ltTYPEgt ? INT
47Scanning Parsing Combined
main() int a,b a b
Scanner
Token INT
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt ltTYPEgt ? INT
48Scanning Parsing Combined
main() int a,b a b
Scanner
Token VAR
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt ltVARLISTgt ? , VAR
ltVARLISTgt ltVARLISTgt ? NULL
49Scanning Parsing Combined
main() int a,b a b
Scanner
Token ',' COMMA
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt ltVARLISTgt ? , VAR
ltVARLISTgt ltVARLISTgt ? NULL
50Scanning Parsing Combined
main() int a,b a b
Scanner
Token VAR
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt ltVARLISTgt ? , VAR
ltVARLISTgt ltVARLISTgt ? NULL
51Scanning Parsing Combined
main() int a,b a b
Scanner
Token ''
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt ltVARLISTgt ? , VAR
ltVARLISTgt ltVARLISTgt ? NULL
52Scanning Parsing Combined
main() int a,b a b
Scanner
Token ''
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt ltVARLISTgt ? , VAR
ltVARLISTgt ltVARLISTgt ? NULL
53Scanning Parsing Combined
main() int a,b a b
Scanner
Token ''
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt ltVARLISTgt ? , VAR
ltVARLISTgt ltVARLISTgt ? NULL
54Scanning Parsing Combined
main() int a,b a b
Scanner
Token ''
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt ltVARLISTgt ? , VAR
ltVARLISTgt ltVARLISTgt ? NULL
55Scanning Parsing Combined
main() int a,b a b
Scanner
Token ''
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt
56Scanning Parsing Combined
main() int a,b a b
Scanner
Token ''
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltDECL-STMTgt ?
ltTYPEgtVARltVAR-LISTgt
57Scanning Parsing Combined
main() int a,b a b
Scanner
Token VAR
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltASSIGN-STMTgt ? VAR
ltEXPRgt ltEXPRgt ? VAR
58Scanning Parsing Combined
main() int a,b a b
Scanner
Token ''
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltASSIGN-STMTgt ? VAR
ltEXPRgt ltEXPRgt ? VAR
59Scanning Parsing Combined
main() int a,b a b
Scanner
Token VAR
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltASSIGN-STMTgt ? VAR
ltEXPRgt ltEXPRgt ? VAR
60Scanning Parsing Combined
main() int a,b a b
Scanner
Token VAR
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltASSIGN-STMTgt ? VAR
ltEXPRgt ltEXPRgt ? VAR
61Scanning Parsing Combined
main() int a,b a b
Scanner
Token VAR
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltASSIGN-STMTgt ? VAR
ltEXPRgt ltEXPRgt ? VAR
62Scanning Parsing Combined
main() int a,b a b
Scanner
Token ''
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltASSIGN-STMTgt ? VAR
ltEXPRgt
63Scanning Parsing Combined
main() int a,b a b
Scanner
Token ''
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE ltASSIGN-STMTgt ? VAR
ltEXPRgt
64Scanning Parsing Combined
main() int a,b a b
Scanner
Token CURLYCLOSE
Parser
ltC-PROGgt ? MAIN OPENPAR ltPARAMETERSgt CLOSEPAR
ltMAIN-BODYgt ltMAIN-BODYgt ? CURLYOPEN ltDECL-STMTgt
ltASSIGN-STMTgt CURLYCLOSE
65What Is Happening?
- During/after parsing?
- Tokens get gobbled
- Symbol tables
- Variables have attributes
- Declaration attached attributes to variables
- Semantic actions
- What are semantic actions?
- Semantic checks
66Symbol Table
- int a,b
- Declares a and b
- Within current scope
- Type integer
- Use of a and b now legal
67Typical Semantic Actions
- 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 ? t1 a b
- t2 t1 c
Semantic Context
68How Are Semantic Actions Called?
- 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 - Semantic stack implements stores semantic
records
69Semantic Actions
- ltdecl-stmtgt ? lttypegtput-typeltvar-listgtdo-decl
- lttypegt ? int float
- ltvar-listgt ? ltvargtadd-decl ltvar-listgt
- ltvar-listgt ? ltvargtadd-decl
- ltvargt ? IDproc-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
decl record
id3
Name
Type
Scope
id2
id1
1
3
do-decl ?
id1
id2
1
3
type
id3
1
3
70Semantic Actions
- What else can semantic actions do in addition to
storing and looking up names in a symbol table? - Two type of semantic actions
- Checking (binding, type compatibility, scoping,
etc.) - Translation (generate temporary values, propagate
them to keep semantic context).
71Full Compiler Structure
Scanner
- Most compilers have two pass
Parser
Start
Semantic Action
Semantic Error
Code Generation
CODE
72Summary
- Front-end of compiler scanner and parser
- Translation takes place in back end
- Scanner, parser and code generator are automated
- How? We will answer this question in this class