L5-CFG - Introduction to Compiler Design Syntax Analysis...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Compiler Design Syntax Analysis: Context-Free Grammar Professor Yi-Ping You Department of Computer Science http://www.cs.nctu.edu.tw/~ypyou/ Page 1 Introduction to Compiler Design, Spring 2010
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Outline Overview of Syntax Analysis C t t F G Context-Free Grammars Writing a Grammar Grammar Transformation Oth I CFG Other Issues on CFGs Page 2 Introduction to Compiler Design, Spring 2010
Image of page 2
Where is Syntax Where is Syntax Analyzer? Analyzer? if (b == 0) a = b; Lexical Analyzer or Scanner if ( b == 0 ) a = b ; Syntax Analyzer or Parser if abstract syntax tree t == = b 0 a b or parse tree Page 3 Introduction to Compiler Design, Spring 2010
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Parsing Analogy Syntax analysis for natural languages Recognize whether a sentence is grammatically correct Identify the function of each word sentence subject verb indirect object object I gave him noun phrase article noun “I gave him the book” Page 4 Introduction to Compiler Design, Spring 2010 book the I gave him the book
Image of page 4
Syntax Analysis Overview Goal – determine if the input token stream satisfies the syntax of the program What do we need to do this? An expressive way to describe the syntax A mechanism that determines if the input token stream satisfies the syntax description For lexical analysis Regular expressions describe tokens Finite automata = mechanisms to generate tokens from input stream Page 5 Introduction to Compiler Design, Spring 2010
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Just Use Regular Expressions? REs can expressively describe tokens Easy to implement via DFAs So just use them to describe the syntax of i l a programming language NO! – They don’t have enough power to express any non- t i i l t trivial syntax Example – Nested constructs (blocks, expressions, statements) – Detect balanced braces: statements) Detect balanced braces: {{} {} {{} { }}} { { { { { } } } } } . . . - We need unbounded counting! - FAs cannot count except in a strictly modulo fashion (FA with stacks i e Page 6 Introduction to Compiler Design, Spring 2010 modulo fashion (FA with stacks, i.e., pushdown automata)
Image of page 6
Outline Overview of Syntax Analysis C t t F G Context-Free Grammars Writing a Grammar Grammar Transformation Oth I CFG R.E. (Recursively enumerable) Other Issues on CFGs Context-sensitive grammar Context-free grammar Regular Expression Page 7 Introduction to Compiler Design, Spring 2010 Chomsky Hierarchy
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Context Context-Free Free Grammars (CFGs) Grammars (CFGs) Definition: G = ( T, N, S, P ) T : terminals = token (name) or ε N : nonterminals = syntactic variables S : start symbol = special nonterminal S special nonterminal P : productions of the form head body head = a single nonterminal body = zero or more terminals and nonterminals Specify how nonterminals may be expanded E R l D fi iti E.g., stmt if expr then stmt stmt if expr then stmt else stmt stmt → ε digit [0-9] digits digit + number digits (.digits)?(E[+-]?digits)?
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern