Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2: A Simple One Pass Compiler

Similar presentations


Presentation on theme: "Chapter 2: A Simple One Pass Compiler"— Presentation transcript:

1 Chapter 2: A Simple One Pass Compiler

2 The Entire Compilation Process
Grammars for Syntax Definition Syntax-Directed Translation Parsing - Top Down & Predictive Pulling Together the Pieces The Lexical Analysis Process Symbol Table Considerations A Brief Look at Code Generation Concluding Remarks/Looking Ahead

3 Grammars for Syntax Definition
A Context-free Grammar (CFG) Is Utilized to Describe the Syntactic Structure of a Language A CFG Is Characterized By: 1. A Set of Tokens or Terminal Symbols 2. A Set of Non-terminals 3. A Set of Production Rules Each Rule Has the Form NT  {T, NT}* 4. A Non-terminal Designated As the Start Symbol

4 Grammars for Syntax Definition Example CFG
list  list + digit list  list - digit list  digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 (the “|” means OR) (So we could have written list  list + digit | list - digit | digit )

5 Grammars are Used to Derive Strings:
Using the CFG defined on the previous slide, we can derive the string: as follows: list  list + digit  list - digit + digit  digit - digit + digit  9 - digit + digit  digit P1 : list  list + digit P2 : list  list - digit P3 : list  digit P4 : digit  9 P4 : digit  5 P4 : digit  2

6 Grammars are Used to Derive Strings:
This derivation could also be represented via a Parse Tree (parents on left, children on right) list  list + digit  list - digit + digit  digit - digit + digit  9 - digit + digit  digit list digit 9 5 2 - +

7 A More Complex Grammar What is this grammar for ?
block  begin opt_stmts end opt_stmts  stmt_list |  stmt_list  stmt_list ; stmt | stmt What is this grammar for ? What does “” represent ? What kind of production rule is this ?

8 Defining a Parse Tree More Formally, a Parse Tree for a CFG Has the Following Properties: Root Is Labeled With the Start Symbol Leaf Node Is a Token or  Interior Node (Now Leaf) Is a Non-Terminal If A  x1x2…xn, Then A Is an Interior; x1x2…xn Are Children of A and May Be Non-Terminals or Tokens

9 Other Important Concepts Ambiguity
Two derivations (Parse Trees) for the same token string. string - 9 + 5 2 string + 2 - 5 9 Grammar: string  string + string | string – string | 0 | 1 | …| 9 Why is this a Problem ?

10 Other Important Concepts Associativity of Operators
Left vs. Right right letter c b a = list digit 9 5 2 - + list  list + digit | | list - digit | digit digit  0 | 1 | 2 | …| 9 right  letter = right | letter letter  a | b | c | …| z

11 Embedding Associativity
The language of arithmetic expressions with + - (ambiguous) grammar that does not enforce associativity string  string + string | string – string | 0 | 1 | …| 9 non-ambiguous grammar enforcing left associativity (parse tree will grow to the left) string  string + digit | string - digit | digit digit  0 | 1 | 2 | …| 9 non-ambiguous grammar enforcing right associativity (parse tree will grow to the right) string  digit + string | digit - string | digit

12 Other Important Concepts Operator Precedence
What does 9 + 5 * 2 mean? ( ) * / + - is precedence order Typically This can be incorporated into a grammar via rules: expr  expr + term | expr – term | term term  term * factor | term / factor | factor factor  digit | ( expr ) digit  0 | 1 | 2 | 3 | … | 9 Precedence Achieved by: expr & term for each precedence level Rules for each are left recursive or associate to the left


Download ppt "Chapter 2: A Simple One Pass Compiler"

Similar presentations


Ads by Google