Grammar Top-Down Parsing Syntax Analysis Sukree Sinthupinyo 1 1 Department of Computer Engineering Chulalongkorn University 24 August 2012
Grammar Top-Down Parsing Outline Grammar 1 Syntax Error Handling Notational Conventions Ambiguity Elimination of Left Recursion Top-Down Parsing 2
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Introduction Type of parsers Universal Top-Down Bottom-Up Type of grammars Left Recursive Non Left Recursive
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Left recursive grammar Non-left-recursive grammar
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Outline Grammar 1 Syntax Error Handling Notational Conventions Ambiguity Elimination of Left Recursion Top-Down Parsing 2
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Error Handling There are two strategies of handling error Panic-mode Phrase-level recovery The detail of each strategy depends on specific parsing methods. Common errors Lexical errors include misspellings of identifiers, keywords, or operators Syntactic errors include misplaced semicolons or extra or missing braces. Semantic errors include type mismatches between operators and operands. Logical errors can be anything from incorrect reasoning on the part of the programmer.
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Grammar for Simple Arithmetic Expression
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Outline Grammar 1 Syntax Error Handling Notational Conventions Ambiguity Elimination of Left Recursion Top-Down Parsing 2
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Terminals
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Nonterminals
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Others
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Example
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Outline Grammar 1 Syntax Error Handling Notational Conventions Ambiguity Elimination of Left Recursion Top-Down Parsing 2
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Example From the grammar above we can parse id + id * id in two ways as shown below.
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Eliminating Ambiguity From the grammar above, a parse tree for a conditional statement is as below.
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion An Ambiguous Sentence
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion An Unambiguous Sentence
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Outline Grammar 1 Syntax Error Handling Notational Conventions Ambiguity Elimination of Left Recursion Top-Down Parsing 2
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion We can change the left-recursive pair of productions A → A α | β to From the productions as We can change it to
Syntax Error Handling Grammar Notational Conventions Top-Down Parsing Ambiguity Elimination of Left Recursion Left Factoring can be written in general as A → αβ 1 | αβ 2 . The left-factored will be
Grammar Top-Down Parsing Recursive-Descent Parsing
Grammar Top-Down Parsing FIRST and FOLLOW FIRST ( α ) , where α is any string of grammar symbols, to be the set of terminals that begin strings derived from α . FOLLOW ( A ) , for nonterminal A , is the set of terminals a that can appear immediately to the right of A in some sentential form.
Grammar Top-Down Parsing FIRST
Grammar Top-Down Parsing FOLLOW
Grammar Top-Down Parsing Constructing a Predictive Parsing Table
Grammar Top-Down Parsing Constructing a Predictive Parsing Table
Grammar Top-Down Parsing Table-Driven Predictive Parsing
Recommend
More recommend