覆盖学起Plus、超星学习通等平台的全学科作业答案、历年真题与考试速查系统
中国大学MOOC作业考试答案

想要快速找到正确答案?

立即关注 九八五题库微信公众号,轻松解决学习难题!

中国大学MOOC
扫码关注

作业辅导
扫码关注
论文指导
轻松解决学习难题!

中国大学MOOC编译原理(双语教学)作业答案

编译原理(双语教学)

学校: 九八五题库

学校: 超星学习通

题目如下:

1. 1. The differences between "Interpreter" and "compiler”are following: A compiler is an executable program that can read a program in one high-level language and translate it into an equivalent executable program in machine language. An interpreter performs directly the operations implied by the source program. The differences between "Interpreter" and "compiler”: portability execution speed with/without object code with/without optimization debugging capability

答案: 正确

2. 1. The Structure of a Compiler is:

答案: 正确

3. 2. The function of the scanner (Lexical Analyzer) is to identify lexical structure. Input: a stream of chars; Output: a token.

答案: 正确

4. 3. Functions of Char handler (or Preprocessor) are: throw away the comments, compress multiple blank characters, include files (include nested files), Maybe perform some macro expansions (nested macro expansion): (a macro facility is a text replacement capability (two aspects: definition & use). - a macro statement will be expanded into a set of programming language statements or other macro.), compiler option (conditional compilation)

答案: 正确

5. 4. The Parser (Syntax Analyzer) identifies syntax structure. Input: a stream of tokens. Output: On a logical level, some representation of a parse tree. Determine how do the tokens fit together to make up the various syntax entity of a program.

答案: 正确

6. 5. Semantics analysis can be done during syntax analysis phase, intermediate code generator phase, or the final code generator.

答案: 正确

7. 6. Code Optimizer: Detection of undefined variables, Detection of loop invariant computation, Constant folding lRemoval of induction variables, Elimination of common expression.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

8. 7. Peephole Optimizer is a simple but effective technique for locally improving the target code. lExamine a short sequence of target instruction (called peephole) and replacing these instruction by a shorter or faster sequence whenever possible. e.g. redundant instruction elimination, flow-of-control optimization, algebraic simplification, use of machine idioms

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

9. 8. Error Handling (Detection & Reporting) is an important function of the compiler. Errors can be encountered by all of the phases of a compiler. The error messages should be reported to allow the programmer to determine where the errors have occurred. Once the error has been noted the compiler must modify the input to allow the latter phases can continue processing.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

10. 9. A symbol table is a data structure that associates names with information about the user-defined data that are denoted by the names. Programming languages have many kinds of symbols. Other programs may have: position labels, user-defined types, enumerations, …

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

11. 1. A “pass” is a complete traversal of the source program, or a complete traversal of some internal representation of the source program (such as an AST). A pass can correspond to a “phase” but it does not have to! Sometimes a single pass corresponds to several phases that are interleaved in time. What and how many passes a compiler does over the source program is an important design decision.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

12. 1. Self-compiling Compiler means that source and implementation languages are the same.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

13. 2. Self-resident Compiler means that implementation and object languages are the same.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

14. 3. Cross compiler is a compiler that runs on one machine and produces object code for another machine.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

15. 1. Suffix is any # of trailing symbols at the string.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

16. 2. The prefix is any # of leading symbols at the string.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

17. 3. The Substring is any string obtained by deleting a prefix and a suffix.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

18. 4. A nonempty string y is a proper prefix, suffix, sub-string of x if it is a prefix, suffix, sub-string of x and x != y.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

19. 5. A language over ∑ is a set of strings made up of characters drawn from ∑.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

20. 1. Kleen closure (means 'any number of instances') ==> L* = L0 ÈL1 È L2 È...

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

21. 2. Positive closure (means 'one or more instances of') ==> L+ = L1 ÈL2 È ...

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

22. 3. Σ={a,b} Σ*={ε,a,b,aa,ab,ba,bb,aaa,aab,…} Σ+={a,b,aa,ab,ba,bb,aaa,aab,…}

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

23. 1. A grammar G is defined as a 4-tuple (VN,VT,P,S ) VN is a set of nonterminals VT is a set of terminals P is a set of productions,each production consists of a left side,an arrow(or ‘::=’),and a right side S is a designation of one of the nonterminals as the start symbol V =VN ∪ VT is the alphabet of G

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

24. 2. Chomsky hierarchy type 0: α→ β type 1: α A β→ αϒβ type 2: A→ α type 3: A→ a B

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

25. 3. Type 3: context-free grammars Productions are of the form X→v where v is an arbitrary string of symbols in V, and X is a single nonterminal. Wherever you find X, you can replace with v (regardless of context).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

26. 4. Type 1: free or unrestricted grammars. These are the most general. Productions are of the form u→v where both u and v are arbitrary strings of symbols in V, with u non-null. There are no restrictions on what appears on the left or right-hand side other than the left-hand side must be non-empty.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

27. 5. Type 2: context-sensitive grammars. Productions are of the form uXw→uvw where u , v and w are arbitrary strings of symbols in V, with v non-null, and X a single nonterminal. In other words, X may be replaced by v but only when it is surrounded by u and w . (i.e. in a particu lar context).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

28. 6. Type 4: regular grammars Productions are of the form X→a or X→aY where X and Y are nonterminals and a is a terminal. That is the left-hand side must be a single nonterminal and the right-hand side can be either a single terminal by itself or with a single nonterminal. These grammars are the most limited in terms of expressive power.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

29. 1. Capabilities of Context-free grammars: Give precise syntactic specification of programming languages A parser can be constructed automatically by CFG The syntax entity specified in CFG can be used for translating into object code. Useful for describing nested structures such as balanced parentheses, matching begin-end‘s, corresponding if-then-else. CFG is a balance between expressive power and ease of parsing.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

