![]() |
CS 15-212: Fundamental Structures of Computer Science II |
Context-free grammars arise naturally in a variety of applications. The underlying machine for a context-free language is a pushdown automaton, which maintains a read-write stack that allows the machine to "count". Shift-reduce parsing uses a stack to delay application of rewrite rules, enabling operator precedence to be enforced. Recursive descent parsing is another style that uses recursion in a way that mirrors the grammar productions. Although parser generator tools exist for restricted classes of grammars, a direct implementation can allow greater flexibility and better error handling. An example is presented of a shift-reduce and recursive descent parser for a grammar of arithmetic expressions.