02-03ambiguityParsing

02-03ambiguityParsing - 1 CSE 450: Compilers K. Stirewalt...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CSE 450: Compilers K. Stirewalt Parsing and syntax analysis Part 3: Ambiguity and parsing Topics: Parse trees and problem of ambiguity How to choose a production during predictive parsing The class of LL(1) grammars A non-recursive predictive parsing algorithm CSE 450: Compilers K. Stirewalt Degrees of freedom in parsing How to choose non-terminal to replace: Leftmost derivation: For each sentential in the derivation, the leftmost non-terminal symbol always chosen for replacement Rightmost derivation: For each sentential in derivation, the rightmost non-terminal symbol always chosen for replacement How to choose a production to apply How much of the input is needed to parse E.g., is parsing done from left-to-right, a token at a time, or does one need to load and store all tokens before parsing can begin? Top-down parsing finds a leftmost derivation Top-down parsing algorithms differ based on how they choose the production and how much input is needed CSE 450: Compilers K. Stirewalt Choosing a production Two major issues surround the choosing a production to expand: 1. Ambiguity in the grammar: grammar provides more than one way to parse the same string; thus when choosing a production, multiple choices are legal 2. The parser may need to look ahead some number of tokens to decide which production to choose CSE 450: Compilers K. Stirewalt Choosing a production Two major issues surround the choosing a production to expand: 1. Ambiguity in the grammar: grammar provides more than one way to parse the same string; thus when choosing a production, multiple choices are legal 2. The parser may need to look ahead some number of tokens to decide which production to choose CSE 450: Compilers K. Stirewalt Ambiguous grammar Defn: A grammar that gives rise to more than one parse tree for a given sentence Undesirable in general Remediation: Rewrite grammar to eliminate the ambiguity Rely on disambiguation rules to allow deterministic use of an ambiguous grammar CSE 450: Compilers K. Stirewalt Recall from last time We were working with a simple grammar for infix expressions: G = S E E E Op E E Int We can represent parse of an expression using this grammar graphically with a parse tree 2 CSE 450: Compilers K. Stirewalt Parse tree for our example S E E Op E E Op E Op...
View Full Document

This note was uploaded on 07/25/2008 for the course CSE 450 taught by Professor Stirewalt during the Spring '08 term at Michigan State University.

Page1 / 5

02-03ambiguityParsing - 1 CSE 450: Compilers K. Stirewalt...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online