Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Parsing Algorithms and LR(0) Parsing for Programming Languages, Slides of Theory of Automata

An overview of parsing algorithms, focusing on the lr(0) parsing algorithm for context-free grammars. It includes the lr(0) parsing algorithm outline, viable item updates, and examples of converting εnfa to dfa. The document also discusses lr(0) grammars and deterministic pdas.

Typology: Slides

2012/2013

Uploaded on 04/29/2013

shamir_69
shamir_69 🇮🇳

5

(4)

66 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Parsers for programming languages
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Parsing Algorithms and LR(0) Parsing for Programming Languages and more Slides Theory of Automata in PDF only on Docsity!

Parsers for programming languages

CFG of the java programming language Identifier: IDENTIFIER QualifiedIdentifier:Identifier {. Identifier } Literal: IntegerLiteral FloatingPointLiteralCharacterLiteral StringLiteralBooleanLiteral NullLiteral Expression: Expression1 [AssignmentOperator Expression1]] AssignmentOperator:= +=-= *=/= &=|=

from http://java.sun.com/docs/books/jls /second_edition/html/syntax.doc.html#

Parsing algorithms

• How long would it take to parse this?

• Can we parse faster?

• No! CYK is the fastest known general-purpose

parsing algorithm

exhaustive algorithm about^10 80 years (longer than life of universe)

CYK algorithm about^1 week!

Another way of thinking

Scientist:

Find an algorithm that can parse strings in any grammar

Engineer:

Design your grammar so it has a very fast parsing algorithm

Items

S → •Tc S → T•c S → Tc•

T → •TA
T → T•A
T → TA•
T → •A
T → A•

A → •aTb A → a•Tb A → aT•b A → aTb•

A → •ab A → a•b A → ab•

S → Tc (1)^ T → TA (2)^ T → A (3) A → aTb(4)^ A → ab(5)

Stack (^) Input ε a ab A T Ta

abaabbc baabbc aabbc aabbc aabbc abbc

Action shift shift reduce (5) reduce (3) shift shift

  • • • • • • Idea of parsing algorithm: Try to match complete items to top of stack

Some terminology

S → Tc (1) T → TA (2)^ | A (3) A → aTb(4)^ | ab (5)

input: abaabbc

Stack (^) Input

ε a ab A T Ta Taa Taab TaA TaT TaTb TA T Tc S

abaabbc baabbc aabbc aabbc aabbc abbc bbc bc bc bc c c c ε ε

Action shift shift reduce (5) reduce (3) shift shift shift reduce (5) reduce (3) shift reduce (4) reduce (2) shift reduce (1)

handle

valid items: a•Tb, a•b

valid items: T•a, T•c, aT•b

Running the algorithm

Stack Input

S S S R S R

ε a

aa

aab aA aAb A

aabb abb

bb

b b ε ε

A Valid Items

A → •aAb A → •ab A → a•Ab A → a•b A → •aAb A → •ab A → a•Ab A → a•b A → •aAb A → •ab A → ab• A → aA•b A → aAb•

A → aAb | ab A ⇒ aAb ⇒ aabb

Running the algorithm

Stack Input

S S S R S R

ε a

aa

aab aA aAb A

aabb abb

bb

b b ε ε

A Valid Items

A → •aAb A → •ab A → a•Ab A → a•b A → •aAb A → •ab A → a•Ab A → a•b A → •aAb A → •ab A → ab• A → aA•b A → aAb•

A → aAb | ab A ⇒ aAb ⇒ aabb

How to update viable items

• Updating valid items on “reduce β to B”

  • First, we backtrack to viable items before reduce
  • Then, we apply same rules as for “shift B” (as if B

were a

terminal)

A → α•Bβ is updated to A^ →^ αB•β A → α• X β disappears if^ X^ ≠ B

C → • δ is added for every valid item A → α•Cβ and production C → • δ

Viable item updates by εNFA

• States of εNFA will be items (plus a start state

q 0 )

• For every item S → • α we have a transition

• For every item A → α• X β we have a transition

• For every item A → α•Cβ and production C →

q 0 ε S → • α

A → α X • β

X

A → α• X β

A → α•Cβ^ ε C → • δ

Convert εNFA to DFA

A → •aAb A→ •ab

A → a•Ab A → a•b A → •aAb A → •ab

A → aA•b

A → aAb•

A → ab•

a

b

A b a

states correspond to sets of valid items

transitions are labeled by variables / terminals

die

Attempt at parsing with DFA

Stack Input

S
S
S
R

ε a

aa

aab aA

aabb abb

bb

b b

A DFA state A → •aAb A → •ab A → a•Ab A → a•b A → •aAb A → •ab A → a•Ab A → a•b A → •aAb A → •ab A → ab• A → aA•b

A → aAb | ab A ⇒ aAb ⇒ aabb

LR(0) grammars and deterministic

PDAs

• The parsing procedure can be implemented by

a

deterministic pushdown automaton

• A PDA is deterministic if in every state there is

at

most one possible transition

  • for every input symbol and pop symbol, including ε

• Example: PDA for w # wR^ is deterministic, but

PDA for Docsity.com

LR(0) grammars and deterministic

PDAs

• Not every PDA can be made deterministic

• Since PDAs are equivalent to CFLs, LR(0)

parsing algorithm must fail for some CFLs!

• When does LR(0) parsing algorithm fail?