[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

extended basic grammar problem



Hi,

I have this shift/reduce conflict that I have trouble fixing. Any of you that can see how I can avoid this?

shift/reduce conflict in state [stack: XTNewLine *] on TNewLine in {
	[ PStatement = * ] followed by TNewLine (reduce),
	[ XTNewLine = XTNewLine * TNewLine ] (shift)
}

### GRAMMAR BEGIN
Package org.basic;

Helpers
	letter = ['A'..'Z'] | ['a'..'z'];
	digit = ['0'..'9'];
	cr = 13;
	lf = 10;
	tab = 9;
	not_cr_lf = [tab + [[32..127] - [cr + lf]]];
	nl = lf | cr | cr lf;
	colon = ':';
	space = ' ';
	underscore = '_';
	point = '.';

	a = 'a' | 'A'; b = 'b' | 'B'; c = 'c' | 'C'; d = 'd' | 'D'; e = 'e' | 'E';
	f = 'f' | 'F'; g = 'g' | 'G'; h = 'h' | 'H'; i = 'i' | 'I'; j = 'j' | 'J';
	k = 'k' | 'K'; l = 'l' | 'L'; m = 'm' | 'M'; n = 'n' | 'N'; o = 'o' | 'O';
	p = 'p' | 'P'; q = 'q' | 'Q'; r = 'r' | 'R'; s = 's' | 'S'; t = 't' | 'T';
	u = 'u' | 'U'; v = 'v' | 'V'; w = 'w' | 'W'; x = 'x' | 'X'; y = 'y' | 'Y';
	z = 'z' | 'Z';

Tokens
  separator = colon;
  new_line = nl;
  colon_or_nl = colon | nl;
  blank = (tab | space)*;

  // Declarations
  global = g l o b a l;
  local = l o c a l;
  const = c o n s t;
  dim = d i m;
  //
  type = t y p e;
  endtype = e n d ' '? t y p e;
  field = f i e l d;
  new = n e w;
  function = f u n c t i o n;
  endfunction = e n d ' '? f u n c t i o n;
  return = r e t u r n;

  // Flow control
  for = f o r;
  to = t o;
  each = e a c h;
  step = s t e p;
  next = n e x t;
  exit = e x i t;
  //
  repeat =  r e p e a t ;
  until = u n t i l ;
  forever = f o r e v e r ;
  //
  while = w h i l e ;
  wend = w e n d ;
  //
  select = s e l e c t ;
  case = c a s e ;
  default = d e f a u l t ;
  endselect = e n d ' '? s e l e c t ;
  //
  if = i f ;
  then = t h e n ;
  else = e l s e ;
  elseif = e l s e ' '? i f ;
  endif = e n d ' '? i f ;
  //

  // Basic - partially
  first = f i r s t;
  last = l a s t;
  before = b e f o r e;
  after = a f t e r;
  insert = i n s e r t;
  delete = d e l e t e;
  include = i n c l u d e;
  // Maths
  floor = f l o o r; // ()
  ceil = c e i l; // ()
  sgn = s g n; // ()
  abs = a b s; // ()
  sqr = s q r; // ()
  sin = s i n; // ()
  cos = c o s; // ()
  tan = t a n; // ()
  asin = a s i n; // ()
  acos = a c o s; // ()
  atan = a t a n; // ()
  atan2 = a t a n '2'; // ()
  exp = e x p; // ()
  log = l o g; // ()
  log10 = l o g '1' '0'; // ()
  rnd = r n d; // ()
  rand = r a n d; // ()
  seedrnd = s e e d r n d; // ()
  rndseed = r n d s e e d; // ()
  // String
  left = l e f t; // ()
  right = r i g h t; // ()
  mid = m i d; // ()
  replace = r e p l a c e; // ()
  instr = i n s t r; // ()
  upper = u p p e r; // ()
  lower = l o w e r; // ()
  trim = t r i m; // ()
  lset = l s e t; // ()
  rset = r s e t; // ()
  chr = c h r; // ()
  asc = a s c; // ()
  len = l e n; // ()
  hex = h e x; // ()
  bin = b i n; // ()
  string = s t r i n g; // ()
  // Text - partially
  print = p r i n t;
  write = w r i t e;
  locate = l o c a t e;
  text = t e x t;
  // Input - partially
  input = i n p u t; // ()
  keydown = k e y d o w n; // ()
  keyhit = k e y h i t; // ()
  getkey = g e t k e y; // ()
  waitkey = w a i t k e y; // ()
  // Graphics - partially
  graphics = g r a p h i c s;
  endgraphics = e n d ' '? g r a p h i c s;
  flip = f l i p;
  vwait = v w a i t;
  color = c o l o r;
  cls = c l s;
  plot = p l o t;
  line = l i n e;
  rect = r e c t;
  oval = o v a l;
  clscolor = c l s c o l o r;
  getcolor = g e t c o l o r;
  colorred = c o l o r r e d; // ()
  colorgreen =  c o l o r g r e e n; // ()
  colorblue = c o l o r b l u e; // ()
  // Image - partially
  loadimage = l o a d i m a g e;
  freeimage = f r e e i m a g e;
  drawimage = d r a w i m a g e;
  drawblock = d r a w b l o c k; 
  // Time - partially
  millisecs = m i l l i s e c s; // ()
  delay = d e l a y;
  // System - partially
  apptitle = a p p t i t l e;
  end = e n d;
  commandline = c o m m a n d l i n e;
  showpointer = s h o w p o i n t e r;
  hidepointer = h i d e p o i n t e r;

  // Constants
  true = t r u e;     // 1
  false = f a l s e;  // 0
  null = n u l l;     // NULL

  l_parenthese = '(';
  r_parenthese = ')';
  comma = ',';
  dot = point;
  assign = '=';
  backslash = '\';

  // Arithmetic operators
  plus = '+';
  minus = '-';
  tilde = '~';
  power = '^';
  times = '*';
  divide = '/';
  mod = m o d;
  shl = s h l;
  shr = s h r;
  sar = s a r;
  
  // Logical operators
  and = a n d;
  or = o r;
  xor = x o r;
  not = n o t;

  // Post operators (Extended BlitzBasic)
  plusplus = '++';
  minusminus = '--';

  // Boolean operators
  less = '<';
  lessequal = '<=';
  greater = '>';
  greaterequal = '>=';
//  equal = '=';
  notequal = '<>';

  // Type Casts
  integer_type = i n t; // Cast to Integer
  float_type = f l o a t; // Cast to Float
  string_type = s t r;  // Cast to String  

  string_identifier = letter (letter | digit | underscore)* '$';
  float_identifier = letter (letter | digit | underscore)* '#';
  integer_identifier = letter (letter | digit | underscore)* '%'?;
  string_literal = '"' [not_cr_lf - '"']* '"';

  integer_constant = digit+;
  float_constant = digit+ point digit+;

Ignored Tokens
  blank;

Productions
  program = [new_lines]:new_line+ [lines]:section*;
  section = statement [statements]:separated_statement* [new_lines]:new_line*;
  separated_statement = separator statement;

  statement = 
    {dec} declaration |
    {allocation} allocation |
    {assignment} field_access assign expression |
    {for} for for_control |
    {exit} exit |
    {next} next |
    {while} while expression |
    {wend} wend |
    {repeat} repeat |
    {forever} forever |
    {until} until expression |
    {return} return expression |
    {select} select expression |
    {case} case expression |
    {default} default |
    {endselect} endselect |
    {if} if expression then? |
    {else} else |
    {elseif} elseif expression |
    {endif} endif |
    {first} first identifier |
    {last} last identifier |
    {before} before identifier |
    {after} after identifier |
    {insert_before} insert identifier [i_before]:before [placement]:identifier |
    {insert_after} insert identifier [i_after]:after [placement]:identifier |
    {delete} delete identifier |
    {delete_all} delete each identifier |
    {include} include string_literal |
    {print} print expression? |
    {write} write expression? |
    {locate} locate [x]:expression comma [y]:expression |
    {text} text [x]:expression [c1]:comma [y]:expression [c2]:comma [string]:expression optional_center_parameters? |
    {graphics} graphics [width]:expression [c1]:comma [height]:expression [c2]:comma [color_depth]:expression optional_mode_paramenter? |
    {endgraphics} endgraphics |
    {flip} flip [vwait]:expression? |
    {vwait} vwait [frames]:expression? |
    {color} color [red]:expression [c1]:comma [green]:expression [c2]:comma [blue]:expression |
    {cls} |
    {plot} plot [x]:expression comma [y]:expression |
    {line} line [x1]:expression [c1]:comma [y1]:expression [c2]:comma [x2]:expression [c3]:comma [y2]:expression |
    {rect} rect [x1]:expression [c1]:comma [y1]:expression [c2]:comma [width]:expression [c3]:comma [height]:expression comma [solid]:expression |
    {oval} oval [x1]:expression [c1]:comma [y1]:expression [c2]:comma [width]:expression [c3]:comma [height]:expression comma optional_solid_parameter? |
    {clscolor} clscolor [red]:expression [c1]:comma [green]:expression [c2]:comma [blue]:expression |
    {getcolor} getcolor [x]:expression comma [y]:expression |
    {loadimage} loadimage [filename]:expression |
    {freeimage} freeimage [image]:expression |
    {drawimage} drawimage [image]:expression [c1]:comma [x]:expression [c2]:comma [y]:expression optional_frame_parameter? |
    {drawblock} drawblock [image]:expression [c1]:comma [x]:expression [c2]:comma [y]:expression optional_frame_parameter? |
    {delay} delay [millisecs]:expression |
    {apptitle} apptitle [title]:expression optional_close_prompt? |
    {end} end |
    {showpointer} showpointer |
    {hidepointer} hidepointer
    ;

  optional_close_prompt =
    [clc]:comma [close_prompt]:expression;

  optional_frame_parameter =
    [fc]:comma [frame]:expression;

  optional_solid_parameter =
    [sc]:comma [solid]:expression;

  optional_mode_paramenter =
    [mc]:comma [mode]:expression;

  optional_center_parameters =
    [cc]:comma [center_x]:expression comma [center_y]:expression;

  for_control = 
    {to} identifier assign [from_exp]:expression to [to_exp]:expression optional_step? |
    {each} identifier dot [collection_type]:identifier assign each [collection_type2]:identifier;
  
  optional_step = step expression;


  allocation =
    {type_alloc} [variable]:identifier dot [collection_type]:identifier assign new [allocate_type]:identifier;

  declaration =
    {type_dec} type identifier new_line field_declaration_list endtype |
    {function_dec} function identifier l_parenthese formal_param_list? r_parenthese |
    {endfunction} endfunction |
    {const_dec} const identifier initialiser |
    {global_dec} global identifier initialiser? |
    {local_dec} local identifier initialiser? |
    {array_dec} dim identifier l_parenthese expression_list r_parenthese;    

  formal_param_list = identifier comma_identifier*;
  
  comma_identifier = comma identifier;

  field_declaration_list =
    {list} field_declaration_list field_declaration |
    {single} field_declaration;
  
  field_declaration = field formal_param_list new_line;

  initialiser = assign expression;

  expression_list =
    {list} expression comma expression_list |
    {single} expression;

  boolean_literal =
    {true} true |
    {false} false;


  // *** TYPES ***
  field_access =
    {internal_function} function_name l_parenthese expression_list r_parenthese |
    {array_or_func} name array_or_func_access |
    {scalar} name;

  name =
    {simple_name} simple_name |
    {qualified_name} qualified_name;

  simple_name =  
    identifier;

  qualified_name =
    field_access backslash identifier;

  type_name	=
    {integer} integer_type |
    {float} float_type |
    {string} string_type;

  array_or_func_access = l_parenthese expression_list r_parenthese;


  // EXPRESSIONS
  expression =
    {boolexp} bool_expression;

  math_expression =
    {term} term |
    {plus} math_expression plus term |
    {minus} math_expression minus term ;

  term =
    {unary} unary_exp |
    {mult} term times unary_exp |
    {div} term divide unary_exp |
    {mod} term mod unary_exp |
    {power} term power unary_exp |
    {shl} term shl unary_exp |
    {shr} term shr unary_exp |
    {sar} term sar unary_exp;

  unary_exp =
    {preincr} preincr |
    {predecr} predecr |
    {minus} minus unary_exp |
    {plus} plus unary_exp |
    {bitwise_complement} tilde unary_exp |
    {postfix_exp} postfix_exp;

  postfix_exp =
    {factor} factor |
    {postincr} postincr |
    {postdecr} postdecr;

  postdecr = postfix_exp minusminus;

  postincr = postfix_exp plusplus;

  predecr = minusminus postfix_exp;

  preincr = plusplus postfix_exp;

  factor =
    {null} null |
    {fieldaccess} type_name? field_access |
    {constant} type_name? constant |
    {stringliteral} type_name? string_literal |
    {boolliteral} type_name? boolean_literal |
    {expression} type_name? l_parenthese bool_expression r_parenthese;


  identifier =
    {string} string_identifier |
    {float} float_identifier |
    {integer} integer_identifier;

  constant =
    {integer} integer_constant |
    {float} float_constant;

  bool_expression =
    {term} bool_term |
    {or} bool_expression or bool_term |
    {xor} bool_expression xor bool_term;

  bool_term =
    {equality} equality |
    {and} bool_term and equality;

  equality =
    {relational} relational |
//    {eq} equality equal relational |
    {eq} equality assign relational |
    {neq} equality notequal relational;

  relational =
    {math} math_expression |
    {lt} relational less math_expression |
    {gt} relational greater math_expression |
    {lteq} relational lessequal math_expression |
    {gteq} relational greaterequal math_expression;

  function_name = 
    {floor} floor |
    {ceil} ceil |
    {sgn} sgn |
    {abs} abs |
    {sqr} sqr |
    {sin} sin |
    {cos} cos |
    {tan} tan |
    {asin} asin |
    {acos} acos |
    {atan} atan |
    {atan2} atan2 |
    {exp} exp |
    {log} log |
    {log10} log10 |
    {rnd} rnd |
    {rand} rand |
    {seedrnd} seedrnd |
    {rndseed} rndseed |
    {left} left |
    {right} right |
    {mid} mid |
    {replace} replace |
    {instr} instr |
    {upper} upper |
    {lower} lower |
    {trim} trim |
    {lset} lset |
    {rset} rset |
    {chr} chr |
    {asc} asc |
    {len} len |
    {hex} hex |
    {bin} bin |
    {string} string |
    {input} input |
    {keydown} keydown |
    {keyhit} keyhit |
    {getkey} getkey |
    {waitkey} waitkey |
    {colorred} colorred |
    {colorgreen} colorgreen |
    {colorblue} colorblue |
    {millisecs} millisecs |
    {commandline} commandline
    ;
### GRAMMAR END