30. 2. A CSG is a 4-tuple (V,T,P,S), where V - a finite set of variables (non-terminals) T - a finite set of terminal symbols (tokens) P - a finite set of productions S - a start symbol and V Ç T = Æ S Î V Productions are of the form: A→θ, where A Î V, θÎ (V+T)*

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

31. 1. Syntax Analysis Goal: we must determine if the input token stream satisfies the syntax of the program.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

32. 2. One-step Derivation (directly derive) ω is derivable from in CFG, if there is a finite sequence of rule applications such that:

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

33. 3. Derivation is a way that a grammar defines a language .In the process of derivation a production is treated as a rewriting rule in which the nonterminal on the left side is replaced by the string on the right side of the production

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

34. 4. In leftmost derivations, the leftmost nonterminal in each sentential is always chosen. If aÞb is a step in which the leftmost nonterminal in a is replaced, we write ab.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

35. 5. In rightmost derivations, the rightmost nonterminal in each sentential is always chosen. If aÞb is a step in which the rightmost nonterminal in a is replaced, we write ab.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

36. 1. Let G=(V, S, P, S) be a CFG. is a sentential form, if

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

37. 2. Let G=(V, S, P, S) be a CFG. is a sentence, if .

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

38. 3. Let G=(V, S, P, S) be a CFG. The Language of G, L(G) =.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

39. 4. If L(G1)=L(G2),grammar G1 is equivalence to G2.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

40. 5. A parse tree is a graphical representation of a derivation that filters out the order in which productions are applied to replace nonterminals. Each interior node of a parse tree represents the application of a production. The interior node is labeled with the nonterminal A in the head of the production; the children of the node are labeled, from left to right, by the symbols in the body of the production by which this A was replaced during the derivation.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

41. 1. A grammar that produces more than one parse tree for some sentence is said to be ambiguous. Put another way, an ambiguous grammar is one that produces more than one leftmost derivation or more than one rightmost derivation for the same sentence.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

42. 2. A grammar symbol X (terminal or nonterminal) is useless if there is no derivation of the form start symbol S. That is, X can never appear in the derivation of any sentence.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

43. 1. Objectives of Top-Down Parsing:An attempt to find a leftmost derivation for an input string. An attempt to construct a parse tree for the input string starting from the root and creating the nodes of the parse tree in preorder.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

44. 2. A grammar is said to be left-recursive if it has a nonterminal A, there is a derivation AAθ for some θ.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

45. 3. Problems for top-down parsing: (1) left-recursion (can cause a top-down parser to go into an infinite loop). (2) backtracking - undo not only the movement but also the semantics entering in symbol table. (3) the order the alternatives are .

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

46. 4. Recursive descent: use a collection of mutually recursive routines to perform the syntax analysis.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

47. 5. Predicative Parsing: Features: maintains a stack rather than recursive calls Components: 1. An input buffer with end marker ($) 2. A stack with endmarker ($) on the bottom 3. A parsing table, a two-dimensional array M[A,a], where ‘A’ is a nonterminal symbol and ‘a’ is the current input symbol (terminal/token).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

48. 1. Objectives of Shift-Reduce (LR) Parsing: Attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working towards the root (the top). i.e., reduce a string ω to the start symbol of a grammar. At each reduction step a particular substring matching the right side of a production is replaced by the left nonterminal symbol. A rightmost derivation is traced out in reverse.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

49. 2. Handles: A substring that matches the right side of a production, and whose reduction to the nonterminal on the left side of the production represents one step along the reverse of a rightmost derivation. However, in many cases the leftmost substring 'b' that matches the right side of some production A→b is not a handle, because a reduction by the production yields a string that cannot be reduced to the start symbol.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

50. 3. A handle of a right sentential form g is a production A→b and a position of g where the string b may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation of g. i.e., S aAωÞ abω, then A→b in the position following a is a handle of abω. The string ω to the right of the handle contains only terminal symbols. Handle = leftmost complete subtree.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

51. 4. Stack Implementation of Shift-Reduce Parsing: There are four actions a parser can make: (1) shift (2) reduce (3) accept (4) error. There is an important fact that justifies the use of a stack in shift-reduce parsing: the handle will always eventually appear on top of the stack, never inside.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

52. 1. DFA M = (Q, S, d, q0, F), where Q = a finite set of states, one of which is designated the initial state or start state, and some of which are designated as final states. S = a set of finite characters, alphabet of possible input symbols. d = A finite set of transitions that specifies for each state and for each symbol of the input alphabet, which state to go to next. Transition d : Q ´ S à Q q0 = starting state. F = a set of accepting states (a subset of Q i.e. F Í Q ).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

53. 2. A DFA is may represented by a transition table. The transition table is usually sparse. Alternatively, DFA can be represented as a program.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

54. 3. A DFA is may represented by a directed graph. Each vertex corresponds to a state. There is an edge from the vertex corresponding to state qi to vertex corresponding to state qj if the FA has a transition from state qi to qj on input a.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

55. 1. The language defined by an F. A. (Finite Automata) is the set of input string it accepts. A string x is accepted by a FA if d (q0, x) Î F. So, the language accepted by M is L(M) = { x | d (q0, x) Î F}. DFA A1 and A2, if L(A1)=L(A2), DFA A1 is Equivalence to A2.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

56. 2. NDFA M' = (Q', S, d', Q0, F), where Q' = a finite set of states, S = a set of finite characters, d' = transition function: d': Q' ´ ( S È e) à Q', Q0 = a set of starting states and F = a set of accepting states, F Í Q'.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

