SyntaxDirectedTranslation

上传人:ldj****22 文档编号:48635222 上传时间:2018-07-18 格式:PPT 页数:23 大小:62KB
返回 下载 相关 举报
SyntaxDirectedTranslation_第1页
第1页 / 共23页
SyntaxDirectedTranslation_第2页
第2页 / 共23页
SyntaxDirectedTranslation_第3页
第3页 / 共23页
SyntaxDirectedTranslation_第4页
第4页 / 共23页
SyntaxDirectedTranslation_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《SyntaxDirectedTranslation》由会员分享,可在线阅读,更多相关《SyntaxDirectedTranslation(23页珍藏版)》请在金锄头文库上搜索。

1、Syntax Directed Translation66.648 Compiler Design Lecture (03/16/98)Computer ScienceRensselaer PolytechnicLecture OutlineSyntax Directed Translation Java Virtual Machine Examples AdministrationPhases of a Compiler1. Lexical Analyzer (Scanner)Takes source Program and Converts into tokens2. Syntax Ana

2、lyzer (Parser)Takes tokens and constructs a parse tree.3. Semantic AnalyzerTakes a parse tree and constructs an abstract syntax tree with attributes.Phases of a Compiler- Contd4. Syntax Directed Translation Takes an abstract syntax tree and produces an Interpreter code (Translation output)5. Interme

3、diate-code Generator Takes an abstract syntax tree and produces un- optimized Intermediate code.Syntax Directed Translation SchemeA syntax directed translation scheme is a syntax directed definition in which the net effect of semantic actions is to print out a translation of the input to a desired o

4、utput form.This is accomplished by including “emit” statements in semantic actions that write out text fragments of the output, as well as string- valued attributes that compute text fragments to be fed into emit statements.1. Postfix and Prefix notations:We have already seen how to generate them.Le

5、t us generate Java Byte code.E - E+ E emit(“iadd”);E- E * E emit(“imul”);E- TT - ICONST emit(“sipush ICONST.string);T- ( E )ExamplesWe now present (Read Java Virtual Machine Spec) a simple stack machine and illustrate how to generate code for it via syntax-directed translations.The abstract machine

6、code for an expression simulates a stack evaluation of the postfix representation for the expression. Expression evaluation proceeds by processing the postfix representation from left to right.Abstract Stack MachineEvaluation1. Pushing each operand onto the stack when encountered.2. Evaluating a k-a

7、ry operator by using the value located k-1 positions below the top of the stack as the leftmost operand, and so on, till the value on the top of the stack is used as the rightmost operand.3. After the evaluation, all k operands are popped from the stack, and the result is pushed onto the stack (or t

8、here could be a side-effect)ExampleStmt - ID = expr stmt.t = expr.t | istore aapplied to a = 3*b -cbipush 3iload bimuliload cisubistore aJava Virtual MachineAnalogous to the abstract stack machine, the Java Virtual machine is an abstract processor architecture that defines the behavior of Java Bytec

9、ode programs.The stack (in JVM) is referred to as the operand stack or value stack. Operands are fetched from the stack and the result is pushed back on to the stack.Advantages: VM code is compact as the operands need not be explicitly named.Data TypesThe int data type ca nold 32 bit signed integers

10、 in the range -231 to 2(31) -1.The long data type can hold 64 bit signed integers.Integer instructions in the Java VM are also used to operate on Boolean values.Other data types that Java VM supports are byte, short, float, double. (Your project should handle at least three data types).Selected Java

11、 VM InstructionsJava VM instructions are typed I.e., the operator explicitly specifies what operand types it expects.Expression Evaluationsipush n push a 2 byte signed int on to stackiload vload/push a local variable vistore vstore top of stack onto local var viaddpop tow elements and push their sum

12、isubpop two elements and push their differenceSelected Java VM InstructionsImul pop two elements and push their productiandpop two lements and push their bitwise andiorinegpop top element and push its negationlcmp pop two elements (64 bit integers), push the comparison result. 1 if Vs0 if expr then

13、stmt1 out = newlabel();stmt.t = expr.t| ifnnonnull | out | stmt1.t |label out: nop example:if ( a +90=7) x = x+1; x = x+3;Translating a while statementStmt - WHILE (expr) stmt1 in =newlabel(); out= neewlabel(); stmt.t = label | in| nop | expr.t | ifnonnull| out| stmt1.t | goto | in| label | out Comments and FeedbackProject 3 is out. Please start working. PLEASE do not wait for the due date to come.We are looking at the relevant portion of Java. Please keep studying this material.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号