2013-02-04 19:56:11 -05:00
|
|
|
= Racc Grammar File Reference
|
|
|
|
|
|
|
|
== Global Structure
|
|
|
|
|
|
|
|
== Class Block and User Code Block
|
|
|
|
|
2016-11-08 14:55:59 -05:00
|
|
|
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.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
== Comments
|
2013-02-04 19:56:11 -05:00
|
|
|
|
2016-11-08 14:55:59 -05:00
|
|
|
You can insert comments about all places. Two styles of comments can be used, Ruby style '#.....' and C style '/\*......*\/'.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
== Class Block
|
|
|
|
|
|
|
|
The class block is formed like this:
|
|
|
|
|
|
|
|
class CLASS_NAME
|
|
|
|
[precedance table]
|
2014-01-24 01:15:15 -05:00
|
|
|
[token declarations]
|
|
|
|
[expected number of S/R conflicts]
|
2013-02-04 19:56:11 -05:00
|
|
|
[options]
|
2015-09-13 22:12:12 -04:00
|
|
|
[semantic value conversion]
|
2013-02-04 19:56:11 -05:00
|
|
|
[start rule]
|
|
|
|
rule
|
|
|
|
GRAMMARS
|
|
|
|
|
2016-11-08 14:55:59 -05:00
|
|
|
CLASS_NAME is a name of the parser class. This is the name of the generating parser
|
2014-01-24 01:15:15 -05:00
|
|
|
class.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
2016-11-08 14:55:59 -05:00
|
|
|
If CLASS_NAME includes '::', Racc outputs the module clause. For example, writing
|
|
|
|
"class M::C" causes the code below to be created:
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
module M
|
|
|
|
class C
|
|
|
|
:
|
|
|
|
:
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
== Grammar Block
|
|
|
|
|
2016-11-08 14:55:59 -05:00
|
|
|
The grammar block describes grammar which is able to be understood by the parser.
|
2014-01-24 01:15:15 -05:00
|
|
|
Syntax is:
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
(token): (token) (token) (token).... (action)
|
|
|
|
|
|
|
|
(token): (token) (token) (token).... (action)
|
|
|
|
| (token) (token) (token).... (action)
|
|
|
|
| (token) (token) (token).... (action)
|
|
|
|
|
|
|
|
(action) is an action which is executed when its (token)s are found.
|
|
|
|
(action) is a ruby code block, which is surrounded by braces:
|
|
|
|
|
|
|
|
{ print val[0]
|
|
|
|
puts val[1] }
|
|
|
|
|
|
|
|
Note that you cannot use '%' string, here document, '%r' regexp in action.
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
Actions can be omitted. When it is omitted, '' (empty string) is used.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
2016-11-08 14:55:59 -05:00
|
|
|
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.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
2016-11-08 14:55:59 -05:00
|
|
|
Here is an example of the whole grammar block.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
rule
|
2014-01-24 01:15:15 -05:00
|
|
|
goal: definition rules source { result = val }
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
definition: /* none */ { result = [] }
|
|
|
|
| definition startdesig { result[0] = val[1] }
|
|
|
|
| definition
|
2014-01-24 01:15:15 -05:00
|
|
|
precrule # this line continues from upper line
|
2013-02-04 19:56:11 -05:00
|
|
|
{
|
|
|
|
result[1] = val[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
startdesig: START TOKEN
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
You can use the following special local variables in action:
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
* result ($$)
|
|
|
|
|
2016-11-08 14:55:59 -05:00
|
|
|
The value of the left-hand side (lhs). A default value is val[0].
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
* val ($1,$2,$3...)
|
|
|
|
|
2016-11-08 14:55:59 -05:00
|
|
|
An array of value of the right-hand side (rhs).
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
* _values (...$-2,$-1,$0)
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
A stack of values. DO NOT MODIFY this stack unless you know what you are doing.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
2013-11-09 10:34:25 -05:00
|
|
|
== Operator Precedence
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
This function is equal to '%prec' in yacc.
|
|
|
|
To designate this block:
|
|
|
|
|
|
|
|
prechigh
|
|
|
|
nonassoc '++'
|
|
|
|
left '*' '/'
|
|
|
|
left '+' '-'
|
|
|
|
right '='
|
|
|
|
preclow
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
`right` is yacc's %right, `left` is yacc's %left.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
`=` + (symbol) means yacc's %prec:
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
prechigh
|
|
|
|
nonassoc UMINUS
|
|
|
|
left '*' '/'
|
|
|
|
left '+' '-'
|
|
|
|
preclow
|
|
|
|
|
|
|
|
rule
|
|
|
|
exp: exp '*' exp
|
|
|
|
| exp '-' exp
|
|
|
|
| '-' exp =UMINUS # equals to "%prec UMINUS"
|
|
|
|
:
|
|
|
|
:
|
|
|
|
|
|
|
|
== expect
|
|
|
|
|
|
|
|
Racc has bison's "expect" directive.
|
|
|
|
|
|
|
|
# Example
|
|
|
|
|
|
|
|
class MyParser
|
|
|
|
rule
|
|
|
|
expect 3
|
|
|
|
:
|
|
|
|
:
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
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.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
== Declaring Tokens
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
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:
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
token TOKEN_NAME AND_IS_THIS
|
|
|
|
ALSO_THIS_IS AGAIN_AND_AGAIN THIS_IS_LAST
|
|
|
|
|
|
|
|
== Options
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
You can write options for Racc command in your Racc file.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
options OPTION OPTION ...
|
|
|
|
|
|
|
|
Options are:
|
|
|
|
|
|
|
|
* omit_action_call
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
omits empty action call or not.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
* result_var
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
uses local variable "result" or not.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
You can use 'no_' prefix to invert their meanings.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
== Converting Token Symbol
|
|
|
|
|
|
|
|
Token symbols are, as default,
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
* naked token string in Racc file (TOK, XFILE, this_is_token, ...)
|
2013-02-04 19:56:11 -05:00
|
|
|
--> symbol (:TOK, :XFILE, :this_is_token, ...)
|
|
|
|
* quoted string (':', '.', '(', ...)
|
|
|
|
--> same string (':', '.', '(', ...)
|
|
|
|
|
|
|
|
You can change this default by "convert" block.
|
|
|
|
Here is an example:
|
|
|
|
|
|
|
|
convert
|
|
|
|
PLUS 'PlusClass' # We use PlusClass for symbol of `PLUS'
|
|
|
|
MIN 'MinusClass' # We use MinusClass for symbol of `MIN'
|
|
|
|
end
|
|
|
|
|
|
|
|
We can use almost all ruby value can be used by token symbol,
|
2014-01-24 01:15:15 -05:00
|
|
|
except 'false' and 'nil'. These cause unexpected parse error.
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
If you want to use String as token symbol, special care is required.
|
|
|
|
For example:
|
|
|
|
|
|
|
|
convert
|
|
|
|
class '"cls"' # in code, "cls"
|
|
|
|
PLUS '"plus\n"' # in code, "plus\n"
|
|
|
|
MIN "\"minus#{val}\"" # in code, \"minus#{val}\"
|
|
|
|
end
|
|
|
|
|
|
|
|
== Start Rule
|
|
|
|
|
|
|
|
'%start' in yacc. This changes start rule.
|
|
|
|
|
|
|
|
start real_target
|
|
|
|
|
|
|
|
== User Code Block
|
|
|
|
|
2014-01-24 01:15:15 -05:00
|
|
|
"User Code Block" is a Ruby source code which is copied to output. There are
|
|
|
|
three user code blocks, "header" "inner" and "footer".
|
2013-02-04 19:56:11 -05:00
|
|
|
|
|
|
|
Format of user code is like this:
|
|
|
|
|
|
|
|
---- header
|
|
|
|
ruby statement
|
|
|
|
ruby statement
|
|
|
|
ruby statement
|
|
|
|
|
|
|
|
---- inner
|
|
|
|
ruby statement
|
|
|
|
:
|
|
|
|
:
|
|
|
|
|
2015-10-26 09:23:32 -04:00
|
|
|
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.
|