Recursive Descent Grammars - Missouri State...

54
Recursive Descent Grammars

Transcript of Recursive Descent Grammars - Missouri State...

Page 1: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Recursive Descent Grammars

Page 2: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Outline

Topics and Learning Objectives• Discuss grammar ambiguity, removing left recursions, and left-

factoring grammars

Assessments• Left recursion and left-factoring activity

Page 3: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Parsing

Top-Down

Recursive Descent

Back-Tracking

Non Back-Tracking

Predictive Parsing

LL Parser

Non-Recursive

Bottom-Up

Shift-Reduce

LR Parsing

SLR Parsing LR Parsing LALR

Parser

Others…

Page 4: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Convert Grammar Into Source Code

1. Each rule defined in the grammar becomes a function/method2. References to that rule become a function call3. The body of a rule function follows the flow of the rule’s RHS4. Alternatives (a1 | a2 | …) are handled with branches (if-then-else, switch

statements, match expressions, etc.)5. An optional grouping (…)* becomes a while loop that can loop zero or

more times6. Epsilon transitions enable a function to return without doing anything7. Each reference to a terminal (token) requires you to check that the

current token is correct, and then either report an error or advance to the next token

Page 5: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

expr’ à (Plus | Minus) term expr' | ε

This is a recognizer, not a parser.It does not output a tree.It just checks for valid input.

Page 6: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

LL(k) Parsing

• Top-down approach• Uses at most the next k tokens to select a production (look-ahead)• Efficient, predictive parsing (no backtracking à linear time parsing)• Implemented via recursive functions calls (ad-hoc, by hand)

• Or implemented using a table-driven approach (generated)• A stack is used in place of recursive function calls• Implement a push-down automaton• We’ll not be discussing this topic

Page 7: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Summary of LL Parsing

Write Grammar

Remove Ambiguity

Remove Left Recursion

Left Factor

Implement the LL(k) Parser

Decide on an LL(k) parser

Top-Down Parser

Better Top-Down Parser

Recursive Descent Parser

Predictive Parser

Page 8: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Grammar Ambiguity1 + 2 * 3

Page 9: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Dangling Else in C

Page 10: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Dangling Else

'if' ( e ) s

'if' ( e ) s1 'else' s2

What if s1 is an if-statement?

'if' ( e1 ) 'if' ( e2 ) s1 'else' s2

We could parse this as either of the following:

'if' ( e1 ) ( 'if' ( e2 ) s1 ) 'else' s2

'if' ( e1 ) ( 'if' ( e2 ) s1 'else' s2 )

C-ish Grammarstatement

: assignment | ifStatement |;assignment

: 'let' Identifier '=' expression ';';ifStatement

: 'if' '(' expression ')' statement| 'if' '(' expression ')' statement 'else' statement;

C handles this problem by the following convention: an 'else' always associates

with the nearest ‘if’.

Some expression Some statement

Page 11: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

statement: assignment | ifStatement | …;

ifStatement: 'if' '(' expression ')' statement| 'if' '(' expression ')' statement 'else' statement;

Input: ‘if’ ‘(‘ expr1 ‘)’ 'if’ ‘(‘ expr2 ‘)’ stmt1 'else’ stmt2

ifStatment

stmtexpr1'if'

stmt2'else'

ifStatement

stmt1expr2'if'

if_expression

stmt2'else'stmtexpr1'if'

stmt1expr2'if'

ifStatement

Page 12: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Unambiguous If Statements

statement: assignment | ifStatement | …;

assignment: 'let' Identifier '=' expression ';’;

ifStatement: matchedIf | unmatchedIf;

matchedIf: 'if' '(' expression ')' matchedIf 'else' matchedIf| statement;

unmatchedIf: 'if' '(' expression ')' statement| 'if' '(' expression ')' matchedIf 'else' unmatchedIf;

Page 13: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Grammar Ambiguity

