Department of Software & Media Technology Chapter 3 Context Free Grammars Comparison to RE Notation Specification of Context Free Grammar Rules Derivations & Language Defined by a Grammar Examples 8 January 2004 Department of Software & Media Technology
Parse Trees & Abstract Syntax Trees Examples 8 January 2004 Department of Software & Media Technology
Department of Software & Media Technology Ambiguity Ambiguous Grammars Precedence and Associativity Dangling Else Problem Inessential Ambiguity 8 January 2004 Department of Software & Media Technology
Department of Software & Media Technology Extended Notations EBNF Notation Syntax Diagrams Examples 8 January 2004 Department of Software & Media Technology
Formal Properties of CFG Grammar Rles as Equations Chomsky Hierarchy Limits of Syntax as Context Free Rules 8 January 2004 Department of Software & Media Technology
Department of Software & Media Technology FA from Grammar One state for each non-terminal A rule of the form Nt1 ::= terminal, generates transition from a state to final state Nt1 ::= terminal Nt2 Generates transition from state 1 to state 2 on an arc labeled by the terminal 8 January 2004 Department of Software & Media Technology
Graphic representation of FA digit letter underscore identifier 8 January 2004 Department of Software & Media Technology
Department of Software & Media Technology 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 8 January 2004 Department of Software & Media Technology
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 8 January 2004 Department of Software & Media Technology
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 8 January 2004 Department of Software & Media Technology
Lookahead & Backtracking in NFA letter start in_id [other] return id finish digit 8 January 2004 Department of Software & Media Technology
Department of Software & Media Technology Implementation of FA letter start in_id [other] return id finish digit 8 January 2004 Department of Software & Media Technology
Department of Software & Media Technology From RE to DFA & RE to NFA letter start in_id [other] return id finish digit 8 January 2004 Department of Software & Media Technology
Department of Software & Media Technology 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. 8 January 2004 Department of Software & Media Technology
Department of Software & Media Technology Example DFA 8 January 2004 Department of Software & Media Technology
Another view of the same DFA 8 January 2004 Department of Software & Media Technology
Yet another view of the same DFA 8 January 2004 Department of Software & Media Technology
State Minimization in DFA 8 January 2004 Department of Software & Media Technology
Department of Software & Media Technology TINY DFA: 8 January 2004 Department of Software & Media Technology
Department of Software & Media Technology Lex for Scanner Lex Conventions for RE Format of a Lex Input File 8 January 2004 Department of Software & Media Technology