# CMSC 330: Organization of Programming Languages .B. S â†’ 0S1| S1 | µ C ......

date post

10-Feb-2019Category

## Documents

view

213download

0

Embed Size (px)

### Transcript of CMSC 330: Organization of Programming Languages .B. S â†’ 0S1| S1 | µ C ......

CMSC 330: Organization of Programming Languages

Context Free Grammars

1CMSC 330 Fall 2018

2

Front End

AbstractSyntax Tree

Back End

Source

Compiler / Interpreter

CodeGenerator

An-alyzer

Opt-imizer

Architecture of Compilers, Interpreters

CMSC 330 Fall 2018

Front End Scanner and Parser

4

Front End

Source Scanner Parser

AST

TokenStream

Scanner / lexer converts program source into tokens (keywords, variable names, operators, numbers, etc.) using regular expressions

Parser converts tokens into an AST (abstract syntax tree) using context free grammars

CMSC 330 Fall 2018

Context-Free Grammar (CFG)

A way of describing sets of strings (= languages)

The notation L(G) denotes the language of strings

defined by grammar G

Example grammar G is S 0S | 1S | ewhich says that string s L(G) iff s = e, or s L(G) such that s = 0s, or s = 1s

Grammar is same as regular expression (0|1)*

Generates / accepts the same set of strings

5CMSC 330 Fall 2018

CFGs Are Expressive

CFGs subsume REs, DFAs, NFAs There is a CFG that generates any regular language But: REs are often better notation for those languages

And CFGs can define languages regexps cannot S ( S ) | e // represents balanced pairs of ( ) s

As a result, CFGs often used as the basis of parsers for programming languages

6CMSC 330 Fall 2018

Parsing with CFGs

CFGs formally define languages, but they do not define an algorithm for accepting stringsSeveral styles of algorithm; each works only for less expressive forms of CFG LL(k) parsing LR(k) parsing LALR(k) parsing SLR(k) parsing

Tools exist for building parsers from grammars JavaCC, Yacc, etc.

7

We will discuss this next lecture

CMSC 330 Fall 2018

Formal Definition: Context-Free Grammar

A CFG G is a 4-tuple (, N, P, S)

alphabet (finite set of symbols, or terminals) Often written in lowercase

N a finite, nonempty set of nonterminal symbols Often written in UPPERCASE It must be that N =

P a set of productions of the form N (|N)* Informally: the nonterminal can be replaced by the string of

zero or more terminals / nonterminals to the right of the Can think of productions as rewriting rules (more later)

S N the start symbol8CMSC 330 Fall 2018

Notational Shortcuts

A production is of the form left-hand side (LHS) right hand side (RHS)

If not specified Assume LHS of first production is the start symbol

Productions with the same LHS Are usually combined with |

If a production has an empty RHS It means the RHS is

S aBc // S is start symbolA aA

| b // A b| // A e

9

S aBc

CMSC 330 Fall 2018

Backus-Naur FormContext-free grammar production rules are also called Backus-Naur Form or BNF Designed by John Backus and Peter Naur

Chair and Secretary of the Algol committee in the early 1960s. Used this notation to describe Algol in 1962

A production A B c D is written in BNF as ::= c Non-terminals written with angle brackets and uses

::= instead of Often see hybrids that use ::= instead of but drop

the angle brackets on non-terminals

10CMSC 330 Fall 2018

Generating Strings

We can think of a grammar as generatingstrings by rewritingExample grammar GS 0S | 1S | eGenerate string 011 from G as follows:S 0S // using S 0S 01S // using S 1S 011S // using S 1S 011 // using S e

11CMSC 330 Fall 2018

Accepting Strings (Informally)

Checking if s L(G) is called acceptance Algorithm: Find a rewriting starting from Gs start

symbol that yields s A rewriting is some sequence of productions

(rewrites) applied starting at the start symbol 011 L(G) according to the previous rewriting

Terminology Such a sequence of rewrites is a derivation or parse Discovering the derivation is called parsing

12CMSC 330 Fall 2018

Derivations

Notation indicates a derivation of one step+ indicates a derivation of one or more steps* indicates a derivation of zero or more steps

Example S 0S | 1S | e

For the string 010 S 0S 01S 010S 010 S + 010 010 * 010

13CMSC 330 Fall 2018

Language Generated by Grammar

L(G) the language defined by G is

L(G) = { s * | S + s }

S is the start symbol of the grammar is the alphabet for that grammar

In other words All strings over that can be derived from the start

symbol via one or more productions

14CMSC 330 Fall 2018

Quiz #1Consider the grammar

S aS | TT bT | UU cU |

Which of the following strings is generated by this grammar?A. cccB. abaC. babD. ca

15CMSC 330 Fall 2018

Quiz #1Consider the grammar

S aS | TT bT | UU cU |

