more context free grammars
play

More context-free grammars 10/11/19 Administrivia HW 4 (proving - PowerPoint PPT Presentation

More context-free grammars 10/11/19 Administrivia HW 4 (proving languages are non-regular) due tonight at 4:30 Midterm out tonight No class on Monday Multi-day take home Open book, notes, and course webpage; closed everything


  1. More context-free grammars 10/11/19

  2. Administrivia • HW 4 (proving languages are non-regular) due tonight at 4:30 • Midterm out tonight • No class on Monday • Multi-day take home • Open book, notes, and course webpage; closed everything else • DFAs, NFAs, regular expressions, showing languages are not regular

  3. Recall: Context-free grammars (CFGs) • Grammar where LHS is single non-terminal • Example: S → aSb | ε

  4. • The parse tree specifies: • Syntax: it demonstrates that a-b*c is in the language • Also, the beginnings of semantics: it is a plan for evaluating the expression when the program is run • First evaluate a-b , then multiply that result by c • It specifies how the parts of the program fit together • And that says something about what happens when the program runs

  5. Parsing • To parse a program is to find a parse tree for it, with respect to a grammar for the language • Every time you compile a program, the compiler must first parse it • The parse tree (or a simplified version called the abstract syntax tree ) is one of the central data structures of almost every compiler • More about algorithms for parsing in chapter 15

  6. < exp > ::= < exp > - < exp > | < exp > * < exp > | < exp > = < exp > | < exp > < < exp > | ( < exp > ) | a | b | c • A grammar is ambiguous if there is a string in the language with more than one parse tree • The grammar above is ambiguous:

  7. Ambiguity • That kind of ambiguity is unacceptable • Part of the definition of the language must be a clear decision on whether a–b*c means ( a-b ) × c or a- ( b × c ) • To resolve this problem, BNF grammars are usually crafted to be unambiguous • They not only specify the syntax, but do so with a unique parse tree for each program, one that agrees with the intended semantics • Not usually difficult, but it generally means making the grammar more complicated

  8. < exp > ::= < ltexp > = < exp > | < ltexp > < ltexp > ::= < ltexp > < < subexp > | < subexp > < subexp > ::= < subexp > - < mulexp > | < mulexp > < mulexp > ::= < mulexp > * < rootexp > | < rootexp > < rootexp > ::= ( < exp > ) | a | b | c

  9. Trade-Off • The new grammar is unambiguous • Strict precedence: * , then - , then < , then = • Strict associativity: left, so a-b-c is computed as ( a - b )- c • On the other hand, it is longer and less readable • Many BNFs are meant to be used both by people and directly by computer programs • The code for the parser part of a compiler can be generated automatically from the grammar by a parser-generator • Such programs really want unambiguous grammars

  10. Inherent Ambiguity • There are CFLs for which it is not possible to give an unambiguous grammar • They are inherently ambiguous • This is not usually a problem for programming languages

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend