4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else...

52

Transcript of 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else...

Page 1: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...
Page 2: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Comp215: Recursive GrammarsMack Joyner, Dan S. Wallach (Rice University)

Copyright Ⓒ 2016, Mack Joyner, Dan S. Wallach. All rights reserved.

Page 3: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Recall: Regular languagesEasiest way to build a scanner: use regular expressions (“regex”)

First, we need to understand a regular language

Formally:Alphabet : S

Empty language : /0 is a regular language

Singletons : 8a 2 S,{a} is a regular language

Combinations : if A and B are regular languages, then

A[B (union),

A•B (concatenation),

A⇤ (Kleene star), are regular languages

Page 4: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Regular languages / Regular expressionsRegular expressions: convenient syntax for regular languages

Union: (aaa|bbb)Concatenation: aaabbbKleene star: (aaa)*

Page 5: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Noam Chomsky: Grammar Pioneer

Page 6: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Regular GrammarRegular grammar: another way to describe regular languages

There’s a regular grammar for every regular language

A grammar is right-linear (recursive) if all productions are of the form:A aA,A a

A grammar is left-linear (recursive) if all productions are of the form:A Aa,A a,

Page 7: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Regular GrammarRegular grammar: another way to describe regular languages

There’s a regular grammar for every regular language

A grammar is right-linear (recursive) if all productions are of the form:A aA,A a

A grammar is left-linear (recursive) if all productions are of the form:A Aa,A a,

Production rule

Page 8: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Regular GrammarRegular grammar: another way to describe regular languages

There’s a regular grammar for every regular language

A grammar is right-linear (recursive) if all productions are of the form:A aA,A a

A grammar is left-linear (recursive) if all productions are of the form:A Aa,A a,

Every non-terminal must appear at least once on LHS

Non-terminals must be rightmost symbol of right-linear grammar

Page 9: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Regular GrammarRegular grammar: another way to describe regular languages

There’s a regular grammar for every regular language

A grammar is right-linear (recursive) if all productions are of the form:A aA,A a

A grammar is left-linear (recursive) if all productions are of the form:A Aa,A a,

Terminals appear only on RHS

Page 10: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Regular GrammarRegular grammar: another way to describe regular languages

There’s a regular grammar for every regular language

A grammar is right-linear (recursive) if all productions are of the form:A aA,A a

A grammar is left-linear (recursive) if all productions are of the form:A Aa,A a,

Terminals appear only on RHS

A regular grammar is either right or left recursive

Page 11: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Regular Expression to Regular grammarRegular expression: (a)*(b)*

Strings you’d expect to match: “aaa”, “aaaa”, “aaaabb”, “bbbbbb”,

A BB bBB empty

S AA aA

Page 12: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Regular Expression to Regular grammarRegular expression: (a)*(b)*

Strings you’d expect to match: “aaa”, “aaaa”, “aaaabb”, “bbbbbb”,

A BB bBB empty

S AA aA

S is the start symbol

Page 13: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Regular Expression to Regular grammarRegular expression: (a)*(b)*

Strings you’d expect to match: “aaa”, “aaaa”, “aaaabb”, “bbbbbb”,

A BB bBB empty

S AA aA

S is the start symbol

empty string terminal

Page 14: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Regular Expression to Regular grammarRegular expression: (a)*(b)*

Strings you’d expect to match: “aaa”, “aaaa”, “aaaabb”, “bbbbbb”,

A BB bBB empty

S AA aA

S is the start symbol

empty string terminal

Can you construct a regular grammar to accept only (a)n(b)n?

Page 15: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Context-Free GrammarRelax the regular grammar restriction to make it context-free

Describes how sentence of a language may be generatedNon-terminal can be anywhere on the production rule’s right hand side

A BB bBB empty

S AA aA

Page 16: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Context-Free GrammarRelax the regular grammar restriction to make it context-free

Describes how sentence of a language may be generatedNon-terminal can be anywhere on the production rule’s right hand side

A BB bBB empty

S AA aA

Can you construct a context-free grammar to accept only (a)n(b)n?

Page 17: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Context-Free GrammarRelax the regular grammar restriction to make it context-free

Describes how sentence of a language may be generatedNon-terminal can be anywhere on the production rule’s right hand side

Can you construct a context-free grammar to accept only (a)n(b)n?

S AA aAbA empty

Non-terminal A can now appear anywhere

Page 18: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Context-Free GrammarRelax the regular grammar restriction to make it context-free

Describes how sentence of a language may be generatedNon-terminal can be anywhere on the production rule’s right hand side

Can you construct a context-free grammar to accept only (a)n(b)n?

S AA aAbA empty

Non-terminal A can now appear anywhere

What if I want to call non-terminal “AB” instead of “A”?

Page 19: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Context-Free GrammarRelax the regular grammar restriction to make it context-free

Describes how sentence of a language may be generatedNon-terminal can be anywhere on the production rule’s right hand side

Can you construct a context-free grammar to accept only (a)n(b)n?

S AA aAbA empty

Non-terminal A can now appear anywhere

What if I want to call non-terminal “AB” instead of “A”?

Multiple non-terminals on LHS is a context-sensitive grammar

Page 20: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Categories of Grammarsregular

good for identifiers, parameter lists, subscriptsan expanded form of regular expressionsleft-regular or right-regular

context freeLHS of production is single non-terminal

context sensitiveLHS has a non-terminal that can be surrounded by terminals and/or non-terminals

recursively enumerableLHS can be any non-empty sequence of terminals and/or non-terminals

Page 21: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Categories of Grammarsregular

good for identifiers, parameter lists, subscriptsan expanded form of regular expressionsleft-regular or right-regular

context freeLHS of production is single non-terminal

context sensitiveLHS has a non-terminal that can be surrounded by terminals and/or non-terminals

recursively enumerableLHS can be any non-empty sequence of terminals and/or non-terminals

COMP 215

Page 22: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Backus-Naur Form (BNF)Used to describe syntax of PL; first used for Algol-60Nonterminals are enclosed in <...>

<expression>, <identifier>Alternatives indicated by |

<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9Options (0 or 1 occurrences) indicated by [...]

<stmt> ::= if <cond> then <stmt> [ else <stmt>]Repetition (0 or more occurrences) indicated by {...}

<unsigned> ::= <digit> {<digit>}Derivation

apply the rules, starting with start symbol and ending with a sentence

Page 23: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Backus-Naur Form (BNF)Used to describe syntax of PL; first used for Algol-60Nonterminals are enclosed in <...>

<expression>, <identifier>Alternatives indicated by |

<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9Options (0 or 1 occurrences) indicated by [...]

<stmt> ::= if <cond> then <stmt> [ else <stmt>]Repetition (0 or more occurrences) indicated by {...}

<unsigned> ::= <digit> {<digit>}Derivation

apply the rules, starting with start symbol and ending with a sentence

How would you do this using regular expressions?

Page 24: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

BNF defined using BNF <syntax> ::= <rule> | <rule> <syntax> <rule> ::= <opt-whitespace> "<" <rule-name> ">" <opt-whitespace> "::=" <opt-whitespace> <expression> <line-end> <opt-whitespace> ::= " " <opt-whitespace> | "" <expression> ::= <list> | <list> <opt-whitespace> "|" <opt-whitespace> <expression> <line-end> ::= <opt-whitespace> <EOL> | <line-end> <line-end> <list> ::= <term> | <term> <opt-whitespace> <list> <term> ::= <literal> | "<" <rule-name> ">" <literal> ::= '"' <text1> '"' | "'" <text2> "'" <text1> ::= "" | <character1> <text1> <text2> ::= "" | <character2> <text2> <character> ::= <letter> | <digit> | <symbol> <letter> ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" <symbol> ::= | "-" | "!" | "#" | "$" | "%" | "&" | "(" | ")" | "*" | "+" | "," | "-" | "." | "/" | ":" | ";" | "<" | "=" | ">" | "?" | "@" | "[" | "\" | "]" | "^" | "_" | "`" | "{" | "|" | "}" | "~" <character1> ::= <character> | "'" <character2> ::= <character> | ‘"' <rule-name> ::= <letter> | <rule-name> <rule-char>

<rule-char> ::= <letter> | <digit> | "-"

Page 25: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Example Revisited

anbn, n = 0, 1, 2, …

Page 26: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Example Revisited

anbn, n = 0, 1, 2, …

<start> ::= <expr><expr> ::= a <expr> b<expr> ::= empty

Page 27: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Example Grammar and Derivation <program> ::= <stmts> <stmts> ::= <stmt> | <stmt> ; <stmts> <stmt> ::= <var> = <expr> <var> ::= a | b | c | d <expr> ::= <term> + <term> | <term> - <term> <term> ::= <var> | const

<program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const

Page 28: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Example Grammar and Derivation <program> ::= <stmts> <stmts> ::= <stmt> | <stmt> ; <stmts> <stmt> ::= <var> = <expr> <var> ::= a | b | c | d <expr> ::= <term> + <term> | <term> - <term> <term> ::= <var> | const

<program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const

Leftmost derivation

Page 29: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Example Grammar and Derivation <program> ::= <stmts> <stmts> ::= <stmt> | <stmt> ; <stmts> <stmt> ::= <var> = <expr> <var> ::= a | b | c | d <expr> ::= <term> + <term> | <term> - <term> <term> ::= <var> | const

<program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const

Sentence: has only terminal symbols

Leftmost derivation

Page 30: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Example Grammar and Derivation <program> ::= <stmts> <stmts> ::= <stmt> | <stmt> ; <stmts> <stmt> ::= <var> = <expr> <var> ::= a | b | c | d <expr> ::= <term> + <term> | <term> - <term> <term> ::= <var> | const

<program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const

Sentence: has only terminal symbols

Leftmost derivation

Language: set of all possible sentences derived from grammar

Page 31: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Derivation / Parse Tree

<program>

<stmts>

<stmt>

<var> = <expr>

a <term> + <term>

<var> const

b

A derivation tree is the tree resulting from applying productions to rewrite start symbol

a parse tree is the same tree starting with terminals and building back to the start symbol (goal symbol)

Page 32: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Derivation / Parse Tree

<program>

<stmts>

<stmt>

<var> = <expr>

a <term> + <term>

<var> const

b

A derivation tree is the tree resulting from applying productions to rewrite start symbol

a parse tree is the same tree starting with terminals and building back to the start symbol (goal symbol)

derivation tree

Page 33: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Derivation / Parse Tree

<program>

<stmts>

<stmt>

<var> = <expr>

a <term> + <term>

<var> const

b

A derivation tree is the tree resulting from applying productions to rewrite start symbol

a parse tree is the same tree starting with terminals and building back to the start symbol (goal symbol)

parse tree

derivation tree

Page 34: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Ambiguity

<expr> <expr>

<expr> <op> <expr> <expr> <op> <expr>

<expr> <op> <expr> <expr> <op> <expr>

const - const / const const - const / const

A grammar is ambiguous iff it generates a sentential form that has two or more distinct parse trees An ambiguous expression grammar:

<expr> ::= <expr> <op> <expr> | const<op> ::= / | -

Page 35: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityOne famous ambiguity is “dangling else”<stmt> ::= if <cond> then <stmt> [else <stmt>]

This can deriveif X > 9 then if B = 4 then X := 5 else X := 0

Page 36: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityOne famous ambiguity is “dangling else”<stmt> ::= if <cond> then <stmt> [else <stmt>]

This can deriveif X > 9 then if B = 4 then X := 5 else X := 0

which “if” does this else belong to?

Page 37: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityCan solve syntactically by adding nonterminals & productions<stmt> ::= <matched> | <unmatched><matched> ::= if <cond> then <matched> else <matched> | <simple><unmatched> ::= if <cond> then <stmt> | if <cond> then <matched> else <unmatched>

Can also solve semantically“elses are associated with immediately preceding unmatched then”

if X > 9 then if B = 4 then X := 5 else X := 0

Page 38: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityCan solve syntactically by adding nonterminals & productions<stmt> ::= <matched> | <unmatched><matched> ::= if <cond> then <matched> else <matched> | <simple><unmatched> ::= if <cond> then <stmt> | if <cond> then <matched> else <unmatched>

Can also solve semantically“elses are associated with immediately preceding unmatched then”

if X > 9 then if B = 4 then X := 5 else X := 0

Page 39: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityCan solve syntactically by adding nonterminals & productions<stmt> ::= <matched> | <unmatched><matched> ::= if <cond> then <matched> else <matched> | <simple><unmatched> ::= if <cond> then <stmt> | if <cond> then <matched> else <unmatched>

Can also solve semantically“elses are associated with immediately preceding unmatched then”

if X > 9 then if B = 4 then X := 5 else X := 0

Page 40: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityCan solve syntactically by adding nonterminals & productions<stmt> ::= <matched> | <unmatched><matched> ::= if <cond> then <matched> else <matched> | <simple><unmatched> ::= if <cond> then <stmt> | if <cond> then <matched> else <unmatched>

Can also solve semantically“elses are associated with immediately preceding unmatched then”

if X > 9 then if B = 4 then X := 5 else X := 0

Page 41: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityCan solve syntactically by adding nonterminals & productions<stmt> ::= <matched> | <unmatched><matched> ::= if <cond> then <matched> else <matched> | <simple><unmatched> ::= if <cond> then <stmt> | if <cond> then <matched> else <unmatched>

Can also solve semantically“elses are associated with immediately preceding unmatched then”

if X > 9 then if B = 4 then X := 5 else X := 0

Page 42: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityCan solve syntactically by adding nonterminals & productions<stmt> ::= <matched> | <unmatched><matched> ::= if <cond> then <matched> else <matched> | <simple><unmatched> ::= if <cond> then <stmt> | if <cond> then <matched> else <unmatched>

Can also solve semantically“elses are associated with immediately preceding unmatched then”

if X > 9 then if B = 4 then X := 5 else X := 0

Page 43: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityCan solve syntactically by adding nonterminals & productions<stmt> ::= <matched> | <unmatched><matched> ::= if <cond> then <matched> else <matched> | <simple><unmatched> ::= if <cond> then <stmt> | if <cond> then <matched> else <unmatched>

Can also solve semantically“elses are associated with immediately preceding unmatched then”

if X > 9 then if B = 4 then X := 5 else X := 0

Page 44: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityCan solve syntactically by adding nonterminals & productions<stmt> ::= <matched> | <unmatched><matched> ::= if <cond> then <matched> else <matched> | <simple><unmatched> ::= if <cond> then <stmt> | if <cond> then <matched> else <unmatched>

Can also solve semantically“elses are associated with immediately preceding unmatched then”

if X > 9 then if B = 4 then X := 5 else X := 0

Page 45: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

“Dangling Else” AmbiguityCan solve syntactically by adding nonterminals & productions<stmt> ::= <matched> | <unmatched><matched> ::= if <cond> then <matched> else <matched> | <simple><unmatched> ::= if <cond> then <stmt> | if <cond> then <matched> else <unmatched>

Can also solve semantically“elses are associated with immediately preceding unmatched then”

if X > 9 then if B = 4 then X := 5 else X := 0

Page 46: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Resolving Ambiguity

<expr>

<expr> - <term>

<term> / const <term>

const const

An ambiguous expression grammar: <expr> ::= <expr> <op> <expr> | const<op> ::= / | -

Unambiguous expression grammar: <expr> ::= <expr> - <term> | <term><term> ::= <term> / const | const

Page 47: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Resolving Ambiguity

<expr>

<expr> - <term>

<term> / const <term>

const const

An ambiguous expression grammar: <expr> ::= <expr> <op> <expr> | const<op> ::= / | -

Unambiguous expression grammar: <expr> ::= <expr> - <term> | <term><term> ::= <term> / const | const

Page 48: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Recursion

Left recursive grammars <expr> ::= <expr> + <term> | <term><term> ::= <term> * const | const

Right recursive grammars <expr> ::= <term> + <remain> | <term><term> ::= const * <term> | const<remain> ::= <term> | <term> + <expr>

Page 49: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Associativity

Left associativity: a - b + c = (a - b) + c

Right associativity: a ** b ** c = a ** (b ** c)

Page 50: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Example: Expressions

Consider following unambiguous grammar for expressions: <expr> ::= [<expr> <addop>] <term><term> ::= [<term> <mulop>] <factor><factor> ::= (<expr>) | <digit><addop> ::= + | -<mulop> ::= * | /<digit> ::= 0 | ... | 9

This grammar is left recursive and generates expressions that are left associative

Page 51: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

Syntax Graphsequivalent to CFGs put the terminals in circles or ellipses and put the nonterminals in rectangles; Lines and arrows indicate how constructs are built

<expr> ::= <term> [ <addop> < expr>]<term> ::= <factor> [ <mulop> <term>]

term

addop

expr:factor

mulop

term:

Page 52: 4 % 2 3 % # 5 2 )4 0 5 9 · “Dangling Else” Ambiguity One famous ambiguity is “dangling else ...

JSON Grammar

json.org :