Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

Context-Free Languages
CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
Courtesy Costas Busch - RPI1 Positive Properties of Context-Free languages.
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Costas Busch - RPI1 Positive Properties of Context-Free languages.
Fall 2003Costas Busch - RPI1 Decidable Problems of Regular Languages.
Courtesy Costas Busch - RPI1 Context-Free Languages.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
Prof. Busch - RPI1 Decidable Problems of Regular Languages.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2005Costas Busch - RPI1 Context-Free Languages.
Context-Free Languages Hinrich Schütze CIS, LMU, Slides based on RPI CSCI 2400 Thanks to Costas Busch.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Grammars CPSC 5135.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS 3240: Languages and Computation Context-Free Languages.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Bernd Fischer RW713: Compiler and Software Language Engineering.
1 Lex. 2 Lex is a lexical analyzer Var = ; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn:
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Context-Free Languages. Regular Languages Context-Free Languages.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Syntax Analyzer (Parser)
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Chapter 5 Context-Free Languages Regular language are effective in describing certain simple patterns. But it is not enough to describe programming languages.
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
1 Context Free Grammars Xiaoyin Wang CS 5363 Spring 2016.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
COMP 3438 – Part II - Lecture 4 Syntax Analysis I Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Last Chapter Review Source code characters combination lexemes tokens pattern Non-Formalization Description Formalization Description Regular Expression.
Introduction to Parsing
Context-Free Languages
G. Pullaiah College of Engineering and Technology
CS510 Compiler Lecture 4.
PDAs Accept Context-Free Languages
NPDAs Accept Context-Free Languages
Simplifications of Context-Free Grammars
NPDAs Accept Context-Free Languages
Syntax Analysis Sections :.
Lecture 7: Introduction to Parsing (Syntax Analysis)
Parsing Costas Busch - LSU.
Decidable Problems of Regular Languages
Context-Free Languages
Normal Forms for Context-free Grammars
Presentation transcript:

Costas Busch - RPI1 Context-Free Languages

Costas Busch - RPI2 Regular Languages

Costas Busch - RPI3 Regular Languages Context-Free Languages

Costas Busch - RPI4 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton

Costas Busch - RPI5 Context-Free Grammars

Costas Busch - RPI6 Example A context-free grammar : A derivation:

Costas Busch - RPI7 A context-free grammar : Another derivation:

Costas Busch - RPI8 (((( )))) Describes parentheses:

Costas Busch - RPI9 A context-free grammar : A derivation: Example

Costas Busch - RPI10 A context-free grammar : Another derivation:

Costas Busch - RPI11

Costas Busch - RPI12 A context-free grammar : A derivation: Example

Costas Busch - RPI13 A context-free grammar : A derivation:

Costas Busch - RPI14 () ((( ))) (( )) Describes matched parentheses:

Costas Busch - RPI15 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable

Costas Busch - RPI16

Costas Busch - RPI17 Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with

Costas Busch - RPI18 Derivation Order Leftmost derivation: Rightmost derivation:

Costas Busch - RPI19 Leftmost derivation: Rightmost derivation:

Costas Busch - RPI20 Derivation Trees

Costas Busch - RPI21

Costas Busch - RPI22

Costas Busch - RPI23

Costas Busch - RPI24

Costas Busch - RPI25 Derivation Tree

Costas Busch - RPI26 yield Derivation Tree

Costas Busch - RPI27 Partial Derivation Trees Partial derivation tree

Costas Busch - RPI28 Partial derivation tree

Costas Busch - RPI29 Partial derivation tree sentential form yield

Costas Busch - RPI30 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:

Costas Busch - RPI31 Ambiguity

Costas Busch - RPI32 leftmost derivation

Costas Busch - RPI33 leftmost derivation

Costas Busch - RPI34 Two derivation trees

Costas Busch - RPI35 The grammar is ambiguous: stringhas two derivation trees

Costas Busch - RPI36 stringhas two leftmost derivations The grammar is ambiguous:

Costas Busch - RPI37 Definition: A context-free grammar is ambiguous if some string has: two or more derivation trees

Costas Busch - RPI38 In other words: A context-free grammar is ambiguous if some string has: two or more leftmost derivations (or rightmost)

Costas Busch - RPI39 Why do we care about ambiguity? take

Costas Busch - RPI40

Costas Busch - RPI41

Costas Busch - RPI42 Correct result:

Costas Busch - RPI43 We want to remove ambiguity Ambiguity is bad for programming languages

Costas Busch - RPI44 We fix the ambiguous grammar: New non-ambiguous grammar:

Costas Busch - RPI45

Costas Busch - RPI46 Unique derivation tree

Costas Busch - RPI47 The grammar : is non-ambiguous: Every string has a unique derivation tree

Costas Busch - RPI48 Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT

Costas Busch - RPI49 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2

Costas Busch - RPI50 Inherent Ambiguity Some context free languages have only ambiguous grammars Example:

Costas Busch - RPI51 The string has two derivation trees

Costas Busch - RPI52 Compilers

Costas Busch - RPI53 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3... Machine Code

Costas Busch - RPI54 Lexical analyzer parser Compiler program machine code input output

Costas Busch - RPI55 A parser knows the grammar of the programming language

Costas Busch - RPI56 Parser PROGRAM STMT_LIST STMT_LIST STMT; STMT_LIST | STMT; STMT EXPR | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR EXPR + EXPR | EXPR - EXPR | ID IF_STMT if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT while (EXPR) do STMT

Costas Busch - RPI57 The parser finds the derivation of a particular input * 5 Parser E -> E + E | E * E | INT E => E + E => E + E * E => 10 + E*E => * E => * 5 input derivation

Costas Busch - RPI58 10 E 25 E => E + E => E + E * E => 10 + E*E => * E => * 5 derivation derivation tree EE EE + *

Costas Busch - RPI59 10 E 25 derivation tree EE EE + * mult a, 2, 5 add b, 10, a machine code

Costas Busch - RPI60 Parsing

Costas Busch - RPI61 grammar Parser input string derivation

Costas Busch - RPI62 Example: Parser derivation input ?

Costas Busch - RPI63 Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of

Costas Busch - RPI64

Costas Busch - RPI65 Phase 2 Phase 1

Costas Busch - RPI66 Phase 2 Phase 3

Costas Busch - RPI67 Final result of exhaustive search Parser derivation input (top-down parsing)

Costas Busch - RPI68 Time complexity of exhaustive search Suppose there are no productions of the form Number of phases for string :

Costas Busch - RPI69 Time for phase 1: possible derivations For grammar with rules

Costas Busch - RPI70 Time for phase 2: possible derivations

Costas Busch - RPI71 Time for phase : possible derivations

Costas Busch - RPI72 Total time needed for string : Extremely bad!!! phase 1 phase 2 phase 2|w|

Costas Busch - RPI73 There exist faster algorithms for specialized grammars S-grammar: symbolstring of variables appears once Pair

Costas Busch - RPI74 S-grammar example: Each string has a unique derivation

Costas Busch - RPI75 In the exhaustive search parsing there is only one choice in each phase For S-grammars: Total time for parsing string : Time for a phase:

Costas Busch - RPI76 For general context-free grammars: There exists a parsing algorithm that parses a string in time (we will show it in the next class)