Chapter 4. Syntax Analysis (1). 2 Application of a production  A  in a derivation step  i   i+1.

Slides:



Advertisements
Similar presentations
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Advertisements

Bottom-up Parsing A general style of bottom-up syntax analysis, known as shift-reduce parsing. Two types of bottom-up parsing: Operator-Precedence parsing.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
1 CSC 3130: Automata theory and formal languages Tutorial 4 KN Hung Office: SHB 1026 Department of Computer Science & Engineering.
1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition.
1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language.
1 、 Alphabet Non-empty set of symbols , usually expressed in  、 V or Other Upper-case Greece Letter 2 、 Symbol(Character) Elements in alphabet, finest.
Chapter 3: Formal Translation Models
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
Context-free Grammars
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
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.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
Normal Forms for Context-Free Grammars Definition: A symbol X in V  T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Languages, Grammars, and Regular Expressions Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Context-Free Grammars
Chapter 5 Context-Free Grammars
Grammars CPSC 5135.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
Introduction to Language Theory
Chapter 4. Syntax Analysis (1). 2 Application of a production  A  in a derivation step  i   i+1.
Phrase-structure grammar A phrase-structure grammar is a quadruple G = (V, T, P, S) where V is a finite set of symbols called nonterminals, T is a set.
Grammar G = (V N, V T, P, S) –V N : Nonterminal symbols –V T : Terminal symbols V N  V T = , V N ∪ V T = V – P : a finite set of production rules α 
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
Context Free Grammars.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Syntax Analyzer (Parser)
Formal Languages and Grammars
Discrete Structures ICS252 Chapter 5 Lecture 2. Languages and Grammars prepared By sabiha begum.
Chapter 5 Context-Free Languages Regular language are effective in describing certain simple patterns. But it is not enough to describe programming languages.
1/44 #-Rewriting Systems Based upon Brno University of Technology, Czech Republic  Křivka, Z., Meduna, A., Schönecker, R.: Generation.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Lecture 15 Ambiguous Grammars Topics: Context Free Grammars Language generated by a grammar Proofs with L(G) Ambiguous grammars October 20, 2008 CSCE 355.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Context Free Grammars & Parsing CPSC 388 Fall 2001 Ellen Walker Hiram College.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Modeling Arithmetic, Computation, and Languages Mathematical Structures for Computer Science Chapter 8 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesAlgebraic.
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Formal Language & Automata Theory
Syntax Specification and Analysis
Language and Grammar classes
The chomsky hierarchy Module 03.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Context-Free Grammars
Shift Reduce Parsing Unit -3
Context-Free Grammars
Context-Free Grammars
Chapter 4. Syntax Analysis (2)
Context-Free Grammars
Chapter 4. Syntax Analysis (2)
Chapter 4. Syntax Analysis (1)
Context-Free Grammars
Presentation transcript:

Chapter 4. Syntax Analysis (1)

2 Application of a production  A  in a derivation step  i   i+1

3 Formal grammars (1/3)  Example : Let G 1 have N = {A, B, C}, T = {a, b, c} and the set of productions   ACB  BC A  aABCbB  bb A  abCbC  bc cC  cc The reader should convince himself that the word a k b k c k is in L(G 1 ) for all k  1 and that only these words are in L(G 1 ). That is, L(G 1 ) = { a k b k c k | k  1}.

4 Formal grammars (2/3)  Example : Grammar G 2 is a modification of G 1 : G 2 :   ACB  BC A  aABCbB  bb A  abCbC  b The reader may verify that L(G 2 ) = { a k b k | k  1}. Note that the last rule, bC  b, erases all the C's from the derivation, and that only this production removes the nonterminal C from sentential forms.

5 Formal grammars (3/3)  Example : A simpler grammar that generates { a k b k | k  1} is the grammar G 3 : G 3 :   S S  aSb S  ab A derivation of a 3 b 3 is   S  aSb  aaSbb  aaabbb The reader may verify that L(G 3 ) = { a k b k | k  1}.

6 TypeFormat of ProductionsRemarks 0φAψ→ φω ψUnrestricted Substitution Rules 1φAψ→ φω ψ, ω≠λ ∑→λ Context Sensitive Context Free Right Linear Left Linear 2A →ω, ω≠λ ∑→λ 3A→aB A→a ∑→λ A→Ba A →a ∑→λ Regular Noncon- tracting Contracting The four types of formal grammars

