1 # Copyright (C) 2005-2007, The Perl Foundation.
4 =head1 ***** WARNING *****
6 =head1 This is an ALPHA pod! The contents herein may not reflect reality. :)
10 docs/optable.pod - PGE operator precedence table and parser
18 PGE::OPTable is the bottom up shift/reduce style parser component of the
19 Parrot Grammar Engine (PGE) suite. PGE is a Parrot implementation of Perl6
22 =head1 FUTURE CONSIDERATIONS
26 =item - Shift reduce application to more general grammar productions than just
29 =item - Static state machine transition table generation. (Optimization)
31 =item - tighter and looser should work even when their argument operator
32 hasn't been defined yet.
40 An operator is a most often mathematical function usually taking one or two
41 arguments(operands, also called terms) and returning a calculated result.
42 Operators are often characterized as ( pure, no side effects ). Obvious
43 exceptions to this rule are increment(++) and decrement(--) operators and
44 assignment operators such as +=, *=, etc.
46 Operators which have one operand are called unary operators. Unary operator
47 symbols may appear in the prefix position (in front of the term) or in the
48 postfix position(following the term. Binary operators have two operators.
49 Binary operator symbols usually appear in the infix position between the two
50 operands. Ternary operators also exist, such as the C style ternary
51 conditional operator C< expression ? true case : false case>. See Synopsis 5
52 Rules for more information.
56 A expression is a combination of operators, operands(terms such as variables
57 and values) and grouping symbols that describe a computation. Expressions
62 A term is the atomic unit which a operator operates on. Operand is the more
63 formal mathematical term for term. :) In OPTable parsed expressions a term is
64 a variable or primitive value.
68 Precedence is the order in which operators are evaluated. Higher precedence
69 operators are evaluated before lower precedence operators.
71 =head2 "precedence level"
73 Computer languages usually have a table of operator precedences. Operators at
74 the top of the table have higher precedence than those below. Operators at
75 the same vertical level in the table have equivalent or equal precedence. An
76 operators level in the table is called its precedence level. OPTable uses
77 integers to signify precedence. The greater the OPTable precedence integer
78 the higher the precedence level of the operator. An operator with precedence
79 level 22 has higher precedence that an operator with precedence level 5 and
80 will be evaluated first.
90 proto OPERATOR_NAME ADVERBIAL_CLAUSES* { ... }
92 The C<grammar> statement at the top of is the namespace in which the optable
93 will be generated. Written in Perl6 style the C<grammar> statement is
94 translated by PGE into valid Parrot namespace syntax.
96 The C<proto> statement declares a operator to be added to the precedence table
97 and parser. All operator attributes are defined using the Perl6 adverbial
98 style of C<is ADVERB()>. Adverbial clauses are separated by white space.
99 Adverbs can have 0-arity in which case they can be written without
100 parenthesis. Adverbial arguments are written comma separated in parenthesis.
102 =head2 "Adverbial Precedence Clauses"
106 =item - is precedence(PRECEDENCE_LEVEL_STRING)
108 C<is precedence> takes a single string argument which contains the precedence
109 level for the current operator that C<is precedence> is modifying. The C<is
110 precedence> argument string is formatted as a integer precedence level
111 followed by equals sign. e.g. '22='
113 =item - is tighter(PREVIOUSLY_DEFINED_OPERATOR_NAME)
115 C<is tighter> takes a B<previously> defined operator name as its single
116 argument. The argument operator's precedence level plus 1 is then used as the
117 precedence level for the current operator that C<is tighter> is modifying.
119 =item - is looser(PREVIOUSLY_DEFINED_OPERATOR_NAME)
121 C<is looser> takes a B<previously> defined operator name as its single
122 argument. The argument operator's precedence level minus 1 is then used as
123 the precedence level for the current operator that C<is looser> is modifying.
125 =item - is equiv(PREVIOUSLY_DEFINED_OPERATOR_NAME)
127 C<is equiv> takes a B<previously> defined operator name as its single
128 argument. The argument operator's precedence level is used as the precedence
129 level for the current operator that C<is equiv> is modifying.
131 =item - is assoc(DESCRIPTION)
133 DESCRIPTION can be one of 'list', 'left', 'right', 'non', or 'chain'.
135 C<is assoc> declares the associativity of the operator. The absence of a C<is
136 assoc> adverb indicates that the operation is associative or that the order of
137 evaluation of two or more instances of an operator in an expression is
138 unimportant. C<'left'> signifies left association; evaluation should occur
139 from the left. Conversely, C<'right'> signifies right association; evaluation
140 should occur from the right. C<'non'> declares that this operator doesn't
141 strongly associate to the left or right. C<'list'> specifies that the
142 operator is associated as a list context. C<'chain'> declares chained
143 association such as C<a = b = c = 10> or C< a < 10 < b>.
148 =head2 "Adverbial Clauses"
154 PGE::OPTable normally generates the parsing code for an operator based on the
155 operator name which usually consists of the operators orientation followed by
156 a colon and then the operator symbol. In Perl6 'infix:*' is an example
157 operator name of the infix multiplication operator. 'infix:x' likewise
158 represents the Perl6 repeat operator. The C<is parsed> adverb declares that
159 this particular operator is parsed using the Perl6 match conforming method
160 specified as the adverbs argument instead of auto-generated code based off of
163 =item - is pastrule()
165 The C<is pastrule> adverb defines the pastrule attribute of a operator.
166 During later processing by the Tree Grammar Engine(TGE) compiler tool, the
167 pastrule attribute can be used to specify custom TGE processing.
169 TODO: needs concrete example
173 The C<is post> adverb specifies the Parrot opcode that implements the
174 semantics of this particular operator. C<is post('add')> is used to annotate
175 the 'infix:+' operator in languages where the infix + symbol denotes addition.
179 The C<is expect> adverb used to specify the hexadecimal identifier of the next
180 token OPTable should expect???
184 The C<is returns> adverb specifies the type of the result for this operator.
185 TGE can use this attribute to construct correctly typed temporary to hold the
186 intermediate results of operations for later use or combination.
190 The C<is pir> adverb specifies a code generation emit string that can be used
191 during code generation. Parrot assembly is emitted as Parrot Intermediate
192 Representation(PIR). Hence the adverb name pir. The argument string is a
193 format string, after the C style printf format string. C<%r> represents the
194 result of the operation. C<%0>, C<%1>, etc represent the operands of the
195 operator. proto 'infix:~&' is equiv('infix:*') is pir(" %r = bands %0, %1") {
201 The C<is nullterm> adverb specifies that the operator is an 0-arity function
202 that doesn't take any operands.
205 TODO: needs help, stolen shamelessly from compilers/pge/PGE/OPTable.pir
207 The C<is stop> adverb declares a string to be matched directly or a sub(rule)
208 to be called to check for a match.
212 =head1 IMPLEMENTATION
214 TODO: how it works inside :)
216 =head1 LANGUAGE NOTES
222 languages/perl6/src/grammar_optok.pg
223 languages/cardinal/src/cardinal_optok.pg
235 http://en.wikipedia.org/wiki/Order_of_operations
236 http://en.wikipedia.org/wiki/Associativity
237 http://en.wikipedia.org/wiki/Bottom-up_parsing
238 http://www.ozonehouse.com/mark/blog/code/PeriodicTable.html
239 http://dev.perl.org/perl6/doc/design/syn/S03.html
243 languages/perl6/src/PAST.pir
244 languages/perl6/src/POST.pir