Replacing recursion with iteration

Slides:



Advertisements
Similar presentations
AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
Advertisements

Lecture # 11 Grammar Problems.
YANGYANG 1 Chap 5 LL(1) Parsing LL(1) left-to-right scanning leftmost derivation 1-token lookahead parser generator: Parsing becomes the easiest! Modifying.
Top-Down Parsing.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
Professor Yihjia Tsai Tamkang University
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Top-Down Parsing.
Chapter 6: Top-Down Parser1 Compiler Designs and Constructions Chapter 6: Top-Down Parser Objectives: Types of Parsers Backtracking Vs. Non-backtracking.
Compiler Construction Sohail Aslam Lecture LL(1) Table Construction For each production A →  1.for each terminal a in FIRST(  ), add A →  to.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
Top-Down Parsing - recursive descent - predictive parsing
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 6, 10/02/2003 Prof. Roy Levow.
Left Recursion Lecture 7 Fri, Feb 4, 2005.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Context-Free Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.
Syntax Analyzer (Parser)
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
LL(1) Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 7.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
Context-free grammars
Lexical and Syntax Analysis
Context-free grammars, derivation trees, and ambiguity
Lecture #12 Parsing Types.
LL(1) grammars Module 07.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Writing a scanner Module 05.5 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Recursive Descent Parsing
Fixing non-ll(1) grammars
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Compiler Construction
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Top-down derivation tree generation
4 (c) parsing.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Regular grammars Module 04.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
CS 540 George Mason University
Recursive Descent Parsing
Bottom-up AST, original grammar
Top-down derivation tree generation
Top-down parsing Module 06.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Bottom-up derivation tree, original grammar
Bottom-up derivation tree, original grammar
TaBle-driven LL(1) Parsing
Bottom-up AST, original grammar
Bottom-up derivation tree generation
TaBle-driven LL(1) Parsing
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Fixing non-ll(1) grammars
Regular Expression to NFA
Regular Expression to NFA
Replacing recursion with iteration
Programming Language Principles
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Operator precedence and AST’s
Bottom-up derivation tree generation
Compiler Construction
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Operator Precedence and Associativity
Programming Language Principles
Programming Language Concepts
Presentation transcript:

Replacing recursion with iteration COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

Replacing recursion with iteration To make our grammar LL(1), we introduced nonterminals X,Y,Z. None are needed. SL isn’t needed, either.

Grammars (So FAR) Modified, LL(1)) grammar. Our “model” PL grammar. S → begin SL end {begin} → id := E ; {id} SL → S Z {begin,id} Z → S Z {begin,id} → {end} E → T Y {(,id} Y → + T Y {+} → {;,)} T → P X {(,id} X → * T {*} → {;,+,)} P → (E) {(} → id {id} S → begin SL end {begin} → id := E; {id} SL → SL S {begin,id} → S {begin,id} E → E+T {(,id} → T {(,id} T → P*T {(,id} → P {(,id} P → (E) {(} → id {id} Procedures SL, X, Y and Z can all be eliminated.

Replacing recursion S → begin SL end → id := E ; SL → S Z Z → S Z proc S; case Next_Token of T_begin : Read(T_begin); repeat S(); until Next_Token  {T_begin,T_id}; Read(T_end); T_id : Read(T_id); Read (T_:=); E(); Read (T_;); otherwise Error; end; Replaces call to SL, and recursion on Z. Regular Right-Part Grammar: S → begin S+ end → id := E ; Still haven’t restored left associativity of ‘’ operator.

Replacing recursion E → TY Y → +TY → T → PX X → *T E → T(+T)* proc E; T(); while Next_Token = T_+ do Read (T_+); od; end; proc T; P(); if Next_Token = T_* then Read (T_*); T(); E → TY Y → +TY → T → PX X → *T Replaces call to Y, and recursion on Y. Replaces call to X. X is not recursive. Regular Right-Part Grammar: E → T(+T)* T → P(*T)? Still have not restored left associativity of ‘+’.

Replacing recursion proc P; case Next Token of T_(: Read(T_(); E(); T_id: Read(T_id); otherwise Error; end; Regular Right-Part Grammar: S → begin S+ end → id := E ; E → T(+T)* T → P(*T)? P → (E) → id Simple, eh ? Simple code, too. No change!

summary To make our grammar LL(1), we introduced nonterminals X,Y,Z. We just got rid of them. Got rid of SL, too. Resulting code is remarkably simple: reflects RRPG. Still have not restored left associativity of operators.