• Grammar ambiguity can cause issues for the programmer using your language• It can also cause problems for you while your writing your

language (unexpected parse trees)

• Unfortunately, we cannot un-ambiguate a grammar automatically• This problem is referred to as “undecidable”• In practice though, it is possible to create unambiguous grammars by

carefully examining your grammar rules

Page 14: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Summary of LL Parsing

Write Grammar

Remove Ambiguity

Remove Left Recursion

Left Factor

Implement the LL(k) Parser

Decide on an LL(k) parser

Top-Down Parser

Better Top-Down Parser

Recursive Descent Parser

Predictive Parser

Page 15: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr : expr ‘+’ term | termterm : ID | ‘(‘ expr ‘)’

Page 16: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr’ : (Plus | Minus) term expr' | ε

FUNCTION ExprPrime()IF MatchInput(‘+’)

|| MatchInput(‘-’)ConsumeToken()Term()ExprPrime()

expr : expr Plus term | term

FUNCTION Expr()TRY

Expr()MatchInput(‘+’ || ‘-’)ConsumeToken()Term()

ELSETerm()

Page 17: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr : expr ‘+’ term | termterm : ID | ‘(‘ expr ‘)’ Transformation Pattern

A : A α | β

A : β A’A’ : α A’ | ε

Page 18: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr : expr ‘+’ term | termterm : ID | ‘(‘ expr ‘)’ Transformation Pattern

A : A α | β

A : β A’A’ : α A’ | ε

Page 19: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr : expr ‘+’ term | termterm : ID | ‘(‘ expr ‘)’ Transformation Pattern

A : A α | β

A : β A’A’ : α A’ | ε

Page 20: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr : expr ‘+’ term | termterm : ID | ‘(‘ expr ‘)’

expr :

Transformation Pattern

A : A α | β

A : β A’A’ : α A’ | ε

What is A?

Page 21: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr : expr ‘+’ term | termterm : ID | ‘(‘ expr ‘)’

expr :: ‘+’ term

Transformation Pattern

A : A α | β

A : β A’A’ : α A’ | ε

What is α?

Page 22: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr : expr ‘+’ term | termterm : ID | ‘(‘ expr ‘)’

expr : term: ‘+’ term

Transformation Pattern

A : A α | β

A : β A’A’ : α A’ | ε

What is β?

Page 23: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr : expr ‘+’ term | termterm : ID | ‘(‘ expr ‘)’

expr : term expr’expr’ : ‘+’ term expr’

Transformation Pattern

A : A α | β

A : β A’A’ : α A’ | ε

What is A’?

Page 24: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr : expr ‘+’ term | termterm : ID | ‘(‘ expr ‘)’

expr : term expr’expr’ : ‘+’ term expr’ | ε

Transformation Pattern

A : A α | β

A : β A’A’ : α A’ | ε

Page 25: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

expr : expr ‘+’ term | termterm : ID | ‘(‘ expr ‘)’

expr : term expr’expr’ : ‘+’ term expr’ | εterm : ID | ‘(‘ expr ‘)’

Transformation Pattern

A : A α | β

A : β A’A’ : α A’ | ε

Copy non-left recursive rules without changing them.

Page 26: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left Recursion

General pattern for replacing left-recursion (removing more than one instance of left recursion)

𝐴 → 𝐴𝛼! 𝐴𝛼" … 𝐴𝛼# β! β" … | β$

Is replaced with

𝐴 → β!𝐴′ β"𝐴% … | β$𝐴%

𝐴% → 𝛼!𝐴% 𝛼"𝐴% … 𝛼#𝐴% 𝜀

Page 27: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Indirect Left-Recursion

What about left recursion in the following grammar:

S : Aa | bA : Ac | Sd | ε

1. A -> A, and2. S -> Ac -> Sd

Left-RecursionTransformation

Input Pattern:𝐴 → 𝐴𝛼! 𝐴𝛼" … 𝐴𝛼# β! β" … | β$

