首页 The principle of compiling is real

The principle of compiling is real

举报
开通vip

The principle of compiling is realThe principle of compiling is real In 2003, the principle of compiling was real (10 points) describe the language of the following formal description and draw a diagram of the state transition of the simplest DFA that accepts the language. (1 | 01) * 0 * T...

The principle of compiling is real
The principle of compiling is real In 2003, the principle of compiling was real (10 points) describe the language of the following formal description and draw a diagram of the state transition of the simplest DFA that accepts the language. (1 | 01) * 0 * There are two words in a language: S ? procedure call statement | index variable assignment statements The form of the procedure call statement is the id (id, id,..., id), which is the variable scale in the parentheses. Subscript variable assignment statement is: in the form of id (id, id,..., id) : = id (id, id,..., id), the assignment number on both sides is the variables in the array name added in parentheses. (a) you complete the procedure call statement and the grammar design of the index variable assignment statement, and you get an LR (1) grammar that starts with statement S. No more than 6 production types, no need to give your grammar the proof of LR (1) grammar. (b) if you want to complete semantic analysis and intermediate code generation at the same time as LR analysis, what's the difficulty with your grammar? 3. (10 points) (a) write a grammar for the following arithmetic expression grammar translation scheme of guidance, it will each subexpression E symbol (that is, the value is greater than zero or less than zero) records in the attribute E.s ign (the attribute values are expressed in POS and NEG). You can assume that all integers are not zero, so you don't have to worry about zero. E ? E * E + E | | -e | unsigned_integer (b) generates stack machine code for the above expression. After the code is executed, the value of the expression is left on the stack. You design the required stack machine instructions and write down the meaning of the instructions. (10) in the C language textbook, it is called an address operator, and a is the address of variable a. But the textbook doesn't say what the expression is and what the type of a is. In addition, the textbook says the array name represents the first address of the array, but it does not specify the type of the value. They brought about by the one problem is that if a is an array, then the expression of a and b & a value is an array of a first address, but there is a difference between their use, beginners when it is difficult to grasp. Below we give you four C files, and you can write the expression a and the type of an & a in terms of compiling the error message and the program. If you can master their types, then their differences are clear and you will use them correctly. (1) file 1: Typedef int A [10], [20]; A, A. A * fun () { Return (a); } When this function is compiled on Linux with GCC, the report's type errors are as follows: Line 6: warning: return from incompatible pointer type (2) file 2: Typedef int A [10], [20]; A, A. A * fun () { Return (& a); } This function is compiled with GCC on Linux without any type of errors. (3) the file 3: Typedef int A [10], [20]; Typedef int B, [20]; A, A. B * fun () { Return (a); } This function is compiled with GCC on Linux without any type of errors. (4) file 4: Typedef int A [10], [20]; A, A. Fun () { Printf (" % d, % d, % d \ n ", a, a + 1, & a + 1); } The main () { Fun (); } The result of this program is: 134518112, 134518192, 134518912 In 2004, the principle of compilation was partially tested The following grammar is LR (1) grammar. Please incorporate the concentric set of the project set of the method LR (1) to illustrate that the method is not LALR (1) grammar. S ? a d | b b d | a | b e b e a A ? c B ? c (10) to write a grammar guide for the following grammar, the value of the binary number produced by S in the following grammar is given by the general attribute of S. For example, when you enter 101.101, s. val: = 5.625. (it is not allowed to modify the grammar.) S ? l. R | L L ? L B | B R ? B R | B? 0 | 1 B (5) on the X86 / Linux machine, the compiler reports that line 15 has an error: Incompatible types in return In the C language, arrays and structures are structural types, why are the following two functions type errors, and the first one? Typedef int A1 [10]. Typedef int A2 [10]. A1. A; Typedef struct {int I; } S1. Typedef struct {int I; } S2; S1 s; A2 * fun1 () { Return (& a); } S2 fun2 () { The return (s); } (5) in C, if a and b are the same structural types, then assigning a = b is ok. But the compiler's implementation of this assignment may be related to their size. Here are two C language programs and the target code that is generated on the X86 / Linux machine (which is a little bit irrelevant to the topic). Briefly describe the difference in the way that these target codes are implemented. (the comments provided in the following assembly program are for reference only.) Struct {long I, j; Double m; A, b}; The main () { A = b; } Main: Pushl % ebp Esp movl %, % ebp Movl $b, % eax Movl $a, % edx Movl (% eax), % ecx Movl % ecx, (% edx) Movl 4 (% eax), % ecx Movl % ecx, 4 (% edx) Movl 8 (% eax), % ecx Movl % ecx, 8 (% edx) Movl 12 (% eax), % eax Movl % eax, 12 (% edx) L1: leave ret Struct {long I, j; Double m, n; A, b}; The main () { A = b; } Main: Pushl % ebp Esp movl %, % ebp Pushl % edi Pushl % esi Movl $a, % edi Movl $b, % esi CLD - sets the direction flag to automatically increment the address pointer Movl $6, % ecx - sets the number of repetitions Rep - means repeat the following instructions Movsl -- transfer instructions L1: (% ebp), % esp Popl % esi Popl % edi leave ret The principles of 2005 compile are real (5) the following is a formal variable statement: (int | float) id () *; Instead of writing a context free grammar, it is equivalent to the formal equivalent. The compiler reports the following error in the C language: 3 + + and (id + id) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Invalid lvalue in increment The following simplified syntax for C expressions: E ? E + E | | E (E) + + | | id num Please write a grammar guidance definition or translation scheme to check if the + + operation object is legal. (10) here is a C program: Long formula (I) Long I; { Return (I * 10); } Long f2 (I) { Return (I * 10); } The main () { Printf (" f1 = % d, f2 = % d \ n ", f1 (10.0), f2 (10.0)) } The functions f1 and f2 are described in different ways. The program runs on the X86 / Linux machine as follows: F1 = 0, f2 = 100 Please explain why it is different to invoke the two functions with the same actual parameter. (5) the optimized compiler does not allocate space to the local variable I and j of the following program. Why? The main () { Long I, j; I = 5; J = I * 2; Printf (" % d \ n ", I + j); } The principle of 2006 compiles the truth (10) here are two different types of grammar: int I, j, k, D D ? ? T L T L T ? int | real T ? int | real L ? L, L ? id | id id, L | id If you use LL (1) analysis,Which grammar should you choose? If you use an LR analysis method, which grammar is better? Give a brief explanation. 2, (6) with the SLR (1) set of grammar can define language, with the LR (1) grammar can define the language of the collection and use LALR (1) grammar can define what is the relationship between language collection? (no reason is needed.) (8 points) here is a function of C: Void f (char c, long j) { Char * p; Char ch; Long m [3]. P = & c; M [0] = j; } The compiled code compiled by a version of the compiler on an x86 / Linux machine is as follows: C ". The file "frame. The version "01.01" Gcc2_compiled. : The text .align 4 . Globl f Type f, @ function F: Parameter j The parameter c The return address Old ebp (control chain) Esp ? Such ebp Stack growth direction High address Lower address Pushl % ebp Esp movl %, % ebp Subl $24, % esp Movl 8 (% ebp), % eax Movb % al, negative 1 (% ebp) Leal-1 (% ebp), % edx Movl % edx, -8 (% ebp) Movl 12 (% ebp), % eax Movl % eax, -24 (% ebp) L1: leave ret Lfe1: Size f, lfe1-f Ident "GCC: (GNU) egcs -2.91.66 19990314 / Linux (egcs-1.1.2 release)" Please will perform a subl $24, % esp, esp and the area between the ebp points to address what the value of a variable (pictured above) is used to store, according to the variable name, relative to the ebp offset, the number of bytes to address list (size). (6) two C language files, link1. C and link2.c are as follows: Int buf [1] = {100}; and Buf extern int *; The main () { Printf (" % d \ n ", * buf); } After the X86 / Linux command cc link1. C link2.c, the runtime produces the following error message: Segmentation fault (core dumped) Please explain why. The principles of compiling in 2007 1, (10 points) Describe the language defined by the normal type b * (abb *) * (a | e), and draw the simplest DFA that accepts the language. 2, (10 points) Prove that grammar E ? E + id | id is the SLR (1) grammar. 3, (10 points) Below is the grammar expression and assignment statements, including the and is of type bool ′ bool ? bool, + is of type int ′ int ? int, = is of type int ′ int ? bool, : = request id and E type is an int or a bool. Write a grammar guidance definition or translation scheme for this article, which completes type checking. S ? id: = E E ? E and E | | E E + E = E | id 4. (5 points) For the following C language file, s.c F1 (int x) { Long x; X = 1; } F2 (int x) { { Long x; X = 1; } } One compiler compiles The Times by mistake: S.c: In function 'f1' : S.c: it's a warning: the declaration of 'x' shadows a parameter Answer: why don't you have a similar warning error for function f2? 5, (5) The following C language program is not optimized and compiled, and the result is when the runtime inputs 2 Area = 12.566360. Addr = -1073743076 After optimized compilation, the result is if run time input 2 Area = 12.566360, addr = -1073743068 Explain why the output is different. The main () { Float s, PI, r; PI = 3.14159; Scanf (" % f ", & r); Printf (" area = % f, addr = % d \ n "). } In 2008, the principle of compiling is a real problem 1, (10 points)Describe the language defined by the formal b * a (bb * a) * b *, and draw the simplest DFA that accepts the language. 2, (10 points) The following grammar produces a series of zeros and ones that represent positive binary Numbers: B ? 0 B | | 1 B The following translation scheme calculates the decimal value of this positive binary number: B ? B1 0 {B.v al: = B1. Val ′ 2} 1 {| B1 B.v al: = B1. Val ′ 2 + 1} | 1 {B.v al: = 1} Please remove the left recursion of this basic grammar and rewrite a translation plan that still computes the decimal value of this positive binary number. 3, (10 points) In C, if the variable I and j are both long types, write the type expression and the type expression for the expression & I and the expression & I - &j. To help you answer the question, here is a program that is output 1 when it runs. The main () { Long I, j; Printf (" % d \ n "); } (10) a C function is as follows: Func (I) long (I); { Long j; J = I - 1; Func (j); } The following assembly code on the left and right sides is the code generated for this function by two different versions of the GCC compiler. The code on the left has the parameter stack before calling the func, and the argument is back on the stack after the call. The right code doesn't really make any difference in the way the parameters are handled. Describe the handling of the parameter passing by the right-hand code and infer the benefits it brings. Func: | func: Pushl % ebp | pushl % ebp Movl % esp, % ebp Subl $4, % esp | subl $8, % esp Movl 8 (% ebp), % edx | movl 8 (% ebp) Decl % edx | decl % eax Movl % edx, -4 (% ebp). Movl-4 (% ebp), % eax, b0 movl-4 (% ebp) Pushl % eax, b0, movl % eax. Call func | call func Addl $4, % esp | leave Leave | ret Ret | 10
本文档为【The principle of compiling is real】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_601191
暂无简介~
格式:doc
大小:43KB
软件:Word
页数:16
分类:文学
上传时间:2017-10-17
浏览量:8