Grammar design: Associativity

Slides:



Advertisements
Similar presentations
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Advertisements

By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
A basis for computer theory and A means of specifying languages
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Chapter 3: Formal Translation Models
COP4020 Programming Languages
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Syntax and Semantics Structure of programming languages.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Context-Free Grammars
Grammars CPSC 5135.
PART I: overview material
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Syntax and Semantics Structure of programming languages.
A Programming Languages Syntax Analysis (1)
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Syntax Analyzer (Parser)
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
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
Chapter 3 – Describing Syntax
CONTEXT-FREE LANGUAGES
Parsing #1 Leonidas Fegaras.
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing & Context-Free Grammars
Chapter 4 - Parsing CSCE 343.
Programming Languages Translator
CS510 Compiler Lecture 4.
Parsing and Parser Parsing methods: top-down & bottom-up
Fall Compiler Principles Context-free Grammars Refresher
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Parsing IV Bottom-up Parsing
Syntax Specification and Analysis
Parsing — Part II (Top-down parsing, left-recursion removal)
Compiler Construction
Compiler Construction (CS-636)
Parsing Techniques.
CSE 3302 Programming Languages
Compiler Design 4. Language Grammars
Top-Down Parsing CS 671 January 29, 2008.
Context-Free Grammars
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Programming Language Syntax 2
Compiler Design 7. Top-Down Table-Driven Parsing
Lecture 7: Introduction to Parsing (Syntax Analysis)
Bottom Up Parsing.
CSC 4181Compiler Construction Context-Free Grammars
Parsing IV Bottom-up Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
Fall Compiler Principles Context-free Grammars Refresher
LEFT RECURSION.
Bottom-up parsing is also known as shift-reduce parsing
lec02-parserCFG May 27, 2019 Syntax Analyzer
Context-Free Grammars
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Grammar design: Associativity Plus and minus are left associative, parse trees should grow to the left L ::= L + number | L - number | number If parse tree grows to the right it is farther from the abstract syntax R ::= number + R | number - R | number However, right associatively is appropriate for right-associative operators such as assignment and exponentiation

BNF for Arithmetic Exp Expression with precedence (only) S  F - S | F + S | F F  T * F | T / F | T T  (S) | number 2 * 3 – 5 * 6 – 7 -17 6 23 30 7 The answer should be –31, but this gives -17

BNF for Arithmetic Exp S  S - F | S + F | F F  F * T | F / T | T Associativity taken into account S  S - F | S + F | F F  F * T | F / T | T T  (S) | number Derivation of 2 * 3 – 5 * 6 – 7 S  S – F  by SS-F S – F – F  by SS-F F – F – F  by SF F * T – F – F  by FF*T T * T – S – F  by FT 2 * T – S – F  by Tnumber 2 * 3 – S – F  by Tnumber 2 * 3 – F – F  by SF 2 * 3 – F * T – T  by FF*T 2 * 3 – T * T – T  by FT 2 * 3 – 5 * T – T  by Tnumber 2 * 3 – 5 * 6 – T  by Tnumber 2 * 3 – 5 * 6 – 7 by Tnumber NOTE: Colors updated, but rule usage was correct as originally shown in class. See usage on the right column Gives “correct” answer, and as a leftmost derivation: “leftmost nonterminal is replaced at each step”

Grammar Design -- Precedence Conventional arithmetic such as a * b + c * d + e Grammar: A ::= E := A | E [right associative & recursive] E ::= E + T | E - T | T [left associative & recursive] T ::= T * F | T / F | F F ::= number | name | ( E ) In a top-down parse, expand E first; this expands into T, which places higher precedence operators * and / closer to the the number, name or parenthesized expression. Viewed bottom-up, the * and / are recognized first and hence have larger precedence Pblm – topdown cannot parse left recursion

Derivation Start with the unique distinguished symbol Derivation Sequence of strings where a non-terminal on is replaced by a production rule in the next step of the derivation Top-down selects a rule where the left side matches a non-terminal of the sentential form. It replaces the non-terminal with the rule’s right side. This is generation Bottom-up selects a rule where the right side matches a non-terminal of the sentential form, or a string that includes a non-terminal. It replaces the matched string with the rule’s left side. This is reduction Sentential Form Any step in the derivation Sentence Sentential form with no non-terminals

Leftmost Derivation A derivation in which only the leftmost non-terminal in any sentential form is replaced at each step. Unique derivation for a string for a non ambiguous grammar For an ambiguous grammar thee may be multiple productions that can replace the non-terminal, thus giving multiple derivations (and resulting parse trees)

Rightmost Derivation The rightmost non-terminal is replaced in the derivation process in each step. Also referred to as Canonical Derivation

Parse Tree Abstracts out the information of the derivation process. Usually the parse tree is the same, even if the derivations for a string are different.

Ambiguity Characteristic of grammar If a grammar has two parse trees or two derivations for a particular string in the language it represents, then ambiguous If all grammars for a particular language are ambiguous then the language is called Inherently Ambiguous

Ambiguous Grammars Has more than one leftmost derivation. Has more than one parse tree. S  S + S | S - S | a | b

Left Recursive A grammar is left recursive if there is a derivation as follows: A  (+) A<string> Some parsing methods cannot handle these grammars (top-down parsing methods)

Immediate Left Recursiveness A  A <alpha> | <beta> A  <beta> B B  <alpha> B | null A  A<alpha1> | ….| A<alpha_m> | <beta1> | …..| <beta_n> Replace the leading nonterminals A  <beta1>B | …| <beta_n>B B  <alpha1>B |…| <alpha_m>B | null

Left Recursion “A grammar is left recursive if it has a nonterminal A such that there is a derivation A A for some string ” Topdown parsers cannot handle immediate left recursion since they do not see a leftmost nonterminal until the recursion is complete Fortunately, can rewrite the left recursive form to begin with leading nonterminals Left recursion can be replaced with non-left-recursive productions. A  A |  becomes two productions A   A’ and A’  A’|  (page 176)

Eliminating Left Recursion (Alg.) Order the non-terminals from A1 to An Next, replaced productions for ( i=1; i<=n; I++ ) { for (j=1; j<=i-1; j++) { replace A_i  A_j <gamma> with productions A_i  <delta_1> <gamma> | … | <delta k> <gamma> where A_j  <delta_1> | <delta_2> | … <delta_k> are the A_j productions Then, remove immediate left recursion among A_i productions

Left Factoring Modifying the grammar in order to be able to expand a non-terminal without non-determinism.

Left Factoring a Grammar For all A, find the longest common prefix <alpha> common to more than one of its rhs Replace A<a><b1>| … |<a><bn>| <c> A  <a>B | <c> B  <b1> | ….. | <bn> S  iEtS | iEtSeS | a , E b S  iEtSS’ | a S’  eS | null, E b

Classic if-then-else problem Grammar S::= if E then S S::= if E then S1 else S2 How to parse this? if E1 then if E2 then S1 else S2 <---------S---------> <-----S1-----> <S2>

Left Factoring The ambiguity of the if/then/else is shown as figures 4.5 and 4.6 in the Compilers text by Aho, Sethi and Ullman They analyze the difficulty and find it is due to left recursion “A grammar is left recursive if it has a nonterminal A such that there is a derivation A A for some string ” Left recursion can be replaced with non-left-recursive productions. A  A |  becomes two productions A   A’ and A’  A’|  (page 176)