1 214 review. 2 What we have learnt Generate scanner and parser –We do not program directly –Instead we write the specifications for the scanner and parser.

Slides:



Advertisements
Similar presentations
Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –
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.
Mooly Sagiv and Roman Manevich School of Computer Science
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Chapter 4 Lexical and Syntax Analysis Sections 1-4.
Context-Free Grammars Lecture 7
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
COP4020 Programming Languages
Compilation 2007 Context-Free Languages Parsers and Scanners Michael I. Schwartzbach BRICS, University of Aarhus.
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Syntax and Semantics Structure of programming languages.
CMSC 330: Organization of Programming Languages
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
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.
Parsing G Programming Languages May 24, 2012 New York University Chanseok Oh
Grammars CPSC 5135.
PART I: overview material
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.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
CS 153 A little bit about LR Parsing. Background We’ve seen three ways to write parsers:  By hand, typically recursive descent  Using parsing combinators.
Syntax and Semantics Structure of programming languages.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Exam 1 Review EECS 483 – Lecture 15 University of Michigan Monday, October 30, 2006.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Lecture 3: Parsing CS 540 George Mason University.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
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 CONSTRUCTION
2016/7/9Page 1 Lecture 11: Semester Review COMP3100 Dept. Computer Science and Technology United International College.
Syntax and Semantics Structure of programming languages.
Chapter 4 - Parsing CSCE 343.
Programming Languages Translator
CS510 Compiler Lecture 4.
50/50 rule You need to get 50% from tests, AND
Lexical and Syntax Analysis
Chapter 2 :: Programming Language Syntax
Syntax Specification and Analysis
Table-driven parsing Parsing performed by a finite state machine.
Compiler Lecture 1 CS510.
Bottom-Up Syntax Analysis
Lexical and Syntax Analysis
CPSC 388 – Compiler Design and Construction
R.Rajkumar Asst.Professor CSE
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Chapter 2 :: Programming Language Syntax
Theory of Computation Lecture #
Kanat Bolazar February 16, 2010
Chapter 3 Syntactic Analysis I.
Chapter 2 :: Programming Language Syntax
Review for the Midterm. Overview (Chapter 1):
Presentation transcript:

1 214 review

2 What we have learnt Generate scanner and parser –We do not program directly –Instead we write the specifications for the scanner and parser Describe specification using (formal) grammar –Grammar for scanner is simpler (regular grammar) –Grammar for parser is more complex (CFG) –Programming languages are defined using BNF and EBNF Understand how grammar is translated into program –RE  NFA  DFA  Minimization –CFG  LALR Diagram  Shift/reduce or reduce/reduce conflict We can write the assignments We can write the grammars We can debug, and write a similar tool in the future

3 What else we can learn Deal with the complexity of programming –Formalize the problem Divide the problem into smaller ones Compiler  scanner  RE  NFA  DFA –Find an algorithm to solve the problem RE  NFA  DFA … Develop a generic solution for a wide range of problems –generate a parser for any language –Guarantee the solution is always correct –Repetitive code is always saved

4 What makes a good programmer (from c2.com) "We will encourage you to develop the three great virtues of a programmer: laziness, impatience, and hubris." -- LarryWall, ProgrammingPerl, OreillyAndAssociatesLarryWallProgrammingPerlOreillyAndAssociates Laziness –The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful, and document what you wrote so you don't have to answer so many questions about it. Impatience –The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Hubris –Excessive pride. Also the quality that makes you write (and maintain) programs that other people won't want to say bad things about.

5

6 Valid topics Anything that was mentioned in the lectures –Also check lecture slides Assignments will be tested

7 Important topics Lexing –RE, NFA, DFA –RE to NFA, NFA to DFA, DFA minimization Parsing –CFG –LL parsing –LR parsing Understand grammar Write a grammar Write a parser or translator Understand how parser works –Shift/reduce conflicts

8 Lexing What is lexing? what is a lexer? How does a lexer relate to NFA/DFA theory? How does a lexer fit in with the rest of a compiler? What is a regular language? How do you write a regular expression, based on a narrative description of the pattern? How do you make an NFA based on an RE? How to transform NFA to DFA? How to minimize DFA? How is an NFA different from a DFA?

9 Parsing What is a context-free grammar? What is the grammar hierarchy? What is parsing? What is a parser? How does a parser relate to CFG theory? What is a leftmost derivation and rightmost derivation? What is a parse tree? What is ambiguity? How to remove ambiguity?

10 LL parsing What is FIRST()? What is FOLLOWS()? How do you fix left recursion? How do you fix common prefixes? How do you build a parse table? How do you run an LL parser?

11 LR parsing What is a shift/reduce conflict? How do you fix a shift/reduce conflict? What is LR(0) configuration (item)? What is LR(1) item? What is CLOSURE()? What is Successor(S, A)? How to draw transition diagram for LR(0), SLR, LR(1)? How to construct parsing table for LR(0), SLR, LR(1)? How to run LR(0)/SLR/LR(1) parser? How to decide whether a grammar is LR(0)/SLR/LR(1)? What is the difference between LR(0), SLR, LR(1) and LALR? Which LR algorithm does javaCUP, yacc use?

12 LL(1) Given the grammar A  aA|bA|b 1.Whether is it an LL(1) grammar? Why? 2.If not, can you change that to an LL(1) grammar? Answer: It is not an LL(1) grammar, because there is a conflict in the LL(1) parse table Modified grammar: A  aA | bA’ A’  A|ε ab$ AA  aAA  bA A  b ab$ AA  aAA  bA’ A’A’  A A’  ε

