FACS Week 15 Recursive Descent Parsers, Table Driven Recursive Descent Parsers TUTORIAL: Creating LL parsers using Tables.

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Compiler Construction
Grammar vs Recursive Descent Parser
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Exercise: Balanced Parentheses
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Lecture # 11 Grammar Problems.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Top-Down Parsing.
FACS Week 14 Grammars and their properties; Introduction to Parsing Lee McCluskey, room 2/07
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
Context-Free Grammars Lecture 7
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Chapter 2 A Simple Compiler
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
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.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Top-Down Parsing.
CPSC 388 – Compiler Design and Construction
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Syntax and Semantics Structure of programming languages.
Top-Down Parsing - recursive descent - predictive parsing
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Chapter 5 Top-Down Parsing.
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
LANGUAGE TRANSLATORS: WEEK 17 scom.hud.ac.uk/scomtlm/cis2380/ See Appel’s book chapter 3 for support reading Last Week: Top-down, Table driven parsers.
Syntax and Semantics Structure of programming languages.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
Exercise 1 A ::= B EOF B ::=  | B B | (B) Tokens: EOF, (, ) Generate constraints and compute nullable and first for this grammar. Check whether first.
CPS 506 Comparative Programming Languages Syntax Specification.
COS 320 Compilers David Walker. The Front End Lexical Analysis: Create sequence of tokens from characters (Chap 2) Syntax Analysis: Create abstract syntax.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Parsing Top-Down.
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.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Parsing III (Top-down parsing: recursive descent & LL(1) )
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Parsing and Parser Parsing methods: top-down & bottom-up
Lecture #12 Parsing Types.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Programming Language Syntax 7
Top-Down Parsing CS 671 January 29, 2008.
Recursive Descent Parsing
Compiler Design 7. Top-Down Table-Driven Parsing
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
LR Parsing. Parser Generators.
Nonrecursive Predictive Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Faculty of Computer Science and Information System
Presentation transcript:

FACS Week 15 Recursive Descent Parsers, Table Driven Recursive Descent Parsers TUTORIAL: Creating LL parsers using Tables

CREATING PARSERS Assume you have a GRAMMAR G and a string S of Tokens of G. How can you tell if G can generate S? How can you extract the structure of S if it can be generated by G? Example G, Tokens a,b,c,d 1. Z ::= d 2. Z ::=XYZ 3. Y ::= b 4. Y ::= c 5. X ::= Y 6 X ::= aZ Example string ‘bbd’

CREATING “RECURSIVE DESCENT” PARSERS Method Outline: (1) Each non-terminal N becomes a Method/Procedure N (2) Each set of productions for a non-terminal N are used to define N’s body: -the RHS forms a statement sequence where non-terminals are method calls -alternative productions for the same non-terminal form if/case statements (3) Each Method/Procedure ‘consumes’ tokens – it inputs a list and outputs a smaller list

RECURSIVE DESCENT PARSER FOR G (NB - This is rough - e.g. it does not handle failure!) Method Z(in: S: list, out: S3: list) if head S == ‘d’ then record(1); S3 = tail S; else record(2); call X(S,S1); call Y(S1,S2); call Z(S2,S3) END Method Y(in: S:list, out S1:list) if head S == ‘b’ then record(3); S1 = tail S; else if head S == ‘c’ then record(4) S1 = tail S; END Method X(in:S: list, out S1:list) if head S == ‘a’ then record(6); call Z(tail S,S1); else record(5); call Y(S,S1) END PARSER_for_G(S) = call Z(S, OUT); if OUT == ““ then SUCCESS.

RECURSIVE DESCENT PARSER FOR G in PROLOG (NB - it does not handle failure!) z([HS|TS], TS) :- HS = ‘d’, write('rule 1 used'),nl. z([HS|TS], TS0) :- write('rule 2 used '),nl, x(S,S1), y(S1,S2), z(S2,TS0). y([HS|TS], TS) :- HS = ‘b’, write('rule 3 used'),nl. y([HS|TS], TS) :- HS = ‘c’, write('rule 4 used'),nl. x([HS|TS], TS0) :- HS = ‘a’, write('rule 6 used'),nl,z(TS,TS0). x([HS|TS], TS0) :- write('rule 2 used '),nl,y(TS,TS0). Eg | ?- z("adbd",C). rule 2 used rule 6 used rule 1 used rule 3 used rule 1 used true ?

Definition of ‘LL’ Parser: A Recursive Descent Parser is termed LL(1) because -- It parsers from Left to right; -- It calls methods corresponding to non- terminals in a Left to right fashion -- It looks for the next 1 Token in the string to decide what branch to take in the parser

PROBLEMS with the RD PARSER BUT THE RD won’t work unless G is LL(1), i.e. (i) G is NOT ambiguous (ii) G is NOT left recursive (iii) for EVERY two of G’s productions of the form X ::= W1, X ::= W2, it is the case that First(W1) and First(W2) have no common element [ e.g. Add extra rule 7: Z ::= b ]

TABLE DRIVEN PARSERS Rather than translating a grammar straight into a program, its much better to translate in into a “machine” or “table”, forming a “table- driven parser”. Translating a grammar into a table is a good form of analysis as well as one step towards a parser: GRAMMAR => TABLE => PARSER

TO AUTOMATICALLY CONSTRUCT AN LL(1) PARSING TABLE The Table is of size i x j – it has i columns corresponding to G’s i Tokens, j rows corresponding to G’s j non-terminals Entries in the table are one or more production rules METHOD: 1. Enter the rule N ::= w in Row N Column m for each m in the First(w) 2. For each rule N ::= w, find if w is nullable. If w is nullable, enter N ::= w in Row N Column m for each m in the set Follow(N)

TO RUN AN LL(1) PARSING TABLE …. on a string in its language (Exercise: adjust it to make it execute on any string) Program Run-parsing-table: % assume input string is consumed a token at a time M = special symbol; a = get_token; call Loop(M,a); end Loop(in: M, in/out: a) 1. Apply the production P in Row M Column a; 2. For each of the symbols w in the RHS of P if w is a Non-Terminal: call Loop(w,a); else if w is a Token: a = get_token end

SUMMARY LL parsers are top-down - they start at the special symbol and try to parse a string from left to right. We have seen how to construct two kinds of LL(1) parser - the recursive descent method and the table driven method NEXT WEEK - LR table driven parsing - how JavaCup constructs its parsers