Output Pattern:𝐴 → β!𝐴′ β"𝐴% … | β$𝐴%𝐴% → 𝛼!𝐴% 𝛼"𝐴% … 𝛼#𝐴% 𝜀

Page 28: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Indirect Left-Recursion

What about left recursion in the following grammar:

S : Aa | bA : Ac | Sd | ε

1. Label all non-terminals 1 through n

1

2 1

2

2

Page 29: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Indirect Left-Recursion

What about left recursion in the following grammar:

S : Aa | bA : Ac | Sd | ε

1. Label all non-terminals 1 through n2. Apply productions when the label on the RHS is less than LHS

1

2 1

2

2

Page 30: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Indirect Left-Recursion

What about left recursion in the following grammar:

S : Aa | bA : Ac | Sd | ε è A : Ac | (Aa | b)d | ε

1. Label all non-terminals 1 through n2. Apply productions when the label on the RHS is less than LHS

1

2 1

2

2

Page 31: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Indirect Left-Recursion

What about left recursion in the following grammar:

S : Aa | bA : Ac | (Aa | b)d | ε è A : Ac | Aad | bd | ε

1. Label all non-terminals 1 through n2. Apply productions when the label on the RHS is less than LHS

You can distribute

Page 32: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Indirect Left-Recursion

What about left recursion in the following grammar:

S : Aa | bA : Ac | Aad | bd | ε

1. Label all non-terminals 1 through n2. Apply productions when the label on the RHS is less than LHS

Page 33: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Indirect Left-Recursion

What about left recursion in the following grammar:

S : Aa | bA : Ac | Aad | bd | ε

1. Label all non-terminals 1 through n2. Apply productions when the label on the RHS is less than LHS3. Remove left recursions

Left-RecursionTransformation

Input Pattern:𝐴 → 𝐴𝛼! 𝐴𝛼" … 𝐴𝛼# β! β" … | β$

Output Pattern:𝐴 → β!𝐴′ β"𝐴% … | β$𝐴%𝐴% → 𝛼!𝐴% 𝛼"𝐴% … 𝛼#𝐴% 𝜀

Page 34: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Indirect Left-Recursion

What about left recursion in the following grammar:

S : Aa | bA : bdA’ | A’A’ : cA’ | adA’ | ε

1. Label all non-terminals 1 through n2. Apply productions when the label on the RHS is less than LHS3. Remove left recursions

Left-RecursionTransformation

Input Pattern:𝐴 → 𝐴𝛼! 𝐴𝛼" … 𝐴𝛼# β! β" … | β$

Output Pattern:𝐴 → β!𝐴′ β"𝐴% … | β$𝐴%𝐴% → 𝛼!𝐴% 𝛼"𝐴% … 𝛼#𝐴% 𝜀

Page 35: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Summary of LL Parsing

Write Grammar

Remove Ambiguity

Remove Left Recursion

Left Factor

Implement the LL(k) Parser

Decide on an LL(k) parser

Top-Down Parser

Better Top-Down Parser

Recursive Descent Parser

Predictive Parser

Page 36: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left-Factoring (Remove Common Prefixes)

We’d like all decisions to be made based on the current token (no backtracking required)A : some_terminal B | some_other_terminal C

For example, how do we choose which branch to take for E?E : int E | int T

This would require a backtracking process if we later find that the first alternative was wrong.

Page 37: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Why Remove Common Prefixes?

A : ‘@’ B | ‘$’ C

FUNCTION A()IF MatchInput(‘@’)

ConsumeToken()B()

ELSE IF MatchInput(‘$’)ConsumeToken()C()

E : ‘#’ E | ‘#’ T