Which of the following strings is generated by this grammar?A. cccB. abaC. babD. ca

16CMSC 330 Fall 2018

Quiz #2Consider the grammar

S aS | TT bT | UU cU |

Which of the following is a derivation of the string bbc?A. S T U bU bbU bbcU bbcB. S bT bbT bbU bbcU bbcC. S T bT bbT bbU bbcU bbcD. S T bT bTbT bbT bbcU bbc

17CMSC 330 Fall 2018

Quiz #2Consider the grammar

S aS | TT bT | UU cU |

Which of the following is a derivation of the string bbc?A. S T U bU bbU bbcU bbcB. S bT bbT bbU bbcU bbcC. S T bT bbT bbU bbcU bbcD. S T bT bTbT bbT bbcU bbc

18CMSC 330 Fall 2018

Quiz #3Consider the grammar

S aS | TT bT | UU cU |

Which of the following regular expressions accepts the same language as this grammar?A. (a|b|c)*B. abc*C. a*b*c*D. (a|ab|abc)*

19CMSC 330 Fall 2018

Quiz #3Consider the grammar

S aS | TT bT | UU cU |

Which of the following regular expressions accepts the same language as this grammar?A. (a|b|c)*B. abc*C. a*b*c*D. (a|ab|abc)*

20CMSC 330 Fall 2018

Designing Grammars

1. Use recursive productions to generate an arbitrary number of symbols

A xA | // Zero or more x sA yA | y // One or more y s

2. Use separate non-terminals to generate disjoint parts of a language, and then combine in a production

a*b* // a s followed by b sS ABA aA | // Zero or more a sB bB | // Zero or more b s

23CMSC 330 Fall 2018

Designing Grammars

3. To generate languages with matching, balanced, or related numbers of symbols, write productions which generate strings from the middle

{anbn | n 0} // N a s followed by N b sS aSb | Example derivation: S aSb aaSbb aabb{anb2n | n 0} // N a s followed by 2N b sS aSbb | Example derivation: S aSbb aaSbbbb aabbbb

24CMSC 330 Fall 2018

Designing Grammars4. For a language that is the union of other

languages, use separate nonterminals for each part of the union and then combine{ an(bm|cm) | m > n 0}Can be rewritten as{ anbm | m > n 0} { ancm | m > n 0}S T | VT aTb | UU Ub | bV aVc | WW Wc | c

25CMSC 330 Fall 2018

Practice

Try to make a grammar which accepts 0*|1* 0n1n where n 0

Give some example strings from this language S 0 | 1S

0, 10, 110, 1110, 11110,

What language is it, as a regexp? 1*0

S A | BA 0A | B 1B |

S 0S1 |

26CMSC 330 Fall 2018

Quiz #4

Which of the following grammars describes the same language as 0n1m where m n ?

A. S 0S1 | B. S 0S1 | S1 | C. S 0S1 | 0S | D. S SS | 0 | 1 |

27CMSC 330 Fall 2018

Quiz #4

Which of the following grammars describes the same language as 0n1m where m n ?

A. S 0S1 | B. S 0S1 | S1 | C. S 0S1 | 0S | D. S SS | 0 | 1 |

28CMSC 330 Fall 2018

CFGs for Language Syntax

When discussing operational semantics, we used BNF-style grammars to define ASTs

e ::= x | n | e + e | let x = e in e

This grammar defined an AST for expressions synonymous with an OCaml datatype

We can also use this grammar to define a language parser However, while it is fine for defining ASTs, this

grammar, if used directly for parsing, is ambiguous

29CMSC 330 Fall 2018

Arithmetic Expressions

E a | b | c | E+E | E-E | E*E | (E) An expression E is either a letter a, b, or c Or an E followed by + followed by an E etc

This describes (or generates) a set of strings {a, b, c, a+b, a+a, a*c, a-(b*a), c*(b + a), }

Example strings not in the language d, c(a), a+, b**c, etc.

30CMSC 330 Fall 2018

Parse Trees

Parse tree shows how a string is produced by a grammar Root node is the start symbol Every internal node is a nonterminal Children of an internal node

Are symbols on RHS of production applied to nonterminal

Every leaf node is a terminal or

Reading the leaves left to right Shows the string corresponding to the tree

32CMSC 330 Fall 2018

Parse Tree Example

S aS | TT bT | UU cU |

S

33

S

CMSC 330 Fall 2018

Parse Tree Example

S aS

34

S

SaS aS | TT bT | UU cU |

CMSC 330 Fall 2018

Parse Tree Example

S aS | TT bT | UU cU |

S aS aT

35

S

S

T

a

CMSC 330 Fall 2018

Parse Tree Example

S aS | TT bT | UU cU |

S aS aT aU

36

S

S

T

U

a

CMSC 330 F

*View more*