Chapter 4: Syntax Analyzer

Similar documents
Syntax Analysis. Chapter 4

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Introduction to Syntax Analysis. Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Some Basic Definitions. Some Basic Definitions. Some Basic Definitions. Language Processing Systems. Syntax Analysis (Parsing) Prof.

CSE302: Compiler Design

A programming language requires two major definitions A simple one pass compiler

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Ambiguity and Errors Syntax-Directed Translation

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 3, vrijdag 22 september 2017

Introduction to Parsing Ambiguity and Syntax Errors

Introduction to Parsing Ambiguity and Syntax Errors

Syntax Analysis Top Down Parsing

A simple syntax-directed

Building Compilers with Phoenix

Syntax Analysis Part I

Top down vs. bottom up parsing

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Syntax Analysis Check syntax and construct abstract syntax tree

CSE431 Translation of Computer Languages

CS 314 Principles of Programming Languages

CIT 3136 Lecture 7. Top-Down Parsing

A Simple Syntax-Directed Translator

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Concepts Introduced in Chapter 4

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

3. Parsing. Oscar Nierstrasz

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

Context-free grammars

CA Compiler Construction

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Compiler Construction: Parsing

Computer Science 160 Translation of Programming Languages

CSE 3302 Programming Languages Lecture 2: Syntax

Error Recovery during Top-Down Parsing: Acceptable-sets derived from continuation

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Recursive Descent Parsers

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

3. Context-free grammars & parsing

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design


Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

CS502: Compilers & Programming Systems

Downloaded from Page 1. LR Parsing

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

CSX-lite Example. LL(1) Parse Tables. LL(1) Parser Driver. Example of LL(1) Parsing. An LL(1) parse table, T, is a twodimensional

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

Compiler Design Concepts. Syntax Analysis

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Parsing II Top-down parsing. Comp 412

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Abstract Syntax Trees & Top-Down Parsing

CS 406/534 Compiler Construction Parsing Part I

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Error Handling Syntax-Directed Translation Recursive Descent Parsing

CS 403: Scanning and Parsing

EECS 6083 Intro to Parsing Context Free Grammars

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

LECTURE 3. Compiler Phases

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

Principles of Programming Languages COMP251: Syntax and Grammars

CSCI312 Principles of Programming Languages

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

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

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Syntactic Analysis. Top-Down Parsing

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Roll No. :... Invigilator's Signature :. CS/B.Tech(CSE)/SEM-7/CS-701/ LANGUAGE PROCESSOR. Time Allotted : 3 Hours Full Marks : 70

