1 / 7

微型语言编译器的实现 编译原理实习

微型语言编译器的实现 编译原理实习. 2006 年 3 月. 语言的基本描述. 一个非常简单的强制式程序设计语言。 具有简单的数据类型: int, real 只可以声明全局变量。变量先定义后使用 具有赋值语句, if-then-else 语句, while 语句,输入输出语句。. 虚拟机. 假设每个 int/real 值占据一个存储单元,机器有 1K 个存储单元。 指令包括运算指令, MOV 指令, read 指令, write 指令

linnea
Download Presentation

微型语言编译器的实现 编译原理实习

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 微型语言编译器的实现编译原理实习 2006年3月

  2. 语言的基本描述 • 一个非常简单的强制式程序设计语言。 • 具有简单的数据类型:int, real • 只可以声明全局变量。变量先定义后使用 • 具有赋值语句,if-then-else语句,while语句,输入输出语句。

  3. 虚拟机 • 假设每个int/real值占据一个存储单元,机器有1K个存储单元。 • 指令包括运算指令,MOV指令,read指令,write指令 • 双目运算指令:op address1 address2 tgt_address。op可以是+,-,*,/,realAdd, realMinus, realTime, realDiv, >, >=, <, <= , =, and, or, xor …。 • 单目运算指令:op address tgt_address。Op可以是neg,RealToInt, IntToReal • MOV指令:Mov srcAddress tgtAddress;Mov #constant tgtAddress • Read/write指令:Read address;Write address • 跳转指令:JMP #IntegerConst ; ZeroJmp address #IntegerConst

  4. 语言文法(1) • PDec ‘;’ Body • Dec VarDec ‘;’ Dec | VarDec • VarDec ‘var’ identifier ‘:’ Type • Type  ‘real’ | ‘int’ • Body  ComposedStatement • Statement  CompSt | IfSt | WhileSt | AssignSt

  5. 语言文法(2) • CompSt  ‘begin’ StatementList ‘end’ • StatmentList  Statement ‘;’ StatementList | Statement • IfSt  ‘if’ BE ‘then’ Statement ‘else’ Statement ‘fi’ | ‘if’ E ‘then’ Statement ‘fi’ • WhileSt  ‘while’ BE ‘do’ Statement • AssignSt  identifier ‘:=’ E • ReadSt  ‘read’ identifier • WriteSt ‘write’ identifier • EE+T | E-T | T • TT*F | T/F | F • F  ‘(’ E ‘)’ | integerConst | realConst | identifier

  6. 语言文法(3) • BE  AndBE | AndBe ‘or’ BE | AndBE ‘xor’ BE • AndBENotBE | NotBE ‘and’ AndBE • NotBE RelBE | ‘not’ ‘(’ E ‘)’ • RelBE E RelOP E • RelOP ‘>’ | ‘>=’ | ‘<’ | ‘<=’ | ‘==’

  7. 语言的语义解释 • If,while等的语义和通常的程序设计语言一致。 • 在处理表达式时需要进行类型检查,并生成不同的代码。 • 比如var1+var2,对于两者都为int时,为正数加,其结果为正数;否则为实数加,其结果为实数。 • 需要考虑类型变换代码。 • 需要考虑赋值语句的类型问题 • Read为从标准输入设备读入一个值(整数或者实数),write为向标准输出设备写一个值。

More Related