Sep 01, 2003 ------------- - Recap syntax - gives structure of language - grammar written in form of rules - productions - single non-terminal on left - nonterminals & terminals on right - Regular expressions and context-free grammars - e.g., of regular expressions - ls "*hw*" - exercise: write it as a CFG - Example CFG - S -> (S)S | e - exercise: write it as a reg exp! - Ans: cannot! - Recap calculator grammar -> + -> * -> () -> -> -> -> 0 | 1 | 2 ... | 9 - E.g., - derive 234 from the above grammar - different derivations, but same parse tree, meaning - Can also have - diff. derivations, diff. parse tree, same meaning - e.g., 2+3+4 - diff. derivations, diff. parse tree, diff meaning - e.g., 2+3*4 - Last two cases - make a grammar ambiguous - Solution - either state a disambiguating rule - * has more precedence over + - most languages, except APL - or revise the grammar - always possible - What does "have higher precedence" mean? - "occurs lower in the parse tree" - Grammar revision - introduce multiple "steps" in the non-terminal cascade - worked out solution for 2+3*4 - fix it yourself for 2+3+4 - Overcoming ambiguity of associativity - left-recursive leads to left associativity - right-recursive leads to right associativity