Presentation is loading. Please wait.

Presentation is loading. Please wait.

How YACC is constructed. How Yacc works To construct a parsing machine for arithmetic expressions, a special case considered to simplify the account of.

Similar presentations


Presentation on theme: "How YACC is constructed. How Yacc works To construct a parsing machine for arithmetic expressions, a special case considered to simplify the account of."— Presentation transcript:

1 How YACC is constructed

2 How Yacc works To construct a parsing machine for arithmetic expressions, a special case considered to simplify the account of the algorithm.

3 Definition of a configuration with respect to some grammar If y ->x 1.....x n is some production of the grammar, and z is a terminal symbol of the grammar, Then, y -> x 1...x r.x r+1...x n, z is a configuration of the grammar. Note the period (.) is marking some position in the production.

4 Example Given the grammar: E -> E + T | a T -> T * a | a some examples of configurations are: E ->.E + T, + E -> E. + T, + E -> E + T., -| T -> T. * a, * T -> T *.a, +

5 Definition of a rightmost derivation A rightmost derivation is one in which at any step, a production can only be applied to the rightmost nonterminal. Example of a derivation which is NOT a rightmost derivation: 1 E 2 E + T 3 T + T 4 T + a 5 a + a

6 Definition of a rightmost derivation (Cont.) The transition from step 3 to step 4 does not meet the definition of a rightmost derivation, since E is NOT the rightmost nonterminal. T is the rightmost nonterminal. A rightmost derivation for this would be: 1 E 2 E + T 3 E + a 4 T + a 5 a + a

7 Theorem Theorem. Every sentence of a grammar has a unique rightmost derivation.

8 Right Sentential Form Definition. A right sentential form is one which occurs in some step of a rightmost derivation. E.g., a sentence is a special right sentential form that consists entirely of terminals.

9 Rightmost Derivation Consider the rightmost derivation 1 E 2 E + T 3 E + a 4 E + T + a 5 E + T * a + a Step 5 was obtained from step 4 using the production T -> T * a and the symbol after the T in step 4 is a '+'. We then say that T ->.T * a, + applies to E +.T * a + a

10 Rightmost Derivation(Cont.1) where the '.' marks the position in the right sentential form in the production, and the '+' is the context symbol obtained from step 4. Also, T -> T. * a, + applies to E + T. * a + a and, T -> T *. a, + applies to E + T *. a + a and, T -> T * a., + applies to E + T * a. + a

11 Rightmost Derivation(Cont.2) Example 1 E 2 E + T 3 E + a 4 E + T + a What configuration applies to:.E + T + a E. + T + a E +.T + a E + T. + a

12 General Explanation Given a right sentential form, such as E + T * a + a, we want to be able to find all the configurations that apply to position 0 of the right sentential form..E + T * a + a Next, we want to find all the configurations that apply to position 1 of some right sentential form that starts with E. 0 E 1 + 2 T 3 * 4 a 5 + 6 a 7

13 General Explanation (Cont.1) Next, we work out all the configurations that apply in turn to positions 2, 3, 4, and 5 of a right sentential form that starts respectively with: E + E + T E + T * E + T * a

14 General Explanation (Cont.2) What we get for position 5 is: T -> T * a., + T -> T * a., * T -> T * a., -| T -> T. * a, + Of these configurations, the only one that fits 0 E 1 + 2 T 3 * 4 a 5 + 6 a 7 is the first one of these possible configurations.

15 Definition of a final configuration T -> T * a., + is an example of a final configuration, which is identified by the fact that the marker is located at the extreme right position of the production.

16 Algorithm that Yacc uses The algorithm that Yacc uses depends on the fact that when we get to the position on the right of the right-hand side involved in deriving a right sentential form from the previous step Then only one configuration applies to sentential forms that start with the same symbols up to that position and also that this configuration is a final one.


Download ppt "How YACC is constructed. How Yacc works To construct a parsing machine for arithmetic expressions, a special case considered to simplify the account of."

Similar presentations


Ads by Google