Related articles |
---|
What's wrong with my grammar definition in sablecc, who can help me? pointerkarajan@gmail.com (zheng yang) (2010-05-09) |
Re: What's wrong with my grammar definition in sablecc, who can help m gneuner2@comcast.net (George Neuner) (2010-05-11) |
Re: What's wrong with my grammar definition in sablecc, who can help m pointerkarajan@gmail.com (zheng yang) (2010-05-14) |
Re: What's wrong with my grammar definition in sablecc, who can help m gneuner2@comcast.net (George Neuner) (2010-05-15) |
From: | zheng yang <pointerkarajan@gmail.com> |
Newsgroups: | comp.compilers |
Date: | Sun, 9 May 2010 19:38:03 -0700 (PDT) |
Organization: | Compilers Central |
Keywords: | parse, question |
Posted-Date: | 10 May 2010 01:23:37 EDT |
When I generate the parer use sablecc with the custom grammar, so many
reduce/shift conflicts comes up, I have modified it, but the error
still there, how can fix it, please help me. below is the grammar
file.
// ************************ Helpers
************************************
Helpers
all = [0..0xffff];
space = ' ';
lf = 0x000a; // line feed
cr = 0x000d; // carriage return
ff = 0x000c; // form feed
ht = 0x0009; // tab
new_line = lf | cr | cr lf ;
not_cr_lf = [all - [cr + lf]];
letter = ['a' .. 'z'] | ['A'..'Z'];
digit = ['0'..'9'];
States
normal;
// ************************ Tokens
*************************************
Tokens
// Example token:
// Do not change the white_space token below (LexerDriver uses
it).
white_space = (space | ht | ff | new_line)*;
// ********************** Comments
*************************************
sl_comment = '//'not_cr_lf* new_line;
// ********************** Keywords
*************************************
k_class = 'class';
l_curly = '{';
k_const = 'const';
semicolon = ';';
k_void = 'void';
k_int = 'int';
k_char = 'char';
k_bool = 'bool';
k_float = 'float';
k_string = 'string';
k_if = 'if';
k_else = 'else';
k_return = 'return';
k_while = 'while';
k_true = 'true';
k_false = 'false';
l_brace = '{';
r_brace = '}';
k_equal = '=';
l_bracket = '[';
r_bracket = ']';
l_par = '(';
r_par = ')';
dot = '.';
comma = ',';
bar_bar = '||';
ampersand_ampersand = '&&';
lt = '<';
lteq = '<=';
gt = '>';
gteq = '>=';
eqeq = '==';
neq = '!=';
plus = '+';
minus = '-';
star = '*';
div = '/';
mod = '%';
excl_mark = '!';
// ********************** Literals
*************************************
identifier = letter (letter | digit)*;
stringlit = '"' [not_cr_lf - ''']+ '"';
integerlit = digit+;
Ignored Tokens
sl_comment,
white_space;
// ********************** Operators
************************************
Productions
program = class_decls*;
class_decls = {class_declars} class_decl class_decls|
{class_declare_single} class_decl;
class_decl = {class_declaration}class_hdr l_brace member_decls*
r_brace;
class_hdr = k_class identifier;
member_decls = {member_declars}member_decls member_decl |
{member_declare_single}member_decl;
member_decl = {field_declaration}field_decl | {method_declaration}
method_decl | semicolon ;
field_decl = {type_id}type identifier semicolon | {assignment}type
identifier k_equal expr semicolon |{array}type identifier l_bracket
integerlit r_bracket;
type = {type_int} k_int | {type_char} k_char | {type_bool} k_bool |
{type_string} k_string | {type_float} k_float;
method_decl = {method_argvs}method_hdr l_par formals+ r_par block |
{method_no_argvs} method_hdr l_par r_par block;
method_hdr = {void_id}k_void identifier | {type_id}type identifier;
formals = {fomal_single}formal | {fomal_multiple} formals formal;
formal = {type_id}type identifier | {fomal_array}type identifier
l_bracket r_bracket;
block = {block_stmts}l_brace stmts+ r_brace |{block_no_stmts}l_brace
r_brace;
stmts = stmt*;
stmt = {stmt_single}simple_stmt | {stmt_condition}k_if condition
k_else stmt;
condition = l_par expr r_par;
local_decl = {type_id}type identifier semicolon |{id_equ_expr}type
identifier k_equal expr semicolon | {array}type identifier l_bracket
integerlit r_bracket semicolon;
simple_stmt = {local}local_decl | {field_assignment}field_access
k_equal expr semicolon | {method_caller} method_call semicolon |
{return_void} k_return semicolon | {return_expr} k_return expr
semicolon | {new_block}block | {while_loop}k_while condition block |
semicolon ;
field_access = {id_field} identifier | {id_id_field}
[caller]:identifier dot [callee]:identifier | {arr_ref_field}
array_ref;
array_ref = {id_array}identifier l_bracket expr r_bracket |
{id_field_array} [caller]:identifier dot [callee]:identifier l_bracket
expr r_bracket;
method_call = {call_no_argv}identifier l_par r_par | {call_argv}
identifier l_par args r_par | {call_field_no_argv} [outer]:identifier
dot [inner]:identifier l_par r_par| {call_field_argv}
[outer]:identifier dot [inner]:identifier l_par args r_par;
args = {args_expr}expr | {args_commar_expr} args comma expr;
expr = {expr_bar_bar}expr bar_bar e2 | {expr_double_ampersand}expr
ampersand_ampersand e2 | e2;
e2 = {less_than}[left]:e3 lt [right]:e3 |{greater_than} [left]:e3 gt
[right]:e3 | {less_than_equ}[left]:e3 lteq [right]:e3 |
{greater_than_equ}[left]:e3 gteq [right]:e3 | {equ_equ}[left]:e3 eqeq
[right]:e3| {not_equ}[left]:e3 neq [right]:e3 | {e3_single}e3;
e3 = {plus} e3 plus e4 | {minus} e3 minus e4 | e4;
e4 = {multiply}e4 star e5 |{divide} e4 div e5 | {mode}e4 mod e5 | e5;
e5 = {not_mark}excl_mark e5 |{plus_pre} plus e5 | {minus_pre} minus e5
| primary;
primary = {field_acc}field_access | {method_caller}method_call |
{int_lit}integerlit | {str_lit}stringlit | {true_word}k_true |
{false_word}k_false | {expr_par}l_par expr r_par;
Return to the
comp.compilers page.
Search the
comp.compilers archives again.