Continued CS 230 - Spring 2020 4-1 Scanning / Lexical Analysis - - PowerPoint PPT Presentation

continued
SMART_READER_LITE
LIVE PREVIEW

Continued CS 230 - Spring 2020 4-1 Scanning / Lexical Analysis - - PowerPoint PPT Presentation

CS 230 Introduction to Computers and Computer Systems Lecture 20 CFGs and Tool Chain Continued CS 230 - Spring 2020 4-1 Scanning / Lexical Analysis First step of compiler convert input string to tokens use regular languages


slide-1
SLIDE 1

CS 230 - Spring 2020 4-1

CS 230 – Introduction to Computers and Computer Systems Lecture 20 – CFGs and Tool Chain Continued

slide-2
SLIDE 2

CS 230 - Spring 2020 4-2

Scanning / Lexical Analysis

 First step of compiler

 convert input string to tokens  use regular languages

 Conflicting rules: need priority

 use order of rules

 Usually: greedy quantification

 produce longest possible match

slide-3
SLIDE 3

CS 230 - Spring 2020 4-3

Parsing / Syntax Analysis

 Second step of compiler  Clear and unambiguous description of

 valid sentences -> natural language  valid program -> programming language

 Need a rule set for classification of valid

sequences

 called a grammar

 Programming language – formal specification  English – informal specification

slide-4
SLIDE 4

CS 230 - Spring 2020 4-4

Grammar

 Context-free grammar (CFG)

 formal description of a context free language  more powerful than a regular language

 Parsing

 compares a sequence of tokens with a CFG  build a parse tree

 if we can make a parse tree: sequence is in the language  if we cannot: sequence is not in the language

slide-5
SLIDE 5

CS 230 - Spring 2020 4-5

Context-Free Grammar

 Formalism to specify languages

 simple and precise recursive definition  building block structure of languages  well-studied in theoretical computer science

 Context-free

 sequences do not depend on any external context

slide-6
SLIDE 6

CS 230 - Spring 2020 4-6

CFG Components

 Token / Terminal

 atomic (undividable) symbol  usually from scanner phase

 Variable / Non-terminal

 abstract component  does not literally appear in input  designated non-terminal start symbol

 first non-terminal in grammar

 notation: angle brackets < ... >

slide-7
SLIDE 7

CS 230 - Spring 2020 4-7

CFG Components

 Production / Rule:

 possible expansion of a non-terminal into

zero or more terminals and/or non-terminals

 more than one rule per non-terminal possible  describes how the language is put together  often recursive  build a parse tree by repeatedly applying

productions to the start symbol

 forms a tree that expands until only terminals remain

slide-8
SLIDE 8

CS 230 - Spring 2020 4-8

Example – Basic Arithmetic CFG

<Expr>  <Expr> + <Term> <Expr>  <Expr> - <Term> <Expr>  <Term> <Term>  ( <Expr> ) <Term>  integer

slide-9
SLIDE 9

CS 230 - Spring 2020 4-9

Example – Parse Tree

 Draw a parse tree for: 1 + 24 - 3

slide-10
SLIDE 10

CS 230 - Spring 2020 4-10

Example – Parse Tree

 Draw a parse tree for: 1 + 24 - 3

<Expr>

slide-11
SLIDE 11

CS 230 - Spring 2020 4-11

Example – Parse Tree

 Draw a parse tree for: 1 + 24 - 3

<Expr> <Expr> <Term>

slide-12
SLIDE 12

CS 230 - Spring 2020 4-12

Example – Parse Tree

 Draw a parse tree for: 1 + 24 - 3

<Expr> <Expr> <Term>

  • 3
slide-13
SLIDE 13

CS 230 - Spring 2020 4-13

Example – Parse Tree

 Draw a parse tree for: 1 + 24 - 3

<Expr> <Expr> <Expr> <Term> <Term>

  • +

3

slide-14
SLIDE 14

CS 230 - Spring 2020 4-14

Example – Parse Tree

 Draw a parse tree for: 1 + 24 - 3

<Expr> <Expr> <Expr> <Term> <Term>

  • +

3 24

slide-15
SLIDE 15

CS 230 - Spring 2020 4-15

Example – Parse Tree

 Draw a parse tree for: 1 + 24 - 3

<Expr> <Expr> <Expr> <Term> <Term>

  • +

<Term> 3 24

slide-16
SLIDE 16

CS 230 - Spring 2020 4-16

Example – Parse Tree

 Draw a parse tree for: 1 + 24 - 3

<Expr> <Expr> <Expr> <Term> <Term>

  • +

<Term> 3 24 1

slide-17
SLIDE 17

CS 230 - Spring 2020 4-17

Example 2 – Parse Tree

 Draw a parse tree for: 1 + (24 – 3)

slide-18
SLIDE 18

CS 230 - Spring 2020 4-18

Example 2 – Parse Tree

 Draw a parse tree for: 1 + (24 – 3)

<Expr> <Expr> <Term> +

slide-19
SLIDE 19

CS 230 - Spring 2020 4-19

Example 2 – Parse Tree

 Draw a parse tree for: 1 + (24 – 3)

<Expr> <Expr> <Term> + <Term>

slide-20
SLIDE 20

CS 230 - Spring 2020 4-20

Example 2 – Parse Tree

 Draw a parse tree for: 1 + (24 – 3)

<Expr> <Expr> <Term> + 1 <Term>

slide-21
SLIDE 21

CS 230 - Spring 2020 4-21

Example 2 – Parse Tree

 Draw a parse tree for: 1 + (24 – 3)

<Expr> <Expr> <Term> + <Expr> 1 <Term> ( )

slide-22
SLIDE 22

CS 230 - Spring 2020 4-22

