1 Week 3 Questions / Concerns What’s due: Lab1b due Friday at midnight Lab1b check-off next week (schedule will be announced on Monday) Homework #2 due.

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

1 Week 9 Questions / Concerns Hand back Test#2 What’s due: Final Project due next Thursday June 5. Final Project check-off on Friday June 6 in class. Next.
Abstract Syntax Mooly Sagiv html:// 1.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Functional Design and Programming Lecture 9: Lexical analysis and parsing.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
1 Chapter 5 Compilers Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements.
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.
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Syntax and Semantics Structure of programming languages.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntax and Semantics Structure of programming languages.
1 Lecture 5: Syntax Analysis (Section 2.2) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC.
CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
1 Week 7 Questions / Concerns What’s due: Lab3 next Monday 5/19 Coming up: Lab2 & Lab3 check-off next week Lab3: LL(1) Bottom-Up Parsers LR(1) parsers.
1 COMP313A Programming Languages Syntax Analysis (2)
CPS 506 Comparative Programming Languages Syntax Specification.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Introduction to Compiling
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
More Parsing CPSC 388 Ellen Walker Hiram College.
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.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Week 5 Questions / Concerns What’s due: Lab2 part a due on Sunday Lab1 check-off by appointment Test#1 HW#5 next Thursday Coming up: Lab3 Posted. Discuss.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
 Welcome Back!  Introduction  Webpage: ◦
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Compiler Syntactic Analysis r Two general classes of parsing techniques m Bottom-up (Operator-Precedence parsing) Begin with the terminal nodes.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 due on Monday February 8 th Name and date your submission Submit electronically in Homework Server.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
Review 1.Structure of the course Lexical Analysis Syntax Analysis Grammar & Language RG & DFA Top-down LL(1) Parsing Bottom-Up LR Layered Automation Semantic.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Syntax and Semantics Structure of programming languages.
Programming Languages Translator
PROGRAMMING LANGUAGES
Top-Down Parsing CS 671 January 29, 2008.
CPSC 388 – Compiler Design and Construction
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Lecture 8: Top-Down Parsing
Programming Language Syntax 5
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

1 Week 3 Questions / Concerns What’s due: Lab1b due Friday at midnight Lab1b check-off next week (schedule will be announced on Monday) Homework #2 due next Monday (Draw a parse tree) Homework #3 due next Wednesday (Define grammar for your language) Homework #4 due next Thursday (Grammar modifications) Top down parser Grammar modifications

2 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Syntax Analysis (Parser) Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator Intermediate Representation Target machine code Symbol Table skeletal source program preprocessor

3 Parser Choose a type of parser Top-Down parser Bottom-Up parser Choose a parsing technique Recursive Descent Table driven parser (LL(1) or LR(1)) Generate a grammar for your language Modify the grammar to fit the particular parsing technique Remove lambda productions Remove unit productions Remove left recursion Left factor the grammar

4 Parser Parser is just a matching tool It matches list of tokens with grammar rules to determine if they are legal constructs/statements or not. Yes/No machine Context-Free It doesn’t care about context (types), it just cares about syntax If it looks like an assignment statement, then it is an assignment statement. int x; x = “Hello”;

5 Grammar #1 S -> aaSc| B B -> bbbB | Generate a parse tree for the input string aaaabbbcc

6 Grammar #2 S -> E E -> E + E E -> E * E E -> a |b | c Generate a parse tree for the input string a + b * c

7 Grammar #3 Lua Grammar