13 Is the grammar LR(0)? A  aA|bA|b It is not LR(0) because there is a conflict in state S4 S0: S'   A A   aA A   bA A  b S2: A  a  A A  aA A   bA A  b a S1: S’  A  A S4: A  b●A A  b  A  aA A  ●bA A  b a S3: A  aA  A A S5: A  bA  b b b StackInputaction S0aab$S2 S0 S2a ab$S2 S0 S2a S2a b$S4 S0 S2a S2a S4b $R A  b S0 S2a S2a S3A $R A  aA S0 S2a S3A $R A  aA S0 S1 $accept

14 Is the grammar LR(0)? A  aA|bA|b It is not LR(0) because there is a conflict in state S4 S0: S'   A A   aA A   bA A  b S2: A  a  A A  aA A   bA A  b a S1: S’  A  A S4: A  b●A A  b  A  aA A  ●bA A  b a S3: A  aA  A A S5: A  bA  b b b StackInputaction S0abb$S2 S0 S2a bb$S4 S0 S2a S4b b$S4 or Reduce? S0 S2a S4b b $If R, A  b S0 S2a S3A b $R A  aA S0 S1A b $?

15 Whether it is LR(0)? A  Aa A  Ab A  b S0: S'   A A   Aa A   Ab A  b S2: A  Ab  a S1: S’  A ● A  A  a A  A  b A S4: A  b  a S3: A  Aa  a b b StackInputaction S0bab$S4 S0S4 ab$R A  b S0S1 ab$S3 S0S1S3 b $R A  Aa S0S1 b $S2 S0 S1S2 $R A  Ab S0S1 $Accept

16 S  cAd A  ab|a w=cad S cAd cad S cAd a S cAd ab

17 Is the grammar LL(1)? S’  S S  cAd A  ab|a abcd$ S’S’  S SS  cAd AA  ab A  a It is not LL(1) 1.because the LL(1) parsing table has conflict; OR 2.Because it is not left factored

18 Is it LR(0)? SLR? S’  S S  cAd A  ab|a S0: S'   S S  cAd S2: S  c  Ad A  ab A  a S1: S’  S ● S S4: A  ab  b S3: A  a●b A  a  a c StackInputaction S0cad$S2 S0 S2c ad$S3 S0 S2c S3a d$Reduce not Shift S0 S2c S5A d $S6 S0 S2c S5A S6d $R S  cAd S0 S1 $Accept S5: S  cA●d S6: S  cAd  d A

19 Sample questions

20 JLex specification defines a –Context Free Grammar; –Regular Grammar; –Context Sensitive Grammar; –None of the above. JLex specification has ____ parts, separated by %. –Two; –Three; –Four; –Five; –None of the above.

21 JLex does not deal with: –DFA minimization; –CFG; –NFA to DFA transformation; –Lexical analysis; –None of the above. A JavaCup specification defines a –Context Free Grammar; –Regular Grammar; –Context Sensitive Grammar; –None of the above.

22 Suppose that you have a grammar that can give two different derivations for the same sentence. Is that grammar ambiguous? –Definitely yes; –Definitely no; –There is no enough information to tell; –It can’t have two derivations; –None of the above.

23 With an ambiguous grammar, how many parse trees are there for any sentence that is not in the language? –0; –exactly 1; –more than 1; –1 or more; –None of the above.

24 Given a grammar that contains the following production rule, where A is a nonterminal and a and b are terminals: A  aAa|abba According to Chomsky hierarchy, the grammar is in –Level 0; –Level 1; –Level 2; –Level 3. –None of the above.

25 Which of the following is not involved in compiler construction: –Lexical analysis; –Linear analysis; –Code generation; –Semantic analysis; –None of the above.

26 Given the following rules of a grammar, where A and B are non-terminals, a and b are terminals, and A is the start symbol: A  aB|bB B  aB|bB Which of the following regular expression can recognize the same language? – (a|b)+ – (a|b)+abb – (a|b)(a|b)+ – ab(a|b)+ – None of the above.

27 Answer true or false for the following questions: (0|1)* = ((1|0)*)* For every language, there is an unambiguous grammar. JLex is used to generate a parser from a JLex specification. Consider the following grammar where S is a non-terminal, if, then, and else are terminals. S→if then | if then else | ε Whether the grammar is ambiguous?. YACC is a parser generator. Top down parsing method has the name because it scans input file from top to down.

28 In the following grammars E is a non terminal and ID is a terminal. –Remove the left recursion of the following grammar. E  E+ID | ID –Write the result of the left factoring of the following grammar E  ID+E | ID

29 Some solutions not so good swap E and ID E  ID+E|ID Indirect left recursion E  E’+ID | ID E’  E E  A|ID A  E+ID

30 Acronyms –FSM –NFA/DFA –BNF –LL –LR –LALR –…

31 Given the following transition diagram. Write the corresponding regular expression. AS b c a B

32 Given the following grammar, where A is a non-terminal, a and b are terminals: A  aA|bA|b Write the regular expression that can recognize the same language.

33 Given the regular expression (ab)*. Write the corresponding regular grammar. Note that you will not get any marks if the grammar is not regular. Some incorrect answers: –A  abA|ε –A  BA|ε –B  ab

34 Write a CFG for the following languages over alphabet {a, b}: –Palindromes, i.e., strings read the same backward and forward, such as “aaa”, “aabbabbaa”.

35 Given the following production rule, where E is a non- terminal, and identifier is a terminal. Is it an ambiguous grammar? Explain your conclusion. E  E * E | identifier Rewrite the grammar into an unambiguous one

36 Given the following grammar E  TE’ E’  +TE’|ε T  FT’ T’  *FT’|ε F  (E)|id –What are the values in First(T)? –What are the values in Follow(T)?