Example 2 – Parse Tree

 Draw a parse tree for: 1 + (24 – 3)

<Expr> <Expr> <Term> + <Expr> 1 <Term> ( ) <Expr> <Term>

slide-23
SLIDE 23

CS 230 - Spring 2020 4-23

Example 2 – Parse Tree

 Draw a parse tree for: 1 + (24 – 3)

<Expr> <Expr> <Term> + <Expr> 1 <Term> ( ) <Expr> <Term>

  • <Term>
slide-24
SLIDE 24

CS 230 - Spring 2020 4-24

Example 2 – Parse Tree

 Draw a parse tree for: 1 + (24 – 3)

<Expr> <Expr> <Term> + <Expr> 1 <Term> ( ) <Expr> <Term>

  • 24

<Term>

slide-25
SLIDE 25

CS 230 - Spring 2020 4-25

Example 2 – Parse Tree

 Draw a parse tree for: 1 + (24 – 3)

<Expr> <Expr> <Term> + <Expr> 1 <Term> ( ) <Expr> <Term>

  • 24

<Term> 3

slide-26
SLIDE 26

CS 230 - Spring 2020 4-26

Example 3 – Parse Tree

 Draw a parse tree for: 1 + 24 +

slide-27
SLIDE 27

CS 230 - Spring 2020 4-27

Example 3 – Parse Tree

 Draw a parse tree for: 1 + 24 +

<Expr>

slide-28
SLIDE 28

CS 230 - Spring 2020 4-28

Example 3 – Parse Tree

 Draw a parse tree for: 1 + 24 +

<Expr> <Expr> <Term> +

slide-29
SLIDE 29

CS 230 - Spring 2020 4-29

Example 3 – Parse Tree

 Draw a parse tree for: 1 + 24 +

<Expr> <Expr> <Term> +

1 + 24 + is not in the language defined by this CFG

slide-30
SLIDE 30

CS 230 - Spring 2020 4-30

Try it Yourself

Consider the following context-free grammar. Draw a parse tree for: 8 + 79 * 5 <Expr>  <Expr> + <Term> <Expr>  <Expr> - <Term> <Expr>  <Term> <Term>  <Term> * <Factor> <Term>  <Term> / <Factor> <Term>  <Factor> <Factor>  ( <Expr> ) <Factor>  integer

slide-31
SLIDE 31

CS 230 - Spring 2020 4-31

Try it Yourself

Consider the following context-free grammar. Draw a parse tree for: 8 + 79 * 5

<Expr>

slide-32
SLIDE 32

CS 230 - Spring 2020 4-32

Try it Yourself

Consider the following context-free grammar. Draw a parse tree for: 8 + 79 * 5

<Expr> <Expr> <Term> +

slide-33
SLIDE 33

CS 230 - Spring 2020 4-33

Try it Yourself

Consider the following context-free grammar. Draw a parse tree for: 8 + 79 * 5

<Expr> <Expr> <Term> + <Term>

slide-34
SLIDE 34

CS 230 - Spring 2020 4-34

Try it Yourself

Consider the following context-free grammar. Draw a parse tree for: 8 + 79 * 5

<Expr> <Expr> <Term> + <Term> <Factor>

slide-35
SLIDE 35

CS 230 - Spring 2020 4-35

Try it Yourself

Consider the following context-free grammar. Draw a parse tree for: 8 + 79 * 5

<Expr> <Expr> <Term> + 8 <Term> <Factor>

slide-36
SLIDE 36

CS 230 - Spring 2020 4-36

Try it Yourself

Consider the following context-free grammar. Draw a parse tree for: 8 + 79 * 5

<Expr> <Expr> <Term> + 8 <Term> <Term> <Factor> * <Factor>

slide-37
SLIDE 37

CS 230 - Spring 2020 4-37

Try it Yourself

Consider the following context-free grammar. Draw a parse tree for: 8 + 79 * 5

<Expr> <Expr> <Term> + 8 <Term> <Term> <Factor> * <Factor> <Factor>

slide-38
SLIDE 38

CS 230 - Spring 2020 4-38

Try it Yourself

Consider the following context-free grammar. Draw a parse tree for: 8 + 79 * 5

<Expr> <Expr> <Term> + 8 <Term> 79 <Term> <Factor> * <Factor> <Factor>

slide-39
SLIDE 39

CS 230 - Spring 2020 4-39

Try it Yourself

Consider the following context-free grammar. Draw a parse tree for: 8 + 79 * 5

<Expr> <Expr> <Term> + 8 <Term> 79 5 <Term> <Factor> * <Factor> <Factor>

slide-40
SLIDE 40

CS 230 - Spring 2020 4-40

Semantic Analysis

 Third step of compiler  Parsed tree is analyzed

 type checking  variables exist  function parameters match

 Build a symbol table

 list of variables, functions, classes, etc.

slide-41
SLIDE 41

CS 230 - Spring 2020 4-41

Code Generation

 Fourth step of compiler  Convert parse tree and symbol table into target

language

 usually assembly language  need to make space in memory for variables based

  • n types from symbol table

 parse tree tells us what order to do instructions

slide-42
SLIDE 42

CS 230 - Spring 2020 4-42

Linking

 Code generation and assembler produce

machine code

 results are object files

 Linker combines object files into executables

 or libraries

 shared object files used by multiple programs

 part of classical tool chain

slide-43
SLIDE 43

CS 230 - Spring 2020 4-43

Loading

 Last part of classical tool chain  Set up memory for new program  Loader loads executable file from HDD/SSD

 into main memory  also load any required libraries

 Start process

 put the program counter at the start of the program

slide-44
SLIDE 44

CS 230 - Spring 2020 4-44

The End!

 CS 230 built up how a computer actually works

 from logic gates all the way up to compiling code