Context-Free Languages. Regular Languages Context-Free 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 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
CFGs and PDAs Sipser 2 (pages ). Last time…
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 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.
COP4020 Programming 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.
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.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
Context-Free Languages Hinrich Schütze CIS, LMU, Slides based on RPI CSCI 2400 Thanks to Costas Busch.
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
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.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
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.
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
5. Context-Free Grammars and Languages
Context-Free Languages
G. Pullaiah College of Engineering and Technology
CS510 Compiler Lecture 4.
Simplifications of Context-Free Grammars
Syntax Analysis Sections :.
Lecture 7: Introduction to Parsing (Syntax Analysis)
Parsing Costas Busch - LSU.
Introduction to Parsing
Normal Forms for Context-free Grammars
Presentation transcript:

Context-Free Languages

Regular Languages

Context-Free Languages

Pushdown Automata Context-Free Grammars stack automaton

Context-Free Grammars

Example A context-free grammar : A derivation:

A context-free grammar : Another derivation:

(((( )))) Describes parentheses:

A context-free grammar : A derivation: Example

A context-free grammar : Another derivation:

A context-free grammar : A derivation: Example

A context-free grammar : A derivation:

() ((( ))) (( )) Describes matched parentheses:

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

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

Derivation Order Leftmost derivation: Rightmost derivation:

Leftmost derivation: Rightmost derivation:

Derivation Trees

Derivation Tree

yield Derivation Tree

Partial Derivation Trees Partial derivation tree

sentential form yield

Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:

Ambiguity

leftmost derivation

Two derivation trees

The grammar is ambiguous: stringhas two derivation trees

stringhas two leftmost derivations The grammar is ambiguous:

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

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

Why do we care about ambiguity? take

Correct result:

We want to remove ambiguity Ambiguity is bad for programming languages

We fix the ambiguous grammar: New non-ambiguous grammar:

Unique derivation tree

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

Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT

If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2

Inherent Ambiguity Some context free languages have only ambiguous grammars Example:

The string has two derivation trees

Compilers

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

Lexical analyzer parser Compiler program machine code input output

A parser knows the grammar of the programming language

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

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

10 E 25 E => E + E => E + E * E => 10 + E*E => * E => * 5 derivation derivation tree EE EE + *

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

Parsing

grammar Parser input string derivation

Example: Parser derivation input ?

Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of

Phase 2 Phase 1

Phase 2 Phase 3

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

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

Time for phase 1: possible derivations For grammar with rules

Time for phase 2: possible derivations

Time for phase : possible derivations

Total time needed for string : Extremely bad!!! phase 1 phase 2 phase 2|w|

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

S-grammar example: Each string has a unique derivation

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:

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