Syntax Analysis Sections 4.1-4.4:.

Slides:



Advertisements
Similar presentations
lec02-parserCFG March 27, 2017 Syntax Analyzer
Advertisements

CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
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.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Compiler Constreuction 1 Chapter 4 Syntax Analysis Topics to cover: Context-Free Grammars: Concepts and Notation Writing and rewriting a grammar Syntax.
COP4020 Programming Languages
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Grammars CPSC 5135.
Overview of Previous Lesson(s) Over View  In our compiler model, the parser obtains a string of tokens from the lexical analyzer & verifies that the.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
COMP 3438 – Part II - Lecture 4 Syntax Analysis I Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Last Chapter Review Source code characters combination lexemes tokens pattern Non-Formalization Description Formalization Description Regular Expression.
Introduction to Parsing
5. Context-Free Grammars and Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
lec02-parserCFG May 8, 2018 Syntax Analyzer
LESSON 16.
Parsing & Context-Free Grammars
Chapter 4 - Parsing CSCE 343.
CS 404 Introduction to Compiler Design
G. Pullaiah College of Engineering and Technology
CS510 Compiler Lecture 4.
Chapter 4 Syntax Analysis
Syntax Specification and Analysis
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Syntax Specification and Analysis
What does it mean? Notes from Robert Sebesta Programming Languages
lec02-parserCFG July 30, 2018 Syntax Analyzer
CSE 105 theory of computation
Syntax versus Semantics
Syntax Analysis Sections :.
Syntax Analysis Sections :.
Compiler Design 4. Language Grammars
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
CPSC 388 – Compiler Design and Construction
Chapter 2: A Simple One Pass Compiler
Programming Language Syntax 2
Context-Free Grammars
Chapter 4: Syntax Analysis
5. Context-Free Grammars and Languages
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
R.Rajkumar Asst.Professor CSE
Context-Free Grammars 1
Programming Language Syntax 5
Designing a Predictive Parser
Theory of Computation Lecture #
BNF 9-Apr-19.
CSE 105 theory of computation
Parsing & Context-Free Grammars Hal Perkins Summer 2004
lec02-parserCFG May 27, 2019 Syntax Analyzer
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
CSE 105 theory of computation
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Syntax Analysis Sections 4.1-4.4:

Other Derivation Concepts Leftmost: Replace the leftmost non-terminal symbol E  E A E  id A E  id * E  id * id Rightmost: Replace the rightmost non-terminal symbol E  E A E  E A id  E * id  id * id lm lm lm lm rm rm rm rm Important Notes: A   If A    , what’s true about  ? If A    , what’s true about  ?  lm  rm Derivations: Actions to parse input can be represented pictorially in a parse tree.

Examples of LM / RM Derivations E  E A E | ( E ) | -E | id A  + | - | * | / |  A leftmost derivation of : id + id * id A rightmost derivation of : id + id * id

Derivations & Parse Tree E  E A E E A *  E * E E A id *  id * E E A id *  id * id

Parse Trees and Derivations Consider the expression grammar: E  E+E | E*E | (E) | -E | id Leftmost derivations of id + id * id E + E  id + E E + id E + E  E + E E * id + E  id + E * E + id

Parse Tree & Derivations - continued * id + E * E  id + id * E + id id + id * E  id + id * id E * + id

Alternative Parse Tree & Derivation E  E * E  E + E * E  id + E * E  id + id * E  id + id * id E + * id WHAT’S THE ISSUE HERE ? Two distinct leftmost derivations!

Resolving Grammar Problems/Difficulties Regular Expressions : Basis of Lexical Analysis Reg. Expr.  generate/represent regular languages Reg. Languages  smallest, most well defined class of languages Context Free Grammars: Basis of Parsing CFGs  represent context free languages CFLs  contain more powerful languages Reg. Lang. CFLs anbn – CFL that’s not regular.

Resolving Problems/Difficulties – (2) Since Reg. Lang.  Context Free Lang., it is possible to go from reg. expr. to CFGs via NFA. Recall: (a | b)*abb start 3 b 2 1 a

Resolving Problems/Difficulties – (3) Construct CFG as follows: Each State I has non-terminal Ai : A0, A1, A2, A3 If then Ai a Aj If then Ai bAj If I is an accepting state, Ai  : A3   If I is a starting state, Ai is the start symbol : A0 i j a : A0 aA0, A0 aA1 : A0 bA0, A1 bA2 : A2 bA3 i j b T={a,b}, NT={A0, A1, A2, A3}, S = A0 PR ={ A0 aA0 | aA1 | bA0 ; A1  bA2 ; A2  bA3 ; A3   } start 3 b 2 1 a

How Does This CFG Derive Strings ? start 3 b 2 1 a vs. A0 aA0, A0 aA1 A0 bA0, A1 bA2 A2 bA3, A3  How is abaabb derived in each ?

Regular Expressions vs. CFGs Regular expressions for lexical syntax CFGs are overkill, lexical rules are quite simple and straightforward REs – concise / easy to understand More efficient lexical analyzer can be constructed RE for lexical analysis and CFGs for parsing promotes modularity, low coupling & high cohesion. CFGs : Match tokens “(“ “)”, begin / end, if-then-else, whiles, proc/func calls, … Intended for structural associations between tokens ! Are tokens in correct order ?

Resolving Grammar Difficulties : Motivation The structure of a grammar affects the compiler design recall “syntax-directed” translation Different parsing approaches have different needs Top-Down vs. Bottom-Up redesigning a grammar may assist in producing better parsing methods. ambiguity -moves cycles left recursion left factoring Grammar Problems

Resolving Problems: Ambiguous Grammars Consider the following grammar segment: stmt  if expr then stmt | if expr then stmt else stmt | other (any other statement) What’s problem here ? Let’s consider a simple parse tree: stmt expr E1 E2 S3 S1 S2 then else if Else must match to previous then. Structure indicates parse subtree for expression.

Example : What Happens with this string? If E1 then if E2 then S1 else S2 How is this parsed ? if E1 then if E2 then S1 else S2 if E1 then if E2 then S1 else S2 vs. What’s the issue here ?

Parse Trees for Example Form 1: stmt expr E1 then if E2 S2 S1 else Form 2: stmt expr E1 S2 then else if E2 S1 What’s the issue here ?