Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compilers Principles, Techniques, & Tools Taught by Jing Zhang

Similar presentations


Presentation on theme: "Compilers Principles, Techniques, & Tools Taught by Jing Zhang"— Presentation transcript:

1 Compilers Principles, Techniques, & Tools Taught by Jing Zhang

2 Syntax Analysis – Bottom UP Method

3 Outlines Bottom-Up Parsing Introduction to LR Parsing: Simple LR
The LR-Parsing Algorithm canonical-LR: LR(1) Lookahead LR Parser: LALR Using Ambiguous Grammars Error Recovery in LR Parsing Parser Generators - Yacc

4 5.1 Bottom-Up Parsing – the first example
A bottom-up parse corresponds to the construction of a parse tree for an input string beginning at the leaves (the bottom) and working up towards the root (the top) .

5 5.1 Bottom-Up Parsing – Reduction
We can think of bottom-up parsing as the process of "reducing" a string w to the start symbol of the grammar. At each reduction step, a specific substring matching the body of a production is replaced by the nonterminal at the head of that production. The key decisions during bottom-up parsing are about when to reduce and about what production to apply, as the parse proceeds. Eg. By definition, a reduction is the reverse of a step in a derivation This derivation is in fact a rightmost derivation.

6 5.1 Bottom-Up Parsing – Handle Pruning
Bottom-up parsing during a left-to-right scan of the input constructs a rightmost derivation in reverse. Informally, a "handle" is a substring that matches the body of a production, and whose reduction represents one step along the reverse of a rightmost derivation. rm rm rm rm rm Formally, if then the production in the position following is a handle of Notice that the string to the right of the handle must contain only terminal symbols. For convenience, we refer to the body rather than as a handle. A rightmost derivation in reverse can be obtained by "handle pruning”.

7 5.1 Bottom-Up Parsing – More Explanations on “Handle”
Right-most derivation is also called “normal derivation”, which guarantees Proof if has a handle , i.e, and , then under the rightmost derivation should not be derived out before If has the form and , we do not call “handle”, because for right-sentential form then last rightmost derivation is , i.e., is previously derived out to . Thus, the reverse of the last rightmost derivation should take place on Thus, a handle is at the leftmost position of a right-sentential form. Note we say "a handle" rather than "the handle," because the grammar could be ambiguous, with more than one rightmost derivation of If a grammar is unambiguous, then every right-sentential form of the grammar has exactly one handle. No handle

8 5.1 Bottom-Up Parsing – More Explanations on “Handle”
Normal Reduction is a reverse of a Normal derivation (rightmost derivation). Specifically, is a sentence of grammar G, we call the sequence of sentential forms is a Normal Reduction of , if the sequence satisfies: (1) (2) (start symbol of a grammar) (3) For , can be obtained by replacing the handle of with the left part of its corresponding production. Thus, Normal Reduction is also called leftmost reduction !

9 5.1 Bottom-Up Parsing – Shift-Reduce Parsing
Shift-reduce parsing is a form of bottom-up parsing in which a stack holds grammar symbols and an input buffer holds the rest of the string to be parsed.

10 5.1 Bottom-Up Parsing – Shift-Reduce Parsing

11 5.1 Bottom-Up Parsing – Conflicts During Shift-Reduce Parsing
Shift/reduce conflict Reduce/reduce conflict

12 5.2 Introduction to LR Parsing: Simple LR
The 'most prevalent type of bottom-up parser today is based on a concept called LR(k) parsing; the "L" is for left-to-right scanning of the input, the "R" for constructing a rightmost derivation in reverse, and the k for the number of input symbols of lookahead that are used in making parsing decisions. The cases k = 0 or k = 1 are of practical interest, and we shall only consider LR parsers with k ≤1 here. When (k) is omitted, k is assumed to be 1 . LR parsing is attractive for a variety of reasons: LR parsers can be constructed to recognize virtually all programming language constructs for which context-free grammars can be written. Non LR context-free grammars exist, but these can generally be avoided for typical programming- language constructs. The LR-parsing method is the most general nonbacktracking shift-reduce parsing method known, yet it can be implemented as efficiently as other, more primitive shift-reduce methods An LR parser can detect a syntactic error as soon as it is possible to do so on a left-to-right scan of the input. 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 or LL methods. The principal drawback of the LR method is that it is too much work to construct an LR parser by hand for a typical programming-language grammar.

13 5.2 Introduction to LR Parsing: Simple LR
Items and the LR(0) Automaton An LR parser makes shift-reduce decisions by maintaining states to keep track of where we are in a parse. States represent sets of "items." An LR(0) item (item for short) of a grammar G is a production of G with a dot at some position of the body. Thus, production yields the four items For , we only have The item indicates that we hope to see a string derivable from XYZ next on the input. Item indicates that we have just seen on the input a string derivable from X and that we hope next to see a string derivable from YZ. Item indicates that we have seen the body XYZ and that it may be time to reduce XYZ to A. One collection of sets of LR(0) items, called the canonical LR(0) collection, provides the basis for constructing a deterministic finite automaton that is used to make parsing decisions.

