afewadminnotes CSC324 Formal Language Theory Dealing with Ambiguity: Precedence Example Office Hours: (in BA 4237) Monday 3 4pm Wednesdays 1 2pm

Similar documents
CS 314 Principles of Programming Languages

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Introduction to Parsing. Lecture 5

Introduction to Parsing. Lecture 5

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

A simple syntax-directed

COP 3402 Systems Software Syntax Analysis (Parser)

A Simple Syntax-Directed Translator

Introduction to Parsing Ambiguity and Syntax Errors

Introduction to Parsing Ambiguity and Syntax Errors

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

CSC324 Functional Programming Efficiency Issues, Parameter Lists

Syntax. In Text: Chapter 3

Introduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

Syntax Analysis Check syntax and construct abstract syntax tree

EECS 6083 Intro to Parsing Context Free Grammars

Lecture 4: Syntax Specification

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Introduction to Parsing

Describing Syntax and Semantics

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

1 Lexical Considerations

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Lexical Analysis. Introduction

Homework & Announcements

2.2 Syntax Definition

Principles of Programming Languages COMP251: Syntax and Grammars

Lexical Considerations

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

Parsing II Top-down parsing. Comp 412

ECS 120 Lesson 7 Regular Expressions, Pt. 1

CS153: Compilers Lecture 4: Recursive Parsing

CPS 506 Comparative Programming Languages. Syntax Specification

A programming language requires two major definitions A simple one pass compiler

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Outline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

CSCE 314 Programming Languages

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

Chapter 3. Describing Syntax and Semantics ISBN

UNIVERSITY OF CALIFORNIA

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

Ambiguity and Errors Syntax-Directed Translation

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

Chapter 3. Describing Syntax and Semantics

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

CSE 401 Midterm Exam 11/5/10

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Formal Languages and Compilers Lecture I: Introduction to Compilers

Compiler Design Overview. Compiler Design 1

Ambiguity. Lecture 8. CS 536 Spring

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Principles of Programming Languages COMP251: Syntax and Grammars

Lexical Considerations

Intro To Parsing. Step By Step

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

CS 314 Principles of Programming Languages. Lecture 3

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CSE 401 Midterm Exam Sample Solution 2/11/15

Building Compilers with Phoenix

Announcements. Written Assignment 1 out, due Friday, July 6th at 5PM.

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

Compilers and computer architecture From strings to ASTs (2): context free grammars

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

SOFTWARE ARCHITECTURE 5. COMPILER

CS415 Compilers. Lexical Analysis

Context Free Languages

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Lexical Scanning COMP360

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Optimizing Finite Automata

Parsing: Derivations, Ambiguity, Precedence, Associativity. Lecture 8. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

Compiler Design Concepts. Syntax Analysis

CJT^jL rafting Cm ompiler

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Introduction to Parsing. Lecture 8

CT32 COMPUTER NETWORKS DEC 2015

This book is licensed under a Creative Commons Attribution 3.0 License

CS 314 Principles of Programming Languages

Midterm Solutions COMS W4115 Programming Languages and Translators Wednesday, March 25, :10-5:25pm, 309 Havemeyer

Habanero Extreme Scale Software Research Project

Context-Free Grammars

Languages and Compilers

Compiling Regular Expressions COMP360

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

Programming Languages Third Edition. Chapter 7 Basic Semantics

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Syntax Analysis Part I

Properties of Regular Expressions and Finite Automata

Transcription:

afewadminnotes CSC324 Formal Language Theory Afsaneh Fazly 1 Office Hours: (in BA 4237) Monday 3 4pm Wednesdays 1 2pm January 16, 2013 There will be a lecture Friday January 18, 2013 @2pm. 1 Thanks to A.Tafliovich, S.McIlraith, E.Joanis, S.Stevenson, G.Penn, D.Horton 1 2 Dealing with Ambiguity: Precedence Impose precedence over operators: Low precedence: addition + and subtraction - Medium precedence: multiplication * and division / Higher precedence: exponentiation ^ Highest precedence: parenthesized expressions ( <expr> ) Change the grammar by: introducing a distinct non-terminal for each precedence level. designing grammar so that operators with higher precedence are lower in the parse tree (i.e., evaluated before lower-precedence operators). Remember G1: Example <expn> --> <expn> + <expn> <expn> - <expn> <expn> * <expn> <expn> / <expn> <expn> ^ <expn> (<expn>) <identifier> <literal> Introduce precedence into G1. 3 4

