CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)

Slides:



Advertisements
Similar presentations
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Advertisements

CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Context-Free Grammars Lecture 7
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #5 Introduction.
ParsingParsing. 2 Front-End: Parser  Checks the stream of words and their parts of speech for grammatical correctness scannerparser source code tokens.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
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.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Context-Free Grammars
Grammars CPSC 5135.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Left Recursion Lecture 7 Fri, Feb 4, 2005.
Syntax Context-Free Grammars, Syntax Trees. CFG for Arithmetic Expressions E::= E op E| (E) | num op ::= + | * num ::= 0 | 1 | 2 | … Backus-Naur Form.
Bernd Fischer RW713: Compiler and Software Language Engineering.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CS 44 – Jan. 29 Expression grammars –Associativity √ –Precedence CFG for entire language (handout) CYK algorithm –General technique for testing for acceptance.
CPS 506 Comparative Programming Languages Syntax Specification.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
LESSON 04.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
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.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
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.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
LECTURE 7 Lex and Intro to Parsing. LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens)
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3.
CS 44 – Jan. 28 Pumping lemma #2 Applications to compiling.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Introduction to Parsing
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
CS 326 Programming Languages, Concepts and Implementation
Introduction to Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
PROGRAMMING LANGUAGES
Compiler Construction
Parsing with Context Free Grammars
Syntax Analysis Sections :.
CSE 3302 Programming Languages
Compiler Design 4. Language Grammars
Syntax-Directed Definition
Programming Language Syntax 2
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
Programming Languages
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Introduction to Parsing
Introduction to Parsing
CSC 4181 Compiler Construction Context-Free Grammars
High-Level Programming Language
Operator Precedence and Associativity
Operator Precedence and Associativity
Faculty of Computer Science and Information System
Presentation transcript:

CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)

Compiling Grammars are used to define programming language and check syntax. Phases of a compiler source code scanner stream of tokens parser parse tree

Scanning Scanner needs to know what to expect when eating your program. –identifiers –numbers –strings –comments Specifications for tokens can be expressed by regular expression (or regular grammar). While scanning, we can be in different states, such as inside a comment.

Parser Purpose is to understand structure of program. All programming structures can be expressed as CFG. Simple example for + and – expr  expr + digit | expr – digit | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 How would we derive the string 9 – ?

9 – expr  expr + digit | expr – digit | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr expr +digit expr - digit 2 digit 5 9 Leftmost derivation: expr  expr + digit  expr – digit + digit  digit – digit + digit  9 – digit + digit  9 – 5 + digit  9 – “parse tree”

Left & right recursion What is the difference between these 2 grammars? Which one is better? expr  expr + digit | expr – digit | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr  digit + expr | digit – expr | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Let’s try 9 – on both of these. The grammar must convey the order of operations! Operators may be left associative or right associative.

+ - * / Question: How do we write grammar for all 4 operators? Can we do it this way… expr  expr + digit | expr – digit | expr * digit | expr / digit | digit digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 BTW, we can ignore “digits” and from now on just replace them with “num”, and understand it’s any single number.

Precedence (* /) bind stronger than (+ -) (+ -) separate better than (* /) Need to break up expression into terms –Ex. 9 – 8 * / 5 –We want to say that an expression consists of “terms” separated by + and – –And each term consists of numbers separated by * and / –But which should we define first, expr or term?

Precedence (2) Which grammar is right? expr  expr + term | expr – term | term term  term * num | term / num | num Or this one: expr  expr * term | expr / term | term term  term + num | term – num | num Let’s try examples * 3 and 1 * 2 + 3

Moral If a grammar is defining something hierarchical, like an expression, define large groupings first. Lower precedence operators appear first in grammar. (They separate better) –Ex. * appears lower in parse tree than + because it gets evaluated first. In a real programming language, there can be more than 10 levels of precedence. C has ~15!

C language Handout –How does the grammar begin? –Where are the mathematical expressions? –Do you agree with the precedence? –Do you see associativity? –What else is defined in grammar? –Where are the terminals?