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

more context free grammars
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

More context-free grammars

10/11/19

slide-2
SLIDE 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
slide-3
SLIDE 3

Recall: Context-free grammars (CFGs)

  • Grammar where LHS is single non-terminal
  • Example: S → aSb | ε
slide-4
SLIDE 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
slide-5
SLIDE 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
slide-6
SLIDE 6
  • A grammar is ambiguous if there is a string in the language with more than one

parse tree

  • The grammar above is ambiguous:

<exp> ::= <exp> - <exp> | <exp> * <exp> | <exp> = <exp> | <exp> < <exp> | (<exp>) | a | b | c

slide-7
SLIDE 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

slide-8
SLIDE 8

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

slide-9
SLIDE 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
slide-10
SLIDE 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