Syntax analysis

Context-Free Grammars

Write a grammar, derive strings one replacement at a time, and see the parse tree each derivation defines.

  • Lecture reference: Introduction to Parsing · slides 11–31

Grammar

One production per line; | separates alternatives, also at the start of a continuation line. Type -> for →, and write ε for an empty right-hand side. The first left-hand side is the start symbol.

Open in LL(1) Parsing Table, Shift-Reduce Parsing, LR(1) Parser Builder or Conflicts & Precedence

Four-tuple G = (N, T, S, P)

Lecture reference: Introduction to Parsing · slide 14
Nnon-terminals
= { E }
Tterminals
= { int, +, *, (, ) }
Sstart symbol
= E
P4 productions
  1. 1. E → int
  2. 2. E → E + E not of the form V → w | wU
  3. 3. E → E * E not of the form V → w | wU
  4. 4. E → ( E ) not of the form V → w | wU

Type 2 (context free) V → α recognizer: push-down automaton (ND)

Productions 2, 3 and 4 do not have the type 3 form V → w | wU.

Chomsky hierarchy
Lecture reference: Introduction to Parsing · slide 23
TypeLanguageFormRecognizer
0 Unrestricted or recursively enumerableαXβ → αδβX ∈ N ∪ Tα, β, δ ∈ (N ∪ T)*Turing machine
1 Context SensitiveαVβ → αδβV ∈ Nδ ≠ εLinear Bounded Automaton (ND)
2 (this grammar)Context FreeV → αV ∈ Nα ∈ (N ∪ T)*Push-down Automaton (ND)
3 RegularV → w | wUw ∈ T*; U, V ∈ NNFA or DFA

From the slides

Lecture reference: Introduction to Parsing · slide 12

The productions viewed as rewrite rules: E → E * E → ( E ) * E → ( E + E ) * E → … → ( int + int ) * int. The builder holds the whole chain; the slide leaves out the three steps E → int.

Derivation 6 steps

Replace

Sentential form

(int+int)*int

Step 6: production 1, E → int, replaces symbol 7 of ( int + int ) * E. Sentential form: ( int + int ) * int. Only terminals remain.

( int + int ) * int is a sentence: the form has only terminals, so it is in L(G).

Derivation leftmost derivation

  1. E
  2. E * E
  3. ( E ) * E
  4. ( E + E ) * E
  5. ( int + E ) * E
  6. ( int + int ) * E
  7. ( int + int ) * int
Derivations of the finished tree

Parse tree

  • Added by the last step
  • Non-terminal to replace

Membership is the string in L(G)?

Terminals separated by spaces. Left empty, the string is ε.

It has one parse tree.

Leftmost derivation

  1. E
  2. E * E
  3. ( E ) * E
  4. ( E + E ) * E
  5. ( int + E ) * E
  6. ( int + int ) * E
  7. ( int + int ) * int

( int + int ) * int is in L(G).

Test strings

  1. in L(G)
  2. in L(G)
  3. in L(G)

One token string per row; an empty row is ε.

Language the sentences of L(G)

Lecture reference: Introduction to Parsing · slide 22

L(G) = { a1a2 … an | S →* a1a2 … an and ai ∈ T }

infinite
5 tokens
Sentences of L(G) by length
1 token
3 tokens
5 tokens

Shorter sentences first; within one length, in the order of T. A sentence opens in the Membership panel when it is selected. Arrow keys move between the sentences.

No sentence drawn yet.

Starting from the start symbol, each non-terminal is replaced with one of its productions, picked at random down to depth 6; deeper non-terminals take their shortest expansion.