Here are some simple languages that we can use for the purpose of seeing how to derive strings in a language and how to associate a tree with a string. These languages are all vaguely "expression" like. --------------------------------------------------------------------- Language S1. S ::= 'a' '+' S This grammar has a problem because it does not have a way to stop its recursion. --------------------------------------------------------------------- Language S2. A simple way to fix S1. S ::= 'a' '+' S | 'a' Some example strings are: "a" "a + a" "a + a + a" "a + a + a + a" Derivation tree: S /|\ / | \ a + S /|\ / | \ a + S /|\ / | \ a + S | | a --------------------------------------------------------------------- Language S3. Almost the same as S2. S ::= S '+' 'a' | 'a' Some example strings are: "a" "a + a" "a + a + a" "a + a + a + a" Derivation tree: S /|\ / | \ S + a /|\ / | \ S + a /|\ / | \ S + a | | a --------------------------------------------------------------------- Language S4. Almost the same as S2 and S3. S ::= S '+' S | 'a' Some example strings are: "a" "a + a" "a + a + a" "a + a + a + a" The last sentence has lots of derivation trees. The grammar is ambiguous. --------------------------------------------------------------------- Language S5. Allow grouping instead of forcing left-to-right or right-to-left. S ::= '(' S ')' '+' 'a' | 'a' '+' '(' S ')' | '(' S ')' '+' '(' S ')' | 'a' '+' 'a' | 'a' Some example strings are: "a + a" "(a + a) + a" "a + (a + a)" "(a + a) + (a + a)" "a + (a + (a + a))" "((a + a) + a) + a" Derivation tree: S / \ / \ ( S ) + a / \ / \ ( S ) + a /|\ / | \ a + a Questions: Is the string "a + (a + a) + a" in this language? Is the string "a + ((a + a) + a)" in this language?