Some CFL Practicalities

Slides:



Advertisements
Similar presentations
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Advertisements

CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Specifying Languages CS 480/680 – Comparative Languages.
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
EECS 6083 Intro to Parsing Context Free Grammars
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Compiler Principle and Technology Prof. Dongming LU Mar. 7th, 2014.
Context-Free Grammars
Context-Free Grammars and Parsing
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Grammars CPSC 5135.
PART I: overview material
ISBN Chapter 3 Describing Syntax and Semantics.
1 COMP313A Programming Languages Syntax Analysis (2)
Syntax Context-Free Grammars, Syntax Trees. CFG for Arithmetic Expressions E::= E op E| (E) | num op ::= + | * num ::= 0 | 1 | 2 | … Backus-Naur Form.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
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.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Context Free Grammars & Parsing CPSC 388 Fall 2001 Ellen Walker Hiram College.
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
CSE 3302 Programming Languages
BBM Programming Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
COMPILER CONSTRUCTION
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Concepts of Programming Languages
Syntax (1).
Syntax Specification and Analysis
Context-Free Grammars
4장 보조자료.
Syntax versus Semantics
Compiler Construction (CS-636)
Syntax Analysis Sections :.
CSE 3302 Programming Languages
Syntax Analysis Sections :.
Lecture 3: Introduction to Syntax (Cont’)
Compiler Design 4. Language Grammars
Context-Free Grammars
Programming Languages 2nd edition Tucker and Noonan
Context-Free Grammars
Programming Language Syntax 2
Programming Languages 2nd edition Tucker and Noonan
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Context–free Grammar CFG is also called BNF (Backus-Naur Form) grammar
Introduction to Parsing
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
BNF 9-Apr-19.
High-Level Programming Language
Context-Free Grammars
COMPILER CONSTRUCTION
Presentation transcript:

Some CFL Practicalities CPSC 388 Ellen Walker Hiram College

Grammar for Expressions Exp -> Exp Op Exp | ( Exp ) | number Op -> + | – | * Non-terminals: Exp, Op Terminals: number, +, –, *, (, ) Terminals are tokens not necessarily characters!

Simple Grammar for Statement Stmt -> If-Stmt | … other statements If-Stmt -> if ( Exp ) Stmt | if ( Exp ) Stmt else Stmt Exp -> … on previous slide

More Concise If-Stmt using  If-Stmt -> if ( Exp ) Stmt ElsePart ElsePart -> else Stmt | 

Epsilon Placement is Significant How do these languages differ? L1: S -> x ; S | x L2: S-> x ; S |  L3: S-> x ; S | A A -> x |  L4: S-> N |  N -> x ; N | x L1: x;x x;x;x x;x;x;x etc L2: empty, x, x; , x;x x;x;

Recursion in Grammars When a rule refers to its own LHS in the RHS, the rule is recursive Left-recursion (LHS is first) Right-recursion (LHS is last) “Exp -> Exp Op Exp” is both left-recursive and right-recursive!

Why Recursion? Without recursion, languages must be finite All possible derivations can be enumerated in advance Example: S-> aB B -> b | Cc C -> c

Recursion in Multiple Rules Stmt -> If-Stmt | … other statements If-Stmt -> if ( Exp ) Stmt | if ( Exp ) Stmt else Stmt Stmt => If-Stmt => if … Stmt … There is a recursion on Stmt, but it takes 2 rules

Ambiguous Grammars If there is a string that has two different derivations (with different parse trees), the grammar is ambiguous. Only one string is needed to determine ambiguity

Exp Grammar is Ambiguous Exp -> Exp Op Exp | ( Exp ) | number Op -> + | – | * Exp Exp Exp Op Exp Exp Op Exp Exp Op Exp Exp Op Exp number + number * number number + number * number

Dangling Else // w true when x!=0 and y=0 If (x!=0) if (y!=0) z=true;

Disambiguating Rule Leave the grammar ambiguous, but create a rule (outside the grammar) that determines which competing parse is “correct” Examples: Multiplication has precedence over addition Subtraction is left-associative Else matches the closest “if”

Modifying the Grammar Create additional non-terminals and rules that prevent ambiguity In this case, the syntax is (again) reflected only by the grammar

Exp Grammar with Precedence Exp -> Exp Adop Exp | Term Term -> Term Mulop Factor | Factor Factor -> ( Exp ) | number Adop -> + | – Mulop -> * * evaluation forced “deeper” into the tree!

If Grammar with “Closest else” Stmt -> MatchedStmt | UnmStmt MatchedStmt -> if (Exp) MatchedStmt else MatchedStmt | Other UnmStmt -> if (Exp) Stmt | if (Exp) MatchedStmt else UnmStmt Other -> { Stmt-list } | … |  Stmt-list -> Stmt Stmt-list | Stmt This is sufficiently awkward that real grammars will likely use the disambiguating rule solution

Avoiding the IF ambiguity Require the else part in all if’s This is the LISP solution Require bracketing keywords If-stmt -> if Exp Stmt endif | if Exp Stmt else Stmt endif In some languages “fi” is used instead of “endif”

Abstract Parse Trees Design trees that include only essential aspects of the language (for semantics); remove “syntactic sugar” and intermediate non-terminals

Parse Tree vs. Abstract Parse Tree Exp Exp Op Exp + Exp Op Exp * number + number * number number number number

Extended BNF “Ordinary BNF” is simply a CFG Extensions Non-terminals named like: <expression> Terminals are bare sequences of characters Extensions { x } Repeat x 0 or more times ( x*) [ x ] x is optional (x | )