14 5.2 Introduction to LR Parsing: Simple LR
To construct the canonical LR(0) collection for a grammar, we define an augmented grammar and two functions, CLOSURE and GOTO . If G is a grammar with start symbol S, then G' , the augmented grammar for G, is G with a new start symbol S' and production The purpose of this new starting production is to indicate to the parser when it should stop parsing and announce acceptance of the input. That is, acceptance occurs when and only when the parser is about to reduce by Closure of Item Sets

15 5.2 Introduction to LR Parsing: Simple LR

16 5.2 Introduction to LR Parsing: Simple LR
The Function GOTO

17 5.2 Introduction to LR Parsing: Simple LR
Use of the LR(0) Automaton The central idea behind "Simple LR," or SLR, parsing is the construction from the grammar of the LR(0) automaton. The states of this automaton are the sets of items from the canonical LR(0) collection, and the transitions are given by the GOTO function. Shift-reduce decisions can be made as follows. Suppose that the string “γ” of grammar symbols takes the LR(0) automaton from the start state 0 to some state j . Then, shift on next input symbol a if state j has a transition on a. Otherwise, we choose to reduce; the items in state j will tell us which production to use.

18 5.2 Introduction to LR Parsing: Simple LR
$ id 05 $id 05 X $F $F 03 $F 03 X $T $T 02 $T

19 5.3 The LR-Parsing Algorithm
The model of LR Parser

20 5.3 The LR-Parsing Algorithm
LR-Parser Configurations Stack Input

21 5.3 The LR-Parsing Algorithm
Behavior of the LR Parser

22 5.3 The LR-Parsing Algorithm

23 5.3 The LR-Parsing Algorithm

24 5.3 The LR-Parsing Algorithm
Constructing an SLR-parsing table The SLR method begins with LR(0) items and LR(0) automata

25 5.3 The LR-Parsing Algorithm

26 5.3 The LR-Parsing Algorithm
Viable Prefixes The stack contents must be a prefix of a right-sentential form. If the stack holds and the rest of the input is x, then a sequence of reductions will take to S. In terms of derivations Not all prefixes of right-sentential forms can appear on the stack, however, since the parser must not shift past the handle. The prefixes of right sentential forms that can appear on the stack of a shift-reduce parser are called viable prefixes.

27 5.4 Canonical LR: LR(1) Canonical LR(1) Items

28 5.4 Canonical LR: LR(1) Eg.

29 5.4 Canonical LR: LR(1) Constructing LR(1) Sets of Items Eg.

30 5.4 Canonical LR: LR(1) Canonical LR( l ) Parsing Tables

31 5.4 Canonical LR: LR(1)

32 5.5 Lookahead LR Parser: LALR
For a comparison of parser size, the SLR and LALR tables for a grammar always have the same number of states, and this number is typically several hundred states for a language like C. The canonical LR table would typically have several thousand states for the same-size language. Errors may be postponed , but will be eventually caught.

33 5.5 Lookahead LR Parser: LALR
Thus, the merging of states with common cores can never produce a shift/reduce conflict that was not present in one of the original states, because shift actions depend only on the core, not the lookahead. It is possible, however, that a merger will produce a reduce/reduce conflict, as the following example shows.

34 5.5 Lookahead LR Parser: LALR

35 5.5 Lookahead LR Parser: LALR - Example

36 5.5 Lookahead LR Parser: LALR
Efficient Construct ion of LALR Parsing Tables In textbook, pp (read by yourselves)

37 5.6 Using Ambiguous Grammars
It is a fact that every ambiguous grammar fails to be LR and thus is not in any of the classes of grammars discussed in the previous two sections. However, certain types of ambiguous grammars are quite useful in the specification and implementation of languages . For language constructs like expressions, an ambiguous grammar provides a shorter, more natural specification than any equivalent unambiguous grammar. Another use of ambiguous grammars is in isolating commonly occurring syntactic constructs for special-case optimization. With an ambiguous grammar , we can specify the special-case constructs by carefully adding new productions to the grammar.

38 5.6 Using Ambiguous Grammars
Precedence and Associativity to Resolve Conflicts id+id*id (id+id)+id

39 5.6 Using Ambiguous Grammars
The " Dangling-Else" Ambiguity

40 5.7 Error Recovery in LR Parsing

41 5.8 Parser Generators - Yacc
Yacc stands for "yet another compiler-cpmpiler," reflecting the popularity of parser generators in the early 1970s when the first version of Yacc was created by S. C. Johnson

42 5.8 Parser Generators - Yacc

43 5.8 Parser Generators - Yacc

44 Homework 5 1.Consider Grammar G:
(1) Provide the rightmost derivation for sentence (((a, a), ^, (a)), a) (2) Point put the handle of each step in the normal reduction of above sentence. Consider Grammar G: (1) List all LR(0) items for G. (2)Construct LR(0) automaton for G. (3)Is this grammar an SLR(1) grammar. If it is, construct its SLR(1) parsing table? (4)Is this grammar an LALR or LR(1) grammar?

45 Homework 5 3. Show that the following grammar is SLR(1).
4. Show that the following grammar is LR(1) but not SLR(1).


Download ppt "Compilers Principles, Techniques, & Tools Taught by Jing Zhang"

Similar presentations


Ads by Google