57. 3. Given a string x belonging to S* if there exists a path in a NFA M', arrived to a final state after reading x, then x is accepted by M'. i.e., if d'(q0, x) = a Í Q', a∩F ≠ Æ and q0∈ Q0 then x belongs to L(M').

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

58. 1. e-closure(I) is defined as the set of states that can be reached from s on e-transition alone. Method: 1. s is added to e-closure(I) if sÎI. 2. if s is in e-closure(I), and there is an edge labeled e from s to t then t is added to e-closure(I) if t is not already there.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

59. 2. move(T, a): a set of NFA states to which there is a transition on input symbol a from some state s in T.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

60. 3. e-closure({5,3,4})={2,3,4,5,6,7,8}

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

61. 4. move({1,2},a)={5,3,4}

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

62. 1. Regular Expression is the notation we use to describe regular sets (regular languages).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

63. 2. Each regular expression denotes a (regular) language. Let r be a regular expression, then L(r) be the (regular) language generated by r.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

64. 3. f is a regular expression denoting { }. i.e., L(f) = { }

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

65. 4. e is a regular expression denoting {e}, the language containing only empty string e. L(e) = {e}

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

66. 5. For each a in S, a is a regular expression denoting {a}, a single string a. i.e., L(a) = { a }

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

67. 6. If R and S are regular expressions then (R)|(S), (R)(S), (R)* are regular expressions denoting union, concatenation, kleen closure of R and S.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

68. 7. Let alphabet = {a,b}, Thus the regular expression a denotes {a} which is different from just the string a.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

69. 8. The regular expression (a|b)(a|b)(a|b)(a|b)* is equal to e|a|b|(a|b)(a|b)(a|b)*

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

70. 1. Give names to regular expressions and then define regular expressions using these names as if they were symbols. A sequence of definitions of the form d1 = r1 d2 = r2 ………. dn = rn where di is a distinct name and ri is a regular expression over the symbols in S È {d1 , d2, ..., di-1}

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

71. 2. For r1•r2 we construct the NFA =

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

72. 3. For r1|r2 we construct the NFA =

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

73. 4. For r1* we construct the NFA =

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

74. 1. Two pointers (one marks the beginning of the token & the other one is a lookahead pointer) delimit the context of the token string.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

75. 1. Using “subset construction” will get the follow table:

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

76. 2. Using “subset construction” will get the follow table: And get the DFA is:

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

77. 3. NFA: so, M is:

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

78. 1. Every DFA has a unique smallest equivalent DFA.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

79. 2. Use splitting to construct the equivalent minimal DFA. Two subsets can be identified as one state of the DFA, provided: 1.They have the same non-e-transition-only states 2.They either both include or both exclude accepting states of the NFA

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

80. 3. DFA: Initially, two sets {1, 2, 3, 5, 6}, {4, 7}. {1, 2, 3, 5, 6} splits {1, 2, 5}, {3, 6} on c. {1, 2, 5} splits {1}, {2, 5} on b. so finally we have:

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

81. 4. DFA: so finally we have:

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

82. 1. Right linearity grammar à Transition Graph: Step 1:Add state t as final state (assume tÏ(VT∪VN) Step 2:Every nonterminals are made to states Step 3: (1) Form as A®aB production, transition from state A to B labeled with character a. (2) Form as A®a production, transition from state A to final state t labeled with character a. Step 4:Make the start symbol S as the start state

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

83. 2. G: S®aA|b A®aA|a Equivalence FA of G is:

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

84. 1. A lexical analyzer must be able to do three things: 1. Remove all whitespace and comments. 2. Identify tokens within a string. 3. Return the lexeme of a found token, as well as the line number it was found on.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

85. 2. The lexical structures are usually specified (described) by sequence of regular expression and analyzed via finite state automata.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

86. 1. A token is a sequence of characters that represents a unit of information in the source program.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

87. 2. The set of strings is described by a rule called a pattern associated with the token. The pattern is said to match each specific string in the set.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

88. 3. A lexeme is a sequence of characters in the source program that is matched by the pattern for a token.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

89. 4. Each pattern is a regular language, i.e., it can be described by a regular expression.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

90. 5. The output of our lexical analysis phase is a streams of tokens. A token is a syntactic category.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

91. 6. Common Lexical Categories: keywords (not necessarily to be a reserved word); Identifiers; Literals (strings) and constants (numbers); Operators; Punctuation symbols: e.g.'(', ';', ','.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

92. 1. In lex, a dot will match any single character except a newline.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

93. 2. In lex, star and plus used to match zero/one or more of the preceding expressions.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

94. 3. In lex, the pattern [0-9]+(\.[0-9]+)? identifies an integer or floating point number.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

95. 1. In top-down parsing, you start with the start symbol and apply the productions until you arrive at the desired string.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

96. 2. In the top-down parsing, we begin with the start symbol and at each step, expand one of the remaining nonterminals by replacing it with the right side of one its productions.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

97. 3. The Precondition of Deterministic Top-Down Parsing is:(1) non-Left Recursion, (2) non-backtracking

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

98. 1. The action of PDA is determined: Current state; Current input character; Current top of stack.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

99. 2. Definition:A PDA is a 7-tuple M=(Q, Σ,H, δ,q0,z0,F),where: Q is a finite set of elements called states ; Σ is a finite input alphabet ; H is a set of stack symbols ; δ is a finite set of transitions from Q×(Σ∪{ε})×H to Q×H*; q0∈Q called the initial state ; z0∈H called the initial symbol in stack; F Í Q called the finish states.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

100. 3. Given a pushdown automaton A, and a string ω Î S*, we say that ω is accepted by A if at least one of the configurations yielded by (q0,ω,ε) is a configuration of the form (f,ε,ε) with f a favorable state

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

101. 4. Accepting Strings of PDA is One may define acceptance “by final state” only. The input is accepted if and only if the last state is a final state, regardless of whether the stack is empty. One may define acceptance “by empty stack” only. The input is accepted if and only if the stack is empty once the input is processed, regardless of which state the PDA is in.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

102. 1. How a table-driven predictive parser works? We push the start symbol on the stack and read the first input token. As the parser works through the input, there are the following possibilities for the top stack symbol X and the input token terminal a: 1. If X = a and a = end of input ($): parser halts and parse completed successfully 2. If X = a and a != $: successful match, pop X and advance to next input token. This is called a match action. 3. If X != a and X is a nonterminal, pop X and consult table at [X,a] to see which production applies, push right side of production on stack. This is called a predict action. 4. If none of the preceding cases applies or the table entry from step 3 is blank, there has been a parse error

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

103. 1. LL(k) Grammar that is the first “L” means we scan the input from Left to right; the second “L” means we create a Leftmost derivation; and the k means k input symbols of Look ahead.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

104. 2. Def. First(a) /*a denotes grammar symbol*/ is the set of terminals that begin the string derived from a. If a⇒*e, then e is also in First(a). First(α)={a|α⇒*a…,a ∈VT} if α⇒*ε,ε∈First(α).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

105. 3. Def. Follow(A), A is a nonterminal, is the set of terminals a that can appear immediately to the right of A in some sentential form, that is, the set of terminals 'a' s.t. there exists a derivation of the form S ⇒+ aAab for some a and b. If A can be the rightmost symbol in some sentential form, then $ is in Follow(A). Follow(A)={a|S ⇒+ …Aa…,a∈VT} If S ⇒+…A,$∈ Follow(A)

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

106. 4. The first set of a sequence of symbols α, written as First(α), is the set of terminals which start all the sequences of symbols derivable from α. A bit more formally, consider all strings derivable from α by a leftmost derivation. If α ⇒* β , where β begins with some terminal, that terminal is in First(α). If α ⇒* e , then e is in First(α).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

107. 5. The follow set of a nonterminal A is the set of terminal symbols that can appear immediately to the right of A in a valid sentence. A bit more formally, for every valid sentence S ⇒* αAβ , where β begins with some terminal, that terminal is in Follow(A).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

108. 6. To calculate First(α) where α has the form X1X2...Xn, do the following: 1. If X1 is a terminal, then add X1 to First(α), otherwise add First(X1) - e to First(α) . 2. If X1 is a nullable nonterminal, i.e., X1 ⇒* e , add First(X2) - e to First(α). Furthermore, if X2 can also go to e , then add First(X3) - e and so on, through all Xn until the first nonnullable one. 3. If X1X2...Xn ⇒* e , add e to the first set.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

109. 1. A LR(1) item is: A→a•b,a where a is the look-ahead of the LR(1) item(a is a terminal or end-marker.)

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

110. 2. When b ( in the LR(1) item A→a•b,a ) is not empty, the look-ahead does not have any affect. When b is empty (A→a•,a ), we do the reduction by A→a only if the next input symbol is a (not for any terminal in FOLLOW(A)). A state will contain A→a•,a1 where {a1,…,an} Í FOLLOW(A) … A→a•,an

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

111. 3. closure(I) is: (where I is a set of LR(1) items) every LR(1) item in I is in closure(I) if A→a•Bb, a in closure(I) and B→g is a production rule of G, then B→•g,b will be in the closure(I) for each terminal b in FIRST(ba).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

112. 4. function closure (I) //I denotes a set of LR(1) items { do { for (each item [A→a•Bb, a] in I, each production B→g in G' and each terminal b in First(ba) if [B→•g, b] is not in I) add [B→•g, b] to I; } while (no more items can be added to I); return I; }

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

113. 5. If I is a set of LR(1) items and X is a grammar symbol (terminal or non-terminal), then goto(I,X) is defined as follows: If A→a•Xb,a in I then every item in closure({A→aX•b,a}) will be in goto(I,X).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

114. 6. function goto(I, X) { Let J be the set of items [A→aX•b, a] such that [A→a•Xb, a] is in I; return closure (J); }

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

115. 7. Construction of The Canonical LR(1) Collection: Algorithm: C is { closure({S'→•S,$}) } repeat the followings until no more set of LR(1) items can be added to C. for each I in C and each grammar symbol X if goto(I,X) is not empty and not in C add goto(I,X) to C goto function is a DFA on the sets in C.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

116. 8. SLR(1) grammar is an LR(1) grammar, but for an SLR(1) grammar the canonical LR parser may have more states than the SLR parser for the same grammar.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

117. 9. A set of LR(1) items containing the following items A→a•b,a1 … A→a•b,an can be written as A→a•b,a1/a2/…/an

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

118. 10. Construction of LR(1) Parsing Tables: 1. Construct the canonical collection of sets of LR(1) items for G'. C¬{I0,…,In} 2. Create the parsing action table as follows If a is a terminal, A→a•ab,b in Ii and goto(Ii,a)=Ij then action[i,a] is shift j. If A→a•,a is in Ii, then action[i,a] is reduce A→a where A¹S'. If S'→S•,$ is in Ii, then action[i,$] is accept. If any conflicting actions generated by these rules, the grammar is not LR(1). 3. Create the parsing goto table for all non-terminals A, if goto(Ii,A)=Ij then goto[i,A]=j 4. All entries not defined by (2) and (3) are errors. 5. Initial state of the parser contains S'→•S,$

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

119. 1. Calculating Follow(A) for all nonterminals A: 1. Place $ in Follow(S), where S is the start symbol and $ is the input buffer end marker. 2. If there is a production A→aBb, then everything in First(b) except for e is placed in Follow(B). 3. If there is a production A→aB, or a production A→aBb where First(b) contains e, then everything in Follow(A) is in Follow(B).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

120. 2. Predictive parser is a non-backtracking top-down parser. A predictive parser is characterized by its ability to choose the production to apply solely on the basis of the next input symbol and the current nonterminal being processed.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

121. 3. A grammar G is LL(1) if whenever A→α|β are two distinct productions of G, the following conditions hold: - for no terminal a do both α and β derive strings beginning with a (i.e. first sets are disjoint) - at most one of α and β can derive the empty string - if β ⇒* e then β does not derive any string beginning with a terminal in Follow(A) - Conditions: 1.FIRST(α)∩FIRST(β ) = f, 2.If β ⇒* e, FIRST(α) ∩ FOLLOW(A)=f.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

122. 4. Making grammars LL(1) Eliminating left recursion Factor out the common prefix Production A®aβ|ag transform as: A®aB B®β|g

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

123. 5. Factor out the common prefix: A→δβ1| δβ2|…| δβn Transform as: A→δA' A'→β1| β2| …|βn General form A→γα1|γα2| βα3|βα4| β Transform as : A→γA'|βA” A'→α1| α2 A”→α3| α4|ε

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

124. 6. G[E]: (1)E→TE' (2)E'→+TE' (3)E'→e (4)T→FT' (5)T'→*FT' (6)T'→e (7)F→(E) (8)F→a FIRST sets of VN: FIRST(E)={(,i} FIRST(E')={+,ε} FIRST(T)={(,i} FIRST(T')={*,ε} FIRST(F)={(,i} FOLLOW sets of VN: FOLLOW(E)={),$} FOLLOW(E')={),$} FOLLOW(T)={+,),$} FOLLOW(T')={+,),$} FOLLOW(F)={*,+,),$}

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

125. 1. Constructing the parsing table: 1. For each production A→α of the grammar, do steps 2 and 3 2. For each terminal a in First(α) , add A→α to M[A,a] 3. If e in First(α) , (i.e. A is nullable) add A→α to M[A,b] for each terminal b in Follow(A), If e in First(α) , and $ is in Follow(A), add A→α to M[A,$] 4. All undefined entries are errors

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

126. 2. How a table-driven predictive parser works: We push the start symbol on the stack and read the first input token. As the parser works through the input, there are the following possibilities for the top stack symbol X and the input token nonterminal a: 1. If X = a and a = end of input ($): parser halts and parse completed successfully 2. If X = a and a != $: successful match, pop X and advance to next input token. This is called a match action. 3. If X != a and X is a nonterminal, pop X and consult table at [X,a] to see which production applies, push right side of production on stack. This is called a predict action. 4. If none of the preceding cases applies or the table entry from step 3 is blank, there has been a parse error

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

127. 3. Panic-mode error recovery is a simple technique that just bails out of the current construct, looking for a safe symbol at which to restart parsing. The parser just discards input tokens until it finds what is called a synchronizing token. The set of synchronizing tokens are those that we believe confirm the end of the invalid statement and allow us to pick up at the next piece of code

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

128. 1. Recursive descent: use a collection of mutually recursive routines to perform the syntax analysis.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

129. 2. Left Factoring : A→ab1 | a b2 ⇒ A→a A' A'→b1 | b2 Methods: 1. For each nonterminal A find the longest prefix a common to two or more of its alternatives. If a ¹ e replace all the A productions A→a b1 | a b2 | ... | a bn |others by A→a A'|others A'→b1| b2 | ... | bn 2. Repeat the transformation until no more found e.g. S→iCtS | iCtSeS | a C→b ⇒ S→iCtSS' | a S'→eS | e C→b

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

130. 3. A recursive descent parser consists of several small functions(procedures), one for each nonterminal in the grammar. As we parse a sentence, we call the functions (procedures) that correspond to the left side nonterminal of the productions we are applying. If these productions are recursive, we end up calling the functions recursively.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

131. 1. Objectives of Shift-Reduce (Bottom-Up) Parsing: Attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working towards the root (the top). i.e., reduce a string ω to the start symbol of a grammar. At each reduction step a particular substring matching the right side of a production is replaced by the left nonterminal symbol. A rightmost derivation is traced out in reverse.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

132. 2. In bottom-up parsing, we start with the tokens in the input string rather than with the starting symbol of the grammar. The problem is really that of the identification of the handle – the substring to be matched by the RHS of a production and replaced by the LHS. The bottom-up parsers described here all fall into the category of shift-reduce parsers. The shift operation reads and stores an input symbol, and the reduce operation matches a group of adjacent stored symbols with the RHS of a production and replaces the stored group with the LHS of the production.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

133. 1. Sentence: u Î Vt*, S ⇒* u Sentential form: b Î (Vt ∪ Vn)*, S ⇒*b Phrase: b Î (Vt ∪ Vn)*, A Î Vn, A ⇒*b Simple phrase: leaves of a subtree of height 1 Handle of a sentential form: leftmost simple phrase of the s.f.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

134. 2. Informally, a handle of a string is a substring that matches the right side of a production rule. But not every substring matches the right side of a production rule is handle A handle of a right sentential form g (=abw) is a production rule A → b and a position of g where the string b may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation of g. S ⇒(*|rm) aAw ⇒(*|rm) abw If the grammar is unambiguous, then every right-sentential form of the grammar has exactly one handle. We will see that w is a string of terminals.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

135. 3. A right-most derivation in reverse can be obtained by handle-pruning. S⇒α0⇒α1⇒α2⇒...⇒αn-1⇒αn= w(input string) Start from αn, find a handle (An→)bn in αn, and replace bn by An to get αn-1. Then find a handle (An-1→)bn-1 in αn-1, and replace bn-1 by An-1 to get αn-2. Repeat this, until we reach S.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

136. 4. Canonical derivation is a right-most derivation.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

137. 1. There are four possible actions of a Shift-Reduce parser action: Shift : The next input symbol is shifted onto the top of the stack. Reduce: Replace the handle on the top of the stack by the non-terminal. Accept: Successful completion of parsing. Error: Parser discovers a syntax error, and calls an error recovery routine. Initial stack just contains only the end-marker $. The end of the input string is marked by the end-marker $.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

138. 2. There are context-free grammars for which shift-reduce parsers cannot be used. Stack contents and the next input symbol may not decide action: shift/reduce conflict: Whether make a shift operation or a reduction. reduce/reduce conflict: The parser cannot decide which of several reductions to make.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

139. 3. Actions of a Shift-Reduce Parser: Shift to sn Push input token into the symbol stack Push sn into state stack Advance to next input symbol Reduce rn Pop both stacks as many times as the number of symbols on the RHS of rule n Push LHS of rule n into symbol stack Lookup [top of the state stack][top of symbol stack] Push that state (in goto k) into state stack

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

140. 1. Given two sets, A and B, the Cartesian product of A and B, denoted by A ´ B, is the following set: {(a,b) | a ÎA and b Î B}

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

141. 2. Given three sets, A, B and C, the Cartesian product of A, B, and C denoted by A ´ B ´ C, is the following set: {(a,b,c) | a ÎA, b Î B, c Î C}

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

142. 3. Given two sets, A and B, A relation R is any subset of A ´ B. In other words, R Í A ´ B. We want to indicate which elements in A are related to which elements in B.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

143. 4. U FIRST S Û U→Sβ U LAST S Û U→αS FIRST+,FIRST* U FIRST+ S Û U→U1β,U1→U2β,… ,Un→Sβ。 U FIRST* S Û U=S or U FIRST+ S LAST+, LAST* U LAST+ S Û U→αU1,U1→αU2,…,Un→αS U LAST* S Û U=S or U LAST+ S

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

144. 1. The precedence relationships between all pairs of symbols. The following rules are used: L±R: if there exists a production U→αLRβ L<∙R: if there exists a production U→αLVβ,and V FIRST+ R; L∙>R: if there exists a production U→αVWβ,and V LAST+ L and W FIRST* R, R is a terminal.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

145. 2. According the definitions: ± construction: every RHS X1X2…Xn, Xi ± Xi+1。 <∙ construction: L <∙ R Û There exists a production U→αLVβ, L ± V V FIRST+ R。 So <∙ = (±)(FIRST+)

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

146. 3. According the definitions: ± construction: every RHS X1X2…Xn, Xi ± Xi+1。 ∙> construction: L∙>R Û There exists a production U→αVWβ, V ±W V LAST+ L L (LAST+ )T V W FIRST* R So ∙> = (LAST+ )T ± FIRST*

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

147. 4. A sentential form Sm…Sn of simple precedence grammar if in this form L-1<∙L±L+1±L+2±…±R-1±R∙>R+1 LL+1L+2…R-1R is the handle

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

148. 5. At first sight, precedence parsing looks like a good technique – it is simple and implementations can be very efficient. However, it is a technique that is now rarely used in practice because it is difficult, if not impossible, to transform an “average” programming language grammar into a precedence form.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

149. 1. Using definitions To Construct Operator Precedence Relations: 1 a≒ b Û P→…ab… or P→..aQb… 2 Definition Û FIRSTVT(P)={a|P ⇒+ a… or P ⇒+ Qa…} LASTVT(P)={a|P ⇒+ … a or P ⇒+ … aQ} If there exists A→…aP…and b∈FIRSTVT(P), a <∙b If there exists A→…Pb…and a∈FIRSTVT(P), a∙>b

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

150. 2. Using other relations To Construct Operator Precedence Relations: 1 a≒ b Û P→…ab… or P→..aQb… 2 <∙ = (±)(FIRST*)(FIRSTTERM) = (±)(FIRSTTERM+) ∙> = ((LAST*)(LASTTERM))T(±) = (LASTTERM+)T(±)

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

151. 3. U FIRSTTERM T Û U→T… or U→VNT… U LASTTERM T Û U→…T or U→…TVN

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

152. 4. Simple Phrase is the phase: A simple phrase contains at least one terminal; A simple phrase does not contain other simple phrases.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

153. 5. Sentential form [N1]T1…[Ni-1]Ti-1[Ni]Ti…[Nj]Tj[Nj+1]Tj+1…[Nk]Tk[Nk+1] If the leftmost substring is Ti-1<∙Ti ≒Ti+1≒…≒Tj∙>Tj+1 Then [Ni]Ti…[Nj]Tj[Nj+1] is the leftmost simple phrase.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

154. 1. The intention of the precedence relations is to find the handle of a right-sentential form, <∙ with marking the left end, ≒ appearing in the interior of the handle, and ∙> marking the right hand. In our input string $a1a2...an$, we insert the precedence relation between the pairs of terminals (the precedence relation holds between the terminals in that pair).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

155. 2. To Find The Handles: 1. Scan the string from left end until the first ∙> is encountered. 2. Then scan backwards (to the left) over any ≒ until a <∙ is encountered. 3. The handle contains everything to left of the first ∙> and to the right of the <∙ is encountered.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

156. 3. Operator-Precedence parsing cannot handle the unary minus when we have also the binary minus in our grammar. The best approach to solve this problem, let the lexical analyzer handle this problem. The lexical analyzer will return two different operators for the unary minus and the binary minus. The lexical analyzer will need a look ahead to distinguish the binary minus from the unary minus.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

157. 1. For symbols a and b. f(a) < g(b) whenever a <∙ b f(a) = g(b) whenever a ≒ b f(a) > g(b) whenever a .> b

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

158. 2. Floyd Algorithm: Step 1: S∈V, f(S)=g(S)=1 Step 2: Sj<∙Si, if f(Sj)>g(Si), then g(Sj)=f(Si)+1; Step 3: Sj∙>Si, if f(Sj) < g(Si), then f(Sj)=g(Si)+1; Step 4: Sj≒Si, if f(Sj) = g(Si), then f(Sj)=g(Si)=max(f(Sj), g(Si)); Step 5: Repeat Step 2-4, until f and g not augmentation. If f or g > 2n, then there is no precedence functions.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

159. 1. LR parsing is attractive because: LR parsing is most general non-backtracking shift-reduce parsing, yet it is still efficient. The class of grammars that can be parsed using LR methods is a proper superset of the class of grammars that can be parsed with predictive parsers. LL(1)-Grammars Ì LR(1)-Grammars An LR-parser can detect a syntactic error as soon as it is possible to do so a left-to-right scan of the input.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

160. 2. LR(k) parsers decide the next action by examining the tokens already shifted and at most k look ahead tokens.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

161. 3. LR (k) is the most powerful of deterministic bottom-up parsers with at most k look ahead tokens.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

162. 4. LR parsers are driven by: Action table, which specifies what actions to take (shift, reduce, accept or error). Goto table, which specifies state transition and we push states, rather than symbols, onto the stack. Each state represents a subtree of the parse tree.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

163. 5. A configuration of a LR parsing is: Stack Rest of Input ( So X1 S1 … Xm Sm, ai ai+1 … an $ ) Sm and ai decides the parser action by consulting the parsing action table. (Initial Stack contains just So ) A configuration of a LR parsing represents the right sentential form: X1 … Xm ai ai+1 … an $

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

164. 6. Actions of A LR-Parser: 1. Shift s -- shifts the next input symbol and the state s onto the stack ( So X1 S1 … Xm Sm, ai ai+1 … an $ ) ⇒ ( So X1 S1 … Xm Sm ai s, ai+1 … an $ ) 2. Reduce A→b (or rn where n is a production number) Pop 2|b| (=r) items from the stack; Then push A and s where s=goto[sm-r,A] ( So X1 S1 … Xm Sm, ai ai+1 … an $ ) ⇒ ( So X1 S1 … Xm-r Sm-r A s, ai … an $ ) Output is the reducing production reduce A→b 3. Accept – Parsing successfully completed 4. Error -- Parser detected an error (an empty entry in the action table).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

165. 7. Reduce Action: Pop 2|b| (=r) items from the stack; let us assume that b = Y1Y2…Yr Then push A and s where s=goto[sm-r,A] ( So X1 S1 … Xm-r Sm-r Y1 Sm-r …Yr Sm, ai ai+1 … an $ ) ⇒ ( So X1 S1 … Xm-r Sm-r A s, ai … an $ ) In fact, Y1Y2…Yr is a handle. X1 … Xm-r A ai … an $ Þ X1 … Xm Y1…Yr ai ai+1 … an $

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

166. 1. LR Parsing Algorithm: shift-reduce-driver /*look ahead 1 token*/ { push( start_state ); T = scanner(); do { S = state on top of stack switch( action(S,T) ) case shift: push( state(S,T) ); T = scanner(); break; case reduce i: m = length of RHS of prod. i; pop( m ); S = state on top of stack after poping; X = LHS of prod. i; push( state(S,X) ); break; case accept: … case error: … end } forever }

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

167. 2. LR Parsers Advantages: (1) LR parsers can be constructed to recognize all programming language construct for which context-free grammars can be written. (2) The LR parsing method is more general and efficient than other shift-reduce technique. (3) The class of grammars that can be parsed by LR parser is the proper superset of the class of grammars that can be parsed by predictive parsers. (4) LR parsers can detect errors in syntax as soon as possible

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

168. 1. Building a LR(0) parsing table steps: Add the special production S'→S Find the items of the CFG Create the DFA – using closure and goto functions Build the parsing table

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

169. 2. The set of prefixes of right sentential forms that can appear on the stack of a shift-reduce parser are called viable prefixes.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

170. 3. At the heart of the table construction is the notion of an LR(0) configuration or item. A configuration is a production of the grammar with a dot at some position on its right side. An LR(0) item of a grammar G is a production of G with a dot (•) at some position of the right side.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

171. 4. A→XYZ has 4 items: A→•XYZ A→X•YZ A→XY•Z A→XYZ• A→e has one item A→• Items can be denoted by pairs of integers in computer.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

172. 5. The • indicates how much of a RHS has been shifted into the stack. An item with the • at the end of the RHS, A→X1 X2 … Xj• indicates (or recognized) that RHS should be reduced to LHS. An item with the • at the beginning of RHS, A→•X1 X2 … Xj predicts that RHS will be shifted into the stack.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

173. 6. Sets of LR(0) items will be the states of action and goto table of the SLR parser. A collection of sets of LR(0) items (the canonical LR(0) collection) is the basis for constructing SLR parsers.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

174. 7. G' is G with a new production rule S'→S where S' is the new starting symbol. Augment the grammar G to become G'. Add a production S'→S S is the start symbol and S' is the new start symbol.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

175. 8. Closure (I) /* I is a set of items for a grammar G */ 1. Every item in I is in Closure(I). 2. If A→a•Bb is in closure (I) and B→g is a production, then add the item B→•g to closure(I), if it is not already there, apply this rule until no more new items can be added to closure (I). Closure (I) for I is exactly the e-closure of a set of NFA states.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

176. 9. G'[E']: E'→E E→E + T | T T→T * F | F F→(E) | id E'→E closure({E'→•E}) = { E→E+T E'→•E (kernel items) E→T E→•E+T T→T*F E→•T T→F T→•T*F F→(E) T→•F F→id F→•(E) F→•id }

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

177. 10. If I is a set of LR(0) items and X is a grammar symbol (terminal or non-terminal), then goto(I,X) is defined as follows: If A→a•Xb in I then every item in closure({A→aX•b}) will be in goto(I,X).

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

178. 11. G'[E']: E'→E E→E + T | T T→T * F | F F→(E) | id I ={E'→•E, E→•E+T, E→•T, T→•T*F, T→•F, F→•(E), F→•id } goto(I,E) = { E'→E•, E→E•+T } goto(I,T) = { E→T•, T→T•*F } goto(I,F) = {T→F• } goto(I,() = { F→(•E), E→•E+T, E→•T, T→•T*F, T→•F, F→•(E), F→•id } goto(I,id) = { F→id• }

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

179. 12. To create the SLR parsing tables for a grammar G, we will create the canonical LR(0) collection of the grammar G'. Algorithm: C is { closure({S'→•S}) } repeat the followings until no more set of LR(0) items can be added to C. for each I in C and each grammar symbol X if goto(I,X) is not empty and not in C add goto(I,X) to C goto function is a DFA on the sets in C.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

