Compiler Construction - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Compiler Construction

Description:

Objectives Given a context-free grammar ... 2 Language of Assignment Statements P S ... Match terminals Use if statement to choose between ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 11
Provided by: JohnG251
Category:

less

Transcript and Presenter's Notes

Title: Compiler Construction


1
Compiler Construction

2
Objectives
  • Given a context-free grammar, G, and the
    grammar-independent functions for a
    recursive-descent parser, complete the
    recursive-descent parser by adding the
    grammar-dependent functions.
  • Given naive intermediate code for a C loop,
    hand-optimize the code, reducing by at least 20
    the number of intermediate code instructions
    needed while maintaining semantic correctness of
    the program.
  • Define "register assignment" in the context of a
    compiler. Explain why register assignment is an
    important compiler optimization.

3
Compiler Tasks
Code Generation
Tokens
Source
Intermediate
Intermediate
Lexor
Parser
Optimizer
Assembler or Binary or Source
4
CFGs Example
  • Simplified description of English grammar
  • G
  • S ? NP VP
  • NP ? N Adj NP
  • N ? car dog
  • Adj ? big green
  • VP ? V V NP
  • V ? is eats
  • Example derivation
  • S ?G NP VP ?G Adj NP VP ?G Adj N VP ?G Adj N V
  • ?G Adj N eats ?G big N eats ?G big dog eats

Production form LHS variable. RHS string of
variables and/or terminals.
5
CFGs Formal Definition
  • G (V, S, P, S)
  • V variables a finite set
  • S alphabet or terminals a finite set
  • P productions a finite set
  • S start variable S?V
  • Productions form, where A?V, a?(V?S)
  • A ? a

6
CFGs Example 1
  • an bn n?0
  • S ? e a S b
  • Formally G (S, a,b,
  • S ? e, S ? a S b, S)

7
CFGs CFLs Example 2
  • all strings of balanced parentheses
  • A core idea of most programming languages.

P ? e ( P ) P P
8
CFGs Example 2
  • Language of Assignment Statements
  • P ? S P
  • P ? S
  • S ? id E
  • E ? expressions (expressions left as exercise)

9
Example Languages
  • Set of strings over a,b with an even number of
    as
  • Arithmetic expressions using , , /, -, (,)
    Assume that all operands are called id

10
Recursive Descent Parser
  • Built from a context-free grammar
  • Simple rules build recursive program
  • Grammar independent part (see pdf file)
  • Grammar dependent
  • Include a function for each non-terminal (lhs)
  • Generate code for each rule, left to right
  • Call non-terminals
  • Match terminals
  • Use if statement to choose between multiple
    rules with the same lhs (non-terminal) symbol
Write a Comment
User Comments (0)
About PowerShow.com