Chapter 4: Programming Languages

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Explain the three reasons why lexical analysis is separated from syntax analysis.

1. Simplicity (Techniques for lexical analysis are less complex than those required for syntax analysis, so the lexical-analysis process can be simpler if it is separate. Also, removing the low level details of lexical analysis from the syntax analyzer makes the syntax analyzer both smaller and less complex) 2. Efficiency (Although it pays to optimize the lexical analyzer, because lexical analysis requires a significant portion of total compilation time, it is not fruitful to optimize the syntax analyzer. Separation facilitates this selective optimization) 3. Portability (Because the lexical analyzer reads input program files and often includes buffering of that input, it is somewhat platform dependent. However, they syntax analyzer can be platform independent. It is always good to isolate machine-dependent parts of any software system.)

What are the two distinct goals of syntax analysis?

1. Syntax analyzer must check input program to determine whether it is syntactically correct. When an error is found the analyzer must produce a diagnostic message and recover. (recovery means get back to a normal state and continue its analysis of the input program) 2. produce complete parse tree, or at least trace the structure of the complete parse tree, for syntactically correct input. Parse tree is used as the basis for translation

Describe three advantages of LR parsers.

1. They can be built for all programming languages. 2. They can detect syntax errors as soon as it possible in a left-to-right scan. 3. The LR class of grammars is a proper superset of the class parsable by LL parsers.

What is the mathematical machine on which both top-down and bottom-up parsers are based?

A PDA (PushDown Automation) machine use a both top-down parsers and bottom-up parsers because it recognizing context free languange.

Explain why compilers use parsing algorithms that work on only a subset of all grammars.

Because parsing algorithms that works for any unambiguous grammar are complex and inefficient. In fact, the complexity of the algorithm is O (n3). Algorithm is O (n3) is not usually useful for practical processes, such as analysis of the syntax for the compiler, because they are too slow. In this situation, computer scientists often look for a faster algorithm, although it is less common. In the case of parsing, faster algorithm has listed who work only a subset of the set of all possible grammar

Explain the two grammar characteristics that prohibit them from being used as the basis for a top-down parser.

Direct or indirect Left Recursion. Recursive decesnt parser subprogram for A immediately calls itself to parse the first symbol in its RHS. That activation of the A parser subprogram then immediately calls itself again, and again, and so forth. It is easy to see that this leads nowhere (stack overflow)

Describe how a recursive-descent parsing subprogram is written for a rule with a single RHS

First it calls the function that aparses terms(term). Then it continues to call that function as long as it finds ADD_OP or SUB_OP tokens (which it passes over by calling lex). A recursive-descent subprogram for a rule with a single RHS is relatively simple. For each terminal symbol in the RHS, that terminal symbol is compared with nextToken. If they do not match, it is a syntax error. If they match, the lexical analyzer is called to get the next input token. For each nonterminal, the parsing subprogram for that nonterminal is called.

What are three reasons why syntax analyzers are based on grammars?

First, using BNF descriptions of the syntax of programs are clear and concise. Second, can be used as the direct basis for the syntax analyzer. Third, implementations based on BNF are relatively easy to maintain because of their modularity.

Define lexeme and token

Lexeme is the logical groupings that the lexical analyzer collects characters into and Token is the internal codes for categories of these groupings.

What are the primary tasks of a lexical analyzer?

Lexical analysis process includes skipping comments and white space outside lexemes, as they are not relevant to the meaning of the program. Also lexical analyzer inserts lexemes for user-defined names into the symbol table, which is used by later phases of the compiler. Finally, lexical analysis detect syntactic errors in tokens, such as ill-formed floating-point literals, and report such errors to the user.

Is left recursion a problem for LR parsers?

NO many left recursive grammars are LR, but none are LL

Why are character classes used, rather than individual characters, for the letter and digit transitions of a state diagram for a lexical analyzer?

Observe that there are 52 different characters that can begin a name, which would require 52 transitions from the transition diagram's initial state. However, a lexical analyzer is interested only in determining that it is a name and is not concerned with which specific name it happens to be. Therefore, we define a character class named LETTER for all 52 letters and use a single transition on the first letter of any name.

Describe the purpose of the ACTION table of an LR parser

The ACTION part specifies what the parser should do, given the state symbol on top of the parse stack and the next token of input.

Describe the purpose of the GOTO table of an LR parser

The rows of the GOTO part of the LR parsing table have state symbols as labels. This part of the table has nonterminals as column labels. The values in the GOTO part of the table indicate which state symbol should be pushed onto the parse stack after a reduction has been completed, which means the handle has been removed from the parse stack and the new nonterminal has been pushed onto the parse stack.

Describe the difference between top-down and bottom-up parser.

Top-down the tree is built from the root downward to the leaves Bottom-up parse tree is built from the leaves upward to the root

What is the handle of a sentential form?

a handle of a right-sentential form γ is a production A → β and a position in γ where β may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation of γ.

Why are named constants used, rather than numbers, for token codes?

for the sake of readability of lexical and syntax analyzers.


Kaugnay na mga set ng pag-aaral

Death of a Salesman Quote(s) Analysis

View Set

Quiz #22: Intro, 22-1, 22-2, 22-3

View Set

Finding the Domain and Range of Functions

View Set

Lesson 3: The requirements for Electrical Installations in Calss I Hazardous (classified) Locations

View Set

Advanced Accounting Test 2 Quizlet- Chapter 3 LearnSmarts

View Set

IB - Chapter 5: Ethics, Corporate Social Responsibility, and Sustainability

View Set