Exercise 28:

Conflict resolution: two valid alternatives with the same lookahead
We have the following grammar for variable declarations:
  var_declarations^: var_declaration*;
  var_declaration: TYPE^ IDENT '='! expr ';'!;
  expr: ordinal_expr | structure;

  ordinal_expr: enumeration | range;
  enumeration^: '('! id_list ')'!;
  id_list: IDENT (','! IDENT)*;
  range: constant '...'^ constant;
  constant: NATURAL_LIT | IDENT;

  structure: list | set;
  list^: '['! term (','! term)* ']'!;
  set^: '{'! term (','! term)* '}'!;
  term: ordinal_expr | IDENT;

  TYPE: 'ord' | 'list' | 'set';
  IDENT: ('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
  NATURAL_LIT: ('0'..'9')+;
However, we have detected a problem: the second alternative of the term variable is never executed. This happens because IDENTFirst(\in\text{First}(ordinal_expr)). Consequently, when the parser has to parse the variable term and has IDENT as lookahead, it chooses the ordinal_expr alternative because it appears first. Fix the grammar so that it has the desired behavior, i.e., that IDENT can be consumed inside term. The AST construction must not be altered.
Authors: Nil Mamano / Documentation:
To be able to submit you need to either log in, register, or become a guest.