Chapter 4 Top-Down Parsing Part-1 September 8, 2018

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Top-Down Parsing.
Parsing III (Eliminating left recursion, recursive descent parsing)
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 3 Lexical and Syntactic Analysis Syntactic.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Syntax and Semantics Structure of programming languages.
Parsing Chapter 4 Parsing2 Outline Top-down v.s. Bottom-up Top-down parsing Recursive-descent parsing LL(1) parsing LL(1) parsing algorithm First.
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?
Chapter 5 Top-Down Parsing.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
Context-Free Grammars and Parsing
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Syntax and Semantics Structure of programming languages.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.
Compiler Principle and Technology Prof. Dongming LU Mar. 27th, 2015.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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 CIS 461 Compiler Design and Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #8 Parsing Techniques.
Compiler Principle and Technology Prof. Dongming LU Mar. 12th, 2014.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #6 Parsing.
Syntax and Semantics Structure of programming languages.
Introduction to Parsing
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
CSE 3302 Programming Languages
COMPILER CONSTRUCTION
Chapter 3 – Describing Syntax
Parsing — Part II (Top-down parsing, left-recursion removal)
Programming Languages 2nd edition Tucker and Noonan
Parsing & Context-Free Grammars
Programming Languages Translator
CS510 Compiler Lecture 4.
Lecture #12 Parsing Types.
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing.
Compiler Construction (CS-636)
4 (c) parsing.
Parsing Techniques.
Programming Language Syntax 7
Lexical and Syntax Analysis
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
CSC 4181 Compiler Construction Parsing
Lecture 7: Introduction to Parsing (Syntax Analysis)
Lecture 8: Top-Down Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Parsing — Part II (Top-down parsing, left-recursion removal)
LL and Recursive-Descent Parsing
Syntax Analysis - Parsing
BNF 9-Apr-19.
Parsing & Context-Free Grammars Hal Perkins Summer 2004
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
COMPILER CONSTRUCTION
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Chapter 4 Top-Down Parsing Part-1 September 8, 2018 Prof. Abdelaziz Khamis

Chapter 4: Part1-Topics Top-Down Parsing Recursive-Descent Parsing Grammar of TINY in EBNF A Recursive-Descent Parser for TINY September 8, 2018 Prof. Abdelaziz Khamis

Top-Down Parsing A top-down parsing algorithm parses an input string of tokens by tracing out the steps in a leftmost derivation. Begin with the start symbol (top of tree) Work down to the leaves (terminals) by expanding rules. Such an algorithm is called top-down because the implied traversal of the parse tree is a pre-order traversal and, thus, occurs from the root to the leaves. September 8, 2018 Prof. Abdelaziz Khamis

Top-Down Parsing (Continued) But how to choose the rule? Top-down parser come in two forms: Backtracking parsers Systematically try every choice, backtrack if one decision turns out to be wrong and make a different choice. (Slow - require exponential time in general) Predictive parsers Look at one or more lookahead tokens (usually one) to pick the right choice. Recursive-descent parsing LL(1) parsing September 8, 2018 Prof. Abdelaziz Khamis

Recursive-Descent Parsing The grammar rule for a non-terminal A is viewed as a definition for a function that will recognize the non-terminal A. The right-hand side of the grammar rule specifies the structure of the code for this function. Each appearance of a terminal causes a token to be matched. Each appearance of a non-terminal corresponds to a call of the associated function. Choices correspond to case- or if- statements. September 8, 2018 Prof. Abdelaziz Khamis

Recursive-Descent Parsing (Continued) Example: Rule to Code Grammar rule for factor: factor  ( exp ) | number Code that recognizes a factor: void factor() { if (token == number) match(number); else if (token == ‘(’) { match(‘(‘); exp(); match(‘)’); } else error(token); This function uses one lookahead token. void match(Token expect) { if (token == expect) getToken(); else error(token,expect); } September 8, 2018 Prof. Abdelaziz Khamis

Recursive-Descent Parsing (Continued) A recursive-descent function can also compute values or syntax trees: int factor(void) { if (token == number) { int temp = atoi(tokStr); match(number); return temp; } else if (token == ‘(‘){ match(‘(‘); int temp = exp(); match(‘)’); return temp; else error(token); September 8, 2018 Prof. Abdelaziz Khamis

Recursive-Descent Parsing (Continued) Consider now the grammar rule for exp: exp  exp addop term | term The corresponding code would lead to an immediate infinite recursive loop: void exp(void) { if (token == ??) { exp(); addop(); term(); } else term(); September 8, 2018 Prof. Abdelaziz Khamis

Recursive-Descent Parsing (Continued) The solution is to use the EBNF rule: exp  term { addop term } The curly brackets expressing repetition can be translated into code for a loop, as follows: void exp(void) { term(); while (token == ‘+’ || token == ‘-’) { match(token); term(); } The non-terminal addop has been eliminated as a separate function, since its only task is to match the operators: + and -. September 8, 2018 Prof. Abdelaziz Khamis

Recursive-Descent Parsing (Continued) The left associativity implied by the curly brackets (and explicit in the original BNF) is maintained in the code: int exp(void) { int temp = term(); while (token == ‘+’|| token == ‘-’) if (token == ‘+’) { match(‘+’); temp += term();} else { match(‘-’); temp -= term();} return temp; } September 8, 2018 Prof. Abdelaziz Khamis

Recursive-Descent Parsing (Continued) The right recursion/associativity is not a problem: exp  term [ addop exp ] void exp(void) { term(); if(token == ‘+’ || token == ‘-’) { match(token); exp(); } September 8, 2018 Prof. Abdelaziz Khamis

Grammar of TINY in EBNF program  stmt-sequence stmt-sequence  statement { ; statement } statement  if-stmt | repeat-stmt | assign-stmt | read-stmt | write-stmt if-stmt  if exp then stmt-sequence [ else stmt-sequence ] end repeat-stmt  repeat stmt-sequence until exp assign-stmt  identifier := exp read-stmt  read identifier write-stmt  write exp exp  simple-exp [ comparison-op simple-exp ] comparison-op  < | = simple-exp  term { addop term } addop  + | - term  factor { mulop factor } mulop  * | / factor  ( exp ) | number | identifier September 8, 2018 Prof. Abdelaziz Khamis

A Recursive-Descent Parser for TINY Sample recursive-descent code in the TINY parser: TreeNode * statement(void) { TreeNode * t = NULL; switch (token) { case IF : t = if_stmt(); break; case REPEAT : t = repeat_stmt(); break; case ID : t = assign_stmt(); break; case READ : t = read_stmt(); break; case WRITE : t = write_stmt(); break; default : syntaxError("unexpected token -> "); printToken(token,tokenString); token = getToken(); break; } return t; September 8, 2018 Prof. Abdelaziz Khamis

A Recursive-Descent Parser for TINY (Continued) Sample recursive-descent code in the TINY parser: TreeNode * if_stmt(void) { TreeNode * t = newStmtNode(IfK); match(IF); if (t!=NULL) t->child[0] = exp(); match(THEN); if (t!=NULL) t->child[1] = stmt_sequence(); if (token==ELSE) { match(ELSE); if (t!=NULL) t->child[2] = stmt_sequence(); } match(END); return t; September 8, 2018 Prof. Abdelaziz Khamis