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 and Context-Free Grammars: Lecture 5 by Dr. Sean Peisert - Prof. Sean P. Peisert, Study Guides, Projects, Research of Computer Science

A transcript from lecture 5 of dr. Sean peisert's intro to parsing course, focusing on formal languages, regular languages, context-free languages, and context-free grammars. The lecture covers the importance of formal languages in computer science, the limitations of regular languages, and the role of parsing in distinguishing valid strings of tokens from invalid ones.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/30/2009

koofers-user-4uw-1
koofers-user-4uw-1 🇺🇸

10 documents

1 / 42

Toggle sidebar

Related documents


Partial preview of the text

Download Parsing and Context-Free Grammars: Lecture 5 by Dr. Sean Peisert - Prof. Sean P. Peisert and more Study Guides, Projects, Research Computer Science in PDF only on Docsity! Intro to Parsing Lecture 5 Dr. Sean Peisert – ECS 142 – Spring 2009 1 Friday, April 10, 2009 Announcements • Project 1 due Friday, Apr. 10 at 11:55pm • Project 2 also to be assigned Friday, and is due Friday, April 24 at 11:55pm • Read ... by lecture this Wednesday 2 Friday, April 10, 2009 What Can Regular Languages Express? • Languages requiring counting, modulo a fixed integer • Intuition: a finite automaton that runs long enough must repeat states • Finite automaton can’t remember the # of times it has visited a particular state 5 Friday, April 10, 2009 Parser Functionality • Input: sequence of tokens from lexer • Output: parse tree of program (usually) 6 Friday, April 10, 2009 (Diagram) 7 Friday, April 10, 2009 Context-Free Grammars (CFGs) • Programming language constructs have a recursive structure • An EXPR is: • if EXPR then EXPR else EXPR fi • while EXPR loop EXPR pool • ... • CFGs are a natural notation for this structure 10 Friday, April 10, 2009 Context-Free Grammars (CFGs) • A CFG consists of • a set of non-terminals N (written in upper case) • a set of terminals T (written in lower case) • a start symbol S (non-terminal; the LHS of the first production) • a set of productions • Assuming some E ∈ N • E → ε or • E → Y1Y2 ...Yn where Yi ⊆ (N U T) 11 Friday, April 10, 2009 Examples • Fragment of Cool: EXPR → if EXPR then EXPR else EXPR fi | while EXPR loop EXPR pool | id 12 Friday, April 10, 2009 Key Idea 1. Begin with a string consisting of the start symbol “S” 2. Replace any non-terminal X in the string by a right-hand side of some production: X → Y1 ... Yn 3. Repeat (2) until there are no non-terminals in the string 15 Friday, April 10, 2009 Language of a CFG • More formally, write: X1 ... Xi ... Xn → X1 Xi-1 Y1 ... Ym Xi+1 ... Xn if there is a production: Xi → Yi ... Ym 16 Friday, April 10, 2009 Language of a CFG • Write: X1 ... Xn → Y1 ... Ym ...if there is a production: X1 ... Xn → ... → ... → Y1 ... Ym ...in 0 or more steps. 17 Friday, April 10, 2009 Examples • L(G) is the language of CFG G • Strings of balanced parentheses { (i)i | i ≥ 0 } • Two grammars S → (S) OR S → (S) S → ε | ε 20 Friday, April 10, 2009 Example All in the language: if if id then id else id fi while id loop id pool if while id loop id pool then id else id if if id then id else id fi then id else id fi 21 Friday, April 10, 2009 Arithmetic Example E → E+E | E*E | (E) | id • elements in the language id id + id (id) id * id (id) * id id * (id) 22 Friday, April 10, 2009 Derivations and Parse Trees 25 • A derivation is a sequence of productions S → ... → ... → ... • A derivation can be drawn as a tree • Start symbol is the tree’s root • For a production X → Y1 .. Yn, add children Y1...Yn to node X Friday, April 10, 2009 Derivation Example • Grammar E → E+E | E&E | (E) | id • String id * id + id 26 Friday, April 10, 2009 (diagrams) 27 Friday, April 10, 2009 (diagrams) 30 Friday, April 10, 2009 Summary of Derivations 31 • We are not just interested in whether S ∈ L(G) • We need a parse tree for s • A derivation defines a parse tree. • But one parse tree may have many derivations • Left-most and right-most derivations are important in parser implementation Friday, April 10, 2009 Ambiguity • Grammar E → E+E | E&E | (E) | id • String id * id + id 32 Friday, April 10, 2009 (diagrams) 35 Friday, April 10, 2009 The Dangling Else 36 • Consider the grammar E → if E then E | if E then E else E | OTHER • This grammar is also ambiguous Friday, April 10, 2009 (diagrams) 37 Friday, April 10, 2009 Ambiguity 40 • No general techniques for handling ambiguity • Impossible to convert automatically an ambiguous grammar to an unambiguous one • Used with care, ambiguity can simplify the grammar • Sometimes allows more natural definitions • We need disambiguation mechanisms Friday, April 10, 2009 Precedence and Associativity Decls. 41 • Instead of rewriting the grammar • Use the more natural (ambiguous grammar) • Along with disambiguating declarations • Most tools allow precedence and associativity declarations to disambiguate grammars Friday, April 10, 2009 (diagrams) 42 Friday, April 10, 2009
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved