Compiling principle structure framework 4 top-down syntax analysis

Source: Internet
Author: User

Compiling principle structure framework 4 top-down syntax analysis

Chapter 4 top-down syntax analysis

Key points: the basic idea of top-down analysis, the overall structure of the prediction analyzer, the structure of the prediction analysis table, the basic idea of recursive descent analysis, and the recursive descent Analyzer of simple arithmetic expressions.

Difficulties: FIRST and FOLLOW sets, understanding of them, and use when constructing LL (1) analysis tables. How to embody the analysis results in recursive subprograms.

Basic concepts:

Top-down analysis problems:

1. Ambiguity

N solution 1: Transform grammar and introduce new grammar Variables
N solution 2: Based on the priority relationship, ensure the priority of high-priority Operators

2. Backtracking

Transform grammar by extracting the Left Factor

3. Infinite derivation problem caused by left recursion

Eliminate direct left recursion:

A-> A α 1 | A α 2 | A α 3 | ...... | A α n | β1 | β2 | ...... | β n

Transformation:

A-> β1a '| β2a' | βa' | ...... | βa'

A'-> α 1A '| α 2A' | α 3A '| ...... | α Na' | ε

Eliminate indirect left recursion:

Convert indirect left recursion to direct left recursion using the proxy method

Use the method described above to eliminate direct left recursion

Question type:

1. Calculate the FIRST set of syntax symbols of expression syntax

If it is an Terminator, FIRSTRUG (X): = {X}

If it is a variable, find whether the first candidate is a Terminator.

If the FIRST is a variable, it contains the FIRST set of the variable.

FIRST (X) + = (FIRST (Y)-{ε })

If Y can be deduced as ε, the FIRST set containing the next syntax variable

2. Calculate the FOLLOW set of syntax variables of expression syntax

FOLLOW (S) := {#}

If A-> α B β, FOLLOW (B) + = FIRST (Beta)-{ε}

If A-> α B, FOLLOW (B) + = FOLLOW ()

If A-> α B β and β => (*) ε, A = B then FOLLOW (B) + = FOLLOW ()

3. Prediction Analysis:

1. constructor syntax

2. Syntax Transformation: remove ambiguity, remove left recursion, and extract Left Factor

3. Calculate the FIRST set of each candidate formula and the FOLLOW set of variables.

4. Check if it is LL (1) grammar.

If it is not LL (1), the complexity of the plain text method exceeds the analysis capability of the top-down method, and a new "information" needs to be added"

5. Construct a prediction analysis table

6. Implement a prediction Analyzer

4. handle errors:

Test:

The syntax G is as follows:

E =>TE’E’ => +E |εT => FT’T’ => T |εF => PF’F’ => *F |εP => (E)| a |∧

(1) Calculate the FIRST and FOLLOW sets of each syntax variable of the grammar.

(2) is the grammar LL (1? If yes, construct its LL (1) analysis table

FIRST(P) = {(, a, ∧}FIRST(F) = FIRST(P) = {(, a, ∧}FIRST(T) = FIRST(F) = {(, a, ∧}FIRST(E) = FIRST(T) = {(, a, ∧}FIRST(E‘) = {+, ε}FIRST(F') = {*, ε}FIRST(T') = FIRST(T) ∪ { ε } = { (, a, ∧, ε }FOLLOW(E') = FOLLOW(E) = { ) , # }FOLLOW(E) = FOLLOW(E') ∪ { ) } = { ), # }FOLLOW(T) = ( FIIRST(E') - ε ) ∪ FOLLOW(T') ∪ FOLLOW(E) = { ), # , + }FOLLOW(T') = FOLLOW(T) = { ), # , + }FOLLOW(F') = FOLLOW(F) = { (, a, ∧, ), #. + }FOLLOW(F) = ( FIRST(T') - { ε } ) ∪ FOLLOW(F') ∪ FOLLOW(T) = { (, a, ∧, ), #. + }FOLLOW(P) = ( FIRST(F') - {ε} ) = {} ∪ FOLLOW(F) = { *, (, a, ∧, ), #. + }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.