CSE 3302 Programming Languages

Slides:



Advertisements
Similar presentations
ISBN Chapter 3 Describing Syntax and Semantics.
Advertisements

CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Compiler Principle and Technology Prof. Dongming LU Mar. 7th, 2014.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
Grammars CPSC 5135.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 will be out this evening Due Monday, 2/8 Submit in HW Server AND at start of class on 2/8 A review.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Last Chapter Review Source code characters combination lexemes tokens pattern Non-Formalization Description Formalization Description Regular Expression.
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
CSE 3302 Programming Languages
CS 3304 Comparative Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
lec02-parserCFG May 8, 2018 Syntax Analyzer
Lexical and Syntax Analysis
Parsing & Context-Free Grammars
CS510 Compiler Lecture 4.
Lexical and Syntax Analysis
Chapter 3 Context-Free Grammar and Parsing
CSE 3302 Programming Languages
Chapter 3 – Describing Syntax
Concepts of Programming Languages
Syntax (1).
Syntax Specification and Analysis
What does it mean? Notes from Robert Sebesta Programming Languages
Compiler Construction
Syntax versus Semantics
Compiler Construction (CS-636)
CS 363 Comparative Programming Languages
Lecture 3: Introduction to Syntax (Cont’)
Parsing & Context-Free Grammars Hal Perkins Autumn 2011
Lexical and Syntax Analysis
Programming Language Syntax 2
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Lecture 7: Introduction to Parsing (Syntax Analysis)
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
CS 3304 Comparative Languages
CS 3304 Comparative Languages
CSC 4181 Compiler Construction Context-Free Grammars
Chapter 3 Describing Syntax and Semantics.
BNF 9-Apr-19.
High-Level Programming Language
Parsing & Context-Free Grammars Hal Perkins Summer 2004
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
lec02-parserCFG May 27, 2019 Syntax Analyzer
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
Programming Languages 2nd edition Tucker and Noonan
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
Presentation transcript:

CSE 3302 Programming Languages Syntax (cont.) Chengkai Li Fall 2007 Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Review of Lecture 3 Scanning and Parsing Lexical Structure: The structure of tokens (words) Syntactical Structure: The structure of programs regular expression grammar Eg identifier is a token that can have lexemes such a s sum and total Examples of tokens and lexemes on p 106 of statement (on previous page) index = 2 * count + 17; character stream scanner (lexical analysis) token stream parser (syntax analysis) parse tree Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Automatic Tools regular expression description of the tokens → (lex/flex) scanner of a language Example: Figure 4.1 (page 82) Context-Free Grammar → (Yacc/Bison) parser Example: Figure 4.13 (page 112) Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Review of Lecture 3 Tokens: Reserved words (keywords) Literals/constants Special symbols Identifiers Principle of Longest Substring The longest possible string of characters is collected into a single token. The principle requires that tokens are separated by white spaces. Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Review of Lecture 3 Context-Free Grammar (Backus-Naur Forms (BNF)) Grammar rules (productions): left-hand side: single nonterminal nonterminals (structured names) terminals (tokens) start symbol Language: the set of token strings that can be produced by derivation from the start symbol Derivation: number  number digit  number digit digit  digit digit digit  2 digit digit  23 digit  234 Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Review of Lecture 3 Derivations: different derivations for the same syntactic structure. Parse trees: capture intrinsic structure, more intuitive, still tedious. Abstract Syntax Tress (Syntax Trees): Remove ``unnecessary’’ symbols, meanwhile completely determine syntactic structure. number digit 2 3 4 4 3 2 page 91 Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Topics Today Topics: Ambiguities in Grammar Parsing Techniques Intuitive analysis and conclusion No formal theorems and rigorous proofs More details: compilers, automata theory Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 What is Parsing? Given a grammar and a token string: determine if the grammar can generate the token string? i.e., is the string a legal program in the language? In other words, to construct a parse tree for the token string. Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

What’s significant about parse tree? A parse tree gives a unique syntactic structure Leftmost, rightmost derivation There is only one leftmost derivation for a parse tree, and symmetrically only one rightmost derivation for a parse tree. Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example expr ® expr + expr | expr  expr | ( expr ) | number number ® number digit | digit digit ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr number digit 3 * 4 5 + parse tree leftmost derivation expr expr + * expr number digit number number 3 digit digit 4 5 Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

What’s significant about parse tree? A parse tree has a unique meaning, thus provides basis for semantic analysis. (Syntax-directed semantics: semantics are attached to syntactic structure.) expr expr.val = expr1.val + expr2.val expr1 + expr2 Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Relationship among language, grammar, parser Chomsky Hierarchy http://en.wikipedia.org/wiki/Chomsky_hierarchy A language can be described by multiple grammars, while a grammar defines one language. A grammar can be parsed by multiple parsers, while a parser accepts one grammar, thus one language. Should design a language that allows simple grammar and efficient parser For a language, we should construct a grammar that allows fast parsing Given a grammar, we should build an efficient parser Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Ambiguity Ambiguous grammar: There can be multiple parse trees for the same sentence (program) In other words, multiple leftmost derivations. Why it is bad? Multiple meaning Multiple derivation can be ok, but multiple leftmost derivation is the same as multiple parse tree. Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Ambiguity Was this ambiguous? How about this? number ® number digit | digit digit ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr ® expr + expr | number Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Deal with Ambiguity disambiguating rules: Use semantics in determining which parse tree to construct or unambiguous grammar Rewrite the grammar to avoid ambiguity. Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Example of Ambiguity: Precedence expr ® expr + expr | expr  expr | ( expr ) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Two different parse tress for expression 3+4*5 parse tree 2 parse tree 1 expr 5 * expr 3 * 4 5 + expr expr + 3 4 Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Eliminating Ambiguity for Precedence Establish “precedence cascade”: using different structured names for different constructs, adding grammar rules. Higher precedence : lower in cascade expr ® expr + expr | expr  expr | ( expr ) | number expr ® expr + expr | term term ® term  term | ( expr ) | number Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Example of Ambiguity: Associativity expr ® expr - expr | ( expr ) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Two different parse tress for expression 5-2-1 parse tree 2 parse tree 1 expr 1 - expr 5 - 2 1 expr.val = 2 expr.val = 4 expr expr - 5 2 - is right-associative, which is against common practice in integer arithmetic - is left-associative, which is what we usually assume Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Associativity Left-Associative: + - * / Right-Associative: = What is meant by a=b=c=1? Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Eliminating Ambiguity for Associativity left-associativity: left-recursion expr ® expr - expr | ( expr ) | number expr ® expr - term | term term ® (expr) | number right-associativity: right-recursion expr ® expr= expr | a | b | c ? Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Putting Together expr ® expr + expr | expr  expr | ( expr ) | number number ® number digit | digit digit ® 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 We want to make + left-associative, and * has higher precedence than + ? Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Example of Ambiguity: Dangling-Else stmt ® if( expr ) stmt | if( expr ) stmt else stmt | other-stmt Two different parse trees for “if( expr ) if( expr ) stmt else stmt” parse tree 1 parse tree 2 stmt stmt (expr) if if (expr) stmt else stmt if (expr) stmt stmt else Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Eliminating Dangling-Else stmt ® matched_stmt | unmatched_stmt matched_stmt ® if( expr ) matched_stmt else matched_stmt | other-stmt unmatched_stmt ® if( expr ) stmt | if( expr ) matched_stmt else unmatched_stmt Lecture 3 - Syntax, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007