JezK
Edit File: grammar2.en.html
<?xml version="1.0" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>doc/en/grammar2.en</title> </head> <body> <h1><a name="label-0" id="label-0">Racc Grammar File Reference</a></h1><!-- RDLabel: "Racc Grammar File Reference" --> <h2><a name="label-1" id="label-1">Global Structure</a></h2><!-- RDLabel: "Global Structure" --> <h2><a name="label-2" id="label-2">Class Block and User Code Block</a></h2><!-- RDLabel: "Class Block and User Code Block" --> <p>There are two blocks on the toplevel. One is the 'class' block, the other is the 'user code' block. The 'user code' block MUST be placed after the 'class' block.</p> <h2><a name="label-3" id="label-3">Comments</a></h2><!-- RDLabel: "Comments" --> <p>You can insert comments about all places. Two styles of comments can be used, Ruby style '#.....' and C style '/\*......*\/'.</p> <h2><a name="label-4" id="label-4">Class Block</a></h2><!-- RDLabel: "Class Block" --> <p>The class block is formed like this:</p> <pre>class CLASS_NAME [precedence table] [token declarations] [expected number of S/R conflicts] [options] [semantic value conversion] [start rule] rule GRAMMARS</pre> <p>CLASS_NAME is a name of the parser class. This is the name of the generating parser class.</p> <p>If CLASS_NAME includes '::', Racc outputs the module clause. For example, writing "class M::C" causes the code below to be created:</p> <pre>module M class C : : end end</pre> <h2><a name="label-5" id="label-5">Grammar Block</a></h2><!-- RDLabel: "Grammar Block" --> <p>The grammar block describes grammar which is able to be understood by the parser. Syntax is:</p> <pre>(token): (token) (token) (token).... (action) (token): (token) (token) (token).... (action) | (token) (token) (token).... (action) | (token) (token) (token).... (action)</pre> <p>(action) is an action which is executed when its (token)s are found. (action) is a ruby code block, which is surrounded by braces:</p> <pre>{ print val[0] puts val[1] }</pre> <p>Note that you cannot use '%' string, here document, '%r' regexp in action.</p> <p>Actions can be omitted. When it is omitted, '' (empty string) is used.</p> <p>A return value of action is a value of the left side value ($$). It is the value of the result, or the returned value by `return` statement.</p> <p>Here is an example of the whole grammar block.</p> <pre>rule goal: definition rules source { result = val } definition: /* none */ { result = [] } | definition startdesig { result[0] = val[1] } | definition precrule # this line continues from upper line { result[1] = val[1] } startdesig: START TOKEN</pre> <p>You can use the following special local variables in action:</p> <ul> <li>result ($$)</li> </ul> <p>The value of the left-hand side (lhs). A default value is val[0].</p> <ul> <li>val ($1,$2,$3...)</li> </ul> <p>An array of value of the right-hand side (rhs).</p> <ul> <li>_values (...$-2,$-1,$0)</li> </ul> <p>A stack of values. DO NOT MODIFY this stack unless you know what you are doing.</p> <h2><a name="label-6" id="label-6">Operator Precedence</a></h2><!-- RDLabel: "Operator Precedence" --> <p>This function is equal to '%prec' in yacc. To designate this block:</p> <pre>prechigh nonassoc '++' left '*' '/' left '+' '-' right '=' preclow</pre> <p>`right` is yacc's %right, `left` is yacc's %left.</p> <p>`=` + (symbol) means yacc's %prec:</p> <pre>prechigh nonassoc UMINUS left '*' '/' left '+' '-' preclow rule exp: exp '*' exp | exp '-' exp | '-' exp =UMINUS # equals to "%prec UMINUS" : :</pre> <h2><a name="label-7" id="label-7">expect</a></h2><!-- RDLabel: "expect" --> <p>Racc has bison's "expect" directive.</p> <pre># Example class MyParser rule expect 3 : :</pre> <p>This directive declares "expected" number of shift/reduce conflicts. If "expected" number is equal to real number of conflicts, Racc does not print conflict warning message.</p> <h2><a name="label-8" id="label-8">Declaring Tokens</a></h2><!-- RDLabel: "Declaring Tokens" --> <p>By declaring tokens, you can avoid many meaningless bugs. If declared token does not exist or existing token does not decleared, Racc output warnings. Declaration syntax is:</p> <pre>token TOKEN_NAME AND_IS_THIS ALSO_THIS_IS AGAIN_AND_AGAIN THIS_IS_LAST</pre> <h2><a name="label-9" id="label-9">Options</a></h2><!-- RDLabel: "Options" --> <p>You can write options for Racc command in your Racc file.</p> <pre>options OPTION OPTION ...</pre> <p>Options are:</p> <ul> <li>omit_action_call</li> </ul> <p>omits empty action call or not.</p> <ul> <li>result_var</li> </ul> <p>uses local variable "result" or not.</p> <p>You can use 'no_' prefix to invert their meanings.</p> <h2><a name="label-10" id="label-10">Converting Token Symbol</a></h2><!-- RDLabel: "Converting Token Symbol" --> <p>Token symbols are, as default,</p> <ul> <li>naked token string in Racc file (TOK, XFILE, this_is_token, ...) --> symbol (:TOK, :XFILE, :this_is_token, ...)</li> <li>quoted string (':', '.', '(', ...) --> same string (':', '.', '(', ...)</li> </ul> <p>You can change this default by "convert" block. Here is an example:</p> <pre>convert PLUS 'PlusClass' # We use PlusClass for symbol of `PLUS' MIN 'MinusClass' # We use MinusClass for symbol of `MIN' end</pre> <p>We can use almost all ruby value can be used by token symbol, except 'false' and 'nil'. These cause unexpected parse error.</p> <p>If you want to use String as token symbol, special care is required. For example:</p> <pre>convert class '"cls"' # in code, "cls" PLUS '"plus\n"' # in code, "plus\n" MIN "\"minus#{val}\"" # in code, \"minus#{val}\" end</pre> <h2><a name="label-11" id="label-11">Start Rule</a></h2><!-- RDLabel: "Start Rule" --> <p>'%start' in yacc. This changes start rule.</p> <pre>start real_target</pre> <h2><a name="label-12" id="label-12">User Code Block</a></h2><!-- RDLabel: "User Code Block" --> <p>"User Code Block" is a Ruby source code which is copied to output. There are three user code blocks, "header" "inner" and "footer".</p> <p>Format of user code is like this:</p> <pre>---- header ruby statement ruby statement ruby statement ---- inner ruby statement : :</pre> <p>If four '-' exist on the line head, Racc treats it as the beginning of the user code block. The name of the user code block must be one word.</p> </body> </html>