180. 1. LALR stands for Look Ahead LR. LALR parsers are often used in practice because LALR parsing tables are smaller than LR(1) parsing tables. The number of states in SLR and LALR parsing tables for a grammar G are equal. But LALR parsers recognize more grammars than SLR parsers. Yacc creates a LALR parser for the given grammar. A state of LALR parser will be again a set of LR(1) items.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

181. 2. Canonical LR(1) Parser ⇒"shrink # of states" ⇒ LALR Parser This shrink process may introduce a reduce/reduce conflict in the resulting LALR parser (so the grammar is NOT LALR) But, this shrink process does not produce a shift/reduce conflict.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

182. 3. The core of a set of LR(1) items is the set of its first component. We will find the states (sets of LR(1) items) in a canonical LR(1) parser with same cores. Then we will merge them as a single state. We will do this for all states of a canonical LR(1) parser to get the states of the LALR parser. In fact, the number of the states of the LALR parser for a grammar will be equal to the number of states of the SLR parser for that grammar.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

183. 4. Create the canonical LR(1) collection of the sets of LR(1) items for the given grammar. Find each core; find all sets having that same core; replace those sets having same cores with a single set which is their union. C={I0, …,In} ⇒ C'={J1,…,Jm} where m £ n Create the parsing tables (action and goto tables) same as the construction of the parsing tables of LR(1) parser. Note that: If J=I1 È … È Ik since I1,…,Ik have same cores ⇒ cores of goto(I1,X),…,goto(I2,X) must be same. So, goto(J,X)=K where K is the union of all sets of items having same cores as goto(I1,X). If no conflict is introduced, the grammar is LALR(1) grammar. (We may only introduce reduce/reduce conflicts; we cannot introduce a shift/reduce conflict)

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

