CPSC 325 - Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.

Slides:



Advertisements
Similar presentations
CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
Advertisements

1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Context-Free Grammars Lecture 7
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
COP4020 Programming Languages
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Parsing II : Top-down Parsing Lecture 7 CS 4318/5331 Apan Qasem Texas State University Spring 2015 *some slides adopted from Cooper and Torczon.
ISBN Chapter 3 Describing Syntax and Semantics.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
EECS 6083 Intro to Parsing Context Free Grammars
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Context-Free Grammars and Parsing
Grammars CPSC 5135.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Introduction to Parsing
CPS 506 Comparative Programming Languages Syntax Specification.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
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.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Context-Free Languages. Regular Languages Context-Free Languages.
Top-Down Parsing.
Syntax Analyzer (Parser)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
CSE P501 – Compilers Parsing Context Free Grammars (CFG) Ambiguous Grammars Next Spring 2014Jim Hogg - UW - CSE - P501C-1.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Introduction to Parsing
Chapter 3 – Describing Syntax
Parsing & Context-Free Grammars
Programming Languages Translator
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Lecture 3: Introduction to Syntax (Cont’)
Parsing & Context-Free Grammars Hal Perkins Autumn 2011
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Lecture 7: Introduction to Parsing (Syntax Analysis)
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Introduction to Parsing
Introduction to Parsing
LL and Recursive-Descent Parsing
Parsing & Context-Free Grammars Hal Perkins Summer 2004
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
Presentation transcript:

CPSC Compiler Tutorial 3 Parser

Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given a grammar and a sentence, traverse the derivation (parse tree) for the sentence in some standard order and do something useful at each node. Input

Example program statement assignStmt id int expr ifStmt statementexpr idint assignStmt id expr int program ::= statement | program statement statement ::= assignStmt | ifStmt assignStmt ::= id = expr; ifStmt ::= if ( expr ) statement expr ::= id | int | expr + expr id ::= a | b | c | i | … | z int ::= 0 | 1 | … | 9 a=1; if(a+1) b=2;

Standard Order When we write a parser, We want the it to be deterministic (no backtracking), and examine the source program from left to right. – (Parse the program in linear time in the order it appears in the source file)

Parsing Top-down – Start with the root – Traverse the parse tree depth-first, left-to-right – Left recursive is evil. (example of if-else) Bottom-up – Start at leaves and build up to the root

Something Useful At each point (node) in the traversal, perform some semantic action: – Construct nodes of full parse tree (rare) – Construct abstract syntax tree (common) – Construct linear, lower-level representation (more common) – Generate target code on the fly (1-pass compiler; not common in production compilers – can’t generate very good code in one pass)

Context-Free Grammars Formally, a grammar G is a 4-tuples where – N: a finite set of non-terminal symbols – T: a finite set of terminal symbols – P: A finite set of productions – S: the start symbol, a distinguished element of N α A γ => α β γ iff A ::= β in P

Reduced Grammars Grammar G is reduced iff there is no useless production in G.

Ambiguity Grammar G is unambiguous iff every sentence in L(G) has a unique leftmost (or rightmost) derivation – Fact: unique leftmost or unique rightmost implies the other A grammar without this property is ambiguous – Note that other grammars that generate the same language may be unambigious We need unambiguous grammars for parsing

Example expr ::= expr + expr | expr – expr | expr * expr | expr / expr | int int ::= 0 | 1 | 2 | … | 9 Exercise: Show that this is ambiguous How? Show two different leftmost or right most derivations for the same string Equivalently: show two different parse trees for the same string

Example (cont) Give a leftmost derivation of 2+3*4 and show the parse tree. Give two different derivations of 7+3+1

Problem? The grammar has no notion of precedence or associatively Solution: – Create a non-terminal for each level of precedence – Isolate the corresponding part of the grammar – Force the parser to recognize higher precedence sub expressions first

Classic Expression Grammar expr ::= expr + term | expr – term | term term ::= term * factor | term / factor | factor factor ::= int | ( expr ) int ::= 0 | 1 | 2 | … | 9 Check Check (5 + 3) * 2

Another Classic example Grammar for conditional statements stmt ::= ifStmt | whileStmt ifStmt ::= if ( cond ) stmt | if ( cond ) stmt lese stmt …… Is this grammar ok?

Solving Ambiguity Fix the grammar to separate if statements with else clause and if statement with no elxse - add lots of non-terminals Use some ad-hoc rule in parser - “else matches closest unpaired if”

Parser tools Most parser tools can cope with ambiguous grammars Be sure that what the tool does is really what you want. next week we will talk about Bison and more Parsers.