8 Grammar Two formats Context-Free Grammar Extended Backus-Naur Form Lua Example laststat ::= return [explist] | break Laststat -> return LaststatOptional | break LaststatOptional -> Explist | varlist ::= var {`,´ var} Varlist -> Var Varlist2 Varlist2 -> `,´ Var Varlist2 |

9 Grammar Two formats Context-Free Grammar Extended Backus-Naur Form Mini C example Program = Definition { Definition } program -> Definition MoreDefinitions MoreDefinitions -> Definition MoreDefinitions | Definition = Data_definition | Function_definition Definition -> Data_definition | Function_definition Function_definition = ['int'] Function_header Function_body Function_definition -> OptionalType Function_header Function_body OptionalType -> ‘int’ |

10 Top-down parser Start with start symbol of the grammar. Grab an input token and select a production rule. Use “stack” to store the production rule. Try to parse that rule by matching input tokens. Keep going until all of the input tokens have been processed. If the rule is not the right one, put all the tokens back and try a different rule. (backtracking)

11 Top-down Parser Ideal grammar: Unique rule for each type of token. One-token look ahead

12 One token look ahead Stat -> local function Name Funcbody | local Namelist LocalOptional Based on one token “local” we should be able to pick one unique rule so we don’t have to backtrack. What if we could combine these 2 rules into one rule by factoring out the common parts, it would eliminate the need for backtracking.

13 One token look ahead Stat -> local function Name Funcbody | local Namelist LocalOptional Left factor the grammar: Stat -> local Morelocal Morelocal -> function Name Funcbody | Namelist LocalOptional

14 Top-down Parser Ideal grammar: Unique rule for each type of token. One-token look ahead Minimize unit productions Unit productions don’t parse tokens immediately. It requires another production. It’s hard to tell which tokens match the unit productions thus more chances for backtracking.

15 Minimize Unit Productions S -> aaSc S -> B B -> bbbB B -> S B b b b B

16 Minimize Unit Productions Exp -> nil | false | true | Number | String | `...´ | Functioncall | Prefixexp | Tableconstructor | Exp Binop Exp | Unop Exp

17 Remove Unit Productions S -> aaSc S -> B B -> bbbB B -> S -> aaSc S -> bbbB S -> B -> bbbB B ->

18 Minimize Unit Productions Exp -> nil | false | true | Number | String | `...´ | Functioncall | Prefixexp | Tableconstructor | Exp Binop Exp | Unop Exp Exp -> nil | false | true | Number | String | `...´ | Functioncall| Prefixexp | { Fieldlistoptional }| Exp Binop Exp | Unop Exp

19 Minimize Unit Productions Exp -> nil | false | true | Number | String | `...´ | Functioncall | Prefixexp | Tableconstructor | Exp Binop Exp | Unop Exp Exp -> nil | false | true | Number | String | `...´ | Prefixexp Args | Prefixexp `:´ Name Args | Prefixexp | { Fieldlistoptional } | Exp Binop Exp | Unop Exp

20 Minimize Unit Productions Exp -> nil | false | true | Number | String | `...´ | Functioncall | Prefixexp | Tableconstructor | Exp Binop Exp | Unop Exp Exp -> nil | false | true | Number | String | `...´ | Prefixexp Args | Prefixexp `:´ Name Args | Prefixexp | { Fieldlistoptional } | Exp Binop Exp | Unop Exp More left factoring needed

21 Top-down Parser Ideal grammar: Unique rule for each type of token. One-token look ahead Minimize unit productions Unit productions don’t parse tokens immediately. It requires another production. It’s hard to tell which tokens match the unit productions thus more chances for backtracking. Lambda productions are okay but we have to process them accordingly. Removing lambdas always add more rules. It’s not possible to remove all lambda productions and still yield unique token-rule matching. Remove left recursion in the grammar.

22 Grammar (left recursive vs. right recursive) Right Recursion A -> aA A -> Left Recursion A -> Aa A -> A aA aA aA A aA a A a A Only non- recursive rule is Same grammar?

23 Grammar (left recursive vs. right recursive) A -> aA A -> A -> Aa A -> A aA aA aA A aA a A a A Which one works for top down?

24 Grammar (left recursive vs. right recursive) A -> aA A -> b A -> Aa A -> b A aA aA aA b A aA a A a A b Non- recursive rules are not only Same grammar?

25 Remove Left Recursion in the Grammar Example: A -> Aa A -> b Step 1: Make all left recursive rules right recursive, but give them a new non- terminal A -> Aa  X -> aX Step 2: Add a lambda production to the new non-terminal X -> Step 3: Identify all non-recursive rules. A -> b Step 4: Append the new non-terminal to the end of all non-recursive rules A -> bX A -> A… Left Recursive rule

26 Grammar (left recursive vs. right recursive) A -> bX X -> aX | A -> Aa A -> b A bX aX aX A aA a A a A b Non- recursive rules are not only Same grammar? a

27 Remove Left Recursion S -> Sab S -> c S -> d X -> abX X -> S -> cX S -> dX

28 Remove Left Recursion PARAMLIST -> IDLIST : TYPE | PARAMLIST ; IDLIST : TYPE PARAMLIST2 -> ; IDLIST : TYPE PARAMLIST2 PARAMLIST2 -> PARAMLIST -> IDLIST : TYPE PARAMLIST2

29 Remove Unit Production Example S -> abSc S -> A S -> AB A -> aA A -> B -> bbB B -> S -> abSc S -> aA S -> S -> AB A -> aA A -> B -> bbB B ->

30 Remove Unit Production Example TERM -> FACTOR FACTOR -> id | id ( EXPR_LIST ) | num | ( EXPRESSION ) | not FACTOR TERM -> id | id ( EXPR_LIST ) | num | ( EXPRESSION ) | not FACTOR FACTOR -> id | id ( EXPR_LIST ) | num | ( EXPRESSION ) | not FACTOR

31 Left Factor Example S -> abS S -> aaA S -> a A -> bA A -> S -> aX X -> bS X -> aA X -> A -> bA A ->

32 Left Factor Example EXPRESSION -> SIMPLE_EXPR | SIMPLE_EXPR relop SIMPLE_EXPR EXPRESSION -> SIMPLE_EXPR RestOfExp RestOfExp -> | relop SIMPLE_EXPR

33 In-Class Exercise #5 Remove Unit Production S -> abS | bSa | A | d A -> c | dA Left Factor this grammar FACTOR -> id | id ( EXPR_LIST ) | num | ( EXPRESSION ) | not FACTOR Remove Left recursion: SIMPLE_EXPR -> TERM | SIGN TERM | SIMPLE_EXPR addop TERM