184. 5. We say that we cannot introduce a shift/reduce conflict during the shrink process for the creation of the states of a LALR parser. Assume that we can introduce a shift/reduce conflict. In this case, a state of LALR parser must have: A→a•,a and B→b•ag,b This means that a state of the canonical LR(1) parser must have: A→a•,a and B→b•ag,c But, this state has also a shift/reduce conflict. i.e. The original canonical LR(1) parser has a conflict. (Reason for this, the shift operation does not depend on look aheads)

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

185. 6. we may introduce a reduce/reduce conflict during the shrink process for the creation of the states of a LALR parser. I1 : A→a•,a I2: A→a•,b B→b•,b B→b•,c ß I12: A→a•,a/b ⇒ reduce/reduce conflict B→b•,b/c

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

186. 7. The revised parser (LALR parser) behaves essentially like the original parser, although it might do wrong action (reduce) in circumstance where the original would declare error. However, the error will eventually be caught; in fact, it will be caught before any more input symbols are shifted.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

187. 1. All grammars used in the construction of LR-parsing tables must be un-ambiguous.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

188. 2. Can we create LR-parsing tables for ambiguous grammars ? Yes, but they will have conflicts. We can resolve these conflicts in favor of one of them to disambiguate the grammar. At the end, we will have again an unambiguous grammar.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

