3production is used during parsing. The parser can then generate appropriate error diagnostics aboutthe erroneous construct that has been recognized in the input.
Ideally, we would like a compiler to make as few changes as possible in processing an incorrectinput string. There are algorithms for choosing a minimal sequence of changes to obtain a globallyleast-cost correction. Given an incorrect input string
these algorithms will find aparse tree for a related string
such that the number of insertions, deletions, and changes of tokensrequired to transform
is as small as possible. Unfortunately, these methods are in generaltoo costly to implement in terms of time and space, so these techniques are currently only of theoretical interest. Do note that a closest correct program may not be what the programmer had inmind. Nevertheless, the notion of least-cost correction provides a yardstick for evaluating error-recovery techniques, and has been used for finding optimal replacement strings for phrase-levelrecovery.
4.2 Context-free grammar
Consider a conditional statementIf S1 and S2 are statements and E is an expression, then
“if E then S1 else S2”
We know that, regular expression can specify the lexical structure of tokens. Using some syntacticvariable, stmt, we can specify grammar production
if expr then stmt else stmt
A context-free grammar consists of terminals, nonterminals, start symbol and set of productions.1.
are the basic symbols from which strings are formed. The term "token name" is asynonym for "terminal" and frequently we will use the word "token" for terminal when it isclear that we are talking about just the token name. We assume that the terminals are the firstcomponents of the tokens output by the lexical analyzer. The terminals are the keywords if andelse and the symbols "(" and " ) ."2.
are syntactic variables that denote sets of strings.
are nonterminals.The sets of strings denoted by nonterminals help define the language generated by the grammar.Nonterminals impose a hierarchical structure on the language that is key to syntax analysis andtranslation.3. In a grammar, one nonterminal is distinguished as the
and the set of strings itdenotes is the language generated by the grammar. Conventionally, the productions for the startsymbol are listed first.4. The productions of a grammar specify the manner in which the terminals and nonterminals canbe combined to form strings. Each
consists of:(a) A nonterminal called the
of the production; this production defines someof the strings denoted by the head.(b) The symbol : = has been used in place of