CMSC 330 Practice Problem 4 Solutions

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Compilers. Yannis Smaragdakis, U. Athens (original slides by Sam

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Table-Driven Top-Down Parsers

Context-free grammars (CFG s)

Solving systems of regular expression equations

LANGUAGE PROCESSORS. Introduction to Language processor:

Context-Free Grammar (CFG)

Types of parsing. CMSC 430 Lecture 4, Page 1

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

MIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

Compilers Course Lecture 4: Context Free Grammars

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Transcription:

Chapter 4: Syntax Analyzer Chapter 4: Syntax Analysis 1 The role of the Parser The parser obtains a string of tokens from the lexical analyzer, and verifies that the string can be generated by the grammar of the source language. Chapter 4: Syntax Analysis 2 1

The role of the Parser Also, the parser should report any syntax errors in an intelligible fashion. It should also recover from commonly occurring errors so it can continue processing the remainder of its input. Chapter 4: Syntax Analysis 3 Syntax Error Handling A good compiler should assist the programmer in identifying and locating errors. Chapter 4: Syntax Analysis 4 2

Errors Types Lexical: such as misspelling id, kw, or opr. Syntactic: such as arith. Exp with unbalanced parentheses Semantic: such as an operator applied to an incompatible operand. Logical: such as an infinitely recursive call. Chapter 4: Syntax Analysis 5 Syntax Error Handling The error handler in a parser has the following simple goals: it should report the presence of an error clearly and accurately it should recover from each error quickly enough to be able to detect subsequent errors it should not significantly slow down the processing of correct programs Chapter 4: Syntax Analysis 6 3

Syntax Error Handling How should an error handler report the presence of an error? It should report the place in the source program where an error is detected because there is good chance that actual error occurred within the previous few tokens, the error handler also give an understandable error message when applicable. Chapter 4: Syntax Analysis 7 Example If x>y then z =10 // ; missing -------------------------------------------------- { { { } } // unexpected end of source Chapter 4: Syntax Analysis 8 4

Error Recovery Strategies no one strategy has proven itself to be universally acceptable: Panic-mode Recovery Phrase level Recovery Chapter 4: Syntax Analysis 9 Panic-mode Recovery On discovering an error, the parser discards input symbols one at a time until one of a designated set of synchronizing tokens is found Synchronizing tokens: delimiter, semicolon, or end. Chapter 4: Syntax Analysis 10 5

Phrase level Recovery On discovering an error, a parser may perform local correction of the remaining input, that is, it may replace a prefix of the remaining input by some string that allows the parser to continue. Replacing, by ; Inserting missing ; Delete an extraneous ; Chapter 4: Syntax Analysis 11 Context Free Grammars RE: for defining the tokens constructs (id, num, Relop) CFG: for describing the syntax of language constructs ( exp, statement) Chapter 4: Syntax Analysis 12 6

Backtracking Problem Backtracking occurs when there are more than one alternative start with the same prefix. Chapter 4: Syntax Analysis 13 Backtracking Problem Input: if E1 then S1 else S2 Since that there are more than one alternative starts with the same prefix, the compiler selects them one alternative if this alternative does not match with the input then the compiler backtracks to the beginning of input and selects the second alternative Chapter 4: Syntax Analysis 14 7

Backtracking Problem The compiler repeats this process until match found between the input and an alternative or trying all alternatives with no match found declaring that the input is wrong. Chapter 4: Syntax Analysis 15 LEFT FACTORING A αβ1 αβ2 A α A' A' β1 β2 depending on input to chose β1 or β2 Chapter 4: Syntax Analysis 16 8

LEFT FACTORING <stmt> if <exp> then <stmt> else <stmt> if <exp> then <stmt> ============================= <stmt> <stmt> ' if <exp> then <stmt> <stmt> ' else <stmt> ε Chapter 4: Syntax Analysis 17 The Modified Grammar Chapter 4: Syntax Analysis 18 9

Example S I E+ S I E +Se S a E b ============================ S I E +S S' a S' e S ε E b Chapter 4: Syntax Analysis 19 Ambiguity Example Some times certain conditions can be set on the grammar to eliminate the ambiguity. Input: If E1 then if E2 then S1 else S2 Chapter 4: Syntax Analysis 20 10

Chapter 4: Syntax Analysis 21 Eliminating the Ambiguity Associate each else with the closest previous then Chapter 4: Syntax Analysis 22 11

LEFT RECURSION A grammar is left recursive if it has a non terminal A such that there is a derivation A A α for some string α. Top - down parsing methods cannot handle left- recursive grammar, why? Chapter 4: Syntax Analysis 23 LEFT RECURSION A A α β α, β are sequence of terminals and non terminals that do not start with A. <exp> <exp> +<term> <term> A= <exp>, α = + <term>, β =<term> Chapter 4: Syntax Analysis 24 12

Elimination of Left Recursion Rewrite the production for A in the following manner. A β A' A' α A' ε right recursive. Chapter 4: Syntax Analysis 25 Example 1 <exp> <exp> ' <term> <exp> ' + <term> <exp> ' ε Chapter 4: Syntax Analysis 26 13

Example 2 E E+T T T T*F F F (E) Id Chapter 4: Syntax Analysis 27 Example 2 Chapter 4: Syntax Analysis 28 14

TOP DOWN Parsing Top down parsing can be viewed as an attempt to find a left most derivation for an input string. It can be viewed as an attempt to construct a parse tree for the input starting from the root and creating nodes of the parse tree in preorder. Chapter 4: Syntax Analysis 29 Recursive descent parsing is a top down method of syntax analysis in which we execute a set of recursive procedures to process the input. A procedure is associate with each nonterminal of a grammar. Chapter 4: Syntax Analysis 30 15

Predictive parsing is a special form of recursive descent parsing in which the look-ahead symbol unambiguously determines the procedure selected for each nonterminal. (No backtracking is required). Chapter 4: Syntax Analysis 31 Non recursive predictive parsing The key problem during predictive parsing is that in determining the production to be applied for a nonterminal. The non-recursive parser looks up the production to be applied in a parsing table. Chapter 4: Syntax Analysis 32 16

Predictive parser By carefully writing grammar, eliminating left recursion from it and left factoring the resulting grammar, we can obtain a grammar that can be parsed by a recursive descent parser that needs no backtracking. i. e.,a predictive parser. The proper alternative must be detectable by looking at only the first symbol it derives. Chapter 4: Syntax Analysis 33 Predictive parser First (<stmt>) = {if, while, begin} The key words if, while and begin tell us which alternative is the only one that could possibly succeed if we are to find a statement. Chapter 4: Syntax Analysis 34 17

Pseudo code for A predictive Parser Consider the following grammar: <state>::=if <exp> then <state> {else <state>}; while <exp> do <state>; <id>:=<exp>; <exp>::=<id>{+ <id>} Chapter 4: Syntax Analysis 35 Pseudo code for A predictive Parser Match (t: token) { If lookahead = t then Lookahead = next token(); else error; } Chapter 4: Syntax Analysis 36 18

Pseudo code for A predictive Parser parsesate( ) { if lookahead=if then parseif(); else if lookahead=while then parsewhile(); else if lookahead=id then parseass(); else Error; } Chapter 4: Syntax Analysis 37 Pseudo code for A predictive Parser parseif() { match(if); parseexp(); match(then); parse(state); if lookahead=else then { match(else); parse(state); } match(;); } Chapter 4: Syntax Analysis 38 19

Pseudo code for A predictive Parser parsewhile() { match(while); parseexp(); match(do); parse(state); match(;); } Chapter 4: Syntax Analysis 39 Pseudo code for A predictive Parser parseass() { match(id); match(ass); parseexp(); match(;); } Chapter 4: Syntax Analysis 40 20

Pseudo code for A predictive Parser parseexp( ) { match(id); if lookahead=+ then { match(+); match(id); } } Chapter 4: Syntax Analysis 41 21