7 Context-Sensitive Grammars(Type1 )  Definition : A context-sensitive grammar G = (N,T,P,  ) is a formal grammar in which all productions are of the form φAψ→φωψ, ω≠ The grammar may also contain the production  →, if G is a context-sensitive (type1) grammar, then L(G) is a context-sensitive (type1) language. Unrestricted Grammars(Type0 )

8 Context-Free Grammars (Type2)  Definition : A context-free grammar G=(N,T,P,  ) is a formal grammar in which all productions are of the form A→ω The grammar may also contain the production  →λ. If G is a context-free (type2) grammar, then L(G) is a context-free (type2) language. A ∈ N ∪ {  } ω ∈ (N ∪ T)* - {λ}

9 Regular Grammars (Type3) (1/2)  Definition : A production of the form A→aB or A→a is called a right linear production. A production of the form A→Ba or A→a is a left linear production. A formal grammar is right linear if it contains only right linear productions, and is left linear if it contains only left linear production  →λ. Left and right linear grammars are also known as regular grammars. If G is a regular (type3) grammar, then L(G) is a regular (type3) language. A ∈ N ∪ {∑} B ∈ N a ∈ T A ∈ N ∪ {∑} B ∈ N a ∈ T

10 Regular Grammars (Type3) (2/2)  Example: A left linear grammar G 1 and a right linear grammar G 2 have productions as follows: G 1 : G 2 : The reader may verify that L(G 1 ) = (10)*1=1(01)*=L(G 2 ) ∑ → 1B ∑ → 1 A → 1B B → 0A A → 1 ∑ → B1 ∑ → 1 A → B1 B → A0 A → 1

11 Fig Operator-precedence relations. id+*$ ·>·> ·>·> ·>·> + <·<··>·><·<··>·> * <·<··>·> ·>·> ·>·> $ <·<· <·<· <·<·

12 Operator-Precedence Relations from Associativity and Precedence (1/2) 1. If operator θ 1 has higher precedence than operator θ 2, make θ 1 ·> θ 2 and θ 2 + and + <· *. These relations ensure that, in an expression of the form E+E*E+E, the central E*E is the handle that will be reduced first. 2. If θ 1 and θ 2 are operators of equal precedence (they may in fact be the same operator), then make θ 1 ·> θ 2 and θ 2 ·> θ 1 if the operators are left- associative, or make θ 1 +, + ·> -, - ·> - and - ·> +. If  is right associative, then make  <· . These relations ensure that E - E+E will have handle E - E selected and E  E  E will have the last E  E selected.

13 Operator-Precedence Relations from Associativity and Precedence (2/2) 3.Make θ θ, θ θ, θ ·> ), θ ·> $, and $ <· θ for all operators θ. Also, let These rules ensure that both id and (E) will be reduced to E. Also, $ serves as both the left and right endmarker, causing handles to be found between $’s wherever possible. ( = )$ < · ( $ < · id ( < · ( id ·> $ ) ·> $ ( < · id id ·> ) ) ·> ) ·

*/↑id()$ + ·>·> ·>·><·<· <·<· <·<· <·<· <·<··>·> ·>·> -·>·> ·>·><·<· <·<· <·<· <·<· <·<··>·> ·>·> * ·>·> ·>·> ·>·> ·>·><·<· <·<· <·<··>·> ·>·> / ·>·> ·>·> ·>·> ·>·><·<· <·<· <·<··>·> ·>·> ↑ ·>·> ·>·> ·>·> ·>·><·<· <·<· <·<··>·> ·>·> ·>·> ·>·> ·>·> ·>·> ·>·> ·>·> ·>·> ( <·<· <·<· <·<· <·<· <·<· <·<· <·<· = ) ·>·> ·>·> ·>·> ·>·> ·>·> ·>·> ·>·> $ <·<· <·<· <·<· <·<· <·<· <·<· <·<· · Fig Operator-precedence relations.

15 Precedence Functions Example 4.29 The Precedence table of Fig has the following pair of precedence functions, For example, * g(id) suggests that id · > id; but, in fact, no precedence relation holds between id and id. Other error entries in Fig are similarly replaced by one or another precedence relation. + - */↑()id$ f g

16 Fig Graph representing precedence functions.

17 id()$ e3 ·>·> ·>·> ( <·<· <·<· =e4 )e3 ·>·> ·>·> $ <·<· <·<· e2e1 · Fig Operator-precedence matrix with error entries