Context Free Grammars - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Context Free Grammars

Description:

Specification of Context Free Grammar Rules. Derivations & Language Defined by a Grammar ... Dangling Else Problem. Inessential Ambiguity. 8 January 2004 ... – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 21
Provided by: drakbar
Category:

less

Transcript and Presenter's Notes

Title: Context Free Grammars


1
Chapter 3
  • Context Free Grammars
  • Comparison to RE Notation
  • Specification of Context Free Grammar Rules
  • Derivations Language Defined by a Grammar
  • Examples

2
Parse Trees Abstract Syntax Trees
  • Parse Trees
  • Abstract Syntax Tree
  • Examples

3
Ambiguity
  • Ambiguous Grammars
  • Precedence and Associativity
  • Dangling Else Problem
  • Inessential Ambiguity

4
Extended Notations
  • EBNF Notation
  • Syntax Diagrams
  • Examples

5
Formal Properties of CFG
  • Grammar Rles as Equations
  • Chomsky Hierarchy
  • Limits of Syntax as Context Free Rules

6
FA from Grammar
  • One state for each non-terminal
  • A rule of the form
  • Nt1 terminal, generates transition from a
    state to final state
  • A rule of the form
  • Nt1 terminal Nt2
  • Generates transition from state 1 to state 2 on
    an arc labeled by the terminal

7
Graphic representation of FA

8
FA from RE
  • Each RE corresponds to a grammar
  • For all REs
  • A natural translation to FSM exists
  • Alternation often leads to non-deterministic
    machines

9
Deterministic Finite Automata (DFA)
  • For all states S
  • For all characters C
  • There is at most one arc from any state S that is
    labeled with C
  • Easier to implement
  • No backtracking
  • Conventions for DFA
  • Error transitions are not explicitly shown
  • Input symbols that result in the same transition
    are grouped together (this set can even be given
    a name)
  • Still not displayed stopping conditions and
    actions

10
Non-Deterministic Finite Automata (NFA)
  • A non-deterministic FA
  • Has at least one state
  • With two arcs to two distinct states
  • Labeled with the same character
  • Example from start state, a digit can begin an
    integer literal or a real literal
  • Implementation requires backtracking

11
Lookahead Backtracking in NFA
12
Implementation of FA
13
From RE to DFA RE to NFA
14
NFA to DFA
  • There is an algorithm for converting a
    non-deterministic machine to a deterministic one
  • Result may have exponentially more states
  • Intuitively need new states to express
    uncertainty about token int or real
  • Other algorithms for minimizing number of states
    of FSM, for showing equivalence, etc.

15
Example DFA
16
Another view of the same DFA
17
Yet another view of the same DFA
18
State Minimization in DFA
19
TINY DFA
20
Lex for Scanner
  • Lex Conventions for RE
  • Format of a Lex Input File
Write a Comment
User Comments (0)
About PowerShow.com