FUNCTION E()MatchInput(‘#’)ConsumeToken()TRY

E()ELSE

T()

Page 38: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left-Factoring (Remove Common Prefixes)

To eliminate backtracking or multiple look-ahead, we need to eliminate common prefixes.

What is the common prefix for the following grammar rule?A : α β1 | α βn | γ

Page 39: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left-Factoring (Remove Common Prefixes)

To eliminate backtracking or multiple look-ahead, we need to eliminate common prefixes.

What is the common prefix for the following grammar rule?A : α β1 | α βn | γ

Page 40: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left-Factoring (Remove Common Prefixes)

To eliminate backtracking or multiple look-ahead, we need to eliminate common prefixes.

What is the common prefix for the following grammar rule?A : α β1 | α βn | γ

Add a new rule that is all possible suffixes for the given common prefix.A : α A’ | γA’ : β1 | βn

Page 41: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Left-Factoring (Remove Common Prefixes)

1. Expr : Term + Expr | Term

2. Term : int ‘*’ Term| int ‘/’ Term | ‘(‘ Expr ‘)’

Left-Factoring Transformation

Input Pattern:A → 𝛼𝛽! 𝛼𝛽" … 𝛼𝛽# 𝛾

Output Pattern:A → 𝛼𝐴$ | 𝛾A′ → 𝛽! 𝛽" … |𝛽#

This leads to right associativity

Page 42: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

• Capitals are non-terminals• Lowercase are terminals

• Is this valid input: gdchfc?

Any left recursion?

SBc

gAcgCAc

gdCAcgdcAcgdcDfcgdchfc

Page 43: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

• Capitals are non-terminals• Lowercase are terminals

• Is this valid input: gdchfc?

Any left recursion?

1

2

3

4

5

2 3

5 4

2

4

SBc

gAcgCAc

gdCAcgdcAcgdcDfcgdchfcNope

Page 44: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

FUNCTION S()TRY

A()MatchInput('b’)ConsumeToken()

ELSEB()MatchInput('c’)ConsumeToken()

Page 45: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

S : (Df | CA)b | (gA | e)cA : Df | CAB : gA | eC : dC | cD : h | i

Page 46: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

S : Dfb | CAb | gAc | ecA : Df | CAB : gA | eC : dC | cD : h | i

Page 47: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

S : Dfb | CAb | gAc | ecA : Df | CAB : gA | eC : dC | cD : h | i

Page 48: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

S : (h | i)fb | (dC | c)Ab | gAc | ecA : Df | CAB : gA | eC : dC | cD : h | i

Page 49: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

S : hfb | ifb | dcAb | cAb | gAc | ecA : Df | CAB : gA | eC : dC | cD : h | i

Now we can easily pick an alternative for the S rule based on the current input.

Page 50: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

S : hfb | ifb | dcAb | cAb | gAc | ecA : Df | CAB : gA | eC : dC | cD : h | i

FUNCTION S()MATCH

'h' -> ...'i' -> ...'d' -> ...'c' -> ...'g' -> ...'e' -> ...

Page 51: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

S : hfb | ifb | dcAb | cAb | gAc | ecA : (h | i)f | (dC | c)AB : gA | eC : dC | cD : h | i

Page 52: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

S : hfb | ifb | dcAb | cAb | gAc | ecA : hf | if | dCA | cAB : gA | eC : dC | cD : h | i

Do we need all rules?

Page 53: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Ensuring Look-Ahead of 1

S : Ab | BcA : Df | CAB : gA | eC : dC | cD : h | i

S : hfb | ifb | dcAb | cAb | gAc | ecA : hf | if | dCA | cAB : gA | eC : dC | cD : h | i

Is this valid input: gdchfc?S

gAcgdCAcgdcAcgdchfc

Page 54: Recursive Descent Grammars - Missouri State Universitycourses.missouristate.edu/.../14-recursive-descent... · LL(k) Parsing •Top-down approach •Uses at most the next k tokens

Summary of LL Parsing

Write Grammar

Remove Ambiguity

Remove Left Recursion

Left Factor

Implement the LL(k) Parser

Decide on an LL(k) parser

Top-Down Parser

Better Top-Down Parser

Recursive Descent Parser

Predictive Parser