Errors in Top-Down Parsing Teoría de Autómatas y Lenguajes Formales M. Luisa González Díaz Universidad de Valladolid, 2005.

Slides:



Advertisements
Similar presentations
Application: Yacc A parser generator A context-free grammar An LR parser Yacc Yacc input file:... definitions... %... production rules... %... user-defined.
Advertisements

Lexical Analysis Dragon Book: chapter 3.
Chapter 2-2 A Simple One-Pass Compiler
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
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.
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Top-Down Parsing. Task Parsing (of course); but do it: Top-Down Easy and algorithmic Efficiently –Knowing (input) as little as possible –Marking errors.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Top-Down Parsing.
ISBN Lecture 04 Lexical and Syntax Analysis.
/t/ / I d/ /d/ Try Again Go on /t/ / I d/ /d/
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
CPSC 388 – Compiler Design and Construction Scanners – Finite State Automata.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
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?
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.
1 Using Yacc: Part II. 2 Main() ? How do I activate the parser generated by yacc in the main() –See mglyac.y.
COMP Parsing 2 of 4 Lecture 22. How do we write programs to do this? The process of getting from the input string to the parse tree consists of.
Lexical Analysis Hira Waseem Lecture
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Review: Regular expression: –How do we define it? Given an alphabet, Base case: – is a regular expression that denote { }, the set that contains the empty.
Lexical and Syntax Analysis
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
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.
Lexical Analysis: Finite Automata CS 471 September 5, 2007.
Compiler Construction Dr. Naveed Ejaz Lecture 5. Lexical Analysis.
Simple One-Pass Compiler
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Bottom-up Parsing. Task Parsing (of course); but do it this way: Bottom-up Easy and algorithmic Efficiently –Knowing (input) as little as possible –Marking.
Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Writing Parsers with Ruby
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 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
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.
The Role of Lexical Analyzer
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Chapter 2: A Simple One Pass Compiler
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
Parsing 2 of 4: Scanner and Parsing
Chapter 4 - Parsing CSCE 343.
Lexical and Syntax Analysis
Context free grammars Terminals Nonterminals Start symbol productions
Lecture #12 Parsing Types.
Chapter 4 Syntax Analysis.
Parsing with Context Free Grammars
Compiler Construction
CS 363 Comparative Programming Languages
4 (c) parsing.
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
Recursive decent parsing
Chapter 2: A Simple One Pass Compiler
COP4020 Programming Languages
Compiler Structures 2. Lexical Analysis Objectives
Compiler Construction
Presentation transcript:

Errors in Top-Down Parsing Teoría de Autómatas y Lenguajes Formales M. Luisa González Díaz Universidad de Valladolid, 2005

PPT integercharnumarray^[of]$ type rule 1 rule 3rule 2 simple rule 4rule 5rule 6 type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 A correct input: array [ char ] of integer

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 char ]ofinteger simple integer $ $

Empty entries

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [^ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [^ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 Error $ char

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [^ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 char ]ofinteger simple integer $ $

Other kind of empty entries

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $ char ]of$ Error type

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 Unexpected token: Mark error. Try again with the next token Unexpected end of string: Mark error. Recognize we can’t expand non terminal Panic mode

Code (panic mode) procedure type; if lookahead = array then match(array); match (‘[‘);simple; match(‘]‘); match(of); type else if lookahead = ‘^’ then match(‘^’); simple else if lookahead in { char, integer, num } then simple else if lookahead in {‘[‘,of, ‘]’} then writeln (‘Error:unexpected’, lookahead); match (lookahead); type else (* lookahead = $ *) writeln (‘Error: I couldn’t find type’) normal errors

Match errors

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftypearray[simple [ Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 char ]ofinteger simple integer $ $ char Error

Other kind of match errors

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftype[simple array Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $ $ Error

match: panic mode lookahead = expected token (t) –OK: read next token into lookahead and return lookahead <> expected token (t) –mark error :unexpected token (t), but lookahead <> $ –try again with the next token into lookahead lookahead = $ –don’t try again, don’t read anymore, just return

Code of match (panic mode) procedure match (t: token); (*Pre: t <> $ * Never try to match end of string) begin if lookahead = t then lookahead := nexttoken (lexical analyzer) else if lookahead <> $ then writeln (‘Unexpected’, lookahead); lookahead := nexttoken; match (t) else (* lookahead = $ *) writeln (‘Unexpected end of string’) end

type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num array type ]oftype[simple array Lex. An. PPTintegercharnumarray^[of]$ typerule 1 rule 3rule 2 simplerule 4rule 5rule 6 $ $ Error [ simple ] Error of Error type

Main program Begin lookahead = nexttoken; type; if lookahead = $ then input finished (errors, if any, were marked) else unexpected input after end of type End.

PPT integercharnumarray^..[of]$ type rule 1 rule 3rule 2 simple rule 4rule 5rule 6er type → simple | ^ simple | array [simple] of type simple → integer | char | num ptpt num An example of phrase-level error-recovery strategy Suposse it’s quite usual forgeting first number in num ptpt num Error action : mark it was forgotten

Code (panic+phrase-level mode) procedure simple; if lookahead = integer then match(integer); else if lookahead = char then match(char); else if lookahead = num then match(num); match(ptpt); match(num); else if lookahead in { array, ‘^’, ‘[‘, of, ‘]’ } then writeln (‘Error:unexpected’, lookahead); match (lookahead); simple else if lookahead = ptpt then writeln (‘Error: forgotten num’); match(ptpt); match(num); else (* lookahead = $ *) writeln (‘Error: I couldn’t find simple’) normal errors