Download presentation
Presentation is loading. Please wait.
Published byPosy Ryan Modified over 8 years ago
1
Syntax Analysis By Noor Dhia 2014 2015
2
Left Recursion: Example1: S → S0s1s | 01 The grammar after eliminate left recursion is: S → 01 S’ S' → 0s1sS’ | ε Ex2: S → ( L ) | x L → L, S | S S O.K L → SL’ L’ →,S L ’ | ε Ex3: A → Aα1 | Aα2 | Aα3 …… | β1 | β2 | β3 …. A → β1A’ | β2A’ | β3A’ ….. A’ → α1A’ | α2 A’ | α3A’ ……
3
Left factoring: In general, if A → αβ1 | αβ2 are two A-productions, and the input begins with a nonempty string derived from α, we do not know whether to expand to αβ1 or to αβ2. Instead, the grammar may be rewrite to eliminate multiple productions starting with the same token is called left factoring. A → αβ1 | αβ2 | αβ3 To find : αβ3 A A A backtracking backtracking backtracking Α β1 Α β2 Α β3 The formal Left Factoring procedure to change the non deterministic grammar to deterministic grammar is : A → αβ1 | αβ2 | αβ3 (non deterministic grammar ) to A A → αA‘ A' → β1 | β2 | β3 (deterministic grammar ) α A‘ β3
4
Elimimnate Left recursion algorithm : Assign an ordering A1; …. ; An to the nonterminals of the grammar. for i := 1 to n do begin for j := 1 to i - 1 do begin for each production of the form Ai → Aj α do begin remove Ai → Aj α from the grammar for each production of the form Aj → β do begin add Ai → βα to the grammar end transform the Ai-productions to eliminate direct left recursion end
5
Left factoring: Example: S → i E t S | i E t S e S | a E → b The grammar after eliminate left factoring is: S → i E t S S | a S' → e S | ε E → b if these grammar ambiguous? Ex: S → aSSbS S → aS’ | b S → aS’ | b | aS a S b S’ → SSbS S’ → SS’’ | bb | a bb | S aSb S’’ → SbS | aSb | b | bb
6
Left factoring: Ex: S → bSSaaS S → bSS’ | a S → bSS’ | a | bSSaSb S’ → SaaS S’ → SaS’’ | b | bSb | SaSb S’’ → aS | Sb | a | b Predictive Parsing: Parser top down parser Bottom up parser TOP BUT full backtracking without backtracking brute force Recursion non Recursion LL(1)
7
Example: given the following grammar generate TOP &BUT parser tree and derivation to string abbcde S → aABe A → Abc | b B → d TOP S a A B e A b c d b S → a ABe Left most derivation → aAbc Be → abbcBe → abbcde Question : what to used ? BUT S A B A a b b c d e S → a ABe Right most derivation → aAd e → aAbcde → abbcde Question : when to reduse?
8
Predictive Parsing: A parsing for the grammar can be based only on the first token, the parser knows which rule to use to derive a statement. Therefore this parser called Predictive parser. The predictive parsing applied to generate predictive table ( the output ) using LL(1) algorithm. LL(1) : This name indicate to the process properties used to it, such that the first L indicate to the scanning of the input is from Left to right. And the second L indicate to use LMD as derivation direction. And finally 1 indicates to how many symbols are going to see from input in this algorithm used one symbol.
9
LL(1) Components This components illustrate in the following figure: input buffer: contains the string to be parsed, followed by $, (a symbol used as a right end marker to indicate the end of the input string). Stack: contains a sequence of grammar symbols with $ on the bottom,( indicating the bottom of the stack). Initially, the stack contains the start symbol of the grammar on the top of $. LL(1) parser: interpreters by taking a word or string depending on the given grammar. parsing table: is a two-dimensional array M[A,a], where A is a nonterminal, and a is a terminal or the symbol $. To constact the LL(1) predictive parsing table LL(1) used two functions : FIRST() and FOLLOW()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.