189. 3. Why we want to use an ambiguous grammar? Some of the ambiguous grammars are much natural, and a corresponding unambiguous grammar can be very complex. Usage of an ambiguous grammar may eliminate unnecessary reductions.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

190. 4. Disambiguating Rules for Yacc (*required only when there exists a conflict): 1. In a shift/reduce conflict the default is to shift. 2. In a reduce/reduce conflict the default is to reduce by the earlier grammar rule in the input sequence. 3. Precedence and associativity (left, right, unassoc) are recorded for each token that have them. 4. Precedence and associativity of a production rule is that (if any) of its final (rightmost) token unless a "% prec" overrides. Then it is the token given following % prec. 5. In a shift/reduce conflict where both the grammar rule and the input (lookahead) have precedence, resolve in favor of the rule of higher precedence. In a tie, use associativity. That is, left assoc. ⇒ reduce; right assoc. ⇒ shift; unassoc ⇒ error. 6. Otherwise use 1 and 2.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。

191. 5. An LR parser will detect an error when it consults the parsing action table and finds an error entry. All empty entries in the action table are error entries. Errors are never detected by consulting the goto table. An LR parser will announce error as soon as there is no valid continuation for the scanned portion of the input. A canonical LR parser (LR(1) parser) will never make even a single reduction before announcing an error. The SLR and LALR parsers may make several reductions before announcing an error. But, all LR parsers (LR(1), LALR and SLR parsers) will never shift an erroneous input symbol onto the stack.

答案:请关注【九八五题库】微信公众号,发送题目获取正确答案。



    文章说明
    本文标签:
    ©版权声明
    本站提供的文章均来自网上,如有侵权请邮件与我们联系处理。敬请谅解!
    评论留言

    昵称

    邮箱

    地址