Here you find information on the basics of writing a formal grammar:Rules Groups Alternatives Optionality Repetitions RulesA rule of the formal grammar describes a constituent, while consisting of constituents itself. A rule might look like this: Goal = Command Object ';' This rule describes the costituent ''Goal''. The rule consists of
three constituents, of which the first and second are, as far as Yuck is concerned, non-terminal constituents. This means, they can be expanded further, by use of more rules. The third constituent is a terminal
constituent, meaning that this is a constituent that occurs in the input just like it is represented in the rule definition.Other rules of this grammar could be these: Command = OPEN | CLOSEObject = “Door” | “Window” It should be fairly obvious that
of the rules of a grammar one is a special rule, i.e. the start rule. Yuck allows only one start rule per grammar. Groups
Each rule consists of one or more consituents. These can be arranged in several ways. You can group constituents, have alternatives between such
groups, declare a group optional, or have a number of repetitions of a group. The smallest group possible is a single constituent. If
you want to declare several constituents a rule, you have to enclose these constituents in brackets. This is an example for a rule with two groups of constituents: Command = Command | (BEGIN Command+ END) When writing your grammar you will come to the point where you have to define
different forms of a rule. For example, you may want to define a programming language, and a command can either be a single command (along with some parameters), or a for-next loop, where you don't want any parameters,
but an enclosed body of commands. So what you need here is a means to define alternatives in your grammar. Object = “Door” | “Window”
This rule defines, that an OBJECT is either a “Window” or a “Door”, but not both.Alternatives You have to be careful when using alternatives to be clear in
your rule writing. Whenever you want an alternative between more than two constituents, it is a good idea to enclose this group in brackets:
Object = (“The” “Cat”) | (“Door” | “Window”) The parser generator will not produce an error message if you happen to leave out
one (or both) of the pairs of brackets. However, what the grammar describes is not defined. You will end up with a working parser, which may
even work fine for you. But there is no guarantee that the next version of Yuck will treat your (unclear) grammar the same way the current version does.Optionality You can declare any group as being optional. That is, it may be present int the input or not, but in both ways the rule can be processed with this input.
This is done using the question mark operator:
Command123 = SET? VarName '=' _number ';' With this rule, both “Set a = 10;” and “a = 10;” will match the rule. You can declare any
group optional by putting the question mark right after the closing bracket of this group. Be sure that there are no blanks, newlines or anything between the end of the group (i.e. the close bracket or the last
character of the constituent, if your group is only one constituent).Repetitions You will need to
declare that a group may occur more than once when writing a complex grammar. Consider a simple batch script: this is a repetition of a rule describing a command. So, you can write: Batch = Command* This will make anything, ranging from no command at all to any number of
commands a valid input.If you feel like a batch script should consist of at least one command, you have to write: Batch = Command+ This will force Yuck to accept the input if there is at least one command. Please note that something like “Command+?” is not possible. This
would mean at least one command, but at the same time render the constituent optional, thus meaning no command, or one command, or any number of command. In this case, you have to use the asterisk.
|