Recursive descent for grammars with contexts

Similar documents
Compiling Techniques

From EBNF to PEG. Roman R. Redziejowski. Concurrency, Specification and Programming Berlin 2012

Compiling Techniques

Top-Down Parsing and Intro to Bottom-Up Parsing

Syntax Analysis - Part 1. Syntax Analysis

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

CSE302: Compiler Design

Automata Theory CS F-08 Context-Free Grammars

Talen en Compilers. Johan Jeuring , period 2. October 29, Department of Information and Computing Sciences Utrecht University

Solution. S ABc Ab c Bc Ac b A ABa Ba Aa a B Bbc bc.

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CS375: Logic and Theory of Computing

Compiling Techniques

MA/CSSE 474 Theory of Computation

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

CS415 Compilers Syntax Analysis Top-down Parsing

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Logic. proof and truth syntacs and semantics. Peter Antal

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

Parsing. Unger s Parser. Laura Kallmeyer. Winter 2016/17. Heinrich-Heine-Universität Düsseldorf 1 / 21

From EBNF to PEG. Roman R. Redziejowski. Giraf s Research

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

EXAMPLE CFG. L = {a 2n : n 1 } L = {a 2n : n 0 } S asa aa. L = {a n b : n 0 } L = {a n b : n 1 } S asb ab S 1S00 S 1S00 100

Context-free Grammars and Languages

From EBNF to PEG. Roman R. Redziejowski

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

Context-free Languages and Pushdown Automata

