For example:
The x+y*z is interpreted as
(x+y)*z
by this grammar (if we use leftmost derivations) and as
x+(y*z)
by G1 or G2.
That is, both G1 and G2 grammar handle the operator precedence correctly (since * has higher precedence than +), while the G3 grammar does not.
In next post, I will explain the most interesting part of context free grammars that is ambiguous grammar.
Showing posts with label examples of context free languages. Show all posts
Showing posts with label examples of context free languages. Show all posts
Monday, July 9, 2007
Tuesday, June 12, 2007
Examples Of Context Free Languages (CFL)
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.
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.
Sunday, May 20, 2007
Context-Free-Language (CFL)
A language L is said to be a Context-Free-Language (CFL) if its grammar is Context-Free. More precisely, it is a language whose words, sentences and phrases are made of symbols and words from a Context-Free-Grammar. Usually, CFL is of the form L=L(G). Given below are examples for CFG but not for CFL.
Here I am giving you one example of context-free grammar
Example:
A context-free grammar for the language consisting of all strings over {a,b} which contain a different number of a's to b's is
S → U | V
U → TaU | TaT
V → TbV | TbT
T → aTbT | bTaT | ε
Here, T can generate all strings with the same number of a's as b's, U generates all strings with more a's than b's and V generates all strings with fewer a's than b's.
Here I am giving you one example of context-free grammar
Example:
A context-free grammar for the language consisting of all strings over {a,b} which contain a different number of a's to b's is
S → U | V
U → TaU | TaT
V → TbV | TbT
T → aTbT | bTaT | ε
Here, T can generate all strings with the same number of a's as b's, U generates all strings with more a's than b's and V generates all strings with fewer a's than b's.
Saturday, May 19, 2007
Context-Free Grammar (formal definition)
Definition of Context-Free Grammar includes the definition of Context free language. So please read the following definition carefully.
Just as any formal grammar, a context-free grammar G can be defined as a 4-tuple:
G = (Vt,Vn,P,S) where
Vt is a finite set of terminals
Vn is a finite set of non-terminals
P is a finite set of production rules
S is an element of Vn, the distinguished starting non-terminal.
elements of P are of the form
Vn(Vt U Vn)*
Just as any formal grammar, a context-free grammar G can be defined as a 4-tuple:
G = (Vt,Vn,P,S) where
Vt is a finite set of terminals
Vn is a finite set of non-terminals
P is a finite set of production rules
S is an element of Vn, the distinguished starting non-terminal.
elements of P are of the form
Vn(Vt U Vn)*
Subscribe to:
Posts (Atom)