So far, we have seen very good examples of Regular expressions and Regular Languages. We will see more difficult problems in future. But at this time, we are going to start examples of context free languages (CFL). There are a lot of problems based on CFL. So we will discuss a lot of problems of CFL.
Example 1
Consider the following input string:
x+2*y
When scanned by a scanner, it produces the following stream of tokens:
id(x)
+
num(2)
*
id(y)
The goal is to parse this expression and construct a data structure (a parse tree) to represent it. One possible syntax for expressions is given by the following grammar G1:
E ::= E + T | E - T | T
T ::= T * F | T / F | F
F ::= num | id
where E, T and F stand for expression, term, and factor respectively. For example, the rule for E indicates that an expression E can take one of the following 3 forms: an expression followed by the token + followed by a term, or an expression followed by the token - followed by a term, or simply a term. The first rule for E is actually a shorthand of 3 productions:
E ::= E + T
E ::= E - T
E ::= T
G1 is an example of a context-free grammar (defined below); the symbols E, T and F are nonterminals and should be defined using production rules, while +, -, *, /, num, and id are terminals (i.e., tokens) produced by the scanner. The nonterminal E is the start symbol of the grammar.
In my next Posts, I will explain types of parsing because knowledge of them is necessary for writing context free grammars.
Tuesday, June 12, 2007
Examples Of Context Free Languages (CFL)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment