1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; }...... Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Grammar vs Recursive Descent Parser
CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
CS 338 Project: Phase 4. Grammar Start  stmt eof Stmt  id = expr | if (expr) then stmt | while (expr) do stmt | begin CS end CS  stmt ; CS |   ote:
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
Simplifying CFGs There are several ways in which context-free grammars can be simplified. One natural way is to eliminate useless symbols those that cannot.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
Fall 2004COMP 3351 Simplifications of Context-Free Grammars.
Prof. Busch - LSU1 Simplifications of Context-Free Grammars.
Courtesy Costas Busch - RPI1 Positive Properties of Context-Free languages.
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Costas Buch - RPI1 Simplifications of Context-Free Grammars.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Costas Busch - RPI1 Positive Properties of Context-Free languages.
Fall 2003Costas Busch - RPI1 Decidable Problems of Regular Languages.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Top-Down Parsing.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
Prof. Busch - RPI1 Decidable Problems of Regular Languages.
Parsing II : Top-down Parsing Lecture 7 CS 4318/5331 Apan Qasem Texas State University Spring 2015 *some slides adopted from Cooper and Torczon.
1 Applications of Regular Closure. 2 The intersection of a context-free language and a regular language is a context-free language context free regular.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Fall 2005Costas Busch - RPI1 Context-Free Languages.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
CYK Algorithm for Parsing General Context-Free Grammars
1 Lex. 2 Lex is a lexical analyzer Var = ; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn:
CPS 506 Comparative Programming Languages Syntax Specification.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
CYK Algorithm for Parsing General Context-Free Grammars.
Context-Free Languages. Regular Languages Context-Free Languages.
Syntax Analyzer (Parser)
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
1 Context Free Grammars Xiaoyin Wang CS 5363 Spring 2016.
lec02-parserCFG May 8, 2018 Syntax Analyzer
Context-Free Languages
G. Pullaiah College of Engineering and Technology
CS510 Compiler Lecture 4.
Simplifications of Context-Free Grammars
Simplifications of Context-Free Grammars
PDAs Accept Context-Free Languages
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
R.Rajkumar Asst.Professor CSE
Parsing Costas Busch - LSU.
Decidable Problems of Regular Languages
BNF 9-Apr-19.
Applications of Regular Closure
Normal Forms for Context-free Grammars
Context-Free Languages
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

1 Compilers

2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3... Machine Code

3 Lexical analyzer parser Compiler program machine code input output

4 A parser knows the grammar of the programming language

5 Parser PROGRAM STMT_LIST STMT_LIST STMT; STMT_LIST | STMT; STMT EXPR | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR EXPR + EXPR | EXPR - EXPR | ID IF_STMT if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT while (EXPR) do STMT

6 The parser finds the derivation of a particular input * 5 Parser E -> E + E | E * E | INT E => E + E => E + E * E => 10 + E*E => * E => * 5 input derivation

7 10 E 25 E => E + E => E + E * E => 10 + E*E => * E => * 5 derivation derivation tree EE EE + *

8 10 E 25 derivation tree EE EE + * mult a, 2, 5 add b, 10, a machine code

9 Parsing

10 grammar Parser input string derivation

11 Example: Parser derivation input ?

12 Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of

13

14 Phase 2 Phase 1

15 Phase 2 Phase 3

16 Final result of exhaustive search Parser derivation input (top-down parsing)

17 Time complexity of exhaustive search Suppose there are no productions of the form Number of phases for string :

18 Time for phase 1: possible derivations For grammar with rules

19 Time for phase 2: possible derivations

20 Time for phase : possible derivations

21 Total time needed for string : Extremely bad!!! phase 1 phase 2 phase 2|w|

22 There exist faster algorithms for specialized grammars S-grammar: symbolstring of variables appears once Pair

23 S-grammar example: Each string has a unique derivation

24 In the exhaustive search parsing there is only one choice in each phase For S-grammars: Total time for parsing string : Time for a phase:

25 For general context-free grammars: There exists a parsing algorithm that parses a string in time

26 Simplifications of Context-Free Grammars

27 A Substitution Rule Substitute B Equivalent grammar

28 In general: Substitute B equivalent grammar

29 Useless Productions Some derivations never terminate... Useless Production

30 Another grammar: Not reachable from S Useless Production

31 In general: If Then variable is useful Otherwise, variable is useless

32 A production is useful if all its variables are useful

33 Removing Useless Productions Example Grammar:

34 First: find all variables that produce strings with only terminals Round 1: Round 2:

35 Keep only the variables that produce terminal symbols

36 Second: Find all variables reachable from Dependency Graph not reachable

37 Keep only the variables reachable from S Final Grammar

38 Nullable Variables Nullable Variable:

39 Removing Nullable Variables Example Grammar: Nullable variable

40 Substitute Final Grammar

41 Unit-Productions Unit Production:

42 Removing Unit Productions Observation: Is removed immediately

43 Example Grammar:

44 Substitute

45 Remove

46 Substitute

47 Remove repeated productions Final grammar

48 Removing All Step 1: Remove Nullable Variables Step 2: Remove Unit-Productions Step 3: Remove Useless Variables