Example (cont d) Dealing with Ambiguity: Associativity Write grammar G3: <expn> --> We still have ambiguity. Example: 3-2 - 1 is still a problem. The grouping of operators of same precedence is not disambiguated. For non-commutative operators, only one parse tree is correct. Operators may be left or right associative. Grouping in parse tree now reflects precedence. Examples: parse 8-3 * 2 parse 3-2 - 1 5 6 Dealing with Ambiguity: Associativity Deals with operators of same precedence. Implicit grouping or parenthesizing. Left associative: *, /, +,-. Right associative: ^. Approach: for left-associative operators, put the recursive term to the left of the non-recursive term in a production rule. for right-associative operators, put the recursive term to the right of the non-recursive term in a production rule. Remember G3: Example <expr> --> <expr> + <expr> <expr> - <expr> <term> <term> --> <term> * <term> <term> / <term> <factor> <factor> --> <factor> ^ <factor> <X> <X> --> (<expr>) <identifier> <literal> Introduce associativity. Grammar G4: <expn> --> 7 8

Example (con td) Dealing with Ambiguity Grammar G4: <expr> --> <expr> + <term> <expr> - <term> <term> <term> --> <term> * <factor> <term> / <factor> <factor> <factor> --> <X> ^ <factor> <X> <X> --> (<expr>) <identifier> <literal> Can t always remove an ambiguity from a grammar by restructuring productions. An inherently ambiguous language does not have a corresponding unambiguous grammar. Example of an Inherently Ambiguous Language: L = {a i b j c k i, j, k 1, i = j or j = k} Write a CFG for L: 9 10 Limitations of CFGs Specifying PL Syntax CFGs are not powerful enough to describe some languages. Examples: { a i b i c i i 1 }. { a m b n c m d n m, n 1 }. Question: Exactly what things can and cannot be expressed with acfg? 11

Specifying PL Syntax Specifying PL Syntax appropriate for lexical analysis: describing tokens such as numbers, identifiers, etc. appropriate for lexical analysis: describing tokens such as numbers, identifiers, etc. CFGs can describe nested constructs, matching pairs of items. Specifying PL Syntax appropriate for lexical analysis: describing tokens such as numbers, identifiers, etc. CFGs for Specifying PL Syntax Some aspects of PL syntax can t be specified with CFGs: CFGs can describe nested constructs, matching pairs of items. appropriate for syntactic analysis: describing the hierarchical structure of program statements, expressions, and other program units. 13

CFGs for Specifying PL Syntax Some aspects of PL syntax can t be specified with CFGs: Cannot declare the same identifier twice in the same block. Must declare an identifier before using it. A[i,j] is valid only if A is two-dimensional. The number of actual parameters must equal the number of formal parameters. The operands of an operator must have compatible types. CFGs for Specifying PL Syntax Some aspects of PL syntax can t be specified with CFGs: Cannot declare the same identifier twice in the same block. Must declare an identifier before using it. A[i,j] is valid only if A is two-dimensional. The number of actual parameters must equal the number of formal parameters. The operands of an operator must have compatible types. These aspects are usually specified informally, separately from the formal grammar, and are checked during semantic analysis. 13 13

Translation Process: Revisited 1. Lexical Analysis: Converts source code into sequence of tokens. We use regular grammars and finite state automata (recognizers). Want to Learn More? Take CSC488 Compilers & Interpreters! 2. Syntactic Analysis: Structures tokens into initial parse tree. We use CFGs and parsing algorithms. 3. Semantic Analysis: Annotates parse tree with semantic actions, e.g., making sure that the types match. 4. Code Generation: Produces final machine code. 16 17 Other Applications of Formal Grammars Identifying strings for an operating system command, e.g., Unix commands that use extended REs: ls s[y--z]* grep Se.h syntax.tex awk /to[kg]e/ {print $1} syntax.tex Automatic voice recognition.: given recorded speech, produce a string containing the words that were spoken. Challenges? How can a grammar help? 18