编译原理 (精品·公开课件).pptVIP

  • 20
  • 0
  • 约9.37千字
  • 约 46页
  • 2018-11-05 发布于广西
  • 举报
* Tree Languages Statement Stm ? Stm; Stm (CompoundStm) | id := Exp (AssignStm) | print(ExpList) (PrintStm) Exp Exp ? id (IdExp) | num (NumExp) | Exp Binop Exp (OpExp) | (Stm, Exp) (EseqExp) * Example a := 5 + 3 ; b := (print(a, a-1), 10*a); print (b) Output 8, 7 80 * CompoundStm AssignStm CompoundStm a OpExp NumExp NumExp plus AssignStm b EseqExp PrintStm LastExpList IdExp b printStm OpExp NumExp NumExp Times 10 a PairExpList a LastExpList OpExp IdExp NumExp Minus a 1 5 3 * Front end scanner parser source IR token errors * Front end Responsibilities: recognize legal procedure report errors produce IR preliminary storage map shape the code for the back end Much of front end construction can be automated * Scanner Maps characters into tokens the basic unit of syntax a := a - 1 becomes id, a; :=, ; id, a; -, ; num, 1 A lexeme is a value for a token : character string, integer typical tokens: numb, id, +, -, …… Eliminates white space (tabs, blanks, comments) A key issue is speed use specialized recognizer (as opposed to lex) * Parser Parser recognize context-free syntax guide context-sensitive analysis construct IR produce meaningful error messages attempt error correction Parser generators mechanize much of the work * Back End Responsibilities translate IR into target machine code choose instructions for each IR operation decide what to keep in registers at each point ensure conformance with system interfaces Automation has been less successful here * Three pass compilers Analysis Synthesis source HIR target Optimizer LIR errors Analyzes and changes IR The goal is to reduce runtime Optimizer must preserve values * Optimizer * * Data Structures typedef struct A_stm_ *A_stm ; typedef struct A_exp_ *A_exp ; typedef struct A_expList *A_expList ; typedef enum { A_plus, A_minus, A_times, A_div } A_binop ; * Data Structures struct A_stm_ { enum { A_CompoundStm, A_assignStm, A_prin

文档评论(0)

1亿VIP精品文档

相关文档