A brief introduction to Logic. (slides from

CPSC 313 Introduction to Computability

CSCI Compiler Construction

Harvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

Information and Computation 194 (2004) Boolean grammars. Alexander Okhotin

CFG Simplification. (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions

Lecture 11 Context-Free Languages

An extension of recursive descent parsing for Boolean grammars

Compiling Techniques

CPS 220 Theory of Computation Pushdown Automata (PDA)

Parsing. Unger s Parser. Introduction (1) Unger s parser [Grune and Jacobs, 2008] is a CFG parser that is

Automata Theory and Formal Grammars: Lecture 1

Introduction to Bottom-Up Parsing

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

Structural Induction

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 3. Y.N. Srikant

CS 314 Principles of Programming Languages

CPS 220 Theory of Computation

THEORY OF COMPILATION

Linear conjunctive languages are closed under complement

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Theory of Computation - Module 3

Derivation in Scattered Context Grammar via Lazy Function Evaluation

Introduction to Bottom-Up Parsing

Grammars and Context Free Languages

Syntax Analysis Part I

Logical Agents. Outline

Symposium on Theoretical Aspects of Computer Science 2008 (Bordeaux), pp

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

Notes for Comp 497 (Comp 454) Week 10 4/5/05

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Syntactic Analysis. Top-Down Parsing

Grammars and Context Free Languages

Everything You Always Wanted to Know About Parsing

02 Propositional Logic

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

Solutions to Problem Set 3

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

Even More on Dynamic Programming

Formal Languages, Grammars and Automata Lecture 5

An Introduction to Modal Logic III

CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar

Concordia University Department of Computer Science & Software Engineering

Introduction to Bottom-Up Parsing

Plan for 2 nd half. Just when you thought it was safe. Just when you thought it was safe. Theory Hall of Fame. Chomsky Normal Form

6.8 The Post Correspondence Problem

Creating a Recursive Descent Parse Table

Tree Adjoining Grammars

Intro to Theory of Computation

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

Compiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Introduction to Bottom-Up Parsing

Computer Science 160 Translation of Programming Languages

Syntax Analysis Part I

Compiler Principles, PS4

CS415 Compilers Syntax Analysis Bottom-up Parsing

5.A Examples of Context-Free Grammars 3.1 and 3.2 of Du & Ko s book(pp )

Computer Science 160 Translation of Programming Languages

CVO103: Programming Languages. Lecture 2 Inductive Definitions (2)

Shift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition

Parikh s Theorem and Descriptional Complexity

Automata-Based String Analysis

Introduction to Artificial Intelligence Propositional Logic & SAT Solving. UIUC CS 440 / ECE 448 Professor: Eyal Amir Spring Semester 2010

Context-Free Grammars: Normal Forms

CS 373: Theory of Computation. Fall 2010

Transcription:

39th International Conference on Current Trends in Theory and Practice of Computer Science Špindleruv Mlýn, Czech Republic Recursive descent parsing for grammars with contexts Ph.D. student, Department of Mathematics and Statistics, University of Turku Turku Centre for Computer Science (TUCS) Finland January 26 31, 2013

What a recursive descent is? subclass of grammars allowing recursive descent: LL(k)-grammars k look-ahead symbols

What a recursive descent is? subclass of grammars allowing recursive descent: LL(k)-grammars k look-ahead symbols LL(1) context-free grammar generating { a n b n n 0 }: S asb ε S() { a() { if (look-ahead is a ) { if (current symbol is a ) a(); S(); b(); advance position by 1 } else error } }

What a recursive descent is? subclass of grammars allowing recursive descent: LL(k)-grammars k look-ahead symbols LL(1) context-free grammar generating { a n b n n 0 }: S asb ε S() { a() { if (look-ahead is a ) { if (current symbol is a ) a(); S(); b(); advance position by 1 } else error } } First compilers for Pascal are recursive descent parsers. Implemented by hand (N. Wirth, 1970). Program code can be easily generated automatically.

Extension of CFGs with Boolean operations Conjunctive grammars (Okhotin, 2001) A α 1 &... & α k, α i (Σ N)

Extension of CFGs with Boolean operations Conjunctive grammars (Okhotin, 2001) A α 1 &... & α k, α i (Σ N) a string w has property A w has all the properties α 1,..., α k

Extension of CFGs with Boolean operations Conjunctive grammars (Okhotin, 2001) A α 1 &... & α k, α i (Σ N) a string w has property A w has all the properties α 1,..., α k { a n b n c n n 0 } S AB & DC A aa ε B bbc ε C cc ε D adb ε

Extension of CFGs with Boolean operations Conjunctive grammars (Okhotin, 2001) A α 1 &... & α k, α i (Σ N) a string w has property A w has all the properties α 1,..., α k { a n b n c n n 0 } S AB & DC A aa ε B bbc ε C cc ε D adb ε non-context-free languages generated complexity of basic parsing algorithms preserved nontrivial properties of subclasses

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n Given a string x = u w v: each α i defines w

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n Given a string x = u w v: each α i defines w each β i defines v (the right context of w)

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n Given a string x = u w v: each α i defines w each β i defines v (the right context of w) each γ i defines wv (the extended right context of w)

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n Given a string x = u w v: each α i defines w each β i defines v (the right context of w) each γ i defines wv (the extended right context of w) Semantics by logical deduction of elementary propositions [X, w v] : a string w written in right context v has the property X

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n Given a string x = u w v: each α i defines w each β i defines v (the right context of w) each γ i defines wv (the extended right context of w) Semantics by logical deduction of elementary propositions [X, w v] : a string w written in right context v has the property X {a n b n c} {a n b 2n d}

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n Given a string x = u w v: each α i defines w each β i defines v (the right context of w) each γ i defines wv (the extended right context of w) Semantics by logical deduction of elementary propositions [X, w v] : a string w written in right context v has the property X {a n b n c} {a n b 2n d} S Ac Bd A aab ε L(A) = {a n b n } B abbb ε L(B) = {a n b 2n }

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n Given a string x = u w v: each α i defines w each β i defines v (the right context of w) each γ i defines wv (the extended right context of w) Semantics by logical deduction of elementary propositions [X, w v] : a string w written in right context v has the property X {a n b n c} {a n b 2n d} S Ac Bd A aab ε L(A) = {a n b n } B abbb ε L(B) = {a n b 2n } the language is not standard LL(k) for any k

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n Given a string x = u w v: each α i defines w each β i defines v (the right context of w) each γ i defines wv (the extended right context of w) Semantics by logical deduction of elementary propositions [X, w v] : a string w written in right context v has the property X {a n b n c} {a n b 2n d} S Ac Bd A aab ε L(A) = {a n b n } B abbb ε L(B) = {a n b 2n } the language is not standard LL(k) for any k way out: use right contexts to peek the last symbol of the string

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n Given a string x = u w v: each α i defines w each β i defines v (the right context of w) each γ i defines wv (the extended right context of w) Semantics by logical deduction of elementary propositions [X, w v] : a string w written in right context v has the property X {a n b n c} {a n b 2n d} S Ac Bd A aab ε L(A) = {a n b n } B abbb ε L(B) = {a n b 2n } X ax bx ε L(X ) = (a b) the language is not standard LL(k) for any k way out: use right contexts to peek the last symbol of the string

Grammars with contexts (B, Okhotin, 2012) A α 1 &... & α k & β 1 &... & β m & γ 1 &... & γ n Given a string x = u w v: each α i defines w each β i defines v (the right context of w) each γ i defines wv (the extended right context of w) Semantics by logical deduction of elementary propositions [X, w v] : a string w written in right context v has the property X {a n b n c} {a n b 2n d} S Xc & Ac Xd & Bd A aab ε L(A) = {a n b n } B abbb ε L(B) = {a n b 2n } X ax bx ε L(X ) = (a b) the language is not standard LL(k) for any k way out: use right contexts to peek the last symbol of the string

Results Recursive descent parser:

Results Recursive descent parser: conjunction in rules: scan the substring multiple times

Results Recursive descent parser: conjunction in rules: scan the substring multiple times right contexts (, ): to choose suitable alternatives of rules

Results Recursive descent parser: conjunction in rules: scan the substring multiple times right contexts (, ): to choose suitable alternatives of rules backtracking: to go over the alternatives

Results Recursive descent parser: conjunction in rules: scan the substring multiple times right contexts (, ): to choose suitable alternatives of rules backtracking: to go over the alternatives memoization: linear time instead of exponential

Results Recursive descent parser: Theory: conjunction in rules: scan the substring multiple times right contexts (, ): to choose suitable alternatives of rules backtracking: to go over the alternatives memoization: linear time instead of exponential mathematically sound definition of right contexts ( look-ahead ) within recursive descent

Results Recursive descent parser: Theory: conjunction in rules: scan the substring multiple times right contexts (, ): to choose suitable alternatives of rules backtracking: to go over the alternatives memoization: linear time instead of exponential mathematically sound definition of right contexts ( look-ahead ) within recursive descent higher expressive power, but still linear time

Results Recursive descent parser: Theory: conjunction in rules: scan the substring multiple times right contexts (, ): to choose suitable alternatives of rules backtracking: to go over the alternatives memoization: linear time instead of exponential mathematically sound definition of right contexts ( look-ahead ) within recursive descent higher expressive power, but still linear time parsing algorithm proven correct

Results Recursive descent parser: Theory: conjunction in rules: scan the substring multiple times right contexts (, ): to choose suitable alternatives of rules backtracking: to go over the alternatives memoization: linear time instead of exponential mathematically sound definition of right contexts ( look-ahead ) within recursive descent Practice: higher expressive power, but still linear time parsing algorithm proven correct implemented as a prototype software

Results Recursive descent parser: Theory: conjunction in rules: scan the substring multiple times right contexts (, ): to choose suitable alternatives of rules backtracking: to go over the alternatives memoization: linear time instead of exponential mathematically sound definition of right contexts ( look-ahead ) within recursive descent Practice: higher expressive power, but still linear time parsing algorithm proven correct implemented as a prototype software