Syntax analysis principle-Parsing

Source: Internet
Author: User
Introduction

Parsing Method category:
Universal: cocke-younger-kasami algorithm and Earley's algorithm.
Top-down: LL (k ).
Bottom-up: LR (k ).

Error recovery mode:
Panic-mode recovery:
Phrase-level recovery:
Error production:
Global correction:

Context-independent grammar:
Terminal: the basic symbol that forms a string.
Nonterminal: represents the syntax variables of the string set, which gives the key to syntax analysis and translation.
Start Symbol: In a grammar, A nonterminal is specified as the start symbol.
Production: A syntax generator that contains a head/left side, conversion symbol->/: =, and body/right side.

Derivation process:
Leftmost derivation (leftmost derivation): Always select the leftmost non-terminator for each sentence.
Rightmost derivation: select the rightmost non-terminator for each sentence. It is also called canonical derivation ).

Algorithm 1: Remove left recursion

Remove left recursion immediately:
For a-> A α 1 | A α 2 |... | A α M | β1 | β2 |... | β N, replace these a generative formula:
A-> β 1A '| β 2a' |... | β Na'
A'-> α 1A '| α 2a' |... | α Ma' | ε

Remove the left recursion of the Loop:
Input: grammar G without loops (a-> A) or ε (a-> ε.
Output: an equivalent non-left recursive syntax G', but G' may have an ε generative formula.
Method:

Eliminate the left recursive algorithm of loops: arrange non-terminologies into A1, A2 ,..., an. for I = (1 to n) {for J = (1 to i-1) {Replace the generative formula of each shape, such as AI-> AJ gamma, with the generative formula Ai-> delta 1 gamma | Delta 2 gamma |... | Delta 3 gamma, where AJ-> delta 1 | Delta 2 |... | Delta K is all AJ Generative Patterns;} removes the instant left recursion between AI Generative Patterns ;}
Algorithm 2: extract Left Factor

Purpose: To postpone the generative selection.
Input: syntax rule G.
Output: equivalent extract left factor syntax G '.
Method: Find the longest public prefix α for each non-terminator! = ε. For all a-> α-β 1 | α-β 1 |... | α-β n | γ, replace:
A-> α a' | Gamma
A'-> β1 | β1 |... | βn
Repeat this behavior until two production types without the same non-terminator contain a public prefix.

Top-down recursive-descent Parsing
Typical process of recursive downloading Parser:
Void A () {choose an-production, A: = x1x2... XK; for I = (I to k) {If (Xi is a nonterminal) Call procedure XI (); else if (XI equals the current input symbol) advance the input to the next symbol; else call error recovery procedure ;}}

 

First and follow Sets

First (α): α is any syntactic symbol. This set is the first terminator of a sentence derived from α.
During computing, the following rules are applied until no new terminator or ε can be added to any first set:
1) if X is a Terminator, first (x) = {x }.
2) If X is a non-Terminator and has a generative formula X: = y1y2... YK (k ≥ 1 ). If first (yi) contains a and first (Y1),... first (Yi-1) can be pushed to export ε, then add a to first (X. If all first (yi) contains ε, add ε to first (X.
3) If X: = ε exists, add it to first (X.

Follow (a): A is a non-Terminator, which is a set of Terminators that can be followed by the right side of a's sentence pattern.
During computing, the following rules are applied until no new Terminator is added to any follow set:
1) if S is the start symbol, add $ to follow (s), and $ is the end mark of the input string.
2) If the generative expression A: = α B β exists, all the symbols except ε in first (β) are in follow (B.
3) If formula A: = α B exists, or formula A: = α B Beta exists and first (Beta) contains ε, then follow (B) contains all the symbols in follow (.

 

Syntax analysis principle-Parsing

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.