Grammars Examples and Issues. Examples from Last Lecture a + b a b + a*bc* First draw a state diagram Then create a rule for each transition.

Slides:



Advertisements
Similar presentations
Compiler construction in4020 – lecture 2 Koen Langendoen Delft University of Technology The Netherlands.
Advertisements

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Lexical and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 4 Lexical and Syntax Analysis.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Context-Free Grammars Lecture 7
ISBN Chapter 4 Lexical and Syntax Analysis.
Slide1 Chapter 4 Lexical and Syntax Analysis. slide2 OutLines: In this chapter a major topics will be discussed : Introduction to lexical analysis, including.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
1 Note As usual, these notes are based on the Sebesta text. The tree diagrams in these slides are from the lecture slides provided in the instructor resources.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
COP4020 Programming Languages
1 Disambiguating the grammar If we use the parse tree to indicate precedence levels of the operators, we can remove the ambiguity. The following rules.
ISBN Lecture 04 Lexical and Syntax Analysis.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
CS 330 Programming Languages 09 / 26 / 2006 Instructor: Michael Eckmann.
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.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
C H A P T E R TWO Syntax and Semantic.
Lexical and Syntax Analysis
CONTEXT FREE GRAMMAR presented by Mahender reddy.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Bernd Fischer RW713: Compiler and Software Language Engineering.
ISBN Chapter 4 Lexical and Syntax Analysis.
CPS 506 Comparative Programming Languages Syntax Specification.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
LESSON 04.
Syntax and Grammars.
Lecture 3: Parsing CS 540 George Mason University.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
ISBN Chapter 4 Lexical and Syntax Analysis.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
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.
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.3-1 Language Specification and Translation Lecture 8.
Lecture 15 Ambiguous Grammars Topics: Context Free Grammars Language generated by a grammar Proofs with L(G) Ambiguous grammars October 20, 2008 CSCE 355.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Chapter 3 – Describing Syntax
4.1 Introduction - Language implementation systems must analyze
Lexical and Syntax Analysis
Parsing & Context-Free Grammars
G. Pullaiah College of Engineering and Technology
Chapter 4 Lexical and Syntax Analysis.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Lexical and Syntax Analysis
Department of Software & Media Technology
Lexical and Syntax Analysis
Lexical and Syntactic Analysis
Programming Language Syntax 2
Chapter 2: A Simple One Pass Compiler
Lexical and Syntax Analysis
Chapter 4: Lexical and Syntax Analysis Sangho Ha
Lexical and Syntax Analysis
Lexical and Syntax Analysis
COMPILER CONSTRUCTION
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
4.1 Introduction - Language implementation systems must analyze
Presentation transcript:

Grammars Examples and Issues

Examples from Last Lecture a + b a b + a*bc* First draw a state diagram Then create a rule for each transition

a+ba+b a a b -> a -> b ->  -> a -> a | b or

a b a b b -> a -> b ->  -> a -> b -> b |  or (  represents an empty string … done)

a*bc* 1 2 b c -> a -> b -> c ->  -> a | b -> b |  or (  represents an empty string … done) a

What does this have to do with programming languages? Text processing is common in applications Generalizing formats make processing more complex html and xml are examples Grammars and Programming Compilers need to process strings

State Diagram

Lexical Analysis (cont.) Implementation (assume initialization): int lex() { getChar(); switch (charClass) { case LETTER: addChar(); getChar(); while (charClass == LETTER || charClass == DIGIT) { addChar(); getChar(); } return lookup(lexeme); break; …

Summary Regular expressions, regular grammars, state machines are all related They represent tools to aid in the processing of text Coding structure (see the addChar/getChar calls in the previous example) follows the state machine structure.

Could you write a state machine to recognize html titles?

Could you write a program to recognize html titles?

Other issues? Compilers start with derived strings. Then try to “reverse engineer” the structure/derivation associated with the program. If the grammar is too simple, there are multiple interpretations …. –AMBIGUITY! First lets look at a Context Free Grammar

An Example Grammar   | ;  =  a | b | c | d  + | -  | const

An Example Derivation => => => = => a = => a = + => a = b + => a = b + const

Parse Tree A hierarchical representation of a derivation const a = b +

Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees

An Ambiguous Expression Grammar  | const  / | - const --//

An Unambiguous Expression Grammar If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity  - |  / const| const const / -

Associativity of Operators Operator associativity can also be indicated by a grammar -> + | const (ambiguous) -> + const | const (unambiguous) const + +

Awkward Appearance of Grammars Not for you For compilers to have unambiguous interpretation As with string processing, provides the basis of program structure for writing compilers.