The MiniJava Parser
October 20/21, 2003
[152 Home]    [Parser Implementation]    [Shift-Reduce Conflicts]    [Resources]

Parser Implementation
  1. Classification of Non-Terminals:
    • Program, MainClass, ClassDecl, VarDecl, MethodDecl, Type, Statement, Exp
    • FormalList, FormalRest, ExpList, ExpRest
    • ClassDeclSeq, VarDeclSeq, MethodDeclSeq, StatementSeq (not defined in the grammar!!!)

  2. Output Specification: Produce a log of actions for Non-Terminals in Group 1. (spec.html)
    For Example:
    ClassDecl : CLASS ID '{' VarDeclSeq MethodDeclSeq '}             { cout << "CLASSDECL1\n"; }
              | CLASS ID EXTENDS ID '{' VarDeclSeq MethodDeclSeq '}' { cout << "CLASSDECL2\n"; }
              ;

Shift Reduce Conflicts
  1. When the parser knows the entire stack contents and the next input symbol, but still cannot decide whether to shift or to reduce, we say there is a shift-reduce conflict.

    stmt -> if expr then stmt
          | if expr then stmt else stmt
          | other

    Stack                         Input
        ... if expr then stmt     else ...$


    Note: This grammar is not LR(1). More generally, no ambiguous grammar can be LR(k) for any k.

  2. Bison is designed to resolve these conflicts by choosing to shift, unless otherwise directed by operator precedence declarations.

Resources
  1. The MiniJava Project
  2. man flex
  3. man bison
  4. Textbook: pp. 213 - 215, pp. 263 - 264