(→Context-free Grammars) |
|||
Line 19: | Line 19: | ||
Even though we gave a formal definition of grammar, seldom will we use described in that form. Most grammars are described in [[wikipedia:Bakus-Naur Form|Bakus-Naur Form]] (BNF), [[wikipedia:Extended Backus–Naur Form|Extended Backus–Naur Form]] (EBNF), or other [[wikipedia:metasyntax|metasyntax]]. | Even though we gave a formal definition of grammar, seldom will we use described in that form. Most grammars are described in [[wikipedia:Bakus-Naur Form|Bakus-Naur Form]] (BNF), [[wikipedia:Extended Backus–Naur Form|Extended Backus–Naur Form]] (EBNF), or other [[wikipedia:metasyntax|metasyntax]]. | ||
− | Consider the following example | + | Consider the following example grammar: |
E -> E + E | T | E -> E + E | T |
An unrestricted grammar is a quadruple , where is an alphabet; is the set of terminal symbols (); is the set of non-terminal symbols; is the initial symbol; and is a set of rules (a finite subset of ).
The following are defined:
The above grammar is unrestricted in its derivations capabilities and directly supports context-dependent rules.
In our study of programming language processing, though, only context-free grammars will be considered. This means that the set is defined on , i.e., only non-terminals are allowed on the left side of a rule.
Even though we gave a formal definition of grammar, seldom will we use described in that form. Most grammars are described in Bakus-Naur Form (BNF), Extended Backus–Naur Form (EBNF), or other metasyntax.
Consider the following example grammar:
E -> E + E | T T -> T * T | F F -> ( E ) | val
This grammar provides a description of the syntax of simples additive and multiplicative expressions. The language will be any expression involving the two operators, sets of parenthesis and values (val). Token val represents any number and is a terminal in this grammar: in a compiler implementation this would be recognized at the lexical level.
The FIRST set for a given string or symbol can be computed as follows:
As an example, consider production X -> Y1...Yn
The FIRST set can also be computed for a string Y1...Yn much in the same way as in case 3 above.
The FOLLOW set is computed for non-terminals and indicates the set of terminal symbols that are possible after a given non-terminal. The special symbol $ is used to represent the end of phrase (end of input).
The algorithm should be repeated until the FOLLOW set remains unchanged.