[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
12.1 Grammar definition
An lalr(1) grammar is defined by the form:
- bigloo syntax: lalr-grammar term-def non-term-def…
-
term-def is a list of terminal elements of the grammar. Terminals can grouped together to form precedence groups by including the related symbols in a sub-lists of the term-def list. Each precedence group must start with one of the keywords
left:
,right:
ornone:
– this indicates the associativity of the terminal symbol. Here is a sample term-def which declares eight terminals:(terminal-1 terminal-2 (left: terminal-3 terminal-4) terminal-5 (right: terminal-6) (none: terminal-7) terminal-8)
In this case,
terminal-3
andterminal-4
both have the same precedence, which is greater than the precedence assigned toterminal-6
. No precedence was assigned to symbolsterminal-1
,terminal-2
,terminal-5
orterminal-8
.Each non-term-def is a list whose first element is the non-terminal being defined, i.e. a symbol. The remaining elements are the production rules associated with this non-terminal. Each rule is a list whose first element is the rule itself (a list of symbols) and the other elements are the semantic actions associated with that particular rule.
For example, consider the following grammar:
E → E1 + id {E.val := E1.val + id.val} | id {E.val := id.val}
With Bigloo, it would be written:
(lalr-grammar (plus id) (e ((e plus id) (+ e id)) ((id) id)))
The semantic value of a symbol in a rule can be accessed by simply using the name of the symbol in the semantic action associated with the rule. Because a rule can contain multiple occurrences of the same symbol, Bigloo provides a way to access these occurrences separately. To do so, the name of each occurrence must be suffixed by
@
var where var is the name of a variable that will be bound to the semantic value of the occurrence. For example, if the rule isifstmt → if E then Stmt else Stmt
then, in Bigloo, it would look like
(if-stmt ((if e then stmt@conseq else stmt@altern) (if (eval e) (eval conseq) (eval altern))))
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.