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.

Slides:



Advertisements
Similar presentations
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

CPSC 388 – Compiler Design and Construction
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.
1 Week 2 Questions / Concerns Schedule this week: Homework1 & Lab1a due at midnight on Friday. Sherry will be in Klamath Falls on Friday Lexical Analyzer.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
1 Chapter 5 Compilers Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
Chapter 4 Lexical and Syntax Analysis Sections 1-4.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
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.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
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.
Top-Down Parsing.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CS 3240: Languages and Computation Course Overview Sasha Boldyreva.
1 CST 320 COMPILER METHODS. 2 Week 1 Introduction Go over syllabus Grammar Review Compiler Overview Preprocessor Symbol Table Preprocessor Directives.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
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.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
CSC 338: Compiler design and implementation
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.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
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.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
TDDD55- Compilers and Interpreters Lesson 1 Zeinab Ganjei Department of Computer and Information Science Linköping University.
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.
CPS 506 Comparative Programming Languages Syntax Specification.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
. 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.
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 CONSTRUCTION Lesson 1 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker (Erik Hansson.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
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.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
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.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
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.
Context-Free Languages. Regular Languages Context-Free Languages.
Top-Down Parsing.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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: ◦
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
PROGRAMMING LANGUAGES
Basic Program Analysis: AST
Top-Down Parsing CS 671 January 29, 2008.
Lecture 7: Introduction to Parsing (Syntax Analysis)
Compilers B V Sai Aravind (11CS10008).
Lecture 4: Lexical Analysis & Chomsky Hierarchy
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Compiler design.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Presentation transcript:

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 language) Homework #4 due Thursday (Grammar modifications) Coming up: Lab2a & Lab2b posted. Test#1 next week Grammar Modifications Recursive Descent Parser

2 Lab1 Data structure for Symbol Table List (dynamic) Dynamic Array std::vector Dynamically allocate more when needed but it’s done in binary (2, 4, 8, 16, etc.) symbol * mySymbolArray Dynamically allocate more space when needed (how many more at a time?) Map Maps string (name) to more info about the name Sorted Binary search tree (red/black tree). Tree is always balanced. Unordered map STL’s hashtable

3 Preprocessor / Symbol Table Given the following code snippet: #define MAX 5 void main() { int x = 5; int y = 6; …… #if x == 5 //do something #endif const int MIN = 0; Add (MAX 5) to the symbol table main, x,y are not added to the symbol table in the preprocessor There is no preprocessor symbol called x in the symbol table Why? This can be added to the symbol in preprocessor because it’s just a constant that’s not going to change

4 Lab1 check-off Schedule Wednesday: I will be in and out most of the day but can check-off labs whenever I am on. Thursday: I will be available in the morning for lab check-off and again in late afternoon.

5 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

6 Grammar Example S -> E E -> E + E E -> E * E E -> id This grammar is ambiguous.

7 Revised & Expanded Grammar Example S -> id = E ; E -> E + T | E – T | T T ->T * F | T / F | F F -> ( E ) | id S (i) E ET T F = ; + * F T id (a) F i = a + b * c; id (b) id (c)

8 But… S -> id = E ; E -> E + T | E – T | T T ->T * F | T / F | F F -> ( E ) | id This grammar doesn’t work for top-down because of left recursion

9 In-Class Exercise #6 S -> id = E ; E -> E + T | E – T | T T ->T * F | T / F | F F -> ( E ) | id Remove left-recursion from this grammar

10 Recursive Descent Parser (RDP) Is a top down parser Start with grammar modifications MUST remove all left recursion from the grammar. Try to remove all unit productions. Try to left factor so the grammar is one-token look ahead. Input to the parser is a list of tokens. Output: Yes/No: Did the input parse? Parse structure: representing all the statements.

11 Grammar HW#2 Let’s look at the grammar for HW#2 Identify left recursion Identify opportunities for one-token look ahead Identify unit productions

12 HW#2 Grammar COMPOUND_STAT -> begin OPTIONAL_STAT end STATEMENT -> VARIABLE := EXPRESSION | COMPOUND_STAT | PROCEDURE_CALL | if EXPRESSION then STATEMENT else STATEMENT | while EXPRESSION do STATEMENT VARIABLE -> id PROCEDURE_CALL -> id | id ( EXPR_LIST )

13 Recursive Descent Parser RDP Lab1 List of Tokens Each token is a pair (Value, Type) void keyword main ID ( symbol ) symbol { symbol int keyword

14 Recursive Descent Parser RDP Lab1 List of Tokens Each token is a pair (Value, Type) void keyword main ID ( symbol ) symbol Tokens can be in a separate file

15 Recursive Descent Parser There are 2 types of rules in the grammar: With productions Without productions Example: Field -> `[´ Exp `]´ `=´ Exp | Name `=´ Exp | Exp Funcname2 -> ‘.’ Name Funcname2 |

16 Recursive Descent Parser Load tokens into a buffer. Need ability to pull out tokens but also put them back if you can’t use them in a rule. (Backtracking) void main ID ( ) current

17 Recursive Descent Parser For every rule in the grammar, generate a bool function. This answers the yes/no question first. Non- rules: If tokens match the rule, return true. If tokens do not match the rule, return false. rules: This means that this rule doesn’t match ANY tokens. It’s not wrong, it’s just that it doesn’t match anything at this point in the matching. One way to handle it is to just return True and let other rules handle the next token.

18 Bool Function S -> a S b | c bool S() { if currentToken == a if (S()) if nextToken == b return True; else return False; else return False; else if currentToken == c return True; else return False; }

19 Bool Function S -> a S b | bool S() { if currentToken == a if (S()) if nextToken == b return True; else return False; else return False; else return True; //for }

20 Bool Function S -> a S b | c bool S() { if currentToken == a if (S()) if nextToken == b return True; else return False; else return False; else if currentToken == c return True; else return False; } Parser is a pushdown automata But where is the “stack”? Call stack

21 Bool function Field -> `[´ Exp `]´ `=´ Exp | Name `=´ Exp | Exp bool Field() { if currentToken == ‘[‘ if (Exp()) if nextToken == ‘]’ if nextToken == ‘=‘ if (Exp()) return True; else if currentToken == Name if nextToken == ‘=‘ if (Exp()) return True; else if (Exp()) return True; } Return false for all other conditions May need to put tokens back before checking this rule

22 Bool function Funcname2 -> ‘.’ Name Funcname2 | bool Funcname2() { if currentToken == ‘.‘ if (nextToken == Name) if (Funcname2()) return True; else return True; //for.. Don’t remove //any tokens }