Lecture 1b: Shift/Reduce Parsing, Attribute Grammars and yacc/bison

Background Material



Just like it was possible to automatically construct a scanner from a regular expression and hence produce a scanner generator like flex, it is possible to automatically produce a parser from a contect free grammar. Typically some restrictions are placed on the grammar (e.g. single look ahead) to make it easier to generate the parser. Most parser generators use a bottom up approach to parsing. The parser generator we will discuss, yacc/bison, uses a technique called shift-reduce parsing. The restrictions on the allowed grammars are less severe than those needed for a recursive descent parser, and some features have been added to allow ambigous grammars.


  1. Introduction to shift/reduce parsing and yacc/bison
  2. Building a parser from bison/flex specification for the grammar and scanner. Given the files paren.y and paren.l containing the bison and flex inputs. Notice how the file paren.l includes paren.tab.h. This is an include file generated from bison from the file paren.y and is used so that the parser and scanner can communicate.
  3. Attribute grammars (inherited and synthesized attributes)
  4. Use of union for multiple attribute types
  5. Example grammars and yacc/bison input
    1. Regular grammar (see reg-gram.y and reg-gram.l) for the regular expression (a|b)*abb (see reg.l).
    2. Grammar for numbers: number.y and number.l
    3. Grammar for balanced parentheses: paren.y and paren.l
    4. Grammar for lists: list.y and list.l
    5. Ambiguous grammar for expressions: exp.y and exp.l
    6. Grammar for expressions (using yacc precedence rules to remove the ambiguity: exp1.y and exp1.l
    7. Unambiguous grammar for expressions: exp2.y and exp2.l
    8. Ambigous Grammar for if statements (dangling else): if.y and if.l
    9. Ambigous Grammar for if statements (disambiguate with precedence): if1.y and if1.l
    10. Unambigous Grammar for if statements (else matches closest if): if2.y and if2.l
    11. Unambigous Grammar for if statements (change grammar to include BEGIN/END): if3.y and if3.l
    12. Example of use of union for multiple return types. union-example.y and union-example.l

    Several of these examples are written in Python

References and programs


Created: April 3, 2008 (updated April 1, 2010) by jjohnson AT cs DOT drexel DOT DOT edu