# 电子工程代写|编译器代写Compilers代考|CMSC426

## 电子工程代写|编译器代写Compilers代考|Syntax-Directed Translation

Syntax-directed translation is done by attaching rules or program fragments to productions in a grammar. For example, consider an expression expr gener ated by the production
expr $\rightarrow \operatorname{expr}{1}+$ term Here, expris the sum of the two subexpressions expr $r{1}$ and term. (The subscript in expr $r_{1}$ is used only to distinguish the in stan $œ$ of expr in the production body from the head of the production). We can translate expr by exploiting its structure, as in the following pseudo-code:
translate expr 1 ;
translate term;
han dle $+$
Using a variant of this pseudocode, we shall build a syntax tree for expr in Section $2.8$ by building syntax trees for expr end $_{1}$ term and then handling + by constructing a node for it. For convenience, the example in this section is the translation of infix expressions into postfix notation.

This section introduces two concepts related to syntax-directed translation:

• Attributes. An attribute is any quantity associated with a programming construct. Examples of attributes are dat a types of expressions, the number of instructions in the generated code, or the location of the first instruction in the generated code for a construct, among many other possibilities. Since we use grammar symbols (nonterminals and terminals) to represent programming constructs, we extend the notion of attributes from constructs to the symbols that represent them.

## 电子工程代写|编译器代写Compilers代考|Postfix Notation

The examples in this section deal with translation into postfix notation. The postfix notation for an expression $E$ can be defined inductively as follows:

1. If $E$ is a variable or constant, then the postfix notation for $E$ is $E$ itself.
2. If $E$ is an expression of the form $E_{1}$ op $E_{2}$, where op is any binary operator, then the postfix notation for $E$ is $E_{1}^{\prime} E_{2}^{\prime}$ op, where $E_{1}^{\prime}$ and $E_{2}^{\prime}$ are the postfix notations for $E_{1}$ and $E_{2}$, respectively.
3. If $E$ is a parenthesized expression of the form $\left(E_{1}\right)$, then the postfix notation for $E$ is the same as the postfix notation for $E_{1}$.

Example 2.8: The postfix notation for $(9-5)+2$ is $95-2+$. That is, the translations of 9,5 , and 2 are the constants themselves, by rule (1). Then, the translation of 9-5 is 95 – by rule (2). The translation of (9-5) is the same by rule (3). Having translated the parenthesized subexpression, we may apply rule (2) to the entire expression, with (9-5) in the role of $E_{1}$ and 2 in the role of $E_{2}$, to get the result $95-2+$.

As another example, the postfix notation for $9-(5+2)$ is $952+-$. That is, $5+2$ is first translated into $52+$, and this expression becomes the second argument of the minus sign.

No parentheses are needed in postfix notation, because the position and arity (number of arguments) of the operators permits only one decoding of a postfix expression. The “trick” is to repeatedly scan the postfix string from the left, until you find an operator. Then, look to the left for the proper number of operands, and group this operator with its operands. Evaluate the operator on the operands, and replace them by the result. Then repeat the process, continuing to the right and searching for another operator.

## 电子工程代写|编译器代写Compilers代考|Syntax-Directed Translation

translate expr 1 ;

• 属性。属性是与编程构造相关的任何数量。属性的示例是 dat 表达式类型、生成代码中的指令数量或构造的生成代码中第一条指令的位置，以及许多其他可能性。由于我们使用语法符号（非终结符和终结符）来表示编程构造，我们将属性的概念从构造扩展到表示它们的符号。

## 电子工程代写|编译器代写Compilers代考|Postfix Notation

1. 如果和是一个变量或常数，那么后缀表示法和是和本身。
2. 如果和是形式的表达和1上和2，其中 op 是任何二元运算符，然后是后缀表示法和是和1′和2′操作，在哪里和1′和和2′是后缀符号和1和和2， 分别。
3. 如果和是形式的括号表达式(和1)，然后是后缀表示法和与后缀表示法相同和1.

myassignments-help数学代考价格说明

1、客户需提供物理代考的网址，相关账户，以及课程名称，Textbook等相关资料~客服会根据作业数量和持续时间给您定价~使收费透明，让您清楚的知道您的钱花在什么地方。

2、数学代写一般每篇报价约为600—1000rmb，费用根据持续时间、周作业量、成绩要求有所浮动(持续时间越长约便宜、周作业量越多约贵、成绩要求越高越贵)，报价后价格觉得合适，可以先付一周的款，我们帮你试做，满意后再继续，遇到Fail全额退款。

3、myassignments-help公司所有MATH作业代写服务支持付半款，全款，周付款，周付款一方面方便大家查阅自己的分数，一方面也方便大家资金周转，注意:每周固定周一时先预付下周的定金，不付定金不予继续做。物理代写一次性付清打9.5折。

Math作业代写、数学代写常见问题

myassignments